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/endianness.h"
32 #include "wimlib/error.h"
33 #include "wimlib/lookup_table.h"
34 #include "wimlib/metadata.h"
36 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
38 #include "wimlib/paths.h"
39 #include "wimlib/progress.h"
40 #include "wimlib/xml.h"
50 /* Saved specification of a "primitive" update operation that was performed. */
51 struct update_primitive {
53 /* Unlinked a dentry from its parent directory. */
56 /* Linked a dentry into its parent directory. */
59 /* Changed the file name of a dentry. */
62 /* Changed the short name of a dentry. */
67 /* For UNLINK_DENTRY and LINK_DENTRY operations */
69 /* Dentry that was linked or unlinked. */
70 struct wim_dentry *subject;
72 /* For link operations, the directory into which
73 * @subject was linked, or NULL if @subject was set as
74 * the root of the image.
76 * For unlink operations, the directory from which
77 * @subject was unlinked, or NULL if @subject was unset
78 * as the root of the image. */
79 struct wim_dentry *parent;
82 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
84 /* Dentry that had its name changed. */
85 struct wim_dentry *subject;
88 utf16lechar *old_name;
93 /* Chronological list of primitive operations that were executed for a single
94 * logical update command, such as 'add', 'delete', or 'rename'. */
95 struct update_primitive_list {
96 struct update_primitive *entries;
97 struct update_primitive inline_entries[4];
99 size_t num_alloc_entries;
102 /* Journal for managing the executing of zero or more logical update commands,
103 * such as 'add', 'delete', or 'rename'. This allows either committing or
104 * rolling back the commands. */
105 struct update_command_journal {
106 /* Number of update commands this journal contains. */
109 /* Index of currently executing update command. */
112 /* Location of the WIM image's root pointer. */
113 struct wim_dentry **root_p;
115 /* Pointer to the lookup table of the WIM (may needed for rollback) */
116 struct wim_lookup_table *lookup_table;
118 /* List of dentries that are currently unlinked from the WIM image.
119 * These must be freed when no longer needed for commit or rollback. */
120 struct list_head orphans;
122 /* Per-command logs. */
123 struct update_primitive_list cmd_prims[];
127 init_update_primitive_list(struct update_primitive_list *l)
129 l->entries = l->inline_entries;
131 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
134 /* Allocates a new journal for managing the execution of up to @num_cmds update
136 static struct update_command_journal *
137 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
138 struct wim_lookup_table *lookup_table)
140 struct update_command_journal *j;
142 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
144 j->num_cmds = num_cmds;
147 j->lookup_table = lookup_table;
148 INIT_LIST_HEAD(&j->orphans);
149 for (size_t i = 0; i < num_cmds; i++)
150 init_update_primitive_list(&j->cmd_prims[i]);
155 /* Don't call this directly; use commit_update() or rollback_update() instead.
158 free_update_command_journal(struct update_command_journal *j)
160 struct wim_dentry *orphan;
162 /* Free orphaned dentry trees */
163 while (!list_empty(&j->orphans)) {
164 orphan = list_first_entry(&j->orphans,
165 struct wim_dentry, tmp_list);
166 list_del(&orphan->tmp_list);
167 free_dentry_tree(orphan, j->lookup_table);
170 for (size_t i = 0; i < j->num_cmds; i++)
171 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
172 FREE(j->cmd_prims[i].entries);
176 /* Add the entry @prim to the update command journal @j. */
178 record_update_primitive(struct update_command_journal *j,
179 struct update_primitive prim)
181 struct update_primitive_list *l;
183 l = &j->cmd_prims[j->cur_cmd];
185 if (l->num_entries == l->num_alloc_entries) {
186 struct update_primitive *new_entries;
187 size_t new_num_alloc_entries;
190 new_num_alloc_entries = l->num_alloc_entries * 2;
191 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
192 if (l->entries == l->inline_entries) {
193 new_entries = MALLOC(new_size);
195 return WIMLIB_ERR_NOMEM;
196 memcpy(new_entries, l->inline_entries,
197 sizeof(l->inline_entries));
199 new_entries = REALLOC(l->entries, new_size);
201 return WIMLIB_ERR_NOMEM;
203 l->entries = new_entries;
204 l->num_alloc_entries = new_num_alloc_entries;
206 l->entries[l->num_entries++] = prim;
211 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
212 struct wim_dentry **root_p)
215 /* Unlink @subject from its @parent. */
216 wimlib_assert(subject->d_parent == parent);
217 unlink_dentry(subject);
219 /* Unset @subject as the root of the image. */
222 subject->d_parent = subject;
226 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
227 struct wim_dentry **root_p)
230 /* Link @subject to its @parent */
231 struct wim_dentry *existing;
233 existing = dentry_add_child(parent, subject);
234 wimlib_assert(!existing);
236 /* Set @subject as root of the image */
241 /* Undo a link operation. */
243 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
244 struct wim_dentry **root_p, struct list_head *orphans)
246 /* Unlink is the opposite of link */
247 do_unlink(subject, parent, root_p);
249 /* @subject is now unlinked. Add it to orphans. */
250 list_add(&subject->tmp_list, orphans);
251 subject->is_orphan = 1;
254 /* Undo an unlink operation. */
256 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
257 struct wim_dentry **root_p)
259 /* Link is the opposite of unlink */
260 do_link(subject, parent, root_p);
262 /* @subject is no longer unlinked. Delete it from orphans. */
263 list_del(&subject->tmp_list);
264 subject->is_orphan = 0;
267 /* Rollback a name change operation. */
269 rollback_name_change(utf16lechar *old_name,
270 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
272 /* Free the new name, then replace it with the old name. */
275 *name_ptr = old_name;
276 *name_nbytes_ptr = utf16le_strlen(old_name);
279 *name_nbytes_ptr = 0;
283 /* Rollback a primitive update operation. */
285 rollback_update_primitive(const struct update_primitive *prim,
286 struct wim_dentry **root_p,
287 struct list_head *orphans)
289 switch (prim->type) {
291 rollback_link(prim->link.subject, prim->link.parent, root_p,
295 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
297 case CHANGE_FILE_NAME:
298 rollback_name_change(prim->name.old_name,
299 &prim->name.subject->file_name,
300 &prim->name.subject->file_name_nbytes);
302 case CHANGE_SHORT_NAME:
303 rollback_name_change(prim->name.old_name,
304 &prim->name.subject->short_name,
305 &prim->name.subject->short_name_nbytes);
310 /* Rollback a logical update command */
312 rollback_update_command(const struct update_primitive_list *l,
313 struct wim_dentry **root_p,
314 struct list_head *orphans)
316 size_t i = l->num_entries;
318 /* Rollback each primitive operation, in reverse order. */
320 rollback_update_primitive(&l->entries[i], root_p, orphans);
323 /****************************************************************************/
325 /* Link @subject into the directory @parent; or, if @parent is NULL, set
326 * @subject as the root of the WIM image.
328 * This is the journaled version, so it can be rolled back. */
330 journaled_link(struct update_command_journal *j,
331 struct wim_dentry *subject, struct wim_dentry *parent)
333 struct update_primitive prim;
336 prim.type = LINK_DENTRY;
337 prim.link.subject = subject;
338 prim.link.parent = parent;
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)
359 struct wim_dentry *parent;
360 struct update_primitive prim;
363 if (dentry_is_root(subject))
366 parent = subject->d_parent;
368 prim.type = UNLINK_DENTRY;
369 prim.link.subject = subject;
370 prim.link.parent = parent;
372 ret = record_update_primitive(j, prim);
376 do_unlink(subject, parent, j->root_p);
378 list_add(&subject->tmp_list, &j->orphans);
379 subject->is_orphan = 1;
383 /* Change the name of @dentry to @new_name_tstr.
385 * This is the journaled version, so it can be rolled back. */
387 journaled_change_name(struct update_command_journal *j,
388 struct wim_dentry *dentry, const tchar *new_name_tstr)
391 utf16lechar *new_name;
392 size_t new_name_nbytes;
393 struct update_primitive prim;
395 /* Set the long name. */
396 ret = tstr_to_utf16le(new_name_tstr,
397 tstrlen(new_name_tstr) * sizeof(tchar),
398 &new_name, &new_name_nbytes);
402 prim.type = CHANGE_FILE_NAME;
403 prim.name.subject = dentry;
404 prim.name.old_name = dentry->file_name;
405 ret = record_update_primitive(j, prim);
411 dentry->file_name = new_name;
412 dentry->file_name_nbytes = new_name_nbytes;
414 /* Clear the short name. */
415 prim.type = CHANGE_SHORT_NAME;
416 prim.name.subject = dentry;
417 prim.name.old_name = dentry->short_name;
418 ret = record_update_primitive(j, prim);
422 dentry->short_name = NULL;
423 dentry->short_name_nbytes = 0;
428 next_command(struct update_command_journal *j)
434 commit_update(struct update_command_journal *j)
436 for (size_t i = 0; i < j->num_cmds; i++)
438 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
440 if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
441 j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
443 FREE(j->cmd_prims[i].entries[k].name.old_name);
447 free_update_command_journal(j);
451 rollback_update(struct update_command_journal *j)
453 /* Rollback each logical update command, in reverse order. */
454 size_t i = j->cur_cmd;
458 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
459 free_update_command_journal(j);
463 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
464 struct update_command_journal *j,
466 wimlib_progress_func_t progfunc, void *progctx)
468 bool branch_is_dir = dentry_is_directory(branch);
469 bool existing_is_dir = dentry_is_directory(existing);
471 if (branch_is_dir != existing_is_dir) {
472 if (existing_is_dir) {
473 ERROR("\"%"TS"\" is a directory!\n"
474 " Specify the path at which "
475 "to place the file inside this directory.",
476 dentry_full_path(existing));
477 return WIMLIB_ERR_IS_DIRECTORY;
479 ERROR("Can't place directory at \"%"TS"\" because "
480 "a nondirectory file already exists there!",
481 dentry_full_path(existing));
482 return WIMLIB_ERR_NOTDIR;
487 /* Directory overlay */
488 while (dentry_has_children(branch)) {
489 struct wim_dentry *new_child;
490 struct wim_dentry *existing_child;
493 new_child = dentry_any_child(branch);
496 get_dentry_child_with_utf16le_name(existing,
497 new_child->file_name,
498 new_child->file_name_nbytes,
499 WIMLIB_CASE_PLATFORM_DEFAULT);
500 unlink_dentry(new_child);
501 if (existing_child) {
502 ret = handle_conflict(new_child, existing_child,
506 ret = journaled_link(j, new_child, existing);
509 dentry_add_child(branch, new_child);
515 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
516 /* Can't replace nondirectory file */
517 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
518 dentry_full_path(existing));
519 return WIMLIB_ERR_INVALID_OVERLAY;
521 /* Replace nondirectory file */
522 struct wim_dentry *parent;
525 parent = existing->d_parent;
527 ret = calculate_dentry_full_path(existing);
531 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
532 union wimlib_progress_info info;
534 info.replace.path_in_wim = existing->_full_path;
535 ret = call_progress(progfunc,
536 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
542 ret = journaled_unlink(j, existing);
546 return journaled_link(j, branch, parent);
551 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
552 struct update_command_journal *j,
553 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
555 struct wim_dentry *parent;
556 struct wim_dentry *existing;
557 const utf16lechar empty_name[1] = {0};
558 const utf16lechar *cur_component_name;
559 size_t cur_component_nbytes;
560 const utf16lechar *next_component_name;
563 /* Attempt to create root directory before proceeding to the "real"
566 existing = *j->root_p;
567 cur_component_name = empty_name;
568 cur_component_nbytes = 0;
570 /* Skip leading slashes */
571 next_component_name = target;
572 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
573 next_component_name++;
575 while (*next_component_name) { /* While not the last component ... */
576 const utf16lechar *end;
579 /* Descend into existing directory */
580 if (!dentry_is_directory(existing)) {
581 ERROR("\"%"TS"\" in the WIM image "
582 "is not a directory!",
583 dentry_full_path(existing));
584 return WIMLIB_ERR_NOTDIR;
587 /* A parent directory of the target didn't exist. Make
588 * the way by creating a filler directory. */
589 struct wim_dentry *filler;
591 ret = new_filler_directory(&filler);
594 ret = dentry_set_name_utf16le(filler,
596 cur_component_nbytes);
601 ret = journaled_link(j, filler, parent);
609 /* Advance to next component */
611 cur_component_name = next_component_name;
612 end = cur_component_name + 1;
613 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
616 next_component_name = end;
618 /* There will still be more components after this. */
620 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
621 wimlib_assert(*next_component_name); /* No trailing slashes */
623 /* This will be the last component */
624 next_component_name = end;
627 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
628 existing = get_dentry_child_with_utf16le_name(
631 cur_component_nbytes,
632 WIMLIB_CASE_PLATFORM_DEFAULT);
637 return handle_conflict(branch, existing, j, add_flags,
640 return journaled_link(j, branch, parent);
645 * Place the directory entry tree @branch at the path @target_tstr in the WIM
648 * @target_tstr cannot contain trailing slashes, and all path separators must be
649 * WIM_PATH_SEPARATOR.
651 * On success, @branch is committed to the journal @j.
652 * Otherwise @branch is freed.
654 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
655 * WIMLIB_ADD_FLAG_VERBOSE.
658 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
659 struct update_command_journal *j, int add_flags,
660 wimlib_progress_func_t progfunc, void *progctx)
663 const utf16lechar *target;
666 if (unlikely(!branch))
669 ret = tstr_get_utf16le(target_tstr, &target);
671 goto out_free_branch;
673 BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
674 ret = dentry_set_name(branch, path_basename(target_tstr));
676 goto out_free_target;
678 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
680 goto out_free_target;
681 /* branch was successfully committed to the journal */
684 tstr_put_utf16le(target);
686 free_dentry_tree(branch, j->lookup_table);
691 static const char wincfg[] =
697 "/System Volume Information\n"
701 static const tchar *wimboot_cfgfile =
702 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
703 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
704 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
707 get_capture_config(const tchar *config_file, struct capture_config *config,
708 int add_flags, const tchar *fs_source_path)
711 tchar *tmp_config_file = NULL;
713 memset(config, 0, sizeof(*config));
715 /* For WIMBoot capture, check for default capture configuration file
716 * unless one was explicitly specified. */
717 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
719 /* XXX: Handle loading file correctly when in NTFS volume. */
721 size_t len = tstrlen(fs_source_path) +
722 tstrlen(wimboot_cfgfile);
723 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
726 tsprintf(tmp_config_file, T("%"TS"%"TS),
727 fs_source_path, wimboot_cfgfile);
728 if (!tstat(tmp_config_file, &st)) {
729 config_file = tmp_config_file;
730 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
732 WARNING("\"%"TS"\" does not exist.\n"
733 " Using default capture configuration!",
738 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
739 /* Use Windows default. */
741 return WIMLIB_ERR_INVALID_PARAM;
742 ret = read_capture_config(T("wincfg"), wincfg,
743 sizeof(wincfg) - 1, config);
744 } else if (config_file) {
745 /* Use the specified configuration file. */
746 ret = read_capture_config(config_file, NULL, 0, config);
748 /* ... Or don't use any configuration file at all. No files
749 * will be excluded from capture, all files will be compressed,
753 FREE(tmp_config_file);
758 execute_add_command(struct update_command_journal *j,
760 const struct wimlib_update_command *add_cmd,
761 struct wim_inode_table *inode_table,
762 struct wim_sd_set *sd_set,
763 struct list_head *unhashed_streams)
767 tchar *fs_source_path;
768 tchar *wim_target_path;
769 const tchar *config_file;
770 struct add_image_params params;
771 struct capture_config config;
772 capture_tree_t capture_tree = platform_default_capture_tree;
774 struct _ntfs_volume *ntfs_vol = NULL;
776 void *extra_arg = NULL;
777 struct wim_dentry *branch;
779 add_flags = add_cmd->add.add_flags;
780 fs_source_path = add_cmd->add.fs_source_path;
781 wim_target_path = add_cmd->add.wim_target_path;
782 config_file = add_cmd->add.config_file;
784 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
785 fs_source_path, wim_target_path, add_flags);
787 memset(¶ms, 0, sizeof(params));
790 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
791 capture_tree = build_dentry_tree_ntfs;
792 extra_arg = &ntfs_vol;
793 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
794 ERROR("NTFS volume already set");
795 ret = WIMLIB_ERR_INVALID_PARAM;
801 ret = get_capture_config(config_file, &config,
802 add_flags, fs_source_path);
806 params.lookup_table = wim->lookup_table;
807 params.unhashed_streams = unhashed_streams;
808 params.inode_table = inode_table;
809 params.sd_set = sd_set;
810 params.config = &config;
811 params.add_flags = add_flags;
812 params.extra_arg = extra_arg;
814 params.progfunc = wim->progfunc;
815 params.progctx = wim->progctx;
816 params.progress.scan.source = fs_source_path;
817 params.progress.scan.wim_target_path = wim_target_path;
818 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
819 ¶ms.progress, params.progctx);
821 goto out_destroy_config;
823 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
824 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
825 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
827 goto out_destroy_config;
829 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
830 ¶ms.progress, params.progctx);
832 free_dentry_tree(branch, wim->lookup_table);
833 goto out_cleanup_after_capture;
836 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
837 branch && !dentry_is_directory(branch))
839 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
840 ret = WIMLIB_ERR_NOTDIR;
841 free_dentry_tree(branch, wim->lookup_table);
842 goto out_cleanup_after_capture;
845 ret = attach_branch(branch, wim_target_path, j,
846 add_flags, params.progfunc, params.progctx);
848 goto out_cleanup_after_capture;
850 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
851 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
853 params.add_flags = 0;
854 params.progfunc = NULL;
855 params.config = NULL;
857 /* If a capture configuration file was explicitly specified when
858 * capturing an image in WIMBoot mode, save it as
859 * /Windows/System32/WimBootCompress.ini in the WIM image. */
860 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
862 goto out_cleanup_after_capture;
864 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
866 goto out_cleanup_after_capture;
870 wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
872 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
873 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
875 goto out_destroy_config;
876 out_cleanup_after_capture:
879 do_ntfs_umount(ntfs_vol);
882 destroy_capture_config(&config);
888 execute_delete_command(struct update_command_journal *j,
890 const struct wimlib_update_command *delete_cmd)
893 const tchar *wim_path;
894 struct wim_dentry *tree;
896 flags = delete_cmd->delete_.delete_flags;
897 wim_path = delete_cmd->delete_.wim_path;
899 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
901 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
903 /* Path to delete does not exist in the WIM. */
904 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
907 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
908 wim_path, wim->current_image);
909 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
913 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
914 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
915 "but a recursive delete was not requested",
916 wim_path, wim->current_image);
917 return WIMLIB_ERR_IS_DIRECTORY;
920 return journaled_unlink(j, tree);
924 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
926 FREE(dentry->_full_path);
927 dentry->_full_path = NULL;
931 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
933 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
938 if (dentry_is_root(d2))
944 /* Rename a file or directory in the WIM.
946 * This returns a -errno value.
948 * The journal @j is optional.
951 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
952 CASE_SENSITIVITY_TYPE case_type,
953 struct update_command_journal *j)
955 struct wim_dentry *src;
956 struct wim_dentry *dst;
957 struct wim_dentry *parent_of_dst;
960 /* This rename() implementation currently only supports actual files
961 * (not alternate data streams) */
963 src = get_dentry(wim, from, case_type);
967 dst = get_dentry(wim, to, case_type);
970 /* Destination file exists */
972 if (src == dst) /* Same file */
975 if (!dentry_is_directory(src)) {
976 /* Cannot rename non-directory to directory. */
977 if (dentry_is_directory(dst))
980 /* Cannot rename directory to a non-directory or a non-empty
982 if (!dentry_is_directory(dst))
984 if (dentry_has_children(dst))
987 parent_of_dst = dst->d_parent;
989 /* Destination does not exist */
990 parent_of_dst = get_parent_dentry(wim, to, case_type);
994 if (!dentry_is_directory(parent_of_dst))
998 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
999 * from the tree and creating a loop... */
1000 if (is_ancestor(src, parent_of_dst))
1005 if (journaled_unlink(j, dst))
1007 if (journaled_unlink(j, src))
1009 if (journaled_change_name(j, src, path_basename(to)))
1011 if (journaled_link(j, src, parent_of_dst))
1014 ret = dentry_set_name(src, path_basename(to));
1019 free_dentry_tree(dst, wim->lookup_table);
1022 dentry_add_child(parent_of_dst, src);
1024 if (src->_full_path)
1025 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1031 execute_rename_command(struct update_command_journal *j,
1033 const struct wimlib_update_command *rename_cmd)
1037 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1038 rename_cmd->rename.wim_target_path,
1039 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1043 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1044 rename_cmd->rename.wim_source_path,
1045 rename_cmd->rename.wim_target_path);
1048 ret = WIMLIB_ERR_NOMEM;
1051 ret = WIMLIB_ERR_NOTDIR;
1055 /* XXX: EBUSY is returned when the rename would create a
1056 * loop. It maybe should have its own error code. */
1057 ret = WIMLIB_ERR_NOTEMPTY;
1060 ret = WIMLIB_ERR_IS_DIRECTORY;
1064 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1071 static inline const tchar *
1072 update_op_to_str(int op)
1075 case WIMLIB_UPDATE_OP_ADD:
1077 case WIMLIB_UPDATE_OP_DELETE:
1079 case WIMLIB_UPDATE_OP_RENAME:
1088 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1089 enum wimlib_update_op op)
1091 for (size_t i = 0; i < num_cmds; i++)
1092 if (cmds[i].op == op)
1098 execute_update_commands(WIMStruct *wim,
1099 const struct wimlib_update_command *cmds,
1103 struct wim_inode_table *inode_table;
1104 struct wim_sd_set *sd_set;
1105 struct list_head unhashed_streams;
1106 struct update_command_journal *j;
1107 union wimlib_progress_info info;
1110 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1111 /* If we have at least one "add" command, create the inode and
1112 * security descriptor tables to index new inodes and new
1113 * security descriptors, respectively. */
1114 inode_table = alloca(sizeof(struct wim_inode_table));
1115 sd_set = alloca(sizeof(struct wim_sd_set));
1117 ret = init_inode_table(inode_table, 9001);
1121 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1123 goto out_destroy_inode_table;
1125 INIT_LIST_HEAD(&unhashed_streams);
1131 /* Start an in-memory journal to allow rollback if something goes wrong
1133 j = new_update_command_journal(num_cmds,
1134 &wim_get_current_image_metadata(wim)->root_dentry,
1137 ret = WIMLIB_ERR_NOMEM;
1138 goto out_destroy_sd_set;
1141 info.update.completed_commands = 0;
1142 info.update.total_commands = num_cmds;
1144 for (size_t i = 0; i < num_cmds; i++) {
1145 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1146 i + 1, num_cmds, update_op_to_str(cmds[i].op));
1147 info.update.command = &cmds[i];
1148 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1149 ret = call_progress(wim->progfunc,
1150 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1151 &info, wim->progctx);
1156 switch (cmds[i].op) {
1157 case WIMLIB_UPDATE_OP_ADD:
1158 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1159 sd_set, &unhashed_streams);
1161 case WIMLIB_UPDATE_OP_DELETE:
1162 ret = execute_delete_command(j, wim, &cmds[i]);
1164 case WIMLIB_UPDATE_OP_RENAME:
1165 ret = execute_rename_command(j, wim, &cmds[i]);
1170 info.update.completed_commands++;
1171 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1172 ret = call_progress(wim->progfunc,
1173 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1174 &info, wim->progctx);
1183 struct wim_image_metadata *imd;
1185 imd = wim_get_current_image_metadata(wim);
1187 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1188 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1190 goto out_destroy_sd_set;
1194 rollback_new_security_descriptors(sd_set);
1198 destroy_sd_set(sd_set);
1199 out_destroy_inode_table:
1201 destroy_inode_table(inode_table);
1208 check_add_command(struct wimlib_update_command *cmd,
1209 const struct wim_header *hdr)
1211 int add_flags = cmd->add.add_flags;
1213 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1214 WIMLIB_ADD_FLAG_DEREFERENCE |
1215 WIMLIB_ADD_FLAG_VERBOSE |
1216 /* BOOT doesn't make sense for wimlib_update_image(). */
1217 /*WIMLIB_ADD_FLAG_BOOT |*/
1218 WIMLIB_ADD_FLAG_UNIX_DATA |
1219 WIMLIB_ADD_FLAG_NO_ACLS |
1220 WIMLIB_ADD_FLAG_STRICT_ACLS |
1221 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1222 WIMLIB_ADD_FLAG_RPFIX |
1223 WIMLIB_ADD_FLAG_NORPFIX |
1224 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1225 WIMLIB_ADD_FLAG_WINCONFIG |
1226 WIMLIB_ADD_FLAG_WIMBOOT |
1227 WIMLIB_ADD_FLAG_NO_REPLACE |
1228 WIMLIB_ADD_FLAG_TEST_FILE_EXCLUSION))
1229 return WIMLIB_ERR_INVALID_PARAM;
1231 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1233 #ifndef WITH_NTFS_3G
1234 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1235 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1236 " we cannot capture a WIM image directly "
1237 "from an NTFS volume");
1238 return WIMLIB_ERR_UNSUPPORTED;
1243 /* Check for flags not supported on Windows */
1244 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1245 ERROR("Capturing UNIX-specific data is not supported on Windows");
1246 return WIMLIB_ERR_UNSUPPORTED;
1248 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1249 ERROR("Dereferencing symbolic links is not supported on Windows");
1250 return WIMLIB_ERR_UNSUPPORTED;
1254 /* VERBOSE implies EXCLUDE_VERBOSE */
1255 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1256 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1258 /* Check for contradictory reparse point fixup flags */
1259 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1260 WIMLIB_ADD_FLAG_NORPFIX)) ==
1261 (WIMLIB_ADD_FLAG_RPFIX |
1262 WIMLIB_ADD_FLAG_NORPFIX))
1264 ERROR("Cannot specify RPFIX and NORPFIX flags "
1265 "at the same time!");
1266 return WIMLIB_ERR_INVALID_PARAM;
1269 /* Set default behavior on reparse point fixups if requested */
1270 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1271 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1273 /* Do reparse-point fixups by default if we are capturing an
1274 * entire image and either the header flag is set from previous
1275 * images, or if this is the first image being added. */
1276 if (is_entire_image &&
1277 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1278 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1281 if (!is_entire_image) {
1282 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1283 ERROR("Cannot add directly from an NTFS volume "
1284 "when not capturing a full image!");
1285 return WIMLIB_ERR_INVALID_PARAM;
1288 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1289 ERROR("Cannot do reparse point fixups when "
1290 "not capturing a full image!");
1291 return WIMLIB_ERR_INVALID_PARAM;
1294 /* We may have modified the add flags. */
1295 cmd->add.add_flags = add_flags;
1300 check_delete_command(const struct wimlib_update_command *cmd)
1302 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1303 WIMLIB_DELETE_FLAG_RECURSIVE))
1304 return WIMLIB_ERR_INVALID_PARAM;
1309 check_rename_command(const struct wimlib_update_command *cmd)
1311 if (cmd->rename.rename_flags != 0)
1312 return WIMLIB_ERR_INVALID_PARAM;
1317 check_update_command(struct wimlib_update_command *cmd,
1318 const struct wim_header *hdr)
1321 case WIMLIB_UPDATE_OP_ADD:
1322 return check_add_command(cmd, hdr);
1323 case WIMLIB_UPDATE_OP_DELETE:
1324 return check_delete_command(cmd);
1325 case WIMLIB_UPDATE_OP_RENAME:
1326 return check_rename_command(cmd);
1332 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1333 const struct wim_header *hdr)
1336 for (size_t i = 0; i < num_cmds; i++) {
1337 ret = check_update_command(&cmds[i], hdr);
1346 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1349 for (size_t i = 0; i < num_cmds; i++) {
1350 switch (cmds[i].op) {
1351 case WIMLIB_UPDATE_OP_ADD:
1352 FREE(cmds[i].add.wim_target_path);
1354 case WIMLIB_UPDATE_OP_DELETE:
1355 FREE(cmds[i].delete_.wim_path);
1357 case WIMLIB_UPDATE_OP_RENAME:
1358 FREE(cmds[i].rename.wim_source_path);
1359 FREE(cmds[i].rename.wim_target_path);
1368 copy_update_commands(const struct wimlib_update_command *cmds,
1370 struct wimlib_update_command **cmds_copy_ret)
1373 struct wimlib_update_command *cmds_copy;
1375 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1379 for (size_t i = 0; i < num_cmds; i++) {
1380 cmds_copy[i].op = cmds[i].op;
1381 switch (cmds[i].op) {
1382 case WIMLIB_UPDATE_OP_ADD:
1383 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1384 cmds_copy[i].add.wim_target_path =
1385 canonicalize_wim_path(cmds[i].add.wim_target_path);
1386 if (!cmds_copy[i].add.wim_target_path)
1388 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1389 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1391 case WIMLIB_UPDATE_OP_DELETE:
1392 cmds_copy[i].delete_.wim_path =
1393 canonicalize_wim_path(cmds[i].delete_.wim_path);
1394 if (!cmds_copy[i].delete_.wim_path)
1396 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1398 case WIMLIB_UPDATE_OP_RENAME:
1399 cmds_copy[i].rename.wim_source_path =
1400 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1401 cmds_copy[i].rename.wim_target_path =
1402 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1403 if (!cmds_copy[i].rename.wim_source_path ||
1404 !cmds_copy[i].rename.wim_target_path)
1408 ERROR("Unknown update operation %u", cmds[i].op);
1409 ret = WIMLIB_ERR_INVALID_PARAM;
1413 *cmds_copy_ret = cmds_copy;
1418 ret = WIMLIB_ERR_NOMEM;
1420 free_update_commands(cmds_copy, num_cmds);
1424 /* API function documented in wimlib.h */
1426 wimlib_update_image(WIMStruct *wim,
1428 const struct wimlib_update_command *cmds,
1433 struct wimlib_update_command *cmds_copy;
1435 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1436 return WIMLIB_ERR_INVALID_PARAM;
1438 DEBUG("Updating image %d with %zu commands", image, num_cmds);
1440 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_DELETE))
1441 ret = can_delete_from_wim(wim);
1443 ret = can_modify_wim(wim);
1448 /* Load the metadata for the image to modify (if not loaded already) */
1449 ret = select_wim_image(wim, image);
1453 DEBUG("Preparing %zu update commands", num_cmds);
1455 /* Make a copy of the update commands, in the process doing certain
1456 * canonicalizations on paths (e.g. translating backslashes to forward
1457 * slashes). This is done to avoid modifying the caller's copy of the
1459 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1463 /* Perform additional checks on the update commands before we execute
1465 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1467 goto out_free_cmds_copy;
1469 /* Actually execute the update commands. */
1470 DEBUG("Executing %zu update commands", num_cmds);
1471 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1473 goto out_free_cmds_copy;
1475 wim->image_metadata[image - 1]->modified = 1;
1477 /* Statistics about the WIM image, such as the numbers of files and
1478 * directories, may have changed. Call xml_update_image_info() to
1479 * recalculate these statistics. */
1480 xml_update_image_info(wim, image);
1482 free_update_commands(cmds_copy, num_cmds);
1488 update1(WIMStruct *wim, int image, const struct wimlib_update_command *cmd)
1490 return wimlib_update_image(wim, image, cmd, 1, 0);
1494 wimlib_delete_path(WIMStruct *wim, int image,
1495 const tchar *path, int delete_flags)
1497 struct wimlib_update_command cmd;
1499 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1500 cmd.delete_.wim_path = (tchar *)path;
1501 cmd.delete_.delete_flags = delete_flags;
1503 return update1(wim, image, &cmd);
1507 wimlib_rename_path(WIMStruct *wim, int image,
1508 const tchar *source_path, const tchar *dest_path)
1510 struct wimlib_update_command cmd;
1512 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1513 cmd.rename.wim_source_path = (tchar *)source_path;
1514 cmd.rename.wim_target_path = (tchar *)dest_path;
1515 cmd.rename.rename_flags = 0;
1517 return update1(wim, image, &cmd);
1521 wimlib_add_tree(WIMStruct *wim, int image,
1522 const tchar *fs_source_path, const tchar *wim_target_path,
1525 struct wimlib_update_command cmd;
1527 cmd.op = WIMLIB_UPDATE_OP_ADD;
1528 cmd.add.fs_source_path = (tchar *)fs_source_path;
1529 cmd.add.wim_target_path = (tchar *)wim_target_path;
1530 cmd.add.add_flags = add_flags;
1531 cmd.add.config_file = NULL;
1533 return update1(wim, image, &cmd);