2 * update_image.c - see description below
6 * Copyright (C) 2013, 2014 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
23 * This file contains the implementation of wimlib_update_image(), which is one
24 * of the two ways by which library users can make changes to a WIM image. (The
25 * other way is by mounting an image read-write.) wimlib_update_image() is also
26 * used in the implementation of wimlib_add_image(), since "create a WIM image
27 * from this directory tree" is equivalent to "create an empty WIM image, then
28 * update it to add this directory tree as the root".
30 * wimlib_update_image() processes a list of commands passed to it. Currently,
31 * the following types of commands are supported:
33 * - Add a directory tree from an external source (filesystem or NTFS volume).
34 * This can be used to add new files or to replace existing files.
35 * - Delete a file or directory tree.
36 * - Rename a file or directory tree.
38 * Not supported are creating links to existing files or changing metadata of
41 * wimlib_update_image() is atomic. If it cannot complete successfully, then
42 * all changes are rolled back and the WIMStruct is left unchanged. Rollback is
43 * implemented by breaking the commands into primitive operations such as "link
44 * this dentry tree here" which can be undone by doing the opposite operations
56 #include "wimlib/alloca.h"
57 #include "wimlib/assert.h"
58 #include "wimlib/blob_table.h"
59 #include "wimlib/dentry.h"
60 #include "wimlib/encoding.h"
61 #include "wimlib/endianness.h"
62 #include "wimlib/error.h"
63 #include "wimlib/metadata.h"
64 #include "wimlib/paths.h"
65 #include "wimlib/progress.h"
66 #include "wimlib/scan.h"
67 #include "wimlib/test_support.h"
68 #include "wimlib/xml_windows.h"
70 /* Saved specification of a "primitive" update operation that was performed. */
71 struct update_primitive {
73 /* Unlinked a dentry from its parent directory. */
76 /* Linked a dentry into its parent directory. */
79 /* Changed the file name of a dentry. */
82 /* Changed the short name of a dentry. */
87 /* For UNLINK_DENTRY and LINK_DENTRY operations */
89 /* Dentry that was linked or unlinked. */
90 struct wim_dentry *subject;
92 /* For link operations, the directory into which
93 * @subject was linked, or NULL if @subject was set as
94 * the root of the image.
96 * For unlink operations, the directory from which
97 * @subject was unlinked, or NULL if @subject was unset
98 * as the root of the image. */
99 struct wim_dentry *parent;
102 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
104 /* Dentry that had its name changed. */
105 struct wim_dentry *subject;
108 utf16lechar *old_name;
113 /* Chronological list of primitive operations that were executed for a single
114 * logical update command, such as 'add', 'delete', or 'rename'. */
115 struct update_primitive_list {
116 struct update_primitive *entries;
117 struct update_primitive inline_entries[4];
119 size_t num_alloc_entries;
122 /* Journal for managing the executing of zero or more logical update commands,
123 * such as 'add', 'delete', or 'rename'. This allows either committing or
124 * rolling back the commands. */
125 struct update_command_journal {
126 /* Number of update commands this journal contains. */
129 /* Index of currently executing update command. */
132 /* Location of the WIM image's root pointer. */
133 struct wim_dentry **root_p;
135 /* Pointer to the blob table of the WIM (may needed for rollback) */
136 struct blob_table *blob_table;
138 /* List of dentries that are currently unlinked from the WIM image.
139 * These must be freed when no longer needed for commit or rollback. */
140 struct list_head orphans;
142 /* Per-command logs. */
143 struct update_primitive_list cmd_prims[];
147 init_update_primitive_list(struct update_primitive_list *l)
149 l->entries = l->inline_entries;
151 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
154 /* Allocates a new journal for managing the execution of up to @num_cmds update
156 static struct update_command_journal *
157 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
158 struct blob_table *blob_table)
160 struct update_command_journal *j;
162 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
164 j->num_cmds = num_cmds;
167 j->blob_table = blob_table;
168 INIT_LIST_HEAD(&j->orphans);
169 for (size_t i = 0; i < num_cmds; i++)
170 init_update_primitive_list(&j->cmd_prims[i]);
175 /* Don't call this directly; use commit_update() or rollback_update() instead.
178 free_update_command_journal(struct update_command_journal *j)
180 struct wim_dentry *orphan;
182 /* Free orphaned dentry trees */
183 while (!list_empty(&j->orphans)) {
184 orphan = list_first_entry(&j->orphans,
185 struct wim_dentry, d_tmp_list);
186 list_del(&orphan->d_tmp_list);
187 free_dentry_tree(orphan, j->blob_table);
190 for (size_t i = 0; i < j->num_cmds; i++)
191 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
192 FREE(j->cmd_prims[i].entries);
196 /* Add the entry @prim to the update command journal @j. */
198 record_update_primitive(struct update_command_journal *j,
199 struct update_primitive prim)
201 struct update_primitive_list *l;
203 l = &j->cmd_prims[j->cur_cmd];
205 if (l->num_entries == l->num_alloc_entries) {
206 struct update_primitive *new_entries;
207 size_t new_num_alloc_entries;
210 new_num_alloc_entries = l->num_alloc_entries * 2;
211 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
212 if (l->entries == l->inline_entries) {
213 new_entries = MALLOC(new_size);
215 return WIMLIB_ERR_NOMEM;
216 memcpy(new_entries, l->inline_entries,
217 sizeof(l->inline_entries));
219 new_entries = REALLOC(l->entries, new_size);
221 return WIMLIB_ERR_NOMEM;
223 l->entries = new_entries;
224 l->num_alloc_entries = new_num_alloc_entries;
226 l->entries[l->num_entries++] = prim;
231 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
232 struct wim_dentry **root_p)
235 /* Unlink @subject from its @parent. */
236 wimlib_assert(subject->d_parent == parent);
237 unlink_dentry(subject);
239 /* Unset @subject as the root of the image. */
242 subject->d_parent = subject;
246 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
247 struct wim_dentry **root_p)
250 /* Link @subject to its @parent */
251 struct wim_dentry *existing;
253 existing = dentry_add_child(parent, subject);
254 wimlib_assert(!existing);
256 /* Set @subject as root of the image */
261 /* Undo a link operation. */
263 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
264 struct wim_dentry **root_p, struct list_head *orphans)
266 /* Unlink is the opposite of link */
267 do_unlink(subject, parent, root_p);
269 /* @subject is now unlinked. Add it to orphans. */
270 list_add(&subject->d_tmp_list, orphans);
271 subject->d_is_orphan = 1;
274 /* Undo an unlink operation. */
276 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
277 struct wim_dentry **root_p)
279 /* Link is the opposite of unlink */
280 do_link(subject, parent, root_p);
282 /* @subject is no longer unlinked. Delete it from orphans. */
283 list_del(&subject->d_tmp_list);
284 subject->d_is_orphan = 0;
287 /* Rollback a name change operation. */
289 rollback_name_change(utf16lechar *old_name,
290 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
292 /* Free the new name, then replace it with the old name. */
295 *name_ptr = old_name;
296 *name_nbytes_ptr = utf16le_len_bytes(old_name);
299 *name_nbytes_ptr = 0;
303 /* Rollback a primitive update operation. */
305 rollback_update_primitive(const struct update_primitive *prim,
306 struct wim_dentry **root_p,
307 struct list_head *orphans)
309 switch (prim->type) {
311 rollback_link(prim->link.subject, prim->link.parent, root_p,
315 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
317 case CHANGE_FILE_NAME:
318 rollback_name_change(prim->name.old_name,
319 &prim->name.subject->d_name,
320 &prim->name.subject->d_name_nbytes);
322 case CHANGE_SHORT_NAME:
323 rollback_name_change(prim->name.old_name,
324 &prim->name.subject->d_short_name,
325 &prim->name.subject->d_short_name_nbytes);
330 /* Rollback a logical update command */
332 rollback_update_command(const struct update_primitive_list *l,
333 struct wim_dentry **root_p,
334 struct list_head *orphans)
336 size_t i = l->num_entries;
338 /* Rollback each primitive operation, in reverse order. */
340 rollback_update_primitive(&l->entries[i], root_p, orphans);
343 /****************************************************************************/
345 /* Link @subject into the directory @parent; or, if @parent is NULL, set
346 * @subject as the root of the WIM image.
348 * This is the journaled version, so it can be rolled back. */
350 journaled_link(struct update_command_journal *j,
351 struct wim_dentry *subject, struct wim_dentry *parent)
353 struct update_primitive prim;
356 prim.type = LINK_DENTRY;
357 prim.link.subject = subject;
358 prim.link.parent = parent;
360 ret = record_update_primitive(j, prim);
364 do_link(subject, parent, j->root_p);
366 if (subject->d_is_orphan) {
367 list_del(&subject->d_tmp_list);
368 subject->d_is_orphan = 0;
373 /* Unlink @subject from the WIM image.
375 * This is the journaled version, so it can be rolled back. */
377 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
379 struct wim_dentry *parent;
380 struct update_primitive prim;
383 if (dentry_is_root(subject))
386 parent = subject->d_parent;
388 prim.type = UNLINK_DENTRY;
389 prim.link.subject = subject;
390 prim.link.parent = parent;
392 ret = record_update_primitive(j, prim);
396 do_unlink(subject, parent, j->root_p);
398 list_add(&subject->d_tmp_list, &j->orphans);
399 subject->d_is_orphan = 1;
403 /* Change the name of @dentry to @new_name_tstr.
405 * This is the journaled version, so it can be rolled back. */
407 journaled_change_name(struct update_command_journal *j,
408 struct wim_dentry *dentry, const tchar *new_name_tstr)
411 utf16lechar *new_name;
412 size_t new_name_nbytes;
413 struct update_primitive prim;
415 /* Set the long name. */
416 ret = tstr_to_utf16le(new_name_tstr,
417 tstrlen(new_name_tstr) * sizeof(tchar),
418 &new_name, &new_name_nbytes);
422 prim.type = CHANGE_FILE_NAME;
423 prim.name.subject = dentry;
424 prim.name.old_name = dentry->d_name;
425 ret = record_update_primitive(j, prim);
431 dentry->d_name = new_name;
432 dentry->d_name_nbytes = new_name_nbytes;
434 /* Clear the short name. */
435 prim.type = CHANGE_SHORT_NAME;
436 prim.name.subject = dentry;
437 prim.name.old_name = dentry->d_short_name;
438 ret = record_update_primitive(j, prim);
442 dentry->d_short_name = NULL;
443 dentry->d_short_name_nbytes = 0;
448 next_command(struct update_command_journal *j)
454 commit_update(struct update_command_journal *j)
456 for (size_t i = 0; i < j->num_cmds; i++)
458 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
460 if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
461 j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
463 FREE(j->cmd_prims[i].entries[k].name.old_name);
467 free_update_command_journal(j);
471 rollback_update(struct update_command_journal *j)
473 /* Rollback each logical update command, in reverse order. */
474 size_t i = j->cur_cmd;
478 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
479 free_update_command_journal(j);
483 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
484 struct update_command_journal *j,
486 wimlib_progress_func_t progfunc, void *progctx)
488 bool branch_is_dir = dentry_is_directory(branch);
489 bool existing_is_dir = dentry_is_directory(existing);
491 if (branch_is_dir != existing_is_dir) {
492 if (existing_is_dir) {
493 ERROR("\"%"TS"\" is a directory!\n"
494 " Specify the path at which "
495 "to place the file inside this directory.",
496 dentry_full_path(existing));
497 return WIMLIB_ERR_IS_DIRECTORY;
499 ERROR("Can't place directory at \"%"TS"\" because "
500 "a nondirectory file already exists there!",
501 dentry_full_path(existing));
502 return WIMLIB_ERR_NOTDIR;
507 /* Directory overlay */
508 while (dentry_has_children(branch)) {
509 struct wim_dentry *new_child;
510 struct wim_dentry *existing_child;
513 new_child = dentry_any_child(branch);
516 get_dentry_child_with_utf16le_name(existing,
518 new_child->d_name_nbytes,
519 WIMLIB_CASE_PLATFORM_DEFAULT);
520 unlink_dentry(new_child);
521 if (existing_child) {
522 ret = handle_conflict(new_child, existing_child,
526 ret = journaled_link(j, new_child, existing);
529 dentry_add_child(branch, new_child);
533 free_dentry_tree(branch, j->blob_table);
535 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
536 /* Can't replace nondirectory file */
537 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
538 dentry_full_path(existing));
539 return WIMLIB_ERR_INVALID_OVERLAY;
541 /* Replace nondirectory file */
542 struct wim_dentry *parent;
545 parent = existing->d_parent;
547 ret = calculate_dentry_full_path(existing);
551 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
552 union wimlib_progress_info info;
554 info.replace.path_in_wim = existing->d_full_path;
555 ret = call_progress(progfunc,
556 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
562 ret = journaled_unlink(j, existing);
566 return journaled_link(j, branch, parent);
571 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
572 struct update_command_journal *j,
573 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
575 struct wim_dentry *parent;
576 struct wim_dentry *existing;
577 const utf16lechar empty_name[1] = {0};
578 const utf16lechar *cur_component_name;
579 size_t cur_component_nbytes;
580 const utf16lechar *next_component_name;
583 /* Attempt to create root directory before proceeding to the "real"
586 existing = *j->root_p;
587 cur_component_name = empty_name;
588 cur_component_nbytes = 0;
590 /* Skip leading slashes */
591 next_component_name = target;
592 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
593 next_component_name++;
595 while (*next_component_name) { /* While not the last component ... */
596 const utf16lechar *end;
599 /* Descend into existing directory */
600 if (!dentry_is_directory(existing)) {
601 ERROR("\"%"TS"\" in the WIM image "
602 "is not a directory!",
603 dentry_full_path(existing));
604 return WIMLIB_ERR_NOTDIR;
607 /* A parent directory of the target didn't exist. Make
608 * the way by creating a filler directory. */
609 struct wim_dentry *filler;
611 ret = new_filler_directory(&filler);
614 ret = dentry_set_name_utf16le(filler,
616 cur_component_nbytes);
621 ret = journaled_link(j, filler, parent);
629 /* Advance to next component */
631 cur_component_name = next_component_name;
632 end = cur_component_name + 1;
633 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
636 next_component_name = end;
638 /* There will still be more components after this. */
640 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
641 wimlib_assert(*next_component_name); /* No trailing slashes */
643 /* This will be the last component */
644 next_component_name = end;
647 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
648 existing = get_dentry_child_with_utf16le_name(
651 cur_component_nbytes,
652 WIMLIB_CASE_PLATFORM_DEFAULT);
657 return handle_conflict(branch, existing, j, add_flags,
660 return journaled_link(j, branch, parent);
665 * Place the directory entry tree @branch at the path @target_tstr in the WIM
668 * @target_tstr cannot contain trailing slashes, and all path separators must be
669 * WIM_PATH_SEPARATOR.
671 * On success, @branch is committed to the journal @j.
672 * Otherwise @branch is freed.
674 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
675 * WIMLIB_ADD_FLAG_VERBOSE.
678 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
679 struct update_command_journal *j, int add_flags,
680 wimlib_progress_func_t progfunc, void *progctx)
683 const utf16lechar *target;
686 if (unlikely(!branch))
689 ret = tstr_get_utf16le(target_tstr, &target);
691 goto out_free_branch;
693 STATIC_ASSERT(WIM_PATH_SEPARATOR == OS_PREFERRED_PATH_SEPARATOR);
694 ret = dentry_set_name(branch, path_basename(target_tstr));
696 goto out_free_target;
698 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
700 goto out_free_target;
701 /* branch was successfully committed to the journal */
704 tstr_put_utf16le(target);
706 free_dentry_tree(branch, j->blob_table);
711 static const char wincfg[] =
717 "/System Volume Information\n"
723 static const tchar *wimboot_cfgfile =
724 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
725 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
726 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
729 get_capture_config(const tchar *config_file, struct capture_config *config,
730 int add_flags, const tchar *fs_source_path)
733 tchar *tmp_config_file = NULL;
735 memset(config, 0, sizeof(*config));
737 /* For WIMBoot capture, check for default capture configuration file
738 * unless one was explicitly specified. */
739 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
741 /* XXX: Handle loading file correctly when in NTFS volume. */
743 size_t len = tstrlen(fs_source_path) +
744 tstrlen(wimboot_cfgfile);
745 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
748 tsprintf(tmp_config_file, T("%"TS"%"TS),
749 fs_source_path, wimboot_cfgfile);
750 if (!tstat(tmp_config_file, &st)) {
751 config_file = tmp_config_file;
752 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
754 WARNING("\"%"TS"\" does not exist.\n"
755 " Using default capture configuration!",
760 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
761 /* Use Windows default. */
763 return WIMLIB_ERR_INVALID_PARAM;
764 ret = read_capture_config(T("wincfg"), wincfg,
765 sizeof(wincfg) - 1, config);
766 } else if (config_file) {
767 /* Use the specified configuration file. */
768 ret = read_capture_config(config_file, NULL, 0, config);
770 /* ... Or don't use any configuration file at all. No files
771 * will be excluded from capture, all files will be compressed,
775 FREE(tmp_config_file);
780 execute_add_command(struct update_command_journal *j,
782 const struct wimlib_update_command *add_cmd,
783 struct wim_inode_table *inode_table,
784 struct wim_sd_set *sd_set,
785 struct list_head *unhashed_blobs)
789 tchar *fs_source_path;
790 tchar *wim_target_path;
791 const tchar *config_file;
792 struct scan_params params;
793 struct capture_config config;
794 scan_tree_t scan_tree = platform_default_scan_tree;
795 struct wim_dentry *branch;
797 add_flags = add_cmd->add.add_flags;
798 fs_source_path = add_cmd->add.fs_source_path;
799 wim_target_path = add_cmd->add.wim_target_path;
800 config_file = add_cmd->add.config_file;
802 memset(¶ms, 0, sizeof(params));
805 if (add_flags & WIMLIB_ADD_FLAG_NTFS)
806 scan_tree = ntfs_3g_build_dentry_tree;
809 #ifdef ENABLE_TEST_SUPPORT
810 if (add_flags & WIMLIB_ADD_FLAG_GENERATE_TEST_DATA)
811 scan_tree = generate_dentry_tree;
814 ret = get_capture_config(config_file, &config,
815 add_flags, fs_source_path);
819 params.blob_table = wim->blob_table;
820 params.unhashed_blobs = unhashed_blobs;
821 params.inode_table = inode_table;
822 params.sd_set = sd_set;
823 params.config = &config;
824 params.add_flags = add_flags;
826 params.progfunc = wim->progfunc;
827 params.progctx = wim->progctx;
828 params.progress.scan.source = fs_source_path;
829 params.progress.scan.wim_target_path = wim_target_path;
830 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
831 ¶ms.progress, params.progctx);
833 goto out_destroy_config;
835 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
836 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
837 ret = (*scan_tree)(&branch, fs_source_path, ¶ms);
839 goto out_destroy_config;
841 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
842 ¶ms.progress, params.progctx);
844 free_dentry_tree(branch, wim->blob_table);
845 goto out_destroy_config;
848 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
849 branch && !dentry_is_directory(branch))
851 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
852 ret = WIMLIB_ERR_NOTDIR;
853 free_dentry_tree(branch, wim->blob_table);
854 goto out_destroy_config;
857 ret = attach_branch(branch, wim_target_path, j,
858 add_flags, params.progfunc, params.progctx);
860 goto out_destroy_config;
862 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
863 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
865 params.add_flags = 0;
866 params.progfunc = NULL;
867 params.config = NULL;
869 /* If a capture configuration file was explicitly specified when
870 * capturing an image in WIMBoot mode, save it as
871 * /Windows/System32/WimBootCompress.ini in the WIM image. */
872 ret = platform_default_scan_tree(&branch, config_file, ¶ms);
874 goto out_destroy_config;
876 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
878 goto out_destroy_config;
881 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path)) {
882 ret = set_windows_specific_info(wim);
884 goto out_destroy_config;
889 destroy_capture_config(&config);
891 FREE(params.cur_path);
896 execute_delete_command(struct update_command_journal *j,
898 const struct wimlib_update_command *delete_cmd)
901 const tchar *wim_path;
902 struct wim_dentry *tree;
904 flags = delete_cmd->delete_.delete_flags;
905 wim_path = delete_cmd->delete_.wim_path;
907 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
909 /* Path to delete does not exist in the WIM. */
910 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
913 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
914 wim_path, wim->current_image);
915 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
919 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
920 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
921 "but a recursive delete was not requested",
922 wim_path, wim->current_image);
923 return WIMLIB_ERR_IS_DIRECTORY;
926 return journaled_unlink(j, tree);
930 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
932 FREE(dentry->d_full_path);
933 dentry->d_full_path = NULL;
937 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
939 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
944 if (dentry_is_root(d2))
950 /* Rename a file or directory in the WIM.
952 * This returns a -errno value.
954 * The journal @j is optional.
957 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
958 CASE_SENSITIVITY_TYPE case_type,
959 struct update_command_journal *j)
961 struct wim_dentry *src;
962 struct wim_dentry *dst;
963 struct wim_dentry *parent_of_dst;
966 /* This rename() implementation currently only supports actual files
967 * (not alternate data streams) */
969 src = get_dentry(wim, from, case_type);
973 dst = get_dentry(wim, to, case_type);
976 /* Destination file exists */
978 if (src == dst) /* Same file */
981 if (!dentry_is_directory(src)) {
982 /* Cannot rename non-directory to directory. */
983 if (dentry_is_directory(dst))
986 /* Cannot rename directory to a non-directory or a non-empty
988 if (!dentry_is_directory(dst))
990 if (dentry_has_children(dst))
993 parent_of_dst = dst->d_parent;
995 /* Destination does not exist */
996 parent_of_dst = get_parent_dentry(wim, to, case_type);
1000 if (!dentry_is_directory(parent_of_dst))
1004 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
1005 * from the tree and creating a loop... */
1006 if (is_ancestor(src, parent_of_dst))
1011 if (journaled_unlink(j, dst))
1013 if (journaled_unlink(j, src))
1015 if (journaled_change_name(j, src, path_basename(to)))
1017 if (journaled_link(j, src, parent_of_dst))
1020 ret = dentry_set_name(src, path_basename(to));
1025 free_dentry_tree(dst, wim->blob_table);
1028 dentry_add_child(parent_of_dst, src);
1030 if (src->d_full_path)
1031 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1037 execute_rename_command(struct update_command_journal *j,
1039 const struct wimlib_update_command *rename_cmd)
1043 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1044 rename_cmd->rename.wim_target_path,
1045 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1049 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1050 rename_cmd->rename.wim_source_path,
1051 rename_cmd->rename.wim_target_path);
1054 ret = WIMLIB_ERR_NOMEM;
1057 ret = WIMLIB_ERR_NOTDIR;
1061 /* XXX: EBUSY is returned when the rename would create a
1062 * loop. It maybe should have its own error code. */
1063 ret = WIMLIB_ERR_NOTEMPTY;
1066 ret = WIMLIB_ERR_IS_DIRECTORY;
1070 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1078 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1079 enum wimlib_update_op op)
1081 for (size_t i = 0; i < num_cmds; i++)
1082 if (cmds[i].op == op)
1088 execute_update_commands(WIMStruct *wim,
1089 const struct wimlib_update_command *cmds,
1093 struct wim_inode_table *inode_table;
1094 struct wim_sd_set *sd_set;
1095 struct list_head unhashed_blobs;
1096 struct update_command_journal *j;
1097 union wimlib_progress_info info;
1100 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1101 /* If we have at least one "add" command, create the inode and
1102 * security descriptor tables to index new inodes and new
1103 * security descriptors, respectively. */
1104 inode_table = alloca(sizeof(struct wim_inode_table));
1105 sd_set = alloca(sizeof(struct wim_sd_set));
1107 ret = init_inode_table(inode_table, 64);
1111 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1113 goto out_destroy_inode_table;
1115 INIT_LIST_HEAD(&unhashed_blobs);
1121 /* Start an in-memory journal to allow rollback if something goes wrong
1123 j = new_update_command_journal(num_cmds,
1124 &wim_get_current_image_metadata(wim)->root_dentry,
1127 ret = WIMLIB_ERR_NOMEM;
1128 goto out_destroy_sd_set;
1131 info.update.completed_commands = 0;
1132 info.update.total_commands = num_cmds;
1134 for (size_t i = 0; i < num_cmds; i++) {
1135 info.update.command = &cmds[i];
1136 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1137 ret = call_progress(wim->progfunc,
1138 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1139 &info, wim->progctx);
1144 switch (cmds[i].op) {
1145 case WIMLIB_UPDATE_OP_ADD:
1146 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1147 sd_set, &unhashed_blobs);
1149 case WIMLIB_UPDATE_OP_DELETE:
1150 ret = execute_delete_command(j, wim, &cmds[i]);
1152 case WIMLIB_UPDATE_OP_RENAME:
1153 ret = execute_rename_command(j, wim, &cmds[i]);
1158 info.update.completed_commands++;
1159 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1160 ret = call_progress(wim->progfunc,
1161 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1162 &info, wim->progctx);
1171 struct wim_image_metadata *imd;
1173 imd = wim_get_current_image_metadata(wim);
1175 list_splice_tail(&unhashed_blobs, &imd->unhashed_blobs);
1176 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1178 goto out_destroy_sd_set;
1182 rollback_new_security_descriptors(sd_set);
1186 destroy_sd_set(sd_set);
1187 out_destroy_inode_table:
1189 destroy_inode_table(inode_table);
1196 check_add_command(struct wimlib_update_command *cmd,
1197 const struct wim_header *hdr)
1199 int add_flags = cmd->add.add_flags;
1201 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1202 WIMLIB_ADD_FLAG_DEREFERENCE |
1203 WIMLIB_ADD_FLAG_VERBOSE |
1204 /* BOOT doesn't make sense for wimlib_update_image(). */
1205 /*WIMLIB_ADD_FLAG_BOOT |*/
1206 WIMLIB_ADD_FLAG_UNIX_DATA |
1207 WIMLIB_ADD_FLAG_NO_ACLS |
1208 WIMLIB_ADD_FLAG_STRICT_ACLS |
1209 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1210 WIMLIB_ADD_FLAG_RPFIX |
1211 WIMLIB_ADD_FLAG_NORPFIX |
1212 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1213 WIMLIB_ADD_FLAG_WINCONFIG |
1214 WIMLIB_ADD_FLAG_WIMBOOT |
1215 WIMLIB_ADD_FLAG_NO_REPLACE |
1216 WIMLIB_ADD_FLAG_TEST_FILE_EXCLUSION |
1217 WIMLIB_ADD_FLAG_SNAPSHOT |
1218 #ifdef ENABLE_TEST_SUPPORT
1219 WIMLIB_ADD_FLAG_GENERATE_TEST_DATA |
1221 WIMLIB_ADD_FLAG_FILE_PATHS_UNNEEDED))
1222 return WIMLIB_ERR_INVALID_PARAM;
1224 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1226 #ifndef WITH_NTFS_3G
1227 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1228 ERROR("NTFS-3G capture mode is unsupported because wimlib "
1229 "was compiled --without-ntfs-3g");
1230 return WIMLIB_ERR_UNSUPPORTED;
1235 /* Check for flags not supported on Windows. */
1236 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1237 ERROR("Capturing UNIX-specific data is not supported on Windows");
1238 return WIMLIB_ERR_UNSUPPORTED;
1240 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1241 ERROR("Dereferencing symbolic links is not supported on Windows");
1242 return WIMLIB_ERR_UNSUPPORTED;
1245 /* Check for flags only supported on Windows. */
1247 /* Currently, SNAPSHOT means Windows VSS. In the future, it perhaps
1248 * could be implemented for other types of snapshots, such as btrfs. */
1249 if (add_flags & WIMLIB_ADD_FLAG_SNAPSHOT) {
1250 ERROR("Snapshot mode is only supported on Windows, where it uses VSS.");
1251 return WIMLIB_ERR_UNSUPPORTED;
1255 /* VERBOSE implies EXCLUDE_VERBOSE */
1256 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1257 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1259 /* Check for contradictory reparse point fixup flags */
1260 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1261 WIMLIB_ADD_FLAG_NORPFIX)) ==
1262 (WIMLIB_ADD_FLAG_RPFIX |
1263 WIMLIB_ADD_FLAG_NORPFIX))
1265 ERROR("Cannot specify RPFIX and NORPFIX flags "
1266 "at the same time!");
1267 return WIMLIB_ERR_INVALID_PARAM;
1270 /* Set default behavior on reparse point fixups if requested */
1271 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1272 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1274 /* Do reparse-point fixups by default if we are capturing an
1275 * entire image and either the header flag is set from previous
1276 * images, or if this is the first image being added. */
1277 if (is_entire_image &&
1278 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1279 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1282 if (!is_entire_image) {
1283 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1284 ERROR("Cannot do reparse point fixups when "
1285 "not capturing a full image!");
1286 return WIMLIB_ERR_INVALID_PARAM;
1289 /* We may have modified the add flags. */
1290 cmd->add.add_flags = add_flags;
1295 check_delete_command(const struct wimlib_update_command *cmd)
1297 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1298 WIMLIB_DELETE_FLAG_RECURSIVE))
1299 return WIMLIB_ERR_INVALID_PARAM;
1304 check_rename_command(const struct wimlib_update_command *cmd)
1306 if (cmd->rename.rename_flags != 0)
1307 return WIMLIB_ERR_INVALID_PARAM;
1312 check_update_command(struct wimlib_update_command *cmd,
1313 const struct wim_header *hdr)
1316 case WIMLIB_UPDATE_OP_ADD:
1317 return check_add_command(cmd, hdr);
1318 case WIMLIB_UPDATE_OP_DELETE:
1319 return check_delete_command(cmd);
1320 case WIMLIB_UPDATE_OP_RENAME:
1321 return check_rename_command(cmd);
1327 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1328 const struct wim_header *hdr)
1331 for (size_t i = 0; i < num_cmds; i++) {
1332 ret = check_update_command(&cmds[i], hdr);
1341 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1344 for (size_t i = 0; i < num_cmds; i++) {
1345 switch (cmds[i].op) {
1346 case WIMLIB_UPDATE_OP_ADD:
1347 FREE(cmds[i].add.wim_target_path);
1349 case WIMLIB_UPDATE_OP_DELETE:
1350 FREE(cmds[i].delete_.wim_path);
1352 case WIMLIB_UPDATE_OP_RENAME:
1353 FREE(cmds[i].rename.wim_source_path);
1354 FREE(cmds[i].rename.wim_target_path);
1363 copy_update_commands(const struct wimlib_update_command *cmds,
1365 struct wimlib_update_command **cmds_copy_ret)
1368 struct wimlib_update_command *cmds_copy;
1370 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1374 for (size_t i = 0; i < num_cmds; i++) {
1375 cmds_copy[i].op = cmds[i].op;
1376 switch (cmds[i].op) {
1377 case WIMLIB_UPDATE_OP_ADD:
1378 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1379 cmds_copy[i].add.wim_target_path =
1380 canonicalize_wim_path(cmds[i].add.wim_target_path);
1381 if (!cmds_copy[i].add.wim_target_path)
1383 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1384 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1386 case WIMLIB_UPDATE_OP_DELETE:
1387 cmds_copy[i].delete_.wim_path =
1388 canonicalize_wim_path(cmds[i].delete_.wim_path);
1389 if (!cmds_copy[i].delete_.wim_path)
1391 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1393 case WIMLIB_UPDATE_OP_RENAME:
1394 cmds_copy[i].rename.wim_source_path =
1395 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1396 cmds_copy[i].rename.wim_target_path =
1397 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1398 if (!cmds_copy[i].rename.wim_source_path ||
1399 !cmds_copy[i].rename.wim_target_path)
1403 ERROR("Unknown update operation %u", cmds[i].op);
1404 ret = WIMLIB_ERR_INVALID_PARAM;
1408 *cmds_copy_ret = cmds_copy;
1413 ret = WIMLIB_ERR_NOMEM;
1415 free_update_commands(cmds_copy, num_cmds);
1419 /* API function documented in wimlib.h */
1421 wimlib_update_image(WIMStruct *wim,
1423 const struct wimlib_update_command *cmds,
1428 struct wim_image_metadata *imd;
1429 struct wimlib_update_command *cmds_copy;
1431 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1432 return WIMLIB_ERR_INVALID_PARAM;
1434 /* Load the metadata for the image to modify (if not loaded already) */
1435 ret = select_wim_image(wim, image);
1439 imd = wim->image_metadata[image - 1];
1441 /* Don't allow updating an image currently being shared by multiple
1442 * WIMStructs (as a result of an export) */
1443 if (imd->refcnt > 1)
1444 return WIMLIB_ERR_IMAGE_HAS_MULTIPLE_REFERENCES;
1446 /* Make a copy of the update commands, in the process doing certain
1447 * canonicalizations on paths (e.g. translating backslashes to forward
1448 * slashes). This is done to avoid modifying the caller's copy of the
1450 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1454 /* Perform additional checks on the update commands before we execute
1456 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1458 goto out_free_cmds_copy;
1460 /* Actually execute the update commands. */
1461 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1463 goto out_free_cmds_copy;
1465 mark_image_dirty(imd);
1467 for (size_t i = 0; i < num_cmds; i++)
1468 if (cmds_copy[i].op == WIMLIB_UPDATE_OP_ADD &&
1469 cmds_copy[i].add.add_flags & WIMLIB_ADD_FLAG_RPFIX)
1470 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
1472 free_update_commands(cmds_copy, num_cmds);
1477 wimlib_delete_path(WIMStruct *wim, int image,
1478 const tchar *path, int delete_flags)
1480 struct wimlib_update_command cmd;
1482 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1483 cmd.delete_.wim_path = (tchar *)path;
1484 cmd.delete_.delete_flags = delete_flags;
1486 return wimlib_update_image(wim, image, &cmd, 1, 0);
1490 wimlib_rename_path(WIMStruct *wim, int image,
1491 const tchar *source_path, const tchar *dest_path)
1493 struct wimlib_update_command cmd;
1495 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1496 cmd.rename.wim_source_path = (tchar *)source_path;
1497 cmd.rename.wim_target_path = (tchar *)dest_path;
1498 cmd.rename.rename_flags = 0;
1500 return wimlib_update_image(wim, image, &cmd, 1, 0);
1504 wimlib_add_tree(WIMStruct *wim, int image,
1505 const tchar *fs_source_path, const tchar *wim_target_path,
1508 struct wimlib_update_command cmd;
1510 cmd.op = WIMLIB_UPDATE_OP_ADD;
1511 cmd.add.fs_source_path = (tchar *)fs_source_path;
1512 cmd.add.wim_target_path = (tchar *)wim_target_path;
1513 cmd.add.add_flags = add_flags;
1514 cmd.add.config_file = NULL;
1516 return wimlib_update_image(wim, image, &cmd, 1, 0);