2 * update_image.c - Update a WIM image.
6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/capture.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/encoding.h"
31 #include "wimlib/error.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/metadata.h"
35 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
37 #include "wimlib/paths.h"
38 #include "wimlib/xml.h"
48 /* Saved specification of a "primitive" update operation that was performed. */
49 struct update_primitive {
51 /* Unlinked a dentry from its parent directory. */
54 /* Linked a dentry into its parent directory. */
57 /* Changed the file name of a dentry. */
60 /* Changed the short name of a dentry. */
65 /* For UNLINK_DENTRY and LINK_DENTRY operations */
67 /* Dentry that was linked or unlinked. */
68 struct wim_dentry *subject;
70 /* For link operations, the directory into which
71 * @subject was linked, or NULL if @subject was set as
72 * the root of the image.
74 * For unlink operations, the directory from which
75 * @subject was unlinked, or NULL if @subject was unset
76 * as the root of the image. */
77 struct wim_dentry *parent;
80 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
82 /* Dentry that had its name changed. */
83 struct wim_dentry *subject;
86 utf16lechar *old_name;
91 /* Chronological list of primitive operations that were executed for a single
92 * logical update command, such as 'add', 'delete', or 'rename'. */
93 struct update_primitive_list {
94 struct update_primitive *entries;
95 struct update_primitive inline_entries[4];
97 size_t num_alloc_entries;
100 /* Journal for managing the executing of zero or more logical update commands,
101 * such as 'add', 'delete', or 'rename'. This allows either committing or
102 * rolling back the commands. */
103 struct update_command_journal {
104 /* Number of update commands this journal contains. */
107 /* Index of currently executing update command. */
110 /* Location of the WIM image's root pointer. */
111 struct wim_dentry **root_p;
113 /* Pointer to the lookup table of the WIM (may needed for rollback) */
114 struct wim_lookup_table *lookup_table;
116 /* List of dentries that are currently unlinked from the WIM image.
117 * These must be freed when no longer needed for commit or rollback. */
118 struct list_head orphans;
120 /* Per-command logs. */
121 struct update_primitive_list cmd_prims[];
125 init_update_primitive_list(struct update_primitive_list *l)
127 l->entries = l->inline_entries;
129 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
132 /* Allocates a new journal for managing the execution of up to @num_cmds update
134 static struct update_command_journal *
135 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
136 struct wim_lookup_table *lookup_table)
138 struct update_command_journal *j;
140 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
142 j->num_cmds = num_cmds;
145 j->lookup_table = lookup_table;
146 INIT_LIST_HEAD(&j->orphans);
147 for (size_t i = 0; i < num_cmds; i++)
148 init_update_primitive_list(&j->cmd_prims[i]);
153 /* Don't call this directly; use commit_update() or rollback_update() instead.
156 free_update_command_journal(struct update_command_journal *j)
158 struct wim_dentry *orphan;
160 /* Free orphaned dentry trees */
161 while (!list_empty(&j->orphans)) {
162 orphan = list_first_entry(&j->orphans,
163 struct wim_dentry, tmp_list);
164 list_del(&orphan->tmp_list);
165 free_dentry_tree(orphan, j->lookup_table);
168 for (size_t i = 0; i < j->num_cmds; i++)
169 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
170 FREE(j->cmd_prims[i].entries);
174 /* Add the entry @prim to the update command journal @j. */
176 record_update_primitive(struct update_command_journal *j,
177 struct update_primitive prim)
179 struct update_primitive_list *l;
181 l = &j->cmd_prims[j->cur_cmd];
183 if (l->num_entries == l->num_alloc_entries) {
184 struct update_primitive *new_entries;
185 size_t new_num_alloc_entries;
188 new_num_alloc_entries = l->num_alloc_entries * 2;
189 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
190 if (l->entries == l->inline_entries) {
191 new_entries = MALLOC(new_size);
193 return WIMLIB_ERR_NOMEM;
194 memcpy(new_entries, l->inline_entries,
195 sizeof(l->inline_entries));
197 new_entries = REALLOC(l->entries, new_size);
199 return WIMLIB_ERR_NOMEM;
201 l->entries = new_entries;
202 l->num_alloc_entries = new_num_alloc_entries;
204 l->entries[l->num_entries++] = prim;
209 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
210 struct wim_dentry **root_p)
213 /* Unlink @subject from its @parent. */
214 wimlib_assert(subject->parent == parent);
215 unlink_dentry(subject);
217 /* Unset @subject as the root of the image. */
220 subject->parent = subject;
224 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
225 struct wim_dentry **root_p)
228 /* Link @subject to its @parent */
229 struct wim_dentry *existing;
231 existing = dentry_add_child(parent, subject);
232 wimlib_assert(!existing);
234 /* Set @subject as root of the image */
239 /* Undo a link operation. */
241 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
242 struct wim_dentry **root_p, struct list_head *orphans)
244 /* Unlink is the opposite of link */
245 do_unlink(subject, parent, root_p);
247 /* @subject is now unlinked. Add it to orphans. */
248 list_add(&subject->tmp_list, orphans);
249 subject->is_orphan = 1;
252 /* Undo an unlink operation. */
254 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
255 struct wim_dentry **root_p)
257 /* Link is the opposite of unlink */
258 do_link(subject, parent, root_p);
260 /* @subject is no longer unlinked. Delete it from orphans. */
261 list_del(&subject->tmp_list);
262 subject->is_orphan = 0;
265 /* Rollback a name change operation. */
267 rollback_name_change(utf16lechar *old_name,
268 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
270 /* Free the new name, then replace it with the old name. */
273 *name_ptr = old_name;
274 *name_nbytes_ptr = utf16le_strlen(old_name);
277 *name_nbytes_ptr = 0;
281 /* Rollback a primitive update operation. */
283 rollback_update_primitive(const struct update_primitive *prim,
284 struct wim_dentry **root_p,
285 struct list_head *orphans)
287 switch (prim->type) {
289 rollback_link(prim->link.subject, prim->link.parent, root_p,
293 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
295 case CHANGE_FILE_NAME:
296 rollback_name_change(prim->name.old_name,
297 &prim->name.subject->file_name,
298 &prim->name.subject->file_name_nbytes);
300 case CHANGE_SHORT_NAME:
301 rollback_name_change(prim->name.old_name,
302 &prim->name.subject->short_name,
303 &prim->name.subject->short_name_nbytes);
308 /* Rollback a logical update command */
310 rollback_update_command(const struct update_primitive_list *l,
311 struct wim_dentry **root_p,
312 struct list_head *orphans)
314 size_t i = l->num_entries;
316 /* Rollback each primitive operation, in reverse order. */
318 rollback_update_primitive(&l->entries[i], root_p, orphans);
321 /****************************************************************************/
323 /* Link @subject into the directory @parent; or, if @parent is NULL, set
324 * @subject as the root of the WIM image.
326 * This is the journaled version, so it can be rolled back. */
328 journaled_link(struct update_command_journal *j,
329 struct wim_dentry *subject, struct wim_dentry *parent)
331 struct update_primitive prim = {
340 ret = record_update_primitive(j, prim);
344 do_link(subject, parent, j->root_p);
346 if (subject->is_orphan) {
347 list_del(&subject->tmp_list);
348 subject->is_orphan = 0;
353 /* Unlink @subject from the WIM image.
355 * This is the journaled version, so it can be rolled back. */
357 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
360 struct wim_dentry *parent;
362 if (dentry_is_root(subject))
365 parent = subject->parent;
367 struct update_primitive prim = {
368 .type = UNLINK_DENTRY,
375 ret = record_update_primitive(j, prim);
379 do_unlink(subject, parent, j->root_p);
381 list_add(&subject->tmp_list, &j->orphans);
382 subject->is_orphan = 1;
386 /* Change the name of @dentry to @new_name_tstr.
388 * This is the journaled version, so it can be rolled back. */
390 journaled_change_name(struct update_command_journal *j,
391 struct wim_dentry *dentry, const tchar *new_name_tstr)
394 utf16lechar *new_name = NULL;
395 u16 new_name_nbytes = 0;
396 struct update_primitive prim;
398 /* Set the long name. */
399 ret = get_utf16le_string(new_name_tstr, &new_name, &new_name_nbytes);
403 prim.type = CHANGE_FILE_NAME;
404 prim.name.subject = dentry;
405 prim.name.old_name = dentry->file_name;
406 ret = record_update_primitive(j, prim);
410 dentry->file_name = new_name;
411 dentry->file_name_nbytes = new_name_nbytes;
413 /* Clear the short name. */
414 prim.type = CHANGE_SHORT_NAME;
415 prim.name.subject = dentry;
416 prim.name.old_name = dentry->short_name;
417 ret = record_update_primitive(j, prim);
421 dentry->short_name = NULL;
422 dentry->short_name_nbytes = 0;
427 next_command(struct update_command_journal *j)
433 commit_update(struct update_command_journal *j)
435 for (size_t i = 0; i < j->num_cmds; i++)
437 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
439 if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
440 j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
442 FREE(j->cmd_prims[i].entries[k].name.old_name);
446 free_update_command_journal(j);
450 rollback_update(struct update_command_journal *j)
452 /* Rollback each logical update command, in reverse order. */
453 size_t i = j->cur_cmd;
457 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
458 free_update_command_journal(j);
462 set_branch_name(struct wim_dentry *branch, const utf16lechar *target)
464 const utf16lechar *p;
470 /* No trailing slashes allowed */
471 wimlib_assert(p == target || *(p - 1) != cpu_to_le16(WIM_PATH_SEPARATOR));
473 while (p > target && *(p - 1) != cpu_to_le16(WIM_PATH_SEPARATOR))
476 return dentry_set_name_utf16le(branch, p);
480 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
481 struct update_command_journal *j,
482 int add_flags, wimlib_progress_func_t progress_func)
484 bool branch_is_dir = dentry_is_directory(branch);
485 bool existing_is_dir = dentry_is_directory(existing);
487 if (branch_is_dir != existing_is_dir) {
488 if (existing_is_dir) {
489 ERROR("\"%"TS"\" is a directory!\n"
490 " Specify the path at which "
491 "to place the file inside this directory.",
492 dentry_full_path(existing));
493 return WIMLIB_ERR_IS_DIRECTORY;
495 ERROR("Can't place directory at \"%"TS"\" because "
496 "a nondirectory file already exists there!",
497 dentry_full_path(existing));
498 return WIMLIB_ERR_NOTDIR;
503 /* Directory overlay */
504 while (dentry_has_children(branch)) {
505 struct wim_dentry *new_child;
506 struct wim_dentry *existing_child;
509 new_child = dentry_any_child(branch);
512 get_dentry_child_with_utf16le_name(existing,
513 new_child->file_name,
514 new_child->file_name_nbytes,
515 WIMLIB_CASE_PLATFORM_DEFAULT);
516 unlink_dentry(new_child);
517 if (existing_child) {
518 ret = handle_conflict(new_child, existing_child,
519 j, add_flags, progress_func);
521 ret = journaled_link(j, new_child, existing);
524 dentry_add_child(branch, new_child);
530 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
531 /* Can't replace nondirectory file */
532 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
533 dentry_full_path(existing));
534 return WIMLIB_ERR_INVALID_OVERLAY;
536 /* Replace nondirectory file */
537 struct wim_dentry *parent;
540 parent = existing->parent;
542 ret = calculate_dentry_full_path(existing);
546 ret = journaled_unlink(j, existing);
550 ret = journaled_link(j, branch, parent);
554 if (progress_func && (add_flags & WIMLIB_ADD_FLAG_VERBOSE)) {
555 union wimlib_progress_info info;
557 info.replace.path_in_wim = existing->_full_path;
558 progress_func(WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM, &info);
565 do_attach_branch(struct wim_dentry *branch, utf16lechar *target,
566 struct update_command_journal *j,
567 int add_flags, wimlib_progress_func_t progress_func)
569 struct wim_dentry *parent;
570 struct wim_dentry *existing;
571 utf16lechar empty_name[1] = {0};
572 utf16lechar *cur_component_name;
573 utf16lechar *next_component_name;
576 /* Attempt to create root directory before proceeding to the "real"
579 existing = *j->root_p;
580 cur_component_name = empty_name;
582 /* Skip leading slashes */
583 next_component_name = target;
584 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
585 next_component_name++;
587 while (*next_component_name) { /* While not the last component ... */
591 /* Descend into existing directory */
592 if (!dentry_is_directory(existing)) {
593 ERROR("\"%"TS"\" in the WIM image "
594 "is not a directory!",
595 dentry_full_path(existing));
596 return WIMLIB_ERR_NOTDIR;
599 /* A parent directory of the target didn't exist. Make
600 * the way by creating a filler directory. */
601 struct wim_dentry *filler;
603 ret = new_filler_directory(T(""), &filler);
606 ret = dentry_set_name_utf16le(filler,
612 ret = journaled_link(j, filler, parent);
620 /* Advance to next component */
622 cur_component_name = next_component_name;
623 end = cur_component_name + 1;
624 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
627 next_component_name = end;
629 /* There will still be more components after this. */
632 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
633 wimlib_assert(*next_component_name); /* No trailing slashes */
635 /* This will be the last component */
636 next_component_name = end;
639 existing = get_dentry_child_with_utf16le_name(
642 (end - cur_component_name) * sizeof(utf16lechar),
643 WIMLIB_CASE_PLATFORM_DEFAULT);
648 return handle_conflict(branch, existing, j,
649 add_flags, progress_func);
651 return journaled_link(j, branch, parent);
656 * Place the directory entry tree @branch at the path @target_tstr in the WIM
659 * @target_tstr cannot contain trailing slashes, and all path separators must be
660 * WIM_PATH_SEPARATOR.
662 * On success, @branch is committed to the journal @j.
663 * Otherwise @branch is freed.
665 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
666 * WIMLIB_ADD_FLAG_VERBOSE.
669 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
670 struct update_command_journal *j,
671 int add_flags, wimlib_progress_func_t progress_func)
676 if (unlikely(!branch))
680 target = memdup(target_tstr,
681 (tstrlen(target_tstr) + 1) * sizeof(target_tstr[0]));
683 ret = WIMLIB_ERR_NOMEM;
684 goto out_free_branch;
688 size_t target_nbytes;
689 ret = tstr_to_utf16le(target_tstr,
690 tstrlen(target_tstr) * sizeof(target_tstr[0]),
691 &target, &target_nbytes);
693 goto out_free_branch;
697 ret = set_branch_name(branch, target);
699 goto out_free_target;
701 ret = do_attach_branch(branch, target, j, add_flags, progress_func);
703 goto out_free_target;
704 /* branch was successfully committed to the journal */
709 free_dentry_tree(branch, j->lookup_table);
713 static const char wincfg[] =
718 "/System Volume Information\n"
722 static const tchar *wimboot_cfgfile =
723 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
724 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
725 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
728 get_capture_config(const tchar *config_file, struct capture_config *config,
729 int add_flags, const tchar *fs_source_path)
732 tchar *tmp_config_file = NULL;
734 memset(config, 0, sizeof(*config));
736 /* For WIMBoot capture, check for default capture configuration file
737 * unless one was explicitly specified. */
738 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
740 /* XXX: Handle loading file correctly when in NTFS volume. */
742 size_t len = tstrlen(fs_source_path) +
743 tstrlen(wimboot_cfgfile);
744 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
747 tsprintf(tmp_config_file, T("%"TS"%"TS),
748 fs_source_path, wimboot_cfgfile);
749 if (!tstat(tmp_config_file, &st)) {
750 config_file = tmp_config_file;
751 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
753 WARNING("\"%"TS"\" does not exist.\n"
754 " Using default capture configuration!",
759 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
760 /* Use Windows default. */
762 return WIMLIB_ERR_INVALID_PARAM;
763 ret = do_read_capture_config_file(T("wincfg"), wincfg,
764 sizeof(wincfg) - 1, config);
765 } else if (config_file) {
766 /* Use the specified configuration file. */
767 ret = do_read_capture_config_file(config_file, NULL, 0, config);
769 /* ... Or don't use any configuration file at all. No files
770 * will be excluded from capture, all files will be compressed,
774 FREE(tmp_config_file);
779 execute_add_command(struct update_command_journal *j,
781 const struct wimlib_update_command *add_cmd,
782 struct wim_inode_table *inode_table,
783 struct wim_sd_set *sd_set,
784 struct list_head *unhashed_streams,
785 wimlib_progress_func_t progress_func)
789 tchar *fs_source_path;
790 tchar *wim_target_path;
791 const tchar *config_file;
792 struct add_image_params params;
793 struct capture_config config;
794 capture_tree_t capture_tree = platform_default_capture_tree;
796 struct _ntfs_volume *ntfs_vol = NULL;
798 void *extra_arg = NULL;
799 struct wim_dentry *branch;
801 add_flags = add_cmd->add.add_flags;
802 fs_source_path = add_cmd->add.fs_source_path;
803 wim_target_path = add_cmd->add.wim_target_path;
804 config_file = add_cmd->add.config_file;
806 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
807 fs_source_path, wim_target_path, add_flags);
809 memset(¶ms, 0, sizeof(params));
811 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
813 capture_tree = build_dentry_tree_ntfs;
814 extra_arg = &ntfs_vol;
815 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
816 ERROR("NTFS volume already set");
817 ret = WIMLIB_ERR_INVALID_PARAM;
821 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.progress_func = progress_func;
840 params.progress.scan.source = fs_source_path;
841 params.progress.scan.wim_target_path = wim_target_path;
843 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, ¶ms.progress);
845 config.prefix = fs_source_path;
846 config.prefix_num_tchars = tstrlen(fs_source_path);
848 if (wim_target_path[0] == T('\0'))
849 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
850 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
852 goto out_destroy_config;
855 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, ¶ms.progress);
857 if (wim_target_path[0] == T('\0') &&
858 branch && !dentry_is_directory(branch))
860 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
861 ret = WIMLIB_ERR_NOTDIR;
862 free_dentry_tree(branch, wim->lookup_table);
863 goto out_cleanup_after_capture;
866 ret = attach_branch(branch, wim_target_path, j,
867 add_flags, params.progress_func);
869 goto out_cleanup_after_capture;
871 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
872 wim_target_path[0] == T('\0'))
874 params.add_flags = 0;
875 params.progress_func = NULL;
876 params.config = NULL;
878 /* If a capture configuration file was explicitly specified when
879 * capturing an image in WIMBoot mode, save it as
880 * /Windows/System32/WimBootCompress.ini in the WIM image. */
881 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
883 goto out_cleanup_after_capture;
885 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL);
887 goto out_cleanup_after_capture;
891 wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
893 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
894 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
896 goto out_destroy_config;
897 out_cleanup_after_capture:
900 do_ntfs_umount(ntfs_vol);
903 destroy_capture_config(&config);
909 execute_delete_command(struct update_command_journal *j,
911 const struct wimlib_update_command *delete_cmd)
914 const tchar *wim_path;
915 struct wim_dentry *tree;
917 flags = delete_cmd->delete_.delete_flags;
918 wim_path = delete_cmd->delete_.wim_path;
920 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
922 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
924 /* Path to delete does not exist in the WIM. */
925 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
928 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
929 wim_path, wim->current_image);
930 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
934 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
935 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
936 "but a recursive delete was not requested",
937 wim_path, wim->current_image);
938 return WIMLIB_ERR_IS_DIRECTORY;
941 return journaled_unlink(j, tree);
945 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
947 FREE(dentry->_full_path);
948 dentry->_full_path = NULL;
952 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
954 is_ancestor(struct wim_dentry *d1, struct wim_dentry *d2)
959 if (dentry_is_root(d2))
965 /* Rename a file or directory in the WIM.
967 * This returns a -errno value.
969 * The journal @j is optional.
972 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
973 CASE_SENSITIVITY_TYPE case_type,
974 struct update_command_journal *j)
976 struct wim_dentry *src;
977 struct wim_dentry *dst;
978 struct wim_dentry *parent_of_dst;
981 /* This rename() implementation currently only supports actual files
982 * (not alternate data streams) */
984 src = get_dentry(wim, from, case_type);
988 dst = get_dentry(wim, to, case_type);
991 /* Destination file exists */
993 if (src == dst) /* Same file */
996 if (!dentry_is_directory(src)) {
997 /* Cannot rename non-directory to directory. */
998 if (dentry_is_directory(dst))
1001 /* Cannot rename directory to a non-directory or a non-empty
1003 if (!dentry_is_directory(dst))
1005 if (dentry_has_children(dst))
1008 parent_of_dst = dst->parent;
1010 /* Destination does not exist */
1011 parent_of_dst = get_parent_dentry(wim, to, case_type);
1015 if (!dentry_is_directory(parent_of_dst))
1019 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
1020 * from the tree and creating a loop... */
1021 if (is_ancestor(src, parent_of_dst))
1026 if (journaled_unlink(j, dst))
1028 if (journaled_unlink(j, src))
1030 if (journaled_change_name(j, src, path_basename(to)))
1032 if (journaled_link(j, src, parent_of_dst))
1035 ret = dentry_set_name(src, path_basename(to));
1040 free_dentry_tree(dst, wim->lookup_table);
1043 dentry_add_child(parent_of_dst, src);
1045 if (src->_full_path)
1046 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1052 execute_rename_command(struct update_command_journal *j,
1054 const struct wimlib_update_command *rename_cmd)
1058 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1059 rename_cmd->rename.wim_target_path,
1060 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1064 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1065 rename_cmd->rename.wim_source_path,
1066 rename_cmd->rename.wim_target_path);
1069 ret = WIMLIB_ERR_NOMEM;
1072 ret = WIMLIB_ERR_NOTDIR;
1076 /* XXX: EBUSY is returned when the rename would create a
1077 * loop. It maybe should have its own error code. */
1078 ret = WIMLIB_ERR_NOTEMPTY;
1081 ret = WIMLIB_ERR_IS_DIRECTORY;
1085 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1092 static inline const tchar *
1093 update_op_to_str(int op)
1096 case WIMLIB_UPDATE_OP_ADD:
1098 case WIMLIB_UPDATE_OP_DELETE:
1100 case WIMLIB_UPDATE_OP_RENAME:
1109 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1110 enum wimlib_update_op op)
1112 for (size_t i = 0; i < num_cmds; i++)
1113 if (cmds[i].op == op)
1119 execute_update_commands(WIMStruct *wim,
1120 const struct wimlib_update_command *cmds,
1123 wimlib_progress_func_t progress_func)
1125 struct wim_inode_table *inode_table;
1126 struct wim_sd_set *sd_set;
1127 struct list_head unhashed_streams;
1128 struct update_command_journal *j;
1129 union wimlib_progress_info info;
1132 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1133 /* If we have at least one "add" command, create the inode and
1134 * security descriptor tables to index new inodes and new
1135 * security descriptors, respectively. */
1136 inode_table = alloca(sizeof(struct wim_inode_table));
1137 sd_set = alloca(sizeof(struct wim_sd_set));
1139 ret = init_inode_table(inode_table, 9001);
1143 ret = init_sd_set(sd_set, wim_security_data(wim));
1145 goto out_destroy_inode_table;
1147 INIT_LIST_HEAD(&unhashed_streams);
1153 /* Start an in-memory journal to allow rollback if something goes wrong
1155 j = new_update_command_journal(num_cmds,
1156 &wim_get_current_image_metadata(wim)->root_dentry,
1159 ret = WIMLIB_ERR_NOMEM;
1160 goto out_destroy_sd_set;
1163 info.update.completed_commands = 0;
1164 info.update.total_commands = num_cmds;
1166 for (size_t i = 0; i < num_cmds; i++) {
1167 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1168 i + 1, num_cmds, update_op_to_str(cmds[i].op));
1169 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
1172 info.update.command = &cmds[i];
1173 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1176 ret = WIMLIB_ERR_INVALID_PARAM;
1177 switch (cmds[i].op) {
1178 case WIMLIB_UPDATE_OP_ADD:
1179 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1180 sd_set, &unhashed_streams,
1183 case WIMLIB_UPDATE_OP_DELETE:
1184 ret = execute_delete_command(j, wim, &cmds[i]);
1186 case WIMLIB_UPDATE_OP_RENAME:
1187 ret = execute_rename_command(j, wim, &cmds[i]);
1192 info.update.completed_commands++;
1193 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
1196 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1204 struct wim_image_metadata *imd;
1206 imd = wim_get_current_image_metadata(wim);
1208 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1209 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1211 goto out_destroy_sd_set;
1215 rollback_new_security_descriptors(sd_set);
1219 destroy_sd_set(sd_set);
1220 out_destroy_inode_table:
1222 destroy_inode_table(inode_table);
1229 check_add_command(struct wimlib_update_command *cmd,
1230 const struct wim_header *hdr)
1232 int add_flags = cmd->add.add_flags;
1234 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1235 WIMLIB_ADD_FLAG_DEREFERENCE |
1236 WIMLIB_ADD_FLAG_VERBOSE |
1237 /* BOOT doesn't make sense for wimlib_update_image(). */
1238 /*WIMLIB_ADD_FLAG_BOOT |*/
1239 WIMLIB_ADD_FLAG_UNIX_DATA |
1240 WIMLIB_ADD_FLAG_NO_ACLS |
1241 WIMLIB_ADD_FLAG_STRICT_ACLS |
1242 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1243 WIMLIB_ADD_FLAG_RPFIX |
1244 WIMLIB_ADD_FLAG_NORPFIX |
1245 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1246 WIMLIB_ADD_FLAG_WINCONFIG |
1247 WIMLIB_ADD_FLAG_WIMBOOT |
1248 WIMLIB_ADD_FLAG_NO_REPLACE))
1249 return WIMLIB_ERR_INVALID_PARAM;
1251 /* Are we adding the entire image or not? An empty wim_target_path
1252 * indicates that the tree we're adding is to be placed in the root of
1253 * the image. We consider this to be capturing the entire image,
1254 * although it could potentially be an overlay on an existing root as
1256 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
1259 /* Check for flags not supported on Windows */
1260 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1261 ERROR("wimlib was compiled without support for NTFS-3g, so");
1262 ERROR("we cannot capture a WIM image directly from a NTFS volume");
1263 return WIMLIB_ERR_UNSUPPORTED;
1265 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1266 ERROR("Capturing UNIX-specific data is not supported on Windows");
1267 return WIMLIB_ERR_UNSUPPORTED;
1269 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1270 ERROR("Dereferencing symbolic links is not supported on Windows");
1271 return WIMLIB_ERR_UNSUPPORTED;
1275 /* VERBOSE implies EXCLUDE_VERBOSE */
1276 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1277 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1279 /* Check for contradictory reparse point fixup flags */
1280 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1281 WIMLIB_ADD_FLAG_NORPFIX)) ==
1282 (WIMLIB_ADD_FLAG_RPFIX |
1283 WIMLIB_ADD_FLAG_NORPFIX))
1285 ERROR("Cannot specify RPFIX and NORPFIX flags "
1286 "at the same time!");
1287 return WIMLIB_ERR_INVALID_PARAM;
1290 /* Set default behavior on reparse point fixups if requested */
1291 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1292 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1294 /* Do reparse-point fixups by default if we are capturing an
1295 * entire image and either the header flag is set from previous
1296 * images, or if this is the first image being added. */
1297 if (is_entire_image &&
1298 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1299 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1302 if (!is_entire_image) {
1303 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1304 ERROR("Cannot add directly from a NTFS volume "
1305 "when not capturing a full image!");
1306 return WIMLIB_ERR_INVALID_PARAM;
1309 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1310 ERROR("Cannot do reparse point fixups when "
1311 "not capturing a full image!");
1312 return WIMLIB_ERR_INVALID_PARAM;
1315 /* We may have modified the add flags. */
1316 cmd->add.add_flags = add_flags;
1321 check_delete_command(const struct wimlib_update_command *cmd)
1323 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1324 WIMLIB_DELETE_FLAG_RECURSIVE))
1325 return WIMLIB_ERR_INVALID_PARAM;
1330 check_rename_command(const struct wimlib_update_command *cmd)
1332 if (cmd->rename.rename_flags != 0)
1333 return WIMLIB_ERR_INVALID_PARAM;
1338 check_update_command(struct wimlib_update_command *cmd,
1339 const struct wim_header *hdr)
1342 case WIMLIB_UPDATE_OP_ADD:
1343 return check_add_command(cmd, hdr);
1344 case WIMLIB_UPDATE_OP_DELETE:
1345 return check_delete_command(cmd);
1346 case WIMLIB_UPDATE_OP_RENAME:
1347 return check_rename_command(cmd);
1353 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1354 const struct wim_header *hdr)
1357 for (size_t i = 0; i < num_cmds; i++) {
1358 ret = check_update_command(&cmds[i], hdr);
1367 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1370 for (size_t i = 0; i < num_cmds; i++) {
1371 switch (cmds[i].op) {
1372 case WIMLIB_UPDATE_OP_ADD:
1373 FREE(cmds[i].add.fs_source_path);
1374 FREE(cmds[i].add.wim_target_path);
1375 FREE(cmds[i].add.config_file);
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 =
1407 canonicalize_fs_path(cmds[i].add.fs_source_path);
1408 cmds_copy[i].add.wim_target_path =
1409 canonicalize_wim_path(cmds[i].add.wim_target_path);
1410 if (!cmds_copy[i].add.fs_source_path ||
1411 !cmds_copy[i].add.wim_target_path)
1413 if (cmds[i].add.config_file) {
1414 cmds_copy[i].add.config_file = TSTRDUP(cmds[i].add.config_file);
1415 if (!cmds_copy[i].add.config_file)
1418 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1420 case WIMLIB_UPDATE_OP_DELETE:
1421 cmds_copy[i].delete_.wim_path =
1422 canonicalize_wim_path(cmds[i].delete_.wim_path);
1423 if (!cmds_copy[i].delete_.wim_path)
1425 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1427 case WIMLIB_UPDATE_OP_RENAME:
1428 cmds_copy[i].rename.wim_source_path =
1429 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1430 cmds_copy[i].rename.wim_target_path =
1431 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1432 if (!cmds_copy[i].rename.wim_source_path ||
1433 !cmds_copy[i].rename.wim_target_path)
1437 ERROR("Unknown update operation %u", cmds[i].op);
1438 ret = WIMLIB_ERR_INVALID_PARAM;
1442 *cmds_copy_ret = cmds_copy;
1447 ret = WIMLIB_ERR_NOMEM;
1449 free_update_commands(cmds_copy, num_cmds);
1453 /* API function documented in wimlib.h */
1455 wimlib_update_image(WIMStruct *wim,
1457 const struct wimlib_update_command *cmds,
1460 wimlib_progress_func_t progress_func)
1463 struct wimlib_update_command *cmds_copy;
1465 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1466 return WIMLIB_ERR_INVALID_PARAM;
1468 DEBUG("Updating image %d with %zu commands", image, num_cmds);
1470 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_DELETE))
1471 ret = can_delete_from_wim(wim);
1473 ret = can_modify_wim(wim);
1478 /* Load the metadata for the image to modify (if not loaded already) */
1479 ret = select_wim_image(wim, image);
1483 DEBUG("Preparing %zu update commands", num_cmds);
1485 /* Make a copy of the update commands, in the process doing certain
1486 * canonicalizations on paths (e.g. translating backslashes to forward
1487 * slashes). This is done to avoid modifying the caller's copy of the
1489 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1493 /* Perform additional checks on the update commands before we execute
1495 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1497 goto out_free_cmds_copy;
1499 /* Actually execute the update commands. */
1500 DEBUG("Executing %zu update commands", num_cmds);
1501 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
1504 goto out_free_cmds_copy;
1506 wim->image_metadata[image - 1]->modified = 1;
1508 /* Statistics about the WIM image, such as the numbers of files and
1509 * directories, may have changed. Call xml_update_image_info() to
1510 * recalculate these statistics. */
1511 xml_update_image_info(wim, image);
1513 free_update_commands(cmds_copy, num_cmds);