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->d_parent == parent);
216 unlink_dentry(subject);
218 /* Unset @subject as the root of the image. */
221 subject->d_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->d_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;
524 parent = existing->d_parent;
526 ret = calculate_dentry_full_path(existing);
530 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
531 union wimlib_progress_info info;
533 info.replace.path_in_wim = existing->_full_path;
534 ret = call_progress(progfunc,
535 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
541 ret = journaled_unlink(j, existing);
545 return journaled_link(j, branch, parent);
550 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
551 struct update_command_journal *j,
552 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
554 struct wim_dentry *parent;
555 struct wim_dentry *existing;
556 const utf16lechar empty_name[1] = {0};
557 const utf16lechar *cur_component_name;
558 size_t cur_component_nbytes;
559 const utf16lechar *next_component_name;
562 /* Attempt to create root directory before proceeding to the "real"
565 existing = *j->root_p;
566 cur_component_name = empty_name;
567 cur_component_nbytes = 0;
569 /* Skip leading slashes */
570 next_component_name = target;
571 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
572 next_component_name++;
574 while (*next_component_name) { /* While not the last component ... */
575 const utf16lechar *end;
578 /* Descend into existing directory */
579 if (!dentry_is_directory(existing)) {
580 ERROR("\"%"TS"\" in the WIM image "
581 "is not a directory!",
582 dentry_full_path(existing));
583 return WIMLIB_ERR_NOTDIR;
586 /* A parent directory of the target didn't exist. Make
587 * the way by creating a filler directory. */
588 struct wim_dentry *filler;
590 ret = new_filler_directory(&filler);
593 ret = dentry_set_name_utf16le(filler,
595 cur_component_nbytes);
600 ret = journaled_link(j, filler, parent);
608 /* Advance to next component */
610 cur_component_name = next_component_name;
611 end = cur_component_name + 1;
612 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
615 next_component_name = end;
617 /* There will still be more components after this. */
619 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
620 wimlib_assert(*next_component_name); /* No trailing slashes */
622 /* This will be the last component */
623 next_component_name = end;
626 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
627 existing = get_dentry_child_with_utf16le_name(
630 cur_component_nbytes,
631 WIMLIB_CASE_PLATFORM_DEFAULT);
636 return handle_conflict(branch, existing, j, add_flags,
639 return journaled_link(j, branch, parent);
644 * Place the directory entry tree @branch at the path @target_tstr in the WIM
647 * @target_tstr cannot contain trailing slashes, and all path separators must be
648 * WIM_PATH_SEPARATOR.
650 * On success, @branch is committed to the journal @j.
651 * Otherwise @branch is freed.
653 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
654 * WIMLIB_ADD_FLAG_VERBOSE.
657 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
658 struct update_command_journal *j, int add_flags,
659 wimlib_progress_func_t progfunc, void *progctx)
662 const utf16lechar *target;
665 if (unlikely(!branch))
668 ret = tstr_get_utf16le(target_tstr, &target);
670 goto out_free_branch;
672 BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
673 ret = dentry_set_name(branch, path_basename(target_tstr));
675 goto out_free_target;
677 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
679 goto out_free_target;
680 /* branch was successfully committed to the journal */
683 tstr_put_utf16le(target);
685 free_dentry_tree(branch, j->lookup_table);
690 static const char wincfg[] =
696 "/System Volume Information\n"
700 static const tchar *wimboot_cfgfile =
701 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
702 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
703 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
706 get_capture_config(const tchar *config_file, struct capture_config *config,
707 int add_flags, const tchar *fs_source_path)
710 tchar *tmp_config_file = NULL;
712 memset(config, 0, sizeof(*config));
714 /* For WIMBoot capture, check for default capture configuration file
715 * unless one was explicitly specified. */
716 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
718 /* XXX: Handle loading file correctly when in NTFS volume. */
720 size_t len = tstrlen(fs_source_path) +
721 tstrlen(wimboot_cfgfile);
722 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
725 tsprintf(tmp_config_file, T("%"TS"%"TS),
726 fs_source_path, wimboot_cfgfile);
727 if (!tstat(tmp_config_file, &st)) {
728 config_file = tmp_config_file;
729 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
731 WARNING("\"%"TS"\" does not exist.\n"
732 " Using default capture configuration!",
737 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
738 /* Use Windows default. */
740 return WIMLIB_ERR_INVALID_PARAM;
741 ret = read_capture_config(T("wincfg"), wincfg,
742 sizeof(wincfg) - 1, config);
743 } else if (config_file) {
744 /* Use the specified configuration file. */
745 ret = read_capture_config(config_file, NULL, 0, config);
747 /* ... Or don't use any configuration file at all. No files
748 * will be excluded from capture, all files will be compressed,
752 FREE(tmp_config_file);
757 execute_add_command(struct update_command_journal *j,
759 const struct wimlib_update_command *add_cmd,
760 struct wim_inode_table *inode_table,
761 struct wim_sd_set *sd_set,
762 struct list_head *unhashed_streams)
766 tchar *fs_source_path;
767 tchar *wim_target_path;
768 const tchar *config_file;
769 struct add_image_params params;
770 struct capture_config config;
771 capture_tree_t capture_tree = platform_default_capture_tree;
773 struct _ntfs_volume *ntfs_vol = NULL;
775 void *extra_arg = NULL;
776 struct wim_dentry *branch;
778 add_flags = add_cmd->add.add_flags;
779 fs_source_path = add_cmd->add.fs_source_path;
780 wim_target_path = add_cmd->add.wim_target_path;
781 config_file = add_cmd->add.config_file;
783 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
784 fs_source_path, wim_target_path, add_flags);
786 memset(¶ms, 0, sizeof(params));
789 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
790 capture_tree = build_dentry_tree_ntfs;
791 extra_arg = &ntfs_vol;
792 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
793 ERROR("NTFS volume already set");
794 ret = WIMLIB_ERR_INVALID_PARAM;
800 ret = get_capture_config(config_file, &config,
801 add_flags, fs_source_path);
805 params.lookup_table = wim->lookup_table;
806 params.unhashed_streams = unhashed_streams;
807 params.inode_table = inode_table;
808 params.sd_set = sd_set;
809 params.config = &config;
810 params.add_flags = add_flags;
811 params.extra_arg = extra_arg;
813 params.progfunc = wim->progfunc;
814 params.progctx = wim->progctx;
815 params.progress.scan.source = fs_source_path;
816 params.progress.scan.wim_target_path = wim_target_path;
817 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
818 ¶ms.progress, params.progctx);
820 goto out_destroy_config;
822 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
823 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
824 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
826 goto out_destroy_config;
828 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
829 ¶ms.progress, params.progctx);
831 free_dentry_tree(branch, wim->lookup_table);
832 goto out_cleanup_after_capture;
835 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
836 branch && !dentry_is_directory(branch))
838 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
839 ret = WIMLIB_ERR_NOTDIR;
840 free_dentry_tree(branch, wim->lookup_table);
841 goto out_cleanup_after_capture;
844 ret = attach_branch(branch, wim_target_path, j,
845 add_flags, params.progfunc, params.progctx);
847 goto out_cleanup_after_capture;
849 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
850 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
852 params.add_flags = 0;
853 params.progfunc = NULL;
854 params.config = NULL;
856 /* If a capture configuration file was explicitly specified when
857 * capturing an image in WIMBoot mode, save it as
858 * /Windows/System32/WimBootCompress.ini in the WIM image. */
859 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
861 goto out_cleanup_after_capture;
863 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
865 goto out_cleanup_after_capture;
869 wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
871 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
872 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
874 goto out_destroy_config;
875 out_cleanup_after_capture:
878 do_ntfs_umount(ntfs_vol);
881 destroy_capture_config(&config);
887 execute_delete_command(struct update_command_journal *j,
889 const struct wimlib_update_command *delete_cmd)
892 const tchar *wim_path;
893 struct wim_dentry *tree;
895 flags = delete_cmd->delete_.delete_flags;
896 wim_path = delete_cmd->delete_.wim_path;
898 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
900 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
902 /* Path to delete does not exist in the WIM. */
903 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
906 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
907 wim_path, wim->current_image);
908 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
912 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
913 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
914 "but a recursive delete was not requested",
915 wim_path, wim->current_image);
916 return WIMLIB_ERR_IS_DIRECTORY;
919 return journaled_unlink(j, tree);
923 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
925 FREE(dentry->_full_path);
926 dentry->_full_path = NULL;
930 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
932 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
937 if (dentry_is_root(d2))
943 /* Rename a file or directory in the WIM.
945 * This returns a -errno value.
947 * The journal @j is optional.
950 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
951 CASE_SENSITIVITY_TYPE case_type,
952 struct update_command_journal *j)
954 struct wim_dentry *src;
955 struct wim_dentry *dst;
956 struct wim_dentry *parent_of_dst;
959 /* This rename() implementation currently only supports actual files
960 * (not alternate data streams) */
962 src = get_dentry(wim, from, case_type);
966 dst = get_dentry(wim, to, case_type);
969 /* Destination file exists */
971 if (src == dst) /* Same file */
974 if (!dentry_is_directory(src)) {
975 /* Cannot rename non-directory to directory. */
976 if (dentry_is_directory(dst))
979 /* Cannot rename directory to a non-directory or a non-empty
981 if (!dentry_is_directory(dst))
983 if (dentry_has_children(dst))
986 parent_of_dst = dst->d_parent;
988 /* Destination does not exist */
989 parent_of_dst = get_parent_dentry(wim, to, case_type);
993 if (!dentry_is_directory(parent_of_dst))
997 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
998 * from the tree and creating a loop... */
999 if (is_ancestor(src, parent_of_dst))
1004 if (journaled_unlink(j, dst))
1006 if (journaled_unlink(j, src))
1008 if (journaled_change_name(j, src, path_basename(to)))
1010 if (journaled_link(j, src, parent_of_dst))
1013 ret = dentry_set_name(src, path_basename(to));
1018 free_dentry_tree(dst, wim->lookup_table);
1021 dentry_add_child(parent_of_dst, src);
1023 if (src->_full_path)
1024 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1030 execute_rename_command(struct update_command_journal *j,
1032 const struct wimlib_update_command *rename_cmd)
1036 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1037 rename_cmd->rename.wim_target_path,
1038 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1042 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1043 rename_cmd->rename.wim_source_path,
1044 rename_cmd->rename.wim_target_path);
1047 ret = WIMLIB_ERR_NOMEM;
1050 ret = WIMLIB_ERR_NOTDIR;
1054 /* XXX: EBUSY is returned when the rename would create a
1055 * loop. It maybe should have its own error code. */
1056 ret = WIMLIB_ERR_NOTEMPTY;
1059 ret = WIMLIB_ERR_IS_DIRECTORY;
1063 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1070 static inline const tchar *
1071 update_op_to_str(int op)
1074 case WIMLIB_UPDATE_OP_ADD:
1076 case WIMLIB_UPDATE_OP_DELETE:
1078 case WIMLIB_UPDATE_OP_RENAME:
1087 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1088 enum wimlib_update_op op)
1090 for (size_t i = 0; i < num_cmds; i++)
1091 if (cmds[i].op == op)
1097 execute_update_commands(WIMStruct *wim,
1098 const struct wimlib_update_command *cmds,
1102 struct wim_inode_table *inode_table;
1103 struct wim_sd_set *sd_set;
1104 struct list_head unhashed_streams;
1105 struct update_command_journal *j;
1106 union wimlib_progress_info info;
1109 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1110 /* If we have at least one "add" command, create the inode and
1111 * security descriptor tables to index new inodes and new
1112 * security descriptors, respectively. */
1113 inode_table = alloca(sizeof(struct wim_inode_table));
1114 sd_set = alloca(sizeof(struct wim_sd_set));
1116 ret = init_inode_table(inode_table, 9001);
1120 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1122 goto out_destroy_inode_table;
1124 INIT_LIST_HEAD(&unhashed_streams);
1130 /* Start an in-memory journal to allow rollback if something goes wrong
1132 j = new_update_command_journal(num_cmds,
1133 &wim_get_current_image_metadata(wim)->root_dentry,
1136 ret = WIMLIB_ERR_NOMEM;
1137 goto out_destroy_sd_set;
1140 info.update.completed_commands = 0;
1141 info.update.total_commands = num_cmds;
1143 for (size_t i = 0; i < num_cmds; i++) {
1144 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1145 i + 1, num_cmds, update_op_to_str(cmds[i].op));
1146 info.update.command = &cmds[i];
1147 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1148 ret = call_progress(wim->progfunc,
1149 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1150 &info, wim->progctx);
1155 switch (cmds[i].op) {
1156 case WIMLIB_UPDATE_OP_ADD:
1157 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1158 sd_set, &unhashed_streams);
1160 case WIMLIB_UPDATE_OP_DELETE:
1161 ret = execute_delete_command(j, wim, &cmds[i]);
1163 case WIMLIB_UPDATE_OP_RENAME:
1164 ret = execute_rename_command(j, wim, &cmds[i]);
1169 info.update.completed_commands++;
1170 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1171 ret = call_progress(wim->progfunc,
1172 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1173 &info, wim->progctx);
1182 struct wim_image_metadata *imd;
1184 imd = wim_get_current_image_metadata(wim);
1186 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1187 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1189 goto out_destroy_sd_set;
1193 rollback_new_security_descriptors(sd_set);
1197 destroy_sd_set(sd_set);
1198 out_destroy_inode_table:
1200 destroy_inode_table(inode_table);
1207 check_add_command(struct wimlib_update_command *cmd,
1208 const struct wim_header *hdr)
1210 int add_flags = cmd->add.add_flags;
1212 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1213 WIMLIB_ADD_FLAG_DEREFERENCE |
1214 WIMLIB_ADD_FLAG_VERBOSE |
1215 /* BOOT doesn't make sense for wimlib_update_image(). */
1216 /*WIMLIB_ADD_FLAG_BOOT |*/
1217 WIMLIB_ADD_FLAG_UNIX_DATA |
1218 WIMLIB_ADD_FLAG_NO_ACLS |
1219 WIMLIB_ADD_FLAG_STRICT_ACLS |
1220 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1221 WIMLIB_ADD_FLAG_RPFIX |
1222 WIMLIB_ADD_FLAG_NORPFIX |
1223 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1224 WIMLIB_ADD_FLAG_WINCONFIG |
1225 WIMLIB_ADD_FLAG_WIMBOOT |
1226 WIMLIB_ADD_FLAG_NO_REPLACE))
1227 return WIMLIB_ERR_INVALID_PARAM;
1229 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1231 #ifndef WITH_NTFS_3G
1232 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1233 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1234 " we cannot capture a WIM image directly "
1235 "from an NTFS volume");
1236 return WIMLIB_ERR_UNSUPPORTED;
1241 /* Check for flags not supported on Windows */
1242 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1243 ERROR("Capturing UNIX-specific data is not supported on Windows");
1244 return WIMLIB_ERR_UNSUPPORTED;
1246 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1247 ERROR("Dereferencing symbolic links is not supported on Windows");
1248 return WIMLIB_ERR_UNSUPPORTED;
1252 /* VERBOSE implies EXCLUDE_VERBOSE */
1253 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1254 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1256 /* Check for contradictory reparse point fixup flags */
1257 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1258 WIMLIB_ADD_FLAG_NORPFIX)) ==
1259 (WIMLIB_ADD_FLAG_RPFIX |
1260 WIMLIB_ADD_FLAG_NORPFIX))
1262 ERROR("Cannot specify RPFIX and NORPFIX flags "
1263 "at the same time!");
1264 return WIMLIB_ERR_INVALID_PARAM;
1267 /* Set default behavior on reparse point fixups if requested */
1268 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1269 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1271 /* Do reparse-point fixups by default if we are capturing an
1272 * entire image and either the header flag is set from previous
1273 * images, or if this is the first image being added. */
1274 if (is_entire_image &&
1275 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1276 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1279 if (!is_entire_image) {
1280 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1281 ERROR("Cannot add directly from an NTFS volume "
1282 "when not capturing a full image!");
1283 return WIMLIB_ERR_INVALID_PARAM;
1286 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1287 ERROR("Cannot do reparse point fixups when "
1288 "not capturing a full image!");
1289 return WIMLIB_ERR_INVALID_PARAM;
1292 /* We may have modified the add flags. */
1293 cmd->add.add_flags = add_flags;
1298 check_delete_command(const struct wimlib_update_command *cmd)
1300 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1301 WIMLIB_DELETE_FLAG_RECURSIVE))
1302 return WIMLIB_ERR_INVALID_PARAM;
1307 check_rename_command(const struct wimlib_update_command *cmd)
1309 if (cmd->rename.rename_flags != 0)
1310 return WIMLIB_ERR_INVALID_PARAM;
1315 check_update_command(struct wimlib_update_command *cmd,
1316 const struct wim_header *hdr)
1319 case WIMLIB_UPDATE_OP_ADD:
1320 return check_add_command(cmd, hdr);
1321 case WIMLIB_UPDATE_OP_DELETE:
1322 return check_delete_command(cmd);
1323 case WIMLIB_UPDATE_OP_RENAME:
1324 return check_rename_command(cmd);
1330 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1331 const struct wim_header *hdr)
1334 for (size_t i = 0; i < num_cmds; i++) {
1335 ret = check_update_command(&cmds[i], hdr);
1344 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1347 for (size_t i = 0; i < num_cmds; i++) {
1348 switch (cmds[i].op) {
1349 case WIMLIB_UPDATE_OP_ADD:
1350 FREE(cmds[i].add.wim_target_path);
1352 case WIMLIB_UPDATE_OP_DELETE:
1353 FREE(cmds[i].delete_.wim_path);
1355 case WIMLIB_UPDATE_OP_RENAME:
1356 FREE(cmds[i].rename.wim_source_path);
1357 FREE(cmds[i].rename.wim_target_path);
1366 copy_update_commands(const struct wimlib_update_command *cmds,
1368 struct wimlib_update_command **cmds_copy_ret)
1371 struct wimlib_update_command *cmds_copy;
1373 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1377 for (size_t i = 0; i < num_cmds; i++) {
1378 cmds_copy[i].op = cmds[i].op;
1379 switch (cmds[i].op) {
1380 case WIMLIB_UPDATE_OP_ADD:
1381 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1382 cmds_copy[i].add.wim_target_path =
1383 canonicalize_wim_path(cmds[i].add.wim_target_path);
1384 if (!cmds_copy[i].add.wim_target_path)
1386 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1387 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1389 case WIMLIB_UPDATE_OP_DELETE:
1390 cmds_copy[i].delete_.wim_path =
1391 canonicalize_wim_path(cmds[i].delete_.wim_path);
1392 if (!cmds_copy[i].delete_.wim_path)
1394 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1396 case WIMLIB_UPDATE_OP_RENAME:
1397 cmds_copy[i].rename.wim_source_path =
1398 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1399 cmds_copy[i].rename.wim_target_path =
1400 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1401 if (!cmds_copy[i].rename.wim_source_path ||
1402 !cmds_copy[i].rename.wim_target_path)
1406 ERROR("Unknown update operation %u", cmds[i].op);
1407 ret = WIMLIB_ERR_INVALID_PARAM;
1411 *cmds_copy_ret = cmds_copy;
1416 ret = WIMLIB_ERR_NOMEM;
1418 free_update_commands(cmds_copy, num_cmds);
1422 /* API function documented in wimlib.h */
1424 wimlib_update_image(WIMStruct *wim,
1426 const struct wimlib_update_command *cmds,
1431 struct wimlib_update_command *cmds_copy;
1433 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1434 return WIMLIB_ERR_INVALID_PARAM;
1436 DEBUG("Updating image %d with %zu commands", image, num_cmds);
1438 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_DELETE))
1439 ret = can_delete_from_wim(wim);
1441 ret = can_modify_wim(wim);
1446 /* Load the metadata for the image to modify (if not loaded already) */
1447 ret = select_wim_image(wim, image);
1451 DEBUG("Preparing %zu update commands", num_cmds);
1453 /* Make a copy of the update commands, in the process doing certain
1454 * canonicalizations on paths (e.g. translating backslashes to forward
1455 * slashes). This is done to avoid modifying the caller's copy of the
1457 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1461 /* Perform additional checks on the update commands before we execute
1463 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1465 goto out_free_cmds_copy;
1467 /* Actually execute the update commands. */
1468 DEBUG("Executing %zu update commands", num_cmds);
1469 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1471 goto out_free_cmds_copy;
1473 wim->image_metadata[image - 1]->modified = 1;
1475 /* Statistics about the WIM image, such as the numbers of files and
1476 * directories, may have changed. Call xml_update_image_info() to
1477 * recalculate these statistics. */
1478 xml_update_image_info(wim, image);
1480 free_update_commands(cmds_copy, num_cmds);
1486 update1(WIMStruct *wim, int image, const struct wimlib_update_command *cmd)
1488 return wimlib_update_image(wim, image, cmd, 1, 0);
1492 wimlib_delete_path(WIMStruct *wim, int image,
1493 const tchar *path, int delete_flags)
1495 struct wimlib_update_command cmd;
1497 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1498 cmd.delete_.wim_path = (tchar *)path;
1499 cmd.delete_.delete_flags = delete_flags;
1501 return update1(wim, image, &cmd);
1505 wimlib_rename_path(WIMStruct *wim, int image,
1506 const tchar *source_path, const tchar *dest_path)
1508 struct wimlib_update_command cmd;
1510 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1511 cmd.rename.wim_source_path = (tchar *)source_path;
1512 cmd.rename.wim_target_path = (tchar *)dest_path;
1513 cmd.rename.rename_flags = 0;
1515 return update1(wim, image, &cmd);
1519 wimlib_add_tree(WIMStruct *wim, int image,
1520 const tchar *fs_source_path, const tchar *wim_target_path,
1523 struct wimlib_update_command cmd;
1525 cmd.op = WIMLIB_UPDATE_OP_ADD;
1526 cmd.add.fs_source_path = (tchar *)fs_source_path;
1527 cmd.add.wim_target_path = (tchar *)wim_target_path;
1528 cmd.add.add_flags = add_flags;
1529 cmd.add.config_file = NULL;
1531 return update1(wim, image, &cmd);