2 * update_image.c - see description below
6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
23 * This file contains the implementation of wimlib_update_image(), which is one
24 * of the two ways by which library users can make changes to a WIM image. (The
25 * other way is by mounting an image read-write.) wimlib_update_image() is also
26 * used in the implementation of wimlib_add_image(), since "create a WIM image
27 * from this directory tree" is equivalent to "create an empty WIM image, then
28 * update it to add this directory tree as the root".
30 * wimlib_update_image() processes a list of commands passed to it. Currently,
31 * the following types of commands are supported:
33 * - Add a directory tree from an external source (filesystem or NTFS volume).
34 * This can be used to add new files or to replace existing files.
35 * - Delete a file or directory tree.
36 * - Rename a file or directory tree.
38 * Not supported are creating links to existing files or changing metadata of
41 * wimlib_update_image() is atomic. If it cannot complete successfully, then
42 * all changes are rolled back and the WIMStruct is left unchanged. Rollback is
43 * implemented by breaking the commands into primitive operations such as "link
44 * this dentry tree here" which can be undone by doing the opposite operations
52 #include "wimlib/assert.h"
53 #include "wimlib/capture.h"
54 #include "wimlib/dentry.h"
55 #include "wimlib/encoding.h"
56 #include "wimlib/endianness.h"
57 #include "wimlib/error.h"
58 #include "wimlib/lookup_table.h"
59 #include "wimlib/metadata.h"
61 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
63 #include "wimlib/paths.h"
64 #include "wimlib/progress.h"
65 #include "wimlib/xml.h"
75 /* Saved specification of a "primitive" update operation that was performed. */
76 struct update_primitive {
78 /* Unlinked a dentry from its parent directory. */
81 /* Linked a dentry into its parent directory. */
84 /* Changed the file name of a dentry. */
87 /* Changed the short name of a dentry. */
92 /* For UNLINK_DENTRY and LINK_DENTRY operations */
94 /* Dentry that was linked or unlinked. */
95 struct wim_dentry *subject;
97 /* For link operations, the directory into which
98 * @subject was linked, or NULL if @subject was set as
99 * the root of the image.
101 * For unlink operations, the directory from which
102 * @subject was unlinked, or NULL if @subject was unset
103 * as the root of the image. */
104 struct wim_dentry *parent;
107 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
109 /* Dentry that had its name changed. */
110 struct wim_dentry *subject;
113 utf16lechar *old_name;
118 /* Chronological list of primitive operations that were executed for a single
119 * logical update command, such as 'add', 'delete', or 'rename'. */
120 struct update_primitive_list {
121 struct update_primitive *entries;
122 struct update_primitive inline_entries[4];
124 size_t num_alloc_entries;
127 /* Journal for managing the executing of zero or more logical update commands,
128 * such as 'add', 'delete', or 'rename'. This allows either committing or
129 * rolling back the commands. */
130 struct update_command_journal {
131 /* Number of update commands this journal contains. */
134 /* Index of currently executing update command. */
137 /* Location of the WIM image's root pointer. */
138 struct wim_dentry **root_p;
140 /* Pointer to the lookup table of the WIM (may needed for rollback) */
141 struct wim_lookup_table *lookup_table;
143 /* List of dentries that are currently unlinked from the WIM image.
144 * These must be freed when no longer needed for commit or rollback. */
145 struct list_head orphans;
147 /* Per-command logs. */
148 struct update_primitive_list cmd_prims[];
152 init_update_primitive_list(struct update_primitive_list *l)
154 l->entries = l->inline_entries;
156 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
159 /* Allocates a new journal for managing the execution of up to @num_cmds update
161 static struct update_command_journal *
162 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
163 struct wim_lookup_table *lookup_table)
165 struct update_command_journal *j;
167 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
169 j->num_cmds = num_cmds;
172 j->lookup_table = lookup_table;
173 INIT_LIST_HEAD(&j->orphans);
174 for (size_t i = 0; i < num_cmds; i++)
175 init_update_primitive_list(&j->cmd_prims[i]);
180 /* Don't call this directly; use commit_update() or rollback_update() instead.
183 free_update_command_journal(struct update_command_journal *j)
185 struct wim_dentry *orphan;
187 /* Free orphaned dentry trees */
188 while (!list_empty(&j->orphans)) {
189 orphan = list_first_entry(&j->orphans,
190 struct wim_dentry, tmp_list);
191 list_del(&orphan->tmp_list);
192 free_dentry_tree(orphan, j->lookup_table);
195 for (size_t i = 0; i < j->num_cmds; i++)
196 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
197 FREE(j->cmd_prims[i].entries);
201 /* Add the entry @prim to the update command journal @j. */
203 record_update_primitive(struct update_command_journal *j,
204 struct update_primitive prim)
206 struct update_primitive_list *l;
208 l = &j->cmd_prims[j->cur_cmd];
210 if (l->num_entries == l->num_alloc_entries) {
211 struct update_primitive *new_entries;
212 size_t new_num_alloc_entries;
215 new_num_alloc_entries = l->num_alloc_entries * 2;
216 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
217 if (l->entries == l->inline_entries) {
218 new_entries = MALLOC(new_size);
220 return WIMLIB_ERR_NOMEM;
221 memcpy(new_entries, l->inline_entries,
222 sizeof(l->inline_entries));
224 new_entries = REALLOC(l->entries, new_size);
226 return WIMLIB_ERR_NOMEM;
228 l->entries = new_entries;
229 l->num_alloc_entries = new_num_alloc_entries;
231 l->entries[l->num_entries++] = prim;
236 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
237 struct wim_dentry **root_p)
240 /* Unlink @subject from its @parent. */
241 wimlib_assert(subject->d_parent == parent);
242 unlink_dentry(subject);
244 /* Unset @subject as the root of the image. */
247 subject->d_parent = subject;
251 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
252 struct wim_dentry **root_p)
255 /* Link @subject to its @parent */
256 struct wim_dentry *existing;
258 existing = dentry_add_child(parent, subject);
259 wimlib_assert(!existing);
261 /* Set @subject as root of the image */
266 /* Undo a link operation. */
268 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
269 struct wim_dentry **root_p, struct list_head *orphans)
271 /* Unlink is the opposite of link */
272 do_unlink(subject, parent, root_p);
274 /* @subject is now unlinked. Add it to orphans. */
275 list_add(&subject->tmp_list, orphans);
276 subject->is_orphan = 1;
279 /* Undo an unlink operation. */
281 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
282 struct wim_dentry **root_p)
284 /* Link is the opposite of unlink */
285 do_link(subject, parent, root_p);
287 /* @subject is no longer unlinked. Delete it from orphans. */
288 list_del(&subject->tmp_list);
289 subject->is_orphan = 0;
292 /* Rollback a name change operation. */
294 rollback_name_change(utf16lechar *old_name,
295 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
297 /* Free the new name, then replace it with the old name. */
300 *name_ptr = old_name;
301 *name_nbytes_ptr = utf16le_strlen(old_name);
304 *name_nbytes_ptr = 0;
308 /* Rollback a primitive update operation. */
310 rollback_update_primitive(const struct update_primitive *prim,
311 struct wim_dentry **root_p,
312 struct list_head *orphans)
314 switch (prim->type) {
316 rollback_link(prim->link.subject, prim->link.parent, root_p,
320 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
322 case CHANGE_FILE_NAME:
323 rollback_name_change(prim->name.old_name,
324 &prim->name.subject->file_name,
325 &prim->name.subject->file_name_nbytes);
327 case CHANGE_SHORT_NAME:
328 rollback_name_change(prim->name.old_name,
329 &prim->name.subject->short_name,
330 &prim->name.subject->short_name_nbytes);
335 /* Rollback a logical update command */
337 rollback_update_command(const struct update_primitive_list *l,
338 struct wim_dentry **root_p,
339 struct list_head *orphans)
341 size_t i = l->num_entries;
343 /* Rollback each primitive operation, in reverse order. */
345 rollback_update_primitive(&l->entries[i], root_p, orphans);
348 /****************************************************************************/
350 /* Link @subject into the directory @parent; or, if @parent is NULL, set
351 * @subject as the root of the WIM image.
353 * This is the journaled version, so it can be rolled back. */
355 journaled_link(struct update_command_journal *j,
356 struct wim_dentry *subject, struct wim_dentry *parent)
358 struct update_primitive prim;
361 prim.type = LINK_DENTRY;
362 prim.link.subject = subject;
363 prim.link.parent = parent;
365 ret = record_update_primitive(j, prim);
369 do_link(subject, parent, j->root_p);
371 if (subject->is_orphan) {
372 list_del(&subject->tmp_list);
373 subject->is_orphan = 0;
378 /* Unlink @subject from the WIM image.
380 * This is the journaled version, so it can be rolled back. */
382 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
384 struct wim_dentry *parent;
385 struct update_primitive prim;
388 if (dentry_is_root(subject))
391 parent = subject->d_parent;
393 prim.type = UNLINK_DENTRY;
394 prim.link.subject = subject;
395 prim.link.parent = parent;
397 ret = record_update_primitive(j, prim);
401 do_unlink(subject, parent, j->root_p);
403 list_add(&subject->tmp_list, &j->orphans);
404 subject->is_orphan = 1;
408 /* Change the name of @dentry to @new_name_tstr.
410 * This is the journaled version, so it can be rolled back. */
412 journaled_change_name(struct update_command_journal *j,
413 struct wim_dentry *dentry, const tchar *new_name_tstr)
416 utf16lechar *new_name;
417 size_t new_name_nbytes;
418 struct update_primitive prim;
420 /* Set the long name. */
421 ret = tstr_to_utf16le(new_name_tstr,
422 tstrlen(new_name_tstr) * sizeof(tchar),
423 &new_name, &new_name_nbytes);
427 prim.type = CHANGE_FILE_NAME;
428 prim.name.subject = dentry;
429 prim.name.old_name = dentry->file_name;
430 ret = record_update_primitive(j, prim);
436 dentry->file_name = new_name;
437 dentry->file_name_nbytes = new_name_nbytes;
439 /* Clear the short name. */
440 prim.type = CHANGE_SHORT_NAME;
441 prim.name.subject = dentry;
442 prim.name.old_name = dentry->short_name;
443 ret = record_update_primitive(j, prim);
447 dentry->short_name = NULL;
448 dentry->short_name_nbytes = 0;
453 next_command(struct update_command_journal *j)
459 commit_update(struct update_command_journal *j)
461 for (size_t i = 0; i < j->num_cmds; i++)
463 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
465 if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
466 j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
468 FREE(j->cmd_prims[i].entries[k].name.old_name);
472 free_update_command_journal(j);
476 rollback_update(struct update_command_journal *j)
478 /* Rollback each logical update command, in reverse order. */
479 size_t i = j->cur_cmd;
483 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
484 free_update_command_journal(j);
488 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
489 struct update_command_journal *j,
491 wimlib_progress_func_t progfunc, void *progctx)
493 bool branch_is_dir = dentry_is_directory(branch);
494 bool existing_is_dir = dentry_is_directory(existing);
496 if (branch_is_dir != existing_is_dir) {
497 if (existing_is_dir) {
498 ERROR("\"%"TS"\" is a directory!\n"
499 " Specify the path at which "
500 "to place the file inside this directory.",
501 dentry_full_path(existing));
502 return WIMLIB_ERR_IS_DIRECTORY;
504 ERROR("Can't place directory at \"%"TS"\" because "
505 "a nondirectory file already exists there!",
506 dentry_full_path(existing));
507 return WIMLIB_ERR_NOTDIR;
512 /* Directory overlay */
513 while (dentry_has_children(branch)) {
514 struct wim_dentry *new_child;
515 struct wim_dentry *existing_child;
518 new_child = dentry_any_child(branch);
521 get_dentry_child_with_utf16le_name(existing,
522 new_child->file_name,
523 new_child->file_name_nbytes,
524 WIMLIB_CASE_PLATFORM_DEFAULT);
525 unlink_dentry(new_child);
526 if (existing_child) {
527 ret = handle_conflict(new_child, existing_child,
531 ret = journaled_link(j, new_child, existing);
534 dentry_add_child(branch, new_child);
540 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
541 /* Can't replace nondirectory file */
542 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
543 dentry_full_path(existing));
544 return WIMLIB_ERR_INVALID_OVERLAY;
546 /* Replace nondirectory file */
547 struct wim_dentry *parent;
550 parent = existing->d_parent;
552 ret = calculate_dentry_full_path(existing);
556 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
557 union wimlib_progress_info info;
559 info.replace.path_in_wim = existing->_full_path;
560 ret = call_progress(progfunc,
561 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
567 ret = journaled_unlink(j, existing);
571 return journaled_link(j, branch, parent);
576 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
577 struct update_command_journal *j,
578 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
580 struct wim_dentry *parent;
581 struct wim_dentry *existing;
582 const utf16lechar empty_name[1] = {0};
583 const utf16lechar *cur_component_name;
584 size_t cur_component_nbytes;
585 const utf16lechar *next_component_name;
588 /* Attempt to create root directory before proceeding to the "real"
591 existing = *j->root_p;
592 cur_component_name = empty_name;
593 cur_component_nbytes = 0;
595 /* Skip leading slashes */
596 next_component_name = target;
597 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
598 next_component_name++;
600 while (*next_component_name) { /* While not the last component ... */
601 const utf16lechar *end;
604 /* Descend into existing directory */
605 if (!dentry_is_directory(existing)) {
606 ERROR("\"%"TS"\" in the WIM image "
607 "is not a directory!",
608 dentry_full_path(existing));
609 return WIMLIB_ERR_NOTDIR;
612 /* A parent directory of the target didn't exist. Make
613 * the way by creating a filler directory. */
614 struct wim_dentry *filler;
616 ret = new_filler_directory(&filler);
619 ret = dentry_set_name_utf16le(filler,
621 cur_component_nbytes);
626 ret = journaled_link(j, filler, parent);
634 /* Advance to next component */
636 cur_component_name = next_component_name;
637 end = cur_component_name + 1;
638 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
641 next_component_name = end;
643 /* There will still be more components after this. */
645 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
646 wimlib_assert(*next_component_name); /* No trailing slashes */
648 /* This will be the last component */
649 next_component_name = end;
652 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
653 existing = get_dentry_child_with_utf16le_name(
656 cur_component_nbytes,
657 WIMLIB_CASE_PLATFORM_DEFAULT);
662 return handle_conflict(branch, existing, j, add_flags,
665 return journaled_link(j, branch, parent);
670 * Place the directory entry tree @branch at the path @target_tstr in the WIM
673 * @target_tstr cannot contain trailing slashes, and all path separators must be
674 * WIM_PATH_SEPARATOR.
676 * On success, @branch is committed to the journal @j.
677 * Otherwise @branch is freed.
679 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
680 * WIMLIB_ADD_FLAG_VERBOSE.
683 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
684 struct update_command_journal *j, int add_flags,
685 wimlib_progress_func_t progfunc, void *progctx)
688 const utf16lechar *target;
691 if (unlikely(!branch))
694 ret = tstr_get_utf16le(target_tstr, &target);
696 goto out_free_branch;
698 BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
699 ret = dentry_set_name(branch, path_basename(target_tstr));
701 goto out_free_target;
703 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
705 goto out_free_target;
706 /* branch was successfully committed to the journal */
709 tstr_put_utf16le(target);
711 free_dentry_tree(branch, j->lookup_table);
716 static const char wincfg[] =
722 "/System Volume Information\n"
726 static const tchar *wimboot_cfgfile =
727 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
728 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
729 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
732 get_capture_config(const tchar *config_file, struct capture_config *config,
733 int add_flags, const tchar *fs_source_path)
736 tchar *tmp_config_file = NULL;
738 memset(config, 0, sizeof(*config));
740 /* For WIMBoot capture, check for default capture configuration file
741 * unless one was explicitly specified. */
742 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
744 /* XXX: Handle loading file correctly when in NTFS volume. */
746 size_t len = tstrlen(fs_source_path) +
747 tstrlen(wimboot_cfgfile);
748 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
751 tsprintf(tmp_config_file, T("%"TS"%"TS),
752 fs_source_path, wimboot_cfgfile);
753 if (!tstat(tmp_config_file, &st)) {
754 config_file = tmp_config_file;
755 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
757 WARNING("\"%"TS"\" does not exist.\n"
758 " Using default capture configuration!",
763 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
764 /* Use Windows default. */
766 return WIMLIB_ERR_INVALID_PARAM;
767 ret = read_capture_config(T("wincfg"), wincfg,
768 sizeof(wincfg) - 1, config);
769 } else if (config_file) {
770 /* Use the specified configuration file. */
771 ret = read_capture_config(config_file, NULL, 0, config);
773 /* ... Or don't use any configuration file at all. No files
774 * will be excluded from capture, all files will be compressed,
778 FREE(tmp_config_file);
783 execute_add_command(struct update_command_journal *j,
785 const struct wimlib_update_command *add_cmd,
786 struct wim_inode_table *inode_table,
787 struct wim_sd_set *sd_set,
788 struct list_head *unhashed_streams)
792 tchar *fs_source_path;
793 tchar *wim_target_path;
794 const tchar *config_file;
795 struct capture_params params;
796 struct capture_config config;
797 capture_tree_t capture_tree = platform_default_capture_tree;
799 struct _ntfs_volume *ntfs_vol = NULL;
801 void *extra_arg = NULL;
802 struct wim_dentry *branch;
804 add_flags = add_cmd->add.add_flags;
805 fs_source_path = add_cmd->add.fs_source_path;
806 wim_target_path = add_cmd->add.wim_target_path;
807 config_file = add_cmd->add.config_file;
809 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
810 fs_source_path, wim_target_path, add_flags);
812 memset(¶ms, 0, sizeof(params));
815 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
816 capture_tree = build_dentry_tree_ntfs;
817 extra_arg = &ntfs_vol;
818 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
819 ERROR("NTFS volume already set");
820 ret = WIMLIB_ERR_INVALID_PARAM;
826 ret = get_capture_config(config_file, &config,
827 add_flags, fs_source_path);
831 params.lookup_table = wim->lookup_table;
832 params.unhashed_streams = unhashed_streams;
833 params.inode_table = inode_table;
834 params.sd_set = sd_set;
835 params.config = &config;
836 params.add_flags = add_flags;
837 params.extra_arg = extra_arg;
839 params.progfunc = wim->progfunc;
840 params.progctx = wim->progctx;
841 params.progress.scan.source = fs_source_path;
842 params.progress.scan.wim_target_path = wim_target_path;
843 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
844 ¶ms.progress, params.progctx);
846 goto out_destroy_config;
848 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
849 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
850 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
852 goto out_destroy_config;
854 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
855 ¶ms.progress, params.progctx);
857 free_dentry_tree(branch, wim->lookup_table);
858 goto out_cleanup_after_capture;
861 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
862 branch && !dentry_is_directory(branch))
864 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
865 ret = WIMLIB_ERR_NOTDIR;
866 free_dentry_tree(branch, wim->lookup_table);
867 goto out_cleanup_after_capture;
870 ret = attach_branch(branch, wim_target_path, j,
871 add_flags, params.progfunc, params.progctx);
873 goto out_cleanup_after_capture;
875 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
876 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
878 params.add_flags = 0;
879 params.progfunc = NULL;
880 params.config = NULL;
882 /* If a capture configuration file was explicitly specified when
883 * capturing an image in WIMBoot mode, save it as
884 * /Windows/System32/WimBootCompress.ini in the WIM image. */
885 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
887 goto out_cleanup_after_capture;
889 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
891 goto out_cleanup_after_capture;
895 wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
898 goto out_destroy_config;
899 out_cleanup_after_capture:
902 do_ntfs_umount(ntfs_vol);
905 destroy_capture_config(&config);
911 execute_delete_command(struct update_command_journal *j,
913 const struct wimlib_update_command *delete_cmd)
916 const tchar *wim_path;
917 struct wim_dentry *tree;
919 flags = delete_cmd->delete_.delete_flags;
920 wim_path = delete_cmd->delete_.wim_path;
922 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
924 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
926 /* Path to delete does not exist in the WIM. */
927 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
930 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
931 wim_path, wim->current_image);
932 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
936 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
937 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
938 "but a recursive delete was not requested",
939 wim_path, wim->current_image);
940 return WIMLIB_ERR_IS_DIRECTORY;
943 return journaled_unlink(j, tree);
947 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
949 FREE(dentry->_full_path);
950 dentry->_full_path = NULL;
954 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
956 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
961 if (dentry_is_root(d2))
967 /* Rename a file or directory in the WIM.
969 * This returns a -errno value.
971 * The journal @j is optional.
974 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
975 CASE_SENSITIVITY_TYPE case_type,
976 struct update_command_journal *j)
978 struct wim_dentry *src;
979 struct wim_dentry *dst;
980 struct wim_dentry *parent_of_dst;
983 /* This rename() implementation currently only supports actual files
984 * (not alternate data streams) */
986 src = get_dentry(wim, from, case_type);
990 dst = get_dentry(wim, to, case_type);
993 /* Destination file exists */
995 if (src == dst) /* Same file */
998 if (!dentry_is_directory(src)) {
999 /* Cannot rename non-directory to directory. */
1000 if (dentry_is_directory(dst))
1003 /* Cannot rename directory to a non-directory or a non-empty
1005 if (!dentry_is_directory(dst))
1007 if (dentry_has_children(dst))
1010 parent_of_dst = dst->d_parent;
1012 /* Destination does not exist */
1013 parent_of_dst = get_parent_dentry(wim, to, case_type);
1017 if (!dentry_is_directory(parent_of_dst))
1021 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
1022 * from the tree and creating a loop... */
1023 if (is_ancestor(src, parent_of_dst))
1028 if (journaled_unlink(j, dst))
1030 if (journaled_unlink(j, src))
1032 if (journaled_change_name(j, src, path_basename(to)))
1034 if (journaled_link(j, src, parent_of_dst))
1037 ret = dentry_set_name(src, path_basename(to));
1042 free_dentry_tree(dst, wim->lookup_table);
1045 dentry_add_child(parent_of_dst, src);
1047 if (src->_full_path)
1048 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1054 execute_rename_command(struct update_command_journal *j,
1056 const struct wimlib_update_command *rename_cmd)
1060 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1061 rename_cmd->rename.wim_target_path,
1062 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1066 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1067 rename_cmd->rename.wim_source_path,
1068 rename_cmd->rename.wim_target_path);
1071 ret = WIMLIB_ERR_NOMEM;
1074 ret = WIMLIB_ERR_NOTDIR;
1078 /* XXX: EBUSY is returned when the rename would create a
1079 * loop. It maybe should have its own error code. */
1080 ret = WIMLIB_ERR_NOTEMPTY;
1083 ret = WIMLIB_ERR_IS_DIRECTORY;
1087 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1094 static inline const tchar *
1095 update_op_to_str(int op)
1098 case WIMLIB_UPDATE_OP_ADD:
1100 case WIMLIB_UPDATE_OP_DELETE:
1102 case WIMLIB_UPDATE_OP_RENAME:
1111 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1112 enum wimlib_update_op op)
1114 for (size_t i = 0; i < num_cmds; i++)
1115 if (cmds[i].op == op)
1121 execute_update_commands(WIMStruct *wim,
1122 const struct wimlib_update_command *cmds,
1126 struct wim_inode_table *inode_table;
1127 struct wim_sd_set *sd_set;
1128 struct list_head unhashed_streams;
1129 struct update_command_journal *j;
1130 union wimlib_progress_info info;
1133 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1134 /* If we have at least one "add" command, create the inode and
1135 * security descriptor tables to index new inodes and new
1136 * security descriptors, respectively. */
1137 inode_table = alloca(sizeof(struct wim_inode_table));
1138 sd_set = alloca(sizeof(struct wim_sd_set));
1140 ret = init_inode_table(inode_table, 9001);
1144 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1146 goto out_destroy_inode_table;
1148 INIT_LIST_HEAD(&unhashed_streams);
1154 /* Start an in-memory journal to allow rollback if something goes wrong
1156 j = new_update_command_journal(num_cmds,
1157 &wim_get_current_image_metadata(wim)->root_dentry,
1160 ret = WIMLIB_ERR_NOMEM;
1161 goto out_destroy_sd_set;
1164 info.update.completed_commands = 0;
1165 info.update.total_commands = num_cmds;
1167 for (size_t i = 0; i < num_cmds; i++) {
1168 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1169 i + 1, num_cmds, update_op_to_str(cmds[i].op));
1170 info.update.command = &cmds[i];
1171 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1172 ret = call_progress(wim->progfunc,
1173 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1174 &info, wim->progctx);
1179 switch (cmds[i].op) {
1180 case WIMLIB_UPDATE_OP_ADD:
1181 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1182 sd_set, &unhashed_streams);
1184 case WIMLIB_UPDATE_OP_DELETE:
1185 ret = execute_delete_command(j, wim, &cmds[i]);
1187 case WIMLIB_UPDATE_OP_RENAME:
1188 ret = execute_rename_command(j, wim, &cmds[i]);
1193 info.update.completed_commands++;
1194 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1195 ret = call_progress(wim->progfunc,
1196 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1197 &info, wim->progctx);
1206 struct wim_image_metadata *imd;
1208 imd = wim_get_current_image_metadata(wim);
1210 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1211 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1213 goto out_destroy_sd_set;
1217 rollback_new_security_descriptors(sd_set);
1221 destroy_sd_set(sd_set);
1222 out_destroy_inode_table:
1224 destroy_inode_table(inode_table);
1231 check_add_command(struct wimlib_update_command *cmd,
1232 const struct wim_header *hdr)
1234 int add_flags = cmd->add.add_flags;
1236 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1237 WIMLIB_ADD_FLAG_DEREFERENCE |
1238 WIMLIB_ADD_FLAG_VERBOSE |
1239 /* BOOT doesn't make sense for wimlib_update_image(). */
1240 /*WIMLIB_ADD_FLAG_BOOT |*/
1241 WIMLIB_ADD_FLAG_UNIX_DATA |
1242 WIMLIB_ADD_FLAG_NO_ACLS |
1243 WIMLIB_ADD_FLAG_STRICT_ACLS |
1244 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1245 WIMLIB_ADD_FLAG_RPFIX |
1246 WIMLIB_ADD_FLAG_NORPFIX |
1247 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1248 WIMLIB_ADD_FLAG_WINCONFIG |
1249 WIMLIB_ADD_FLAG_WIMBOOT |
1250 WIMLIB_ADD_FLAG_NO_REPLACE |
1251 WIMLIB_ADD_FLAG_TEST_FILE_EXCLUSION))
1252 return WIMLIB_ERR_INVALID_PARAM;
1254 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1256 #ifndef WITH_NTFS_3G
1257 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1258 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1259 " we cannot capture a WIM image directly "
1260 "from an NTFS volume");
1261 return WIMLIB_ERR_UNSUPPORTED;
1266 /* Check for flags not supported on Windows */
1267 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1268 ERROR("Capturing UNIX-specific data is not supported on Windows");
1269 return WIMLIB_ERR_UNSUPPORTED;
1271 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1272 ERROR("Dereferencing symbolic links is not supported on Windows");
1273 return WIMLIB_ERR_UNSUPPORTED;
1277 /* VERBOSE implies EXCLUDE_VERBOSE */
1278 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1279 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1281 /* Check for contradictory reparse point fixup flags */
1282 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1283 WIMLIB_ADD_FLAG_NORPFIX)) ==
1284 (WIMLIB_ADD_FLAG_RPFIX |
1285 WIMLIB_ADD_FLAG_NORPFIX))
1287 ERROR("Cannot specify RPFIX and NORPFIX flags "
1288 "at the same time!");
1289 return WIMLIB_ERR_INVALID_PARAM;
1292 /* Set default behavior on reparse point fixups if requested */
1293 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1294 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1296 /* Do reparse-point fixups by default if we are capturing an
1297 * entire image and either the header flag is set from previous
1298 * images, or if this is the first image being added. */
1299 if (is_entire_image &&
1300 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1301 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1304 if (!is_entire_image) {
1305 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1306 ERROR("Cannot add directly from an NTFS volume "
1307 "when not capturing a full image!");
1308 return WIMLIB_ERR_INVALID_PARAM;
1311 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1312 ERROR("Cannot do reparse point fixups when "
1313 "not capturing a full image!");
1314 return WIMLIB_ERR_INVALID_PARAM;
1317 /* We may have modified the add flags. */
1318 cmd->add.add_flags = add_flags;
1323 check_delete_command(const struct wimlib_update_command *cmd)
1325 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1326 WIMLIB_DELETE_FLAG_RECURSIVE))
1327 return WIMLIB_ERR_INVALID_PARAM;
1332 check_rename_command(const struct wimlib_update_command *cmd)
1334 if (cmd->rename.rename_flags != 0)
1335 return WIMLIB_ERR_INVALID_PARAM;
1340 check_update_command(struct wimlib_update_command *cmd,
1341 const struct wim_header *hdr)
1344 case WIMLIB_UPDATE_OP_ADD:
1345 return check_add_command(cmd, hdr);
1346 case WIMLIB_UPDATE_OP_DELETE:
1347 return check_delete_command(cmd);
1348 case WIMLIB_UPDATE_OP_RENAME:
1349 return check_rename_command(cmd);
1355 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1356 const struct wim_header *hdr)
1359 for (size_t i = 0; i < num_cmds; i++) {
1360 ret = check_update_command(&cmds[i], hdr);
1369 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1372 for (size_t i = 0; i < num_cmds; i++) {
1373 switch (cmds[i].op) {
1374 case WIMLIB_UPDATE_OP_ADD:
1375 FREE(cmds[i].add.wim_target_path);
1377 case WIMLIB_UPDATE_OP_DELETE:
1378 FREE(cmds[i].delete_.wim_path);
1380 case WIMLIB_UPDATE_OP_RENAME:
1381 FREE(cmds[i].rename.wim_source_path);
1382 FREE(cmds[i].rename.wim_target_path);
1391 copy_update_commands(const struct wimlib_update_command *cmds,
1393 struct wimlib_update_command **cmds_copy_ret)
1396 struct wimlib_update_command *cmds_copy;
1398 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1402 for (size_t i = 0; i < num_cmds; i++) {
1403 cmds_copy[i].op = cmds[i].op;
1404 switch (cmds[i].op) {
1405 case WIMLIB_UPDATE_OP_ADD:
1406 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1407 cmds_copy[i].add.wim_target_path =
1408 canonicalize_wim_path(cmds[i].add.wim_target_path);
1409 if (!cmds_copy[i].add.wim_target_path)
1411 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1412 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1414 case WIMLIB_UPDATE_OP_DELETE:
1415 cmds_copy[i].delete_.wim_path =
1416 canonicalize_wim_path(cmds[i].delete_.wim_path);
1417 if (!cmds_copy[i].delete_.wim_path)
1419 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1421 case WIMLIB_UPDATE_OP_RENAME:
1422 cmds_copy[i].rename.wim_source_path =
1423 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1424 cmds_copy[i].rename.wim_target_path =
1425 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1426 if (!cmds_copy[i].rename.wim_source_path ||
1427 !cmds_copy[i].rename.wim_target_path)
1431 ERROR("Unknown update operation %u", cmds[i].op);
1432 ret = WIMLIB_ERR_INVALID_PARAM;
1436 *cmds_copy_ret = cmds_copy;
1441 ret = WIMLIB_ERR_NOMEM;
1443 free_update_commands(cmds_copy, num_cmds);
1447 /* API function documented in wimlib.h */
1449 wimlib_update_image(WIMStruct *wim,
1451 const struct wimlib_update_command *cmds,
1456 struct wimlib_update_command *cmds_copy;
1458 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1459 return WIMLIB_ERR_INVALID_PARAM;
1461 DEBUG("Updating image %d with %zu commands", image, num_cmds);
1463 /* Load the metadata for the image to modify (if not loaded already) */
1464 ret = select_wim_image(wim, image);
1468 DEBUG("Preparing %zu update commands", num_cmds);
1470 /* Make a copy of the update commands, in the process doing certain
1471 * canonicalizations on paths (e.g. translating backslashes to forward
1472 * slashes). This is done to avoid modifying the caller's copy of the
1474 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1478 /* Perform additional checks on the update commands before we execute
1480 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1482 goto out_free_cmds_copy;
1484 /* Actually execute the update commands. */
1485 DEBUG("Executing %zu update commands", num_cmds);
1486 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1488 goto out_free_cmds_copy;
1490 wim->image_metadata[image - 1]->modified = 1;
1492 /* Statistics about the WIM image, such as the numbers of files and
1493 * directories, may have changed. Call xml_update_image_info() to
1494 * recalculate these statistics. */
1495 xml_update_image_info(wim, image);
1497 for (size_t i = 0; i < num_cmds; i++)
1498 if (cmds_copy[i].op == WIMLIB_UPDATE_OP_ADD &&
1499 cmds_copy[i].add.add_flags & WIMLIB_ADD_FLAG_RPFIX)
1500 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
1502 free_update_commands(cmds_copy, num_cmds);
1508 wimlib_delete_path(WIMStruct *wim, int image,
1509 const tchar *path, int delete_flags)
1511 struct wimlib_update_command cmd;
1513 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1514 cmd.delete_.wim_path = (tchar *)path;
1515 cmd.delete_.delete_flags = delete_flags;
1517 return wimlib_update_image(wim, image, &cmd, 1, 0);
1521 wimlib_rename_path(WIMStruct *wim, int image,
1522 const tchar *source_path, const tchar *dest_path)
1524 struct wimlib_update_command cmd;
1526 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1527 cmd.rename.wim_source_path = (tchar *)source_path;
1528 cmd.rename.wim_target_path = (tchar *)dest_path;
1529 cmd.rename.rename_flags = 0;
1531 return wimlib_update_image(wim, image, &cmd, 1, 0);
1535 wimlib_add_tree(WIMStruct *wim, int image,
1536 const tchar *fs_source_path, const tchar *wim_target_path,
1539 struct wimlib_update_command cmd;
1541 cmd.op = WIMLIB_UPDATE_OP_ADD;
1542 cmd.add.fs_source_path = (tchar *)fs_source_path;
1543 cmd.add.wim_target_path = (tchar *)wim_target_path;
1544 cmd.add.add_flags = add_flags;
1545 cmd.add.config_file = NULL;
1547 return wimlib_update_image(wim, image, &cmd, 1, 0);