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/progress.h"
39 #include "wimlib/xml.h"
49 /* Saved specification of a "primitive" update operation that was performed. */
50 struct update_primitive {
52 /* Unlinked a dentry from its parent directory. */
55 /* Linked a dentry into its parent directory. */
58 /* Changed the file name of a dentry. */
61 /* Changed the short name of a dentry. */
66 /* For UNLINK_DENTRY and LINK_DENTRY operations */
68 /* Dentry that was linked or unlinked. */
69 struct wim_dentry *subject;
71 /* For link operations, the directory into which
72 * @subject was linked, or NULL if @subject was set as
73 * the root of the image.
75 * For unlink operations, the directory from which
76 * @subject was unlinked, or NULL if @subject was unset
77 * as the root of the image. */
78 struct wim_dentry *parent;
81 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
83 /* Dentry that had its name changed. */
84 struct wim_dentry *subject;
87 utf16lechar *old_name;
92 /* Chronological list of primitive operations that were executed for a single
93 * logical update command, such as 'add', 'delete', or 'rename'. */
94 struct update_primitive_list {
95 struct update_primitive *entries;
96 struct update_primitive inline_entries[4];
98 size_t num_alloc_entries;
101 /* Journal for managing the executing of zero or more logical update commands,
102 * such as 'add', 'delete', or 'rename'. This allows either committing or
103 * rolling back the commands. */
104 struct update_command_journal {
105 /* Number of update commands this journal contains. */
108 /* Index of currently executing update command. */
111 /* Location of the WIM image's root pointer. */
112 struct wim_dentry **root_p;
114 /* Pointer to the lookup table of the WIM (may needed for rollback) */
115 struct wim_lookup_table *lookup_table;
117 /* List of dentries that are currently unlinked from the WIM image.
118 * These must be freed when no longer needed for commit or rollback. */
119 struct list_head orphans;
121 /* Per-command logs. */
122 struct update_primitive_list cmd_prims[];
126 init_update_primitive_list(struct update_primitive_list *l)
128 l->entries = l->inline_entries;
130 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
133 /* Allocates a new journal for managing the execution of up to @num_cmds update
135 static struct update_command_journal *
136 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
137 struct wim_lookup_table *lookup_table)
139 struct update_command_journal *j;
141 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
143 j->num_cmds = num_cmds;
146 j->lookup_table = lookup_table;
147 INIT_LIST_HEAD(&j->orphans);
148 for (size_t i = 0; i < num_cmds; i++)
149 init_update_primitive_list(&j->cmd_prims[i]);
154 /* Don't call this directly; use commit_update() or rollback_update() instead.
157 free_update_command_journal(struct update_command_journal *j)
159 struct wim_dentry *orphan;
161 /* Free orphaned dentry trees */
162 while (!list_empty(&j->orphans)) {
163 orphan = list_first_entry(&j->orphans,
164 struct wim_dentry, tmp_list);
165 list_del(&orphan->tmp_list);
166 free_dentry_tree(orphan, j->lookup_table);
169 for (size_t i = 0; i < j->num_cmds; i++)
170 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
171 FREE(j->cmd_prims[i].entries);
175 /* Add the entry @prim to the update command journal @j. */
177 record_update_primitive(struct update_command_journal *j,
178 struct update_primitive prim)
180 struct update_primitive_list *l;
182 l = &j->cmd_prims[j->cur_cmd];
184 if (l->num_entries == l->num_alloc_entries) {
185 struct update_primitive *new_entries;
186 size_t new_num_alloc_entries;
189 new_num_alloc_entries = l->num_alloc_entries * 2;
190 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
191 if (l->entries == l->inline_entries) {
192 new_entries = MALLOC(new_size);
194 return WIMLIB_ERR_NOMEM;
195 memcpy(new_entries, l->inline_entries,
196 sizeof(l->inline_entries));
198 new_entries = REALLOC(l->entries, new_size);
200 return WIMLIB_ERR_NOMEM;
202 l->entries = new_entries;
203 l->num_alloc_entries = new_num_alloc_entries;
205 l->entries[l->num_entries++] = prim;
210 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
211 struct wim_dentry **root_p)
214 /* Unlink @subject from its @parent. */
215 wimlib_assert(subject->parent == parent);
216 unlink_dentry(subject);
218 /* Unset @subject as the root of the image. */
221 subject->parent = subject;
225 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
226 struct wim_dentry **root_p)
229 /* Link @subject to its @parent */
230 struct wim_dentry *existing;
232 existing = dentry_add_child(parent, subject);
233 wimlib_assert(!existing);
235 /* Set @subject as root of the image */
240 /* Undo a link operation. */
242 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
243 struct wim_dentry **root_p, struct list_head *orphans)
245 /* Unlink is the opposite of link */
246 do_unlink(subject, parent, root_p);
248 /* @subject is now unlinked. Add it to orphans. */
249 list_add(&subject->tmp_list, orphans);
250 subject->is_orphan = 1;
253 /* Undo an unlink operation. */
255 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
256 struct wim_dentry **root_p)
258 /* Link is the opposite of unlink */
259 do_link(subject, parent, root_p);
261 /* @subject is no longer unlinked. Delete it from orphans. */
262 list_del(&subject->tmp_list);
263 subject->is_orphan = 0;
266 /* Rollback a name change operation. */
268 rollback_name_change(utf16lechar *old_name,
269 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
271 /* Free the new name, then replace it with the old name. */
274 *name_ptr = old_name;
275 *name_nbytes_ptr = utf16le_strlen(old_name);
278 *name_nbytes_ptr = 0;
282 /* Rollback a primitive update operation. */
284 rollback_update_primitive(const struct update_primitive *prim,
285 struct wim_dentry **root_p,
286 struct list_head *orphans)
288 switch (prim->type) {
290 rollback_link(prim->link.subject, prim->link.parent, root_p,
294 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
296 case CHANGE_FILE_NAME:
297 rollback_name_change(prim->name.old_name,
298 &prim->name.subject->file_name,
299 &prim->name.subject->file_name_nbytes);
301 case CHANGE_SHORT_NAME:
302 rollback_name_change(prim->name.old_name,
303 &prim->name.subject->short_name,
304 &prim->name.subject->short_name_nbytes);
309 /* Rollback a logical update command */
311 rollback_update_command(const struct update_primitive_list *l,
312 struct wim_dentry **root_p,
313 struct list_head *orphans)
315 size_t i = l->num_entries;
317 /* Rollback each primitive operation, in reverse order. */
319 rollback_update_primitive(&l->entries[i], root_p, orphans);
322 /****************************************************************************/
324 /* Link @subject into the directory @parent; or, if @parent is NULL, set
325 * @subject as the root of the WIM image.
327 * This is the journaled version, so it can be rolled back. */
329 journaled_link(struct update_command_journal *j,
330 struct wim_dentry *subject, struct wim_dentry *parent)
332 struct update_primitive prim;
335 prim.type = LINK_DENTRY;
336 prim.link.subject = subject;
337 prim.link.parent = parent;
339 ret = record_update_primitive(j, prim);
343 do_link(subject, parent, j->root_p);
345 if (subject->is_orphan) {
346 list_del(&subject->tmp_list);
347 subject->is_orphan = 0;
352 /* Unlink @subject from the WIM image.
354 * This is the journaled version, so it can be rolled back. */
356 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
358 struct wim_dentry *parent;
359 struct update_primitive prim;
362 if (dentry_is_root(subject))
365 parent = subject->parent;
367 prim.type = UNLINK_DENTRY;
368 prim.link.subject = subject;
369 prim.link.parent = parent;
371 ret = record_update_primitive(j, prim);
375 do_unlink(subject, parent, j->root_p);
377 list_add(&subject->tmp_list, &j->orphans);
378 subject->is_orphan = 1;
382 /* Change the name of @dentry to @new_name_tstr.
384 * This is the journaled version, so it can be rolled back. */
386 journaled_change_name(struct update_command_journal *j,
387 struct wim_dentry *dentry, const tchar *new_name_tstr)
390 utf16lechar *new_name;
391 size_t new_name_nbytes;
392 struct update_primitive prim;
394 /* Set the long name. */
395 ret = tstr_to_utf16le(new_name_tstr,
396 tstrlen(new_name_tstr) * sizeof(tchar),
397 &new_name, &new_name_nbytes);
401 prim.type = CHANGE_FILE_NAME;
402 prim.name.subject = dentry;
403 prim.name.old_name = dentry->file_name;
404 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 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
463 struct update_command_journal *j,
465 wimlib_progress_func_t progfunc, void *progctx)
467 bool branch_is_dir = dentry_is_directory(branch);
468 bool existing_is_dir = dentry_is_directory(existing);
470 if (branch_is_dir != existing_is_dir) {
471 if (existing_is_dir) {
472 ERROR("\"%"TS"\" is a directory!\n"
473 " Specify the path at which "
474 "to place the file inside this directory.",
475 dentry_full_path(existing));
476 return WIMLIB_ERR_IS_DIRECTORY;
478 ERROR("Can't place directory at \"%"TS"\" because "
479 "a nondirectory file already exists there!",
480 dentry_full_path(existing));
481 return WIMLIB_ERR_NOTDIR;
486 /* Directory overlay */
487 while (dentry_has_children(branch)) {
488 struct wim_dentry *new_child;
489 struct wim_dentry *existing_child;
492 new_child = dentry_any_child(branch);
495 get_dentry_child_with_utf16le_name(existing,
496 new_child->file_name,
497 new_child->file_name_nbytes,
498 WIMLIB_CASE_PLATFORM_DEFAULT);
499 unlink_dentry(new_child);
500 if (existing_child) {
501 ret = handle_conflict(new_child, existing_child,
505 ret = journaled_link(j, new_child, existing);
508 dentry_add_child(branch, new_child);
514 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
515 /* Can't replace nondirectory file */
516 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
517 dentry_full_path(existing));
518 return WIMLIB_ERR_INVALID_OVERLAY;
520 /* Replace nondirectory file */
521 struct wim_dentry *parent;
523 union wimlib_progress_info info;
525 parent = existing->parent;
527 ret = calculate_dentry_full_path(existing);
531 ret = journaled_unlink(j, existing);
535 ret = journaled_link(j, branch, parent);
540 info.replace.path_in_wim = existing->_full_path;
541 return call_progress(progfunc,
542 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
548 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
549 struct update_command_journal *j,
550 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
552 struct wim_dentry *parent;
553 struct wim_dentry *existing;
554 const utf16lechar empty_name[1] = {0};
555 const utf16lechar *cur_component_name;
556 size_t cur_component_nbytes;
557 const utf16lechar *next_component_name;
560 /* Attempt to create root directory before proceeding to the "real"
563 existing = *j->root_p;
564 cur_component_name = empty_name;
565 cur_component_nbytes = 0;
567 /* Skip leading slashes */
568 next_component_name = target;
569 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
570 next_component_name++;
572 while (*next_component_name) { /* While not the last component ... */
573 const utf16lechar *end;
576 /* Descend into existing directory */
577 if (!dentry_is_directory(existing)) {
578 ERROR("\"%"TS"\" in the WIM image "
579 "is not a directory!",
580 dentry_full_path(existing));
581 return WIMLIB_ERR_NOTDIR;
584 /* A parent directory of the target didn't exist. Make
585 * the way by creating a filler directory. */
586 struct wim_dentry *filler;
588 ret = new_filler_directory(&filler);
591 ret = dentry_set_name_utf16le(filler,
593 cur_component_nbytes);
598 ret = journaled_link(j, filler, parent);
606 /* Advance to next component */
608 cur_component_name = next_component_name;
609 end = cur_component_name + 1;
610 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
613 next_component_name = end;
615 /* There will still be more components after this. */
617 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
618 wimlib_assert(*next_component_name); /* No trailing slashes */
620 /* This will be the last component */
621 next_component_name = end;
624 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
625 existing = get_dentry_child_with_utf16le_name(
628 cur_component_nbytes,
629 WIMLIB_CASE_PLATFORM_DEFAULT);
634 return handle_conflict(branch, existing, j, add_flags,
637 return journaled_link(j, branch, parent);
642 * Place the directory entry tree @branch at the path @target_tstr in the WIM
645 * @target_tstr cannot contain trailing slashes, and all path separators must be
646 * WIM_PATH_SEPARATOR.
648 * On success, @branch is committed to the journal @j.
649 * Otherwise @branch is freed.
651 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
652 * WIMLIB_ADD_FLAG_VERBOSE.
655 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
656 struct update_command_journal *j, int add_flags,
657 wimlib_progress_func_t progfunc, void *progctx)
660 const utf16lechar *target;
663 if (unlikely(!branch))
666 ret = tstr_get_utf16le(target_tstr, &target);
668 goto out_free_branch;
670 BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
671 ret = dentry_set_name(branch, path_basename(target_tstr));
673 goto out_free_target;
675 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
677 goto out_free_target;
678 /* branch was successfully committed to the journal */
681 tstr_put_utf16le(target);
683 free_dentry_tree(branch, j->lookup_table);
688 static const char wincfg[] =
694 "/System Volume Information\n"
698 static const tchar *wimboot_cfgfile =
699 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
700 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
701 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
704 get_capture_config(const tchar *config_file, struct capture_config *config,
705 int add_flags, const tchar *fs_source_path)
708 tchar *tmp_config_file = NULL;
710 memset(config, 0, sizeof(*config));
712 /* For WIMBoot capture, check for default capture configuration file
713 * unless one was explicitly specified. */
714 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
716 /* XXX: Handle loading file correctly when in NTFS volume. */
718 size_t len = tstrlen(fs_source_path) +
719 tstrlen(wimboot_cfgfile);
720 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
723 tsprintf(tmp_config_file, T("%"TS"%"TS),
724 fs_source_path, wimboot_cfgfile);
725 if (!tstat(tmp_config_file, &st)) {
726 config_file = tmp_config_file;
727 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
729 WARNING("\"%"TS"\" does not exist.\n"
730 " Using default capture configuration!",
735 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
736 /* Use Windows default. */
738 return WIMLIB_ERR_INVALID_PARAM;
739 ret = read_capture_config(T("wincfg"), wincfg,
740 sizeof(wincfg) - 1, config);
741 } else if (config_file) {
742 /* Use the specified configuration file. */
743 ret = read_capture_config(config_file, NULL, 0, config);
745 /* ... Or don't use any configuration file at all. No files
746 * will be excluded from capture, all files will be compressed,
750 FREE(tmp_config_file);
755 execute_add_command(struct update_command_journal *j,
757 const struct wimlib_update_command *add_cmd,
758 struct wim_inode_table *inode_table,
759 struct wim_sd_set *sd_set,
760 struct list_head *unhashed_streams)
764 tchar *fs_source_path;
765 tchar *wim_target_path;
766 const tchar *config_file;
767 struct add_image_params params;
768 struct capture_config config;
769 capture_tree_t capture_tree = platform_default_capture_tree;
771 struct _ntfs_volume *ntfs_vol = NULL;
773 void *extra_arg = NULL;
774 struct wim_dentry *branch;
776 add_flags = add_cmd->add.add_flags;
777 fs_source_path = add_cmd->add.fs_source_path;
778 wim_target_path = add_cmd->add.wim_target_path;
779 config_file = add_cmd->add.config_file;
781 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
782 fs_source_path, wim_target_path, add_flags);
784 memset(¶ms, 0, sizeof(params));
787 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
788 capture_tree = build_dentry_tree_ntfs;
789 extra_arg = &ntfs_vol;
790 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
791 ERROR("NTFS volume already set");
792 ret = WIMLIB_ERR_INVALID_PARAM;
798 ret = get_capture_config(config_file, &config,
799 add_flags, fs_source_path);
803 params.lookup_table = wim->lookup_table;
804 params.unhashed_streams = unhashed_streams;
805 params.inode_table = inode_table;
806 params.sd_set = sd_set;
807 params.config = &config;
808 params.add_flags = add_flags;
809 params.extra_arg = extra_arg;
811 params.progfunc = wim->progfunc;
812 params.progctx = wim->progctx;
813 params.progress.scan.source = fs_source_path;
814 params.progress.scan.wim_target_path = wim_target_path;
815 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
816 ¶ms.progress, params.progctx);
818 goto out_destroy_config;
820 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
821 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
822 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
824 goto out_destroy_config;
826 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
827 ¶ms.progress, params.progctx);
829 free_dentry_tree(branch, wim->lookup_table);
830 goto out_cleanup_after_capture;
833 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
834 branch && !dentry_is_directory(branch))
836 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
837 ret = WIMLIB_ERR_NOTDIR;
838 free_dentry_tree(branch, wim->lookup_table);
839 goto out_cleanup_after_capture;
842 ret = attach_branch(branch, wim_target_path, j,
843 add_flags, params.progfunc, params.progctx);
845 goto out_cleanup_after_capture;
847 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
848 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
850 params.add_flags = 0;
851 params.progfunc = NULL;
852 params.config = NULL;
854 /* If a capture configuration file was explicitly specified when
855 * capturing an image in WIMBoot mode, save it as
856 * /Windows/System32/WimBootCompress.ini in the WIM image. */
857 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
859 goto out_cleanup_after_capture;
861 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
863 goto out_cleanup_after_capture;
867 wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
869 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
870 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
872 goto out_destroy_config;
873 out_cleanup_after_capture:
876 do_ntfs_umount(ntfs_vol);
879 destroy_capture_config(&config);
885 execute_delete_command(struct update_command_journal *j,
887 const struct wimlib_update_command *delete_cmd)
890 const tchar *wim_path;
891 struct wim_dentry *tree;
893 flags = delete_cmd->delete_.delete_flags;
894 wim_path = delete_cmd->delete_.wim_path;
896 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
898 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
900 /* Path to delete does not exist in the WIM. */
901 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
904 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
905 wim_path, wim->current_image);
906 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
910 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
911 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
912 "but a recursive delete was not requested",
913 wim_path, wim->current_image);
914 return WIMLIB_ERR_IS_DIRECTORY;
917 return journaled_unlink(j, tree);
921 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
923 FREE(dentry->_full_path);
924 dentry->_full_path = NULL;
928 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
930 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
935 if (dentry_is_root(d2))
941 /* Rename a file or directory in the WIM.
943 * This returns a -errno value.
945 * The journal @j is optional.
948 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
949 CASE_SENSITIVITY_TYPE case_type,
950 struct update_command_journal *j)
952 struct wim_dentry *src;
953 struct wim_dentry *dst;
954 struct wim_dentry *parent_of_dst;
957 /* This rename() implementation currently only supports actual files
958 * (not alternate data streams) */
960 src = get_dentry(wim, from, case_type);
964 dst = get_dentry(wim, to, case_type);
967 /* Destination file exists */
969 if (src == dst) /* Same file */
972 if (!dentry_is_directory(src)) {
973 /* Cannot rename non-directory to directory. */
974 if (dentry_is_directory(dst))
977 /* Cannot rename directory to a non-directory or a non-empty
979 if (!dentry_is_directory(dst))
981 if (dentry_has_children(dst))
984 parent_of_dst = dst->parent;
986 /* Destination does not exist */
987 parent_of_dst = get_parent_dentry(wim, to, case_type);
991 if (!dentry_is_directory(parent_of_dst))
995 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
996 * from the tree and creating a loop... */
997 if (is_ancestor(src, parent_of_dst))
1002 if (journaled_unlink(j, dst))
1004 if (journaled_unlink(j, src))
1006 if (journaled_change_name(j, src, path_basename(to)))
1008 if (journaled_link(j, src, parent_of_dst))
1011 ret = dentry_set_name(src, path_basename(to));
1016 free_dentry_tree(dst, wim->lookup_table);
1019 dentry_add_child(parent_of_dst, src);
1021 if (src->_full_path)
1022 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1028 execute_rename_command(struct update_command_journal *j,
1030 const struct wimlib_update_command *rename_cmd)
1034 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1035 rename_cmd->rename.wim_target_path,
1036 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1040 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1041 rename_cmd->rename.wim_source_path,
1042 rename_cmd->rename.wim_target_path);
1045 ret = WIMLIB_ERR_NOMEM;
1048 ret = WIMLIB_ERR_NOTDIR;
1052 /* XXX: EBUSY is returned when the rename would create a
1053 * loop. It maybe should have its own error code. */
1054 ret = WIMLIB_ERR_NOTEMPTY;
1057 ret = WIMLIB_ERR_IS_DIRECTORY;
1061 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1068 static inline const tchar *
1069 update_op_to_str(int op)
1072 case WIMLIB_UPDATE_OP_ADD:
1074 case WIMLIB_UPDATE_OP_DELETE:
1076 case WIMLIB_UPDATE_OP_RENAME:
1085 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1086 enum wimlib_update_op op)
1088 for (size_t i = 0; i < num_cmds; i++)
1089 if (cmds[i].op == op)
1095 execute_update_commands(WIMStruct *wim,
1096 const struct wimlib_update_command *cmds,
1100 struct wim_inode_table *inode_table;
1101 struct wim_sd_set *sd_set;
1102 struct list_head unhashed_streams;
1103 struct update_command_journal *j;
1104 union wimlib_progress_info info;
1107 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1108 /* If we have at least one "add" command, create the inode and
1109 * security descriptor tables to index new inodes and new
1110 * security descriptors, respectively. */
1111 inode_table = alloca(sizeof(struct wim_inode_table));
1112 sd_set = alloca(sizeof(struct wim_sd_set));
1114 ret = init_inode_table(inode_table, 9001);
1118 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1120 goto out_destroy_inode_table;
1122 INIT_LIST_HEAD(&unhashed_streams);
1128 /* Start an in-memory journal to allow rollback if something goes wrong
1130 j = new_update_command_journal(num_cmds,
1131 &wim_get_current_image_metadata(wim)->root_dentry,
1134 ret = WIMLIB_ERR_NOMEM;
1135 goto out_destroy_sd_set;
1138 info.update.completed_commands = 0;
1139 info.update.total_commands = num_cmds;
1141 for (size_t i = 0; i < num_cmds; i++) {
1142 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1143 i + 1, num_cmds, update_op_to_str(cmds[i].op));
1144 info.update.command = &cmds[i];
1145 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1146 ret = call_progress(wim->progfunc,
1147 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1148 &info, wim->progctx);
1153 ret = WIMLIB_ERR_INVALID_PARAM;
1154 switch (cmds[i].op) {
1155 case WIMLIB_UPDATE_OP_ADD:
1156 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1157 sd_set, &unhashed_streams);
1159 case WIMLIB_UPDATE_OP_DELETE:
1160 ret = execute_delete_command(j, wim, &cmds[i]);
1162 case WIMLIB_UPDATE_OP_RENAME:
1163 ret = execute_rename_command(j, wim, &cmds[i]);
1168 info.update.completed_commands++;
1169 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1170 ret = call_progress(wim->progfunc,
1171 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1172 &info, wim->progctx);
1181 struct wim_image_metadata *imd;
1183 imd = wim_get_current_image_metadata(wim);
1185 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1186 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1188 goto out_destroy_sd_set;
1192 rollback_new_security_descriptors(sd_set);
1196 destroy_sd_set(sd_set);
1197 out_destroy_inode_table:
1199 destroy_inode_table(inode_table);
1206 check_add_command(struct wimlib_update_command *cmd,
1207 const struct wim_header *hdr)
1209 int add_flags = cmd->add.add_flags;
1211 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1212 WIMLIB_ADD_FLAG_DEREFERENCE |
1213 WIMLIB_ADD_FLAG_VERBOSE |
1214 /* BOOT doesn't make sense for wimlib_update_image(). */
1215 /*WIMLIB_ADD_FLAG_BOOT |*/
1216 WIMLIB_ADD_FLAG_UNIX_DATA |
1217 WIMLIB_ADD_FLAG_NO_ACLS |
1218 WIMLIB_ADD_FLAG_STRICT_ACLS |
1219 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1220 WIMLIB_ADD_FLAG_RPFIX |
1221 WIMLIB_ADD_FLAG_NORPFIX |
1222 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1223 WIMLIB_ADD_FLAG_WINCONFIG |
1224 WIMLIB_ADD_FLAG_WIMBOOT |
1225 WIMLIB_ADD_FLAG_NO_REPLACE))
1226 return WIMLIB_ERR_INVALID_PARAM;
1228 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1230 #ifndef WITH_NTFS_3G
1231 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1232 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1233 " we cannot capture a WIM image directly "
1234 "from an NTFS volume");
1235 return WIMLIB_ERR_UNSUPPORTED;
1240 /* Check for flags not supported on Windows */
1241 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1242 ERROR("Capturing UNIX-specific data is not supported on Windows");
1243 return WIMLIB_ERR_UNSUPPORTED;
1245 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1246 ERROR("Dereferencing symbolic links is not supported on Windows");
1247 return WIMLIB_ERR_UNSUPPORTED;
1251 /* VERBOSE implies EXCLUDE_VERBOSE */
1252 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1253 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1255 /* Check for contradictory reparse point fixup flags */
1256 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1257 WIMLIB_ADD_FLAG_NORPFIX)) ==
1258 (WIMLIB_ADD_FLAG_RPFIX |
1259 WIMLIB_ADD_FLAG_NORPFIX))
1261 ERROR("Cannot specify RPFIX and NORPFIX flags "
1262 "at the same time!");
1263 return WIMLIB_ERR_INVALID_PARAM;
1266 /* Set default behavior on reparse point fixups if requested */
1267 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1268 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1270 /* Do reparse-point fixups by default if we are capturing an
1271 * entire image and either the header flag is set from previous
1272 * images, or if this is the first image being added. */
1273 if (is_entire_image &&
1274 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1275 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1278 if (!is_entire_image) {
1279 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1280 ERROR("Cannot add directly from an NTFS volume "
1281 "when not capturing a full image!");
1282 return WIMLIB_ERR_INVALID_PARAM;
1285 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1286 ERROR("Cannot do reparse point fixups when "
1287 "not capturing a full image!");
1288 return WIMLIB_ERR_INVALID_PARAM;
1291 /* We may have modified the add flags. */
1292 cmd->add.add_flags = add_flags;
1297 check_delete_command(const struct wimlib_update_command *cmd)
1299 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1300 WIMLIB_DELETE_FLAG_RECURSIVE))
1301 return WIMLIB_ERR_INVALID_PARAM;
1306 check_rename_command(const struct wimlib_update_command *cmd)
1308 if (cmd->rename.rename_flags != 0)
1309 return WIMLIB_ERR_INVALID_PARAM;
1314 check_update_command(struct wimlib_update_command *cmd,
1315 const struct wim_header *hdr)
1318 case WIMLIB_UPDATE_OP_ADD:
1319 return check_add_command(cmd, hdr);
1320 case WIMLIB_UPDATE_OP_DELETE:
1321 return check_delete_command(cmd);
1322 case WIMLIB_UPDATE_OP_RENAME:
1323 return check_rename_command(cmd);
1329 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1330 const struct wim_header *hdr)
1333 for (size_t i = 0; i < num_cmds; i++) {
1334 ret = check_update_command(&cmds[i], hdr);
1343 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1346 for (size_t i = 0; i < num_cmds; i++) {
1347 switch (cmds[i].op) {
1348 case WIMLIB_UPDATE_OP_ADD:
1349 FREE(cmds[i].add.wim_target_path);
1351 case WIMLIB_UPDATE_OP_DELETE:
1352 FREE(cmds[i].delete_.wim_path);
1354 case WIMLIB_UPDATE_OP_RENAME:
1355 FREE(cmds[i].rename.wim_source_path);
1356 FREE(cmds[i].rename.wim_target_path);
1365 copy_update_commands(const struct wimlib_update_command *cmds,
1367 struct wimlib_update_command **cmds_copy_ret)
1370 struct wimlib_update_command *cmds_copy;
1372 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1376 for (size_t i = 0; i < num_cmds; i++) {
1377 cmds_copy[i].op = cmds[i].op;
1378 switch (cmds[i].op) {
1379 case WIMLIB_UPDATE_OP_ADD:
1380 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1381 cmds_copy[i].add.wim_target_path =
1382 canonicalize_wim_path(cmds[i].add.wim_target_path);
1383 if (!cmds_copy[i].add.wim_target_path)
1385 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1386 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1388 case WIMLIB_UPDATE_OP_DELETE:
1389 cmds_copy[i].delete_.wim_path =
1390 canonicalize_wim_path(cmds[i].delete_.wim_path);
1391 if (!cmds_copy[i].delete_.wim_path)
1393 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1395 case WIMLIB_UPDATE_OP_RENAME:
1396 cmds_copy[i].rename.wim_source_path =
1397 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1398 cmds_copy[i].rename.wim_target_path =
1399 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1400 if (!cmds_copy[i].rename.wim_source_path ||
1401 !cmds_copy[i].rename.wim_target_path)
1405 ERROR("Unknown update operation %u", cmds[i].op);
1406 ret = WIMLIB_ERR_INVALID_PARAM;
1410 *cmds_copy_ret = cmds_copy;
1415 ret = WIMLIB_ERR_NOMEM;
1417 free_update_commands(cmds_copy, num_cmds);
1421 /* API function documented in wimlib.h */
1423 wimlib_update_image(WIMStruct *wim,
1425 const struct wimlib_update_command *cmds,
1430 struct wimlib_update_command *cmds_copy;
1432 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1433 return WIMLIB_ERR_INVALID_PARAM;
1435 DEBUG("Updating image %d with %zu commands", image, num_cmds);
1437 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_DELETE))
1438 ret = can_delete_from_wim(wim);
1440 ret = can_modify_wim(wim);
1445 /* Load the metadata for the image to modify (if not loaded already) */
1446 ret = select_wim_image(wim, image);
1450 DEBUG("Preparing %zu update commands", num_cmds);
1452 /* Make a copy of the update commands, in the process doing certain
1453 * canonicalizations on paths (e.g. translating backslashes to forward
1454 * slashes). This is done to avoid modifying the caller's copy of the
1456 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1460 /* Perform additional checks on the update commands before we execute
1462 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1464 goto out_free_cmds_copy;
1466 /* Actually execute the update commands. */
1467 DEBUG("Executing %zu update commands", num_cmds);
1468 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1470 goto out_free_cmds_copy;
1472 wim->image_metadata[image - 1]->modified = 1;
1474 /* Statistics about the WIM image, such as the numbers of files and
1475 * directories, may have changed. Call xml_update_image_info() to
1476 * recalculate these statistics. */
1477 xml_update_image_info(wim, image);
1479 free_update_commands(cmds_copy, num_cmds);
1485 update1(WIMStruct *wim, int image, const struct wimlib_update_command *cmd)
1487 return wimlib_update_image(wim, image, cmd, 1, 0);
1491 wimlib_delete_path(WIMStruct *wim, int image,
1492 const tchar *path, int delete_flags)
1494 struct wimlib_update_command cmd;
1496 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1497 cmd.delete_.wim_path = (tchar *)path;
1498 cmd.delete_.delete_flags = delete_flags;
1500 return update1(wim, image, &cmd);
1504 wimlib_rename_path(WIMStruct *wim, int image,
1505 const tchar *source_path, const tchar *dest_path)
1507 struct wimlib_update_command cmd;
1509 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1510 cmd.rename.wim_source_path = (tchar *)source_path;
1511 cmd.rename.wim_target_path = (tchar *)dest_path;
1512 cmd.rename.rename_flags = 0;
1514 return update1(wim, image, &cmd);
1518 wimlib_add_tree(WIMStruct *wim, int image,
1519 const tchar *fs_source_path, const tchar *wim_target_path,
1522 struct wimlib_update_command cmd;
1524 cmd.op = WIMLIB_UPDATE_OP_ADD;
1525 cmd.add.fs_source_path = (tchar *)fs_source_path;
1526 cmd.add.wim_target_path = (tchar *)wim_target_path;
1527 cmd.add.add_flags = add_flags;
1528 cmd.add.config_file = NULL;
1530 return update1(wim, image, &cmd);