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
52 #include "wimlib/capture.h"
53 #include "wimlib/dentry.h"
54 #include "wimlib/encoding.h"
55 #include "wimlib/endianness.h"
56 #include "wimlib/error.h"
57 #include "wimlib/lookup_table.h"
58 #include "wimlib/metadata.h"
60 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
62 #include "wimlib/paths.h"
63 #include "wimlib/progress.h"
64 #include "wimlib/xml.h"
74 /* Saved specification of a "primitive" update operation that was performed. */
75 struct update_primitive {
77 /* Unlinked a dentry from its parent directory. */
80 /* Linked a dentry into its parent directory. */
83 /* Changed the file name of a dentry. */
86 /* Changed the short name of a dentry. */
91 /* For UNLINK_DENTRY and LINK_DENTRY operations */
93 /* Dentry that was linked or unlinked. */
94 struct wim_dentry *subject;
96 /* For link operations, the directory into which
97 * @subject was linked, or NULL if @subject was set as
98 * the root of the image.
100 * For unlink operations, the directory from which
101 * @subject was unlinked, or NULL if @subject was unset
102 * as the root of the image. */
103 struct wim_dentry *parent;
106 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
108 /* Dentry that had its name changed. */
109 struct wim_dentry *subject;
112 utf16lechar *old_name;
117 /* Chronological list of primitive operations that were executed for a single
118 * logical update command, such as 'add', 'delete', or 'rename'. */
119 struct update_primitive_list {
120 struct update_primitive *entries;
121 struct update_primitive inline_entries[4];
123 size_t num_alloc_entries;
126 /* Journal for managing the executing of zero or more logical update commands,
127 * such as 'add', 'delete', or 'rename'. This allows either committing or
128 * rolling back the commands. */
129 struct update_command_journal {
130 /* Number of update commands this journal contains. */
133 /* Index of currently executing update command. */
136 /* Location of the WIM image's root pointer. */
137 struct wim_dentry **root_p;
139 /* Pointer to the lookup table of the WIM (may needed for rollback) */
140 struct wim_lookup_table *lookup_table;
142 /* List of dentries that are currently unlinked from the WIM image.
143 * These must be freed when no longer needed for commit or rollback. */
144 struct list_head orphans;
146 /* Per-command logs. */
147 struct update_primitive_list cmd_prims[];
151 init_update_primitive_list(struct update_primitive_list *l)
153 l->entries = l->inline_entries;
155 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
158 /* Allocates a new journal for managing the execution of up to @num_cmds update
160 static struct update_command_journal *
161 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
162 struct wim_lookup_table *lookup_table)
164 struct update_command_journal *j;
166 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
168 j->num_cmds = num_cmds;
171 j->lookup_table = lookup_table;
172 INIT_LIST_HEAD(&j->orphans);
173 for (size_t i = 0; i < num_cmds; i++)
174 init_update_primitive_list(&j->cmd_prims[i]);
179 /* Don't call this directly; use commit_update() or rollback_update() instead.
182 free_update_command_journal(struct update_command_journal *j)
184 struct wim_dentry *orphan;
186 /* Free orphaned dentry trees */
187 while (!list_empty(&j->orphans)) {
188 orphan = list_first_entry(&j->orphans,
189 struct wim_dentry, tmp_list);
190 list_del(&orphan->tmp_list);
191 free_dentry_tree(orphan, j->lookup_table);
194 for (size_t i = 0; i < j->num_cmds; i++)
195 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
196 FREE(j->cmd_prims[i].entries);
200 /* Add the entry @prim to the update command journal @j. */
202 record_update_primitive(struct update_command_journal *j,
203 struct update_primitive prim)
205 struct update_primitive_list *l;
207 l = &j->cmd_prims[j->cur_cmd];
209 if (l->num_entries == l->num_alloc_entries) {
210 struct update_primitive *new_entries;
211 size_t new_num_alloc_entries;
214 new_num_alloc_entries = l->num_alloc_entries * 2;
215 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
216 if (l->entries == l->inline_entries) {
217 new_entries = MALLOC(new_size);
219 return WIMLIB_ERR_NOMEM;
220 memcpy(new_entries, l->inline_entries,
221 sizeof(l->inline_entries));
223 new_entries = REALLOC(l->entries, new_size);
225 return WIMLIB_ERR_NOMEM;
227 l->entries = new_entries;
228 l->num_alloc_entries = new_num_alloc_entries;
230 l->entries[l->num_entries++] = prim;
235 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
236 struct wim_dentry **root_p)
239 /* Unlink @subject from its @parent. */
240 wimlib_assert(subject->d_parent == parent);
241 unlink_dentry(subject);
243 /* Unset @subject as the root of the image. */
246 subject->d_parent = subject;
250 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
251 struct wim_dentry **root_p)
254 /* Link @subject to its @parent */
255 struct wim_dentry *existing;
257 existing = dentry_add_child(parent, subject);
258 wimlib_assert(!existing);
260 /* Set @subject as root of the image */
265 /* Undo a link operation. */
267 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
268 struct wim_dentry **root_p, struct list_head *orphans)
270 /* Unlink is the opposite of link */
271 do_unlink(subject, parent, root_p);
273 /* @subject is now unlinked. Add it to orphans. */
274 list_add(&subject->tmp_list, orphans);
275 subject->is_orphan = 1;
278 /* Undo an unlink operation. */
280 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
281 struct wim_dentry **root_p)
283 /* Link is the opposite of unlink */
284 do_link(subject, parent, root_p);
286 /* @subject is no longer unlinked. Delete it from orphans. */
287 list_del(&subject->tmp_list);
288 subject->is_orphan = 0;
291 /* Rollback a name change operation. */
293 rollback_name_change(utf16lechar *old_name,
294 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
296 /* Free the new name, then replace it with the old name. */
299 *name_ptr = old_name;
300 *name_nbytes_ptr = utf16le_strlen(old_name);
303 *name_nbytes_ptr = 0;
307 /* Rollback a primitive update operation. */
309 rollback_update_primitive(const struct update_primitive *prim,
310 struct wim_dentry **root_p,
311 struct list_head *orphans)
313 switch (prim->type) {
315 rollback_link(prim->link.subject, prim->link.parent, root_p,
319 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
321 case CHANGE_FILE_NAME:
322 rollback_name_change(prim->name.old_name,
323 &prim->name.subject->file_name,
324 &prim->name.subject->file_name_nbytes);
326 case CHANGE_SHORT_NAME:
327 rollback_name_change(prim->name.old_name,
328 &prim->name.subject->short_name,
329 &prim->name.subject->short_name_nbytes);
334 /* Rollback a logical update command */
336 rollback_update_command(const struct update_primitive_list *l,
337 struct wim_dentry **root_p,
338 struct list_head *orphans)
340 size_t i = l->num_entries;
342 /* Rollback each primitive operation, in reverse order. */
344 rollback_update_primitive(&l->entries[i], root_p, orphans);
347 /****************************************************************************/
349 /* Link @subject into the directory @parent; or, if @parent is NULL, set
350 * @subject as the root of the WIM image.
352 * This is the journaled version, so it can be rolled back. */
354 journaled_link(struct update_command_journal *j,
355 struct wim_dentry *subject, struct wim_dentry *parent)
357 struct update_primitive prim;
360 prim.type = LINK_DENTRY;
361 prim.link.subject = subject;
362 prim.link.parent = parent;
364 ret = record_update_primitive(j, prim);
368 do_link(subject, parent, j->root_p);
370 if (subject->is_orphan) {
371 list_del(&subject->tmp_list);
372 subject->is_orphan = 0;
377 /* Unlink @subject from the WIM image.
379 * This is the journaled version, so it can be rolled back. */
381 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
383 struct wim_dentry *parent;
384 struct update_primitive prim;
387 if (dentry_is_root(subject))
390 parent = subject->d_parent;
392 prim.type = UNLINK_DENTRY;
393 prim.link.subject = subject;
394 prim.link.parent = parent;
396 ret = record_update_primitive(j, prim);
400 do_unlink(subject, parent, j->root_p);
402 list_add(&subject->tmp_list, &j->orphans);
403 subject->is_orphan = 1;
407 /* Change the name of @dentry to @new_name_tstr.
409 * This is the journaled version, so it can be rolled back. */
411 journaled_change_name(struct update_command_journal *j,
412 struct wim_dentry *dentry, const tchar *new_name_tstr)
415 utf16lechar *new_name;
416 size_t new_name_nbytes;
417 struct update_primitive prim;
419 /* Set the long name. */
420 ret = tstr_to_utf16le(new_name_tstr,
421 tstrlen(new_name_tstr) * sizeof(tchar),
422 &new_name, &new_name_nbytes);
426 prim.type = CHANGE_FILE_NAME;
427 prim.name.subject = dentry;
428 prim.name.old_name = dentry->file_name;
429 ret = record_update_primitive(j, prim);
435 dentry->file_name = new_name;
436 dentry->file_name_nbytes = new_name_nbytes;
438 /* Clear the short name. */
439 prim.type = CHANGE_SHORT_NAME;
440 prim.name.subject = dentry;
441 prim.name.old_name = dentry->short_name;
442 ret = record_update_primitive(j, prim);
446 dentry->short_name = NULL;
447 dentry->short_name_nbytes = 0;
452 next_command(struct update_command_journal *j)
458 commit_update(struct update_command_journal *j)
460 for (size_t i = 0; i < j->num_cmds; i++)
462 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
464 if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
465 j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
467 FREE(j->cmd_prims[i].entries[k].name.old_name);
471 free_update_command_journal(j);
475 rollback_update(struct update_command_journal *j)
477 /* Rollback each logical update command, in reverse order. */
478 size_t i = j->cur_cmd;
482 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
483 free_update_command_journal(j);
487 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
488 struct update_command_journal *j,
490 wimlib_progress_func_t progfunc, void *progctx)
492 bool branch_is_dir = dentry_is_directory(branch);
493 bool existing_is_dir = dentry_is_directory(existing);
495 if (branch_is_dir != existing_is_dir) {
496 if (existing_is_dir) {
497 ERROR("\"%"TS"\" is a directory!\n"
498 " Specify the path at which "
499 "to place the file inside this directory.",
500 dentry_full_path(existing));
501 return WIMLIB_ERR_IS_DIRECTORY;
503 ERROR("Can't place directory at \"%"TS"\" because "
504 "a nondirectory file already exists there!",
505 dentry_full_path(existing));
506 return WIMLIB_ERR_NOTDIR;
511 /* Directory overlay */
512 while (dentry_has_children(branch)) {
513 struct wim_dentry *new_child;
514 struct wim_dentry *existing_child;
517 new_child = dentry_any_child(branch);
520 get_dentry_child_with_utf16le_name(existing,
521 new_child->file_name,
522 new_child->file_name_nbytes,
523 WIMLIB_CASE_PLATFORM_DEFAULT);
524 unlink_dentry(new_child);
525 if (existing_child) {
526 ret = handle_conflict(new_child, existing_child,
530 ret = journaled_link(j, new_child, existing);
533 dentry_add_child(branch, new_child);
539 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
540 /* Can't replace nondirectory file */
541 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
542 dentry_full_path(existing));
543 return WIMLIB_ERR_INVALID_OVERLAY;
545 /* Replace nondirectory file */
546 struct wim_dentry *parent;
549 parent = existing->d_parent;
551 ret = calculate_dentry_full_path(existing);
555 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
556 union wimlib_progress_info info;
558 info.replace.path_in_wim = existing->_full_path;
559 ret = call_progress(progfunc,
560 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
566 ret = journaled_unlink(j, existing);
570 return journaled_link(j, branch, parent);
575 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
576 struct update_command_journal *j,
577 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
579 struct wim_dentry *parent;
580 struct wim_dentry *existing;
581 const utf16lechar empty_name[1] = {0};
582 const utf16lechar *cur_component_name;
583 size_t cur_component_nbytes;
584 const utf16lechar *next_component_name;
587 /* Attempt to create root directory before proceeding to the "real"
590 existing = *j->root_p;
591 cur_component_name = empty_name;
592 cur_component_nbytes = 0;
594 /* Skip leading slashes */
595 next_component_name = target;
596 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
597 next_component_name++;
599 while (*next_component_name) { /* While not the last component ... */
600 const utf16lechar *end;
603 /* Descend into existing directory */
604 if (!dentry_is_directory(existing)) {
605 ERROR("\"%"TS"\" in the WIM image "
606 "is not a directory!",
607 dentry_full_path(existing));
608 return WIMLIB_ERR_NOTDIR;
611 /* A parent directory of the target didn't exist. Make
612 * the way by creating a filler directory. */
613 struct wim_dentry *filler;
615 ret = new_filler_directory(&filler);
618 ret = dentry_set_name_utf16le(filler,
620 cur_component_nbytes);
625 ret = journaled_link(j, filler, parent);
633 /* Advance to next component */
635 cur_component_name = next_component_name;
636 end = cur_component_name + 1;
637 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
640 next_component_name = end;
642 /* There will still be more components after this. */
644 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
645 wimlib_assert(*next_component_name); /* No trailing slashes */
647 /* This will be the last component */
648 next_component_name = end;
651 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
652 existing = get_dentry_child_with_utf16le_name(
655 cur_component_nbytes,
656 WIMLIB_CASE_PLATFORM_DEFAULT);
661 return handle_conflict(branch, existing, j, add_flags,
664 return journaled_link(j, branch, parent);
669 * Place the directory entry tree @branch at the path @target_tstr in the WIM
672 * @target_tstr cannot contain trailing slashes, and all path separators must be
673 * WIM_PATH_SEPARATOR.
675 * On success, @branch is committed to the journal @j.
676 * Otherwise @branch is freed.
678 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
679 * WIMLIB_ADD_FLAG_VERBOSE.
682 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
683 struct update_command_journal *j, int add_flags,
684 wimlib_progress_func_t progfunc, void *progctx)
687 const utf16lechar *target;
690 if (unlikely(!branch))
693 ret = tstr_get_utf16le(target_tstr, &target);
695 goto out_free_branch;
697 BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
698 ret = dentry_set_name(branch, path_basename(target_tstr));
700 goto out_free_target;
702 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
704 goto out_free_target;
705 /* branch was successfully committed to the journal */
708 tstr_put_utf16le(target);
710 free_dentry_tree(branch, j->lookup_table);
715 static const char wincfg[] =
721 "/System Volume Information\n"
725 static const tchar *wimboot_cfgfile =
726 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
727 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
728 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
731 get_capture_config(const tchar *config_file, struct capture_config *config,
732 int add_flags, const tchar *fs_source_path)
735 tchar *tmp_config_file = NULL;
737 memset(config, 0, sizeof(*config));
739 /* For WIMBoot capture, check for default capture configuration file
740 * unless one was explicitly specified. */
741 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
743 /* XXX: Handle loading file correctly when in NTFS volume. */
745 size_t len = tstrlen(fs_source_path) +
746 tstrlen(wimboot_cfgfile);
747 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
750 tsprintf(tmp_config_file, T("%"TS"%"TS),
751 fs_source_path, wimboot_cfgfile);
752 if (!tstat(tmp_config_file, &st)) {
753 config_file = tmp_config_file;
754 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
756 WARNING("\"%"TS"\" does not exist.\n"
757 " Using default capture configuration!",
762 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
763 /* Use Windows default. */
765 return WIMLIB_ERR_INVALID_PARAM;
766 ret = read_capture_config(T("wincfg"), wincfg,
767 sizeof(wincfg) - 1, config);
768 } else if (config_file) {
769 /* Use the specified configuration file. */
770 ret = read_capture_config(config_file, NULL, 0, config);
772 /* ... Or don't use any configuration file at all. No files
773 * will be excluded from capture, all files will be compressed,
777 FREE(tmp_config_file);
782 execute_add_command(struct update_command_journal *j,
784 const struct wimlib_update_command *add_cmd,
785 struct wim_inode_table *inode_table,
786 struct wim_sd_set *sd_set,
787 struct list_head *unhashed_streams)
791 tchar *fs_source_path;
792 tchar *wim_target_path;
793 const tchar *config_file;
794 struct capture_params params;
795 struct capture_config config;
796 capture_tree_t capture_tree = platform_default_capture_tree;
798 struct _ntfs_volume *ntfs_vol = NULL;
800 void *extra_arg = NULL;
801 struct wim_dentry *branch;
803 add_flags = add_cmd->add.add_flags;
804 fs_source_path = add_cmd->add.fs_source_path;
805 wim_target_path = add_cmd->add.wim_target_path;
806 config_file = add_cmd->add.config_file;
808 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
809 fs_source_path, wim_target_path, add_flags);
811 memset(¶ms, 0, sizeof(params));
814 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
815 capture_tree = build_dentry_tree_ntfs;
816 extra_arg = &ntfs_vol;
817 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
818 ERROR("NTFS volume already set");
819 ret = WIMLIB_ERR_INVALID_PARAM;
825 ret = get_capture_config(config_file, &config,
826 add_flags, fs_source_path);
830 params.lookup_table = wim->lookup_table;
831 params.unhashed_streams = unhashed_streams;
832 params.inode_table = inode_table;
833 params.sd_set = sd_set;
834 params.config = &config;
835 params.add_flags = add_flags;
836 params.extra_arg = extra_arg;
838 params.progfunc = wim->progfunc;
839 params.progctx = wim->progctx;
840 params.progress.scan.source = fs_source_path;
841 params.progress.scan.wim_target_path = wim_target_path;
842 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
843 ¶ms.progress, params.progctx);
845 goto out_destroy_config;
847 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
848 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
849 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
851 goto out_destroy_config;
853 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
854 ¶ms.progress, params.progctx);
856 free_dentry_tree(branch, wim->lookup_table);
857 goto out_cleanup_after_capture;
860 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
861 branch && !dentry_is_directory(branch))
863 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
864 ret = WIMLIB_ERR_NOTDIR;
865 free_dentry_tree(branch, wim->lookup_table);
866 goto out_cleanup_after_capture;
869 ret = attach_branch(branch, wim_target_path, j,
870 add_flags, params.progfunc, params.progctx);
872 goto out_cleanup_after_capture;
874 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
875 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
877 params.add_flags = 0;
878 params.progfunc = NULL;
879 params.config = NULL;
881 /* If a capture configuration file was explicitly specified when
882 * capturing an image in WIMBoot mode, save it as
883 * /Windows/System32/WimBootCompress.ini in the WIM image. */
884 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
886 goto out_cleanup_after_capture;
888 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
890 goto out_cleanup_after_capture;
894 wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
897 goto out_destroy_config;
898 out_cleanup_after_capture:
901 do_ntfs_umount(ntfs_vol);
904 destroy_capture_config(&config);
910 execute_delete_command(struct update_command_journal *j,
912 const struct wimlib_update_command *delete_cmd)
915 const tchar *wim_path;
916 struct wim_dentry *tree;
918 flags = delete_cmd->delete_.delete_flags;
919 wim_path = delete_cmd->delete_.wim_path;
921 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
923 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
925 /* Path to delete does not exist in the WIM. */
926 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
929 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
930 wim_path, wim->current_image);
931 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
935 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
936 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
937 "but a recursive delete was not requested",
938 wim_path, wim->current_image);
939 return WIMLIB_ERR_IS_DIRECTORY;
942 return journaled_unlink(j, tree);
946 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
948 FREE(dentry->_full_path);
949 dentry->_full_path = NULL;
953 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
955 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
960 if (dentry_is_root(d2))
966 /* Rename a file or directory in the WIM.
968 * This returns a -errno value.
970 * The journal @j is optional.
973 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
974 CASE_SENSITIVITY_TYPE case_type,
975 struct update_command_journal *j)
977 struct wim_dentry *src;
978 struct wim_dentry *dst;
979 struct wim_dentry *parent_of_dst;
982 /* This rename() implementation currently only supports actual files
983 * (not alternate data streams) */
985 src = get_dentry(wim, from, case_type);
989 dst = get_dentry(wim, to, case_type);
992 /* Destination file exists */
994 if (src == dst) /* Same file */
997 if (!dentry_is_directory(src)) {
998 /* Cannot rename non-directory to directory. */
999 if (dentry_is_directory(dst))
1002 /* Cannot rename directory to a non-directory or a non-empty
1004 if (!dentry_is_directory(dst))
1006 if (dentry_has_children(dst))
1009 parent_of_dst = dst->d_parent;
1011 /* Destination does not exist */
1012 parent_of_dst = get_parent_dentry(wim, to, case_type);
1016 if (!dentry_is_directory(parent_of_dst))
1020 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
1021 * from the tree and creating a loop... */
1022 if (is_ancestor(src, parent_of_dst))
1027 if (journaled_unlink(j, dst))
1029 if (journaled_unlink(j, src))
1031 if (journaled_change_name(j, src, path_basename(to)))
1033 if (journaled_link(j, src, parent_of_dst))
1036 ret = dentry_set_name(src, path_basename(to));
1041 free_dentry_tree(dst, wim->lookup_table);
1044 dentry_add_child(parent_of_dst, src);
1046 if (src->_full_path)
1047 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1053 execute_rename_command(struct update_command_journal *j,
1055 const struct wimlib_update_command *rename_cmd)
1059 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1060 rename_cmd->rename.wim_target_path,
1061 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1065 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1066 rename_cmd->rename.wim_source_path,
1067 rename_cmd->rename.wim_target_path);
1070 ret = WIMLIB_ERR_NOMEM;
1073 ret = WIMLIB_ERR_NOTDIR;
1077 /* XXX: EBUSY is returned when the rename would create a
1078 * loop. It maybe should have its own error code. */
1079 ret = WIMLIB_ERR_NOTEMPTY;
1082 ret = WIMLIB_ERR_IS_DIRECTORY;
1086 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1093 static inline const tchar *
1094 update_op_to_str(int op)
1097 case WIMLIB_UPDATE_OP_ADD:
1099 case WIMLIB_UPDATE_OP_DELETE:
1101 case WIMLIB_UPDATE_OP_RENAME:
1110 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1111 enum wimlib_update_op op)
1113 for (size_t i = 0; i < num_cmds; i++)
1114 if (cmds[i].op == op)
1120 execute_update_commands(WIMStruct *wim,
1121 const struct wimlib_update_command *cmds,
1125 struct wim_inode_table *inode_table;
1126 struct wim_sd_set *sd_set;
1127 struct list_head unhashed_streams;
1128 struct update_command_journal *j;
1129 union wimlib_progress_info info;
1132 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1133 /* If we have at least one "add" command, create the inode and
1134 * security descriptor tables to index new inodes and new
1135 * security descriptors, respectively. */
1136 inode_table = alloca(sizeof(struct wim_inode_table));
1137 sd_set = alloca(sizeof(struct wim_sd_set));
1139 ret = init_inode_table(inode_table, 9001);
1143 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1145 goto out_destroy_inode_table;
1147 INIT_LIST_HEAD(&unhashed_streams);
1153 /* Start an in-memory journal to allow rollback if something goes wrong
1155 j = new_update_command_journal(num_cmds,
1156 &wim_get_current_image_metadata(wim)->root_dentry,
1159 ret = WIMLIB_ERR_NOMEM;
1160 goto out_destroy_sd_set;
1163 info.update.completed_commands = 0;
1164 info.update.total_commands = num_cmds;
1166 for (size_t i = 0; i < num_cmds; i++) {
1167 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1168 i + 1, num_cmds, update_op_to_str(cmds[i].op));
1169 info.update.command = &cmds[i];
1170 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1171 ret = call_progress(wim->progfunc,
1172 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1173 &info, wim->progctx);
1178 switch (cmds[i].op) {
1179 case WIMLIB_UPDATE_OP_ADD:
1180 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1181 sd_set, &unhashed_streams);
1183 case WIMLIB_UPDATE_OP_DELETE:
1184 ret = execute_delete_command(j, wim, &cmds[i]);
1186 case WIMLIB_UPDATE_OP_RENAME:
1187 ret = execute_rename_command(j, wim, &cmds[i]);
1192 info.update.completed_commands++;
1193 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1194 ret = call_progress(wim->progfunc,
1195 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1196 &info, wim->progctx);
1205 struct wim_image_metadata *imd;
1207 imd = wim_get_current_image_metadata(wim);
1209 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1210 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1212 goto out_destroy_sd_set;
1216 rollback_new_security_descriptors(sd_set);
1220 destroy_sd_set(sd_set);
1221 out_destroy_inode_table:
1223 destroy_inode_table(inode_table);
1230 check_add_command(struct wimlib_update_command *cmd,
1231 const struct wim_header *hdr)
1233 int add_flags = cmd->add.add_flags;
1235 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1236 WIMLIB_ADD_FLAG_DEREFERENCE |
1237 WIMLIB_ADD_FLAG_VERBOSE |
1238 /* BOOT doesn't make sense for wimlib_update_image(). */
1239 /*WIMLIB_ADD_FLAG_BOOT |*/
1240 WIMLIB_ADD_FLAG_UNIX_DATA |
1241 WIMLIB_ADD_FLAG_NO_ACLS |
1242 WIMLIB_ADD_FLAG_STRICT_ACLS |
1243 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1244 WIMLIB_ADD_FLAG_RPFIX |
1245 WIMLIB_ADD_FLAG_NORPFIX |
1246 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1247 WIMLIB_ADD_FLAG_WINCONFIG |
1248 WIMLIB_ADD_FLAG_WIMBOOT |
1249 WIMLIB_ADD_FLAG_NO_REPLACE |
1250 WIMLIB_ADD_FLAG_TEST_FILE_EXCLUSION))
1251 return WIMLIB_ERR_INVALID_PARAM;
1253 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1255 #ifndef WITH_NTFS_3G
1256 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1257 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1258 " we cannot capture a WIM image directly "
1259 "from an NTFS volume");
1260 return WIMLIB_ERR_UNSUPPORTED;
1265 /* Check for flags not supported on Windows */
1266 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1267 ERROR("Capturing UNIX-specific data is not supported on Windows");
1268 return WIMLIB_ERR_UNSUPPORTED;
1270 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1271 ERROR("Dereferencing symbolic links is not supported on Windows");
1272 return WIMLIB_ERR_UNSUPPORTED;
1276 /* VERBOSE implies EXCLUDE_VERBOSE */
1277 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1278 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1280 /* Check for contradictory reparse point fixup flags */
1281 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1282 WIMLIB_ADD_FLAG_NORPFIX)) ==
1283 (WIMLIB_ADD_FLAG_RPFIX |
1284 WIMLIB_ADD_FLAG_NORPFIX))
1286 ERROR("Cannot specify RPFIX and NORPFIX flags "
1287 "at the same time!");
1288 return WIMLIB_ERR_INVALID_PARAM;
1291 /* Set default behavior on reparse point fixups if requested */
1292 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1293 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1295 /* Do reparse-point fixups by default if we are capturing an
1296 * entire image and either the header flag is set from previous
1297 * images, or if this is the first image being added. */
1298 if (is_entire_image &&
1299 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1300 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1303 if (!is_entire_image) {
1304 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1305 ERROR("Cannot add directly from an NTFS volume "
1306 "when not capturing a full image!");
1307 return WIMLIB_ERR_INVALID_PARAM;
1310 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1311 ERROR("Cannot do reparse point fixups when "
1312 "not capturing a full image!");
1313 return WIMLIB_ERR_INVALID_PARAM;
1316 /* We may have modified the add flags. */
1317 cmd->add.add_flags = add_flags;
1322 check_delete_command(const struct wimlib_update_command *cmd)
1324 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1325 WIMLIB_DELETE_FLAG_RECURSIVE))
1326 return WIMLIB_ERR_INVALID_PARAM;
1331 check_rename_command(const struct wimlib_update_command *cmd)
1333 if (cmd->rename.rename_flags != 0)
1334 return WIMLIB_ERR_INVALID_PARAM;
1339 check_update_command(struct wimlib_update_command *cmd,
1340 const struct wim_header *hdr)
1343 case WIMLIB_UPDATE_OP_ADD:
1344 return check_add_command(cmd, hdr);
1345 case WIMLIB_UPDATE_OP_DELETE:
1346 return check_delete_command(cmd);
1347 case WIMLIB_UPDATE_OP_RENAME:
1348 return check_rename_command(cmd);
1354 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1355 const struct wim_header *hdr)
1358 for (size_t i = 0; i < num_cmds; i++) {
1359 ret = check_update_command(&cmds[i], hdr);
1368 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1371 for (size_t i = 0; i < num_cmds; i++) {
1372 switch (cmds[i].op) {
1373 case WIMLIB_UPDATE_OP_ADD:
1374 FREE(cmds[i].add.wim_target_path);
1376 case WIMLIB_UPDATE_OP_DELETE:
1377 FREE(cmds[i].delete_.wim_path);
1379 case WIMLIB_UPDATE_OP_RENAME:
1380 FREE(cmds[i].rename.wim_source_path);
1381 FREE(cmds[i].rename.wim_target_path);
1390 copy_update_commands(const struct wimlib_update_command *cmds,
1392 struct wimlib_update_command **cmds_copy_ret)
1395 struct wimlib_update_command *cmds_copy;
1397 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1401 for (size_t i = 0; i < num_cmds; i++) {
1402 cmds_copy[i].op = cmds[i].op;
1403 switch (cmds[i].op) {
1404 case WIMLIB_UPDATE_OP_ADD:
1405 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1406 cmds_copy[i].add.wim_target_path =
1407 canonicalize_wim_path(cmds[i].add.wim_target_path);
1408 if (!cmds_copy[i].add.wim_target_path)
1410 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1411 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1413 case WIMLIB_UPDATE_OP_DELETE:
1414 cmds_copy[i].delete_.wim_path =
1415 canonicalize_wim_path(cmds[i].delete_.wim_path);
1416 if (!cmds_copy[i].delete_.wim_path)
1418 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1420 case WIMLIB_UPDATE_OP_RENAME:
1421 cmds_copy[i].rename.wim_source_path =
1422 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1423 cmds_copy[i].rename.wim_target_path =
1424 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1425 if (!cmds_copy[i].rename.wim_source_path ||
1426 !cmds_copy[i].rename.wim_target_path)
1430 ERROR("Unknown update operation %u", cmds[i].op);
1431 ret = WIMLIB_ERR_INVALID_PARAM;
1435 *cmds_copy_ret = cmds_copy;
1440 ret = WIMLIB_ERR_NOMEM;
1442 free_update_commands(cmds_copy, num_cmds);
1446 /* API function documented in wimlib.h */
1448 wimlib_update_image(WIMStruct *wim,
1450 const struct wimlib_update_command *cmds,
1455 struct wimlib_update_command *cmds_copy;
1457 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1458 return WIMLIB_ERR_INVALID_PARAM;
1460 DEBUG("Updating image %d with %zu commands", image, num_cmds);
1462 /* Load the metadata for the image to modify (if not loaded already) */
1463 ret = select_wim_image(wim, image);
1467 DEBUG("Preparing %zu update commands", num_cmds);
1469 /* Make a copy of the update commands, in the process doing certain
1470 * canonicalizations on paths (e.g. translating backslashes to forward
1471 * slashes). This is done to avoid modifying the caller's copy of the
1473 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1477 /* Perform additional checks on the update commands before we execute
1479 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1481 goto out_free_cmds_copy;
1483 /* Actually execute the update commands. */
1484 DEBUG("Executing %zu update commands", num_cmds);
1485 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1487 goto out_free_cmds_copy;
1489 wim->image_metadata[image - 1]->modified = 1;
1491 /* Statistics about the WIM image, such as the numbers of files and
1492 * directories, may have changed. Call xml_update_image_info() to
1493 * recalculate these statistics. */
1494 xml_update_image_info(wim, image);
1496 for (size_t i = 0; i < num_cmds; i++)
1497 if (cmds_copy[i].op == WIMLIB_UPDATE_OP_ADD &&
1498 cmds_copy[i].add.add_flags & WIMLIB_ADD_FLAG_RPFIX)
1499 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
1501 free_update_commands(cmds_copy, num_cmds);
1507 wimlib_delete_path(WIMStruct *wim, int image,
1508 const tchar *path, int delete_flags)
1510 struct wimlib_update_command cmd;
1512 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1513 cmd.delete_.wim_path = (tchar *)path;
1514 cmd.delete_.delete_flags = delete_flags;
1516 return wimlib_update_image(wim, image, &cmd, 1, 0);
1520 wimlib_rename_path(WIMStruct *wim, int image,
1521 const tchar *source_path, const tchar *dest_path)
1523 struct wimlib_update_command cmd;
1525 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1526 cmd.rename.wim_source_path = (tchar *)source_path;
1527 cmd.rename.wim_target_path = (tchar *)dest_path;
1528 cmd.rename.rename_flags = 0;
1530 return wimlib_update_image(wim, image, &cmd, 1, 0);
1534 wimlib_add_tree(WIMStruct *wim, int image,
1535 const tchar *fs_source_path, const tchar *wim_target_path,
1538 struct wimlib_update_command cmd;
1540 cmd.op = WIMLIB_UPDATE_OP_ADD;
1541 cmd.add.fs_source_path = (tchar *)fs_source_path;
1542 cmd.add.wim_target_path = (tchar *)wim_target_path;
1543 cmd.add.add_flags = add_flags;
1544 cmd.add.config_file = NULL;
1546 return wimlib_update_image(wim, image, &cmd, 1, 0);