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/capture.h"
60 #include "wimlib/dentry.h"
61 #include "wimlib/encoding.h"
62 #include "wimlib/endianness.h"
63 #include "wimlib/error.h"
64 #include "wimlib/metadata.h"
66 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
68 #include "wimlib/paths.h"
69 #include "wimlib/progress.h"
70 #include "wimlib/xml.h"
72 /* Saved specification of a "primitive" update operation that was performed. */
73 struct update_primitive {
75 /* Unlinked a dentry from its parent directory. */
78 /* Linked a dentry into its parent directory. */
81 /* Changed the file name of a dentry. */
84 /* Changed the short name of a dentry. */
89 /* For UNLINK_DENTRY and LINK_DENTRY operations */
91 /* Dentry that was linked or unlinked. */
92 struct wim_dentry *subject;
94 /* For link operations, the directory into which
95 * @subject was linked, or NULL if @subject was set as
96 * the root of the image.
98 * For unlink operations, the directory from which
99 * @subject was unlinked, or NULL if @subject was unset
100 * as the root of the image. */
101 struct wim_dentry *parent;
104 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
106 /* Dentry that had its name changed. */
107 struct wim_dentry *subject;
110 utf16lechar *old_name;
115 /* Chronological list of primitive operations that were executed for a single
116 * logical update command, such as 'add', 'delete', or 'rename'. */
117 struct update_primitive_list {
118 struct update_primitive *entries;
119 struct update_primitive inline_entries[4];
121 size_t num_alloc_entries;
124 /* Journal for managing the executing of zero or more logical update commands,
125 * such as 'add', 'delete', or 'rename'. This allows either committing or
126 * rolling back the commands. */
127 struct update_command_journal {
128 /* Number of update commands this journal contains. */
131 /* Index of currently executing update command. */
134 /* Location of the WIM image's root pointer. */
135 struct wim_dentry **root_p;
137 /* Pointer to the blob table of the WIM (may needed for rollback) */
138 struct blob_table *blob_table;
140 /* List of dentries that are currently unlinked from the WIM image.
141 * These must be freed when no longer needed for commit or rollback. */
142 struct list_head orphans;
144 /* Per-command logs. */
145 struct update_primitive_list cmd_prims[];
149 init_update_primitive_list(struct update_primitive_list *l)
151 l->entries = l->inline_entries;
153 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
156 /* Allocates a new journal for managing the execution of up to @num_cmds update
158 static struct update_command_journal *
159 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
160 struct blob_table *blob_table)
162 struct update_command_journal *j;
164 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
166 j->num_cmds = num_cmds;
169 j->blob_table = blob_table;
170 INIT_LIST_HEAD(&j->orphans);
171 for (size_t i = 0; i < num_cmds; i++)
172 init_update_primitive_list(&j->cmd_prims[i]);
177 /* Don't call this directly; use commit_update() or rollback_update() instead.
180 free_update_command_journal(struct update_command_journal *j)
182 struct wim_dentry *orphan;
184 /* Free orphaned dentry trees */
185 while (!list_empty(&j->orphans)) {
186 orphan = list_first_entry(&j->orphans,
187 struct wim_dentry, tmp_list);
188 list_del(&orphan->tmp_list);
189 free_dentry_tree(orphan, j->blob_table);
192 for (size_t i = 0; i < j->num_cmds; i++)
193 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
194 FREE(j->cmd_prims[i].entries);
198 /* Add the entry @prim to the update command journal @j. */
200 record_update_primitive(struct update_command_journal *j,
201 struct update_primitive prim)
203 struct update_primitive_list *l;
205 l = &j->cmd_prims[j->cur_cmd];
207 if (l->num_entries == l->num_alloc_entries) {
208 struct update_primitive *new_entries;
209 size_t new_num_alloc_entries;
212 new_num_alloc_entries = l->num_alloc_entries * 2;
213 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
214 if (l->entries == l->inline_entries) {
215 new_entries = MALLOC(new_size);
217 return WIMLIB_ERR_NOMEM;
218 memcpy(new_entries, l->inline_entries,
219 sizeof(l->inline_entries));
221 new_entries = REALLOC(l->entries, new_size);
223 return WIMLIB_ERR_NOMEM;
225 l->entries = new_entries;
226 l->num_alloc_entries = new_num_alloc_entries;
228 l->entries[l->num_entries++] = prim;
233 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
234 struct wim_dentry **root_p)
237 /* Unlink @subject from its @parent. */
238 wimlib_assert(subject->d_parent == parent);
239 unlink_dentry(subject);
241 /* Unset @subject as the root of the image. */
244 subject->d_parent = subject;
248 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
249 struct wim_dentry **root_p)
252 /* Link @subject to its @parent */
253 struct wim_dentry *existing;
255 existing = dentry_add_child(parent, subject);
256 wimlib_assert(!existing);
258 /* Set @subject as root of the image */
263 /* Undo a link operation. */
265 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
266 struct wim_dentry **root_p, struct list_head *orphans)
268 /* Unlink is the opposite of link */
269 do_unlink(subject, parent, root_p);
271 /* @subject is now unlinked. Add it to orphans. */
272 list_add(&subject->tmp_list, orphans);
273 subject->is_orphan = 1;
276 /* Undo an unlink operation. */
278 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
279 struct wim_dentry **root_p)
281 /* Link is the opposite of unlink */
282 do_link(subject, parent, root_p);
284 /* @subject is no longer unlinked. Delete it from orphans. */
285 list_del(&subject->tmp_list);
286 subject->is_orphan = 0;
289 /* Rollback a name change operation. */
291 rollback_name_change(utf16lechar *old_name,
292 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
294 /* Free the new name, then replace it with the old name. */
297 *name_ptr = old_name;
298 *name_nbytes_ptr = utf16le_len_bytes(old_name);
301 *name_nbytes_ptr = 0;
305 /* Rollback a primitive update operation. */
307 rollback_update_primitive(const struct update_primitive *prim,
308 struct wim_dentry **root_p,
309 struct list_head *orphans)
311 switch (prim->type) {
313 rollback_link(prim->link.subject, prim->link.parent, root_p,
317 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
319 case CHANGE_FILE_NAME:
320 rollback_name_change(prim->name.old_name,
321 &prim->name.subject->file_name,
322 &prim->name.subject->file_name_nbytes);
324 case CHANGE_SHORT_NAME:
325 rollback_name_change(prim->name.old_name,
326 &prim->name.subject->short_name,
327 &prim->name.subject->short_name_nbytes);
332 /* Rollback a logical update command */
334 rollback_update_command(const struct update_primitive_list *l,
335 struct wim_dentry **root_p,
336 struct list_head *orphans)
338 size_t i = l->num_entries;
340 /* Rollback each primitive operation, in reverse order. */
342 rollback_update_primitive(&l->entries[i], root_p, orphans);
345 /****************************************************************************/
347 /* Link @subject into the directory @parent; or, if @parent is NULL, set
348 * @subject as the root of the WIM image.
350 * This is the journaled version, so it can be rolled back. */
352 journaled_link(struct update_command_journal *j,
353 struct wim_dentry *subject, struct wim_dentry *parent)
355 struct update_primitive prim;
358 prim.type = LINK_DENTRY;
359 prim.link.subject = subject;
360 prim.link.parent = parent;
362 ret = record_update_primitive(j, prim);
366 do_link(subject, parent, j->root_p);
368 if (subject->is_orphan) {
369 list_del(&subject->tmp_list);
370 subject->is_orphan = 0;
375 /* Unlink @subject from the WIM image.
377 * This is the journaled version, so it can be rolled back. */
379 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
381 struct wim_dentry *parent;
382 struct update_primitive prim;
385 if (dentry_is_root(subject))
388 parent = subject->d_parent;
390 prim.type = UNLINK_DENTRY;
391 prim.link.subject = subject;
392 prim.link.parent = parent;
394 ret = record_update_primitive(j, prim);
398 do_unlink(subject, parent, j->root_p);
400 list_add(&subject->tmp_list, &j->orphans);
401 subject->is_orphan = 1;
405 /* Change the name of @dentry to @new_name_tstr.
407 * This is the journaled version, so it can be rolled back. */
409 journaled_change_name(struct update_command_journal *j,
410 struct wim_dentry *dentry, const tchar *new_name_tstr)
413 utf16lechar *new_name;
414 size_t new_name_nbytes;
415 struct update_primitive prim;
417 /* Set the long name. */
418 ret = tstr_to_utf16le(new_name_tstr,
419 tstrlen(new_name_tstr) * sizeof(tchar),
420 &new_name, &new_name_nbytes);
424 prim.type = CHANGE_FILE_NAME;
425 prim.name.subject = dentry;
426 prim.name.old_name = dentry->file_name;
427 ret = record_update_primitive(j, prim);
433 dentry->file_name = new_name;
434 dentry->file_name_nbytes = new_name_nbytes;
436 /* Clear the short name. */
437 prim.type = CHANGE_SHORT_NAME;
438 prim.name.subject = dentry;
439 prim.name.old_name = dentry->short_name;
440 ret = record_update_primitive(j, prim);
444 dentry->short_name = NULL;
445 dentry->short_name_nbytes = 0;
450 next_command(struct update_command_journal *j)
456 commit_update(struct update_command_journal *j)
458 for (size_t i = 0; i < j->num_cmds; i++)
460 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
462 if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
463 j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
465 FREE(j->cmd_prims[i].entries[k].name.old_name);
469 free_update_command_journal(j);
473 rollback_update(struct update_command_journal *j)
475 /* Rollback each logical update command, in reverse order. */
476 size_t i = j->cur_cmd;
480 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
481 free_update_command_journal(j);
485 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
486 struct update_command_journal *j,
488 wimlib_progress_func_t progfunc, void *progctx)
490 bool branch_is_dir = dentry_is_directory(branch);
491 bool existing_is_dir = dentry_is_directory(existing);
493 if (branch_is_dir != existing_is_dir) {
494 if (existing_is_dir) {
495 ERROR("\"%"TS"\" is a directory!\n"
496 " Specify the path at which "
497 "to place the file inside this directory.",
498 dentry_full_path(existing));
499 return WIMLIB_ERR_IS_DIRECTORY;
501 ERROR("Can't place directory at \"%"TS"\" because "
502 "a nondirectory file already exists there!",
503 dentry_full_path(existing));
504 return WIMLIB_ERR_NOTDIR;
509 /* Directory overlay */
510 while (dentry_has_children(branch)) {
511 struct wim_dentry *new_child;
512 struct wim_dentry *existing_child;
515 new_child = dentry_any_child(branch);
518 get_dentry_child_with_utf16le_name(existing,
519 new_child->file_name,
520 new_child->file_name_nbytes,
521 WIMLIB_CASE_PLATFORM_DEFAULT);
522 unlink_dentry(new_child);
523 if (existing_child) {
524 ret = handle_conflict(new_child, existing_child,
528 ret = journaled_link(j, new_child, existing);
531 dentry_add_child(branch, new_child);
535 free_dentry_tree(branch, j->blob_table);
537 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
538 /* Can't replace nondirectory file */
539 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
540 dentry_full_path(existing));
541 return WIMLIB_ERR_INVALID_OVERLAY;
543 /* Replace nondirectory file */
544 struct wim_dentry *parent;
547 parent = existing->d_parent;
549 ret = calculate_dentry_full_path(existing);
553 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
554 union wimlib_progress_info info;
556 info.replace.path_in_wim = existing->_full_path;
557 ret = call_progress(progfunc,
558 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
564 ret = journaled_unlink(j, existing);
568 return journaled_link(j, branch, parent);
573 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
574 struct update_command_journal *j,
575 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
577 struct wim_dentry *parent;
578 struct wim_dentry *existing;
579 const utf16lechar empty_name[1] = {0};
580 const utf16lechar *cur_component_name;
581 size_t cur_component_nbytes;
582 const utf16lechar *next_component_name;
585 /* Attempt to create root directory before proceeding to the "real"
588 existing = *j->root_p;
589 cur_component_name = empty_name;
590 cur_component_nbytes = 0;
592 /* Skip leading slashes */
593 next_component_name = target;
594 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
595 next_component_name++;
597 while (*next_component_name) { /* While not the last component ... */
598 const utf16lechar *end;
601 /* Descend into existing directory */
602 if (!dentry_is_directory(existing)) {
603 ERROR("\"%"TS"\" in the WIM image "
604 "is not a directory!",
605 dentry_full_path(existing));
606 return WIMLIB_ERR_NOTDIR;
609 /* A parent directory of the target didn't exist. Make
610 * the way by creating a filler directory. */
611 struct wim_dentry *filler;
613 ret = new_filler_directory(&filler);
616 ret = dentry_set_name_utf16le(filler,
618 cur_component_nbytes);
623 ret = journaled_link(j, filler, parent);
631 /* Advance to next component */
633 cur_component_name = next_component_name;
634 end = cur_component_name + 1;
635 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
638 next_component_name = end;
640 /* There will still be more components after this. */
642 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
643 wimlib_assert(*next_component_name); /* No trailing slashes */
645 /* This will be the last component */
646 next_component_name = end;
649 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
650 existing = get_dentry_child_with_utf16le_name(
653 cur_component_nbytes,
654 WIMLIB_CASE_PLATFORM_DEFAULT);
659 return handle_conflict(branch, existing, j, add_flags,
662 return journaled_link(j, branch, parent);
667 * Place the directory entry tree @branch at the path @target_tstr in the WIM
670 * @target_tstr cannot contain trailing slashes, and all path separators must be
671 * WIM_PATH_SEPARATOR.
673 * On success, @branch is committed to the journal @j.
674 * Otherwise @branch is freed.
676 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
677 * WIMLIB_ADD_FLAG_VERBOSE.
680 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
681 struct update_command_journal *j, int add_flags,
682 wimlib_progress_func_t progfunc, void *progctx)
685 const utf16lechar *target;
688 if (unlikely(!branch))
691 ret = tstr_get_utf16le(target_tstr, &target);
693 goto out_free_branch;
695 BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
696 ret = dentry_set_name(branch, path_basename(target_tstr));
698 goto out_free_target;
700 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
702 goto out_free_target;
703 /* branch was successfully committed to the journal */
706 tstr_put_utf16le(target);
708 free_dentry_tree(branch, j->blob_table);
713 static const char wincfg[] =
719 "/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 capture_params params;
793 struct capture_config config;
794 capture_tree_t capture_tree = platform_default_capture_tree;
796 struct _ntfs_volume *ntfs_vol = NULL;
798 void *extra_arg = NULL;
799 struct wim_dentry *branch;
801 add_flags = add_cmd->add.add_flags;
802 fs_source_path = add_cmd->add.fs_source_path;
803 wim_target_path = add_cmd->add.wim_target_path;
804 config_file = add_cmd->add.config_file;
806 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
807 fs_source_path, wim_target_path, add_flags);
809 memset(¶ms, 0, sizeof(params));
812 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
813 capture_tree = build_dentry_tree_ntfs;
814 extra_arg = &ntfs_vol;
815 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
816 ERROR("NTFS volume already set");
817 ret = WIMLIB_ERR_INVALID_PARAM;
823 ret = get_capture_config(config_file, &config,
824 add_flags, fs_source_path);
828 params.blob_table = wim->blob_table;
829 params.unhashed_blobs = unhashed_blobs;
830 params.inode_table = inode_table;
831 params.sd_set = sd_set;
832 params.config = &config;
833 params.add_flags = add_flags;
834 params.extra_arg = extra_arg;
836 params.progfunc = wim->progfunc;
837 params.progctx = wim->progctx;
838 params.progress.scan.source = fs_source_path;
839 params.progress.scan.wim_target_path = wim_target_path;
840 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
841 ¶ms.progress, params.progctx);
843 goto out_destroy_config;
845 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
846 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
847 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
849 goto out_destroy_config;
851 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
852 ¶ms.progress, params.progctx);
854 free_dentry_tree(branch, wim->blob_table);
855 goto out_cleanup_after_capture;
858 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
859 branch && !dentry_is_directory(branch))
861 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
862 ret = WIMLIB_ERR_NOTDIR;
863 free_dentry_tree(branch, wim->blob_table);
864 goto out_cleanup_after_capture;
867 ret = attach_branch(branch, wim_target_path, j,
868 add_flags, params.progfunc, params.progctx);
870 goto out_cleanup_after_capture;
872 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
873 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
875 params.add_flags = 0;
876 params.progfunc = NULL;
877 params.config = NULL;
879 /* If a capture configuration file was explicitly specified when
880 * capturing an image in WIMBoot mode, save it as
881 * /Windows/System32/WimBootCompress.ini in the WIM image. */
882 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
884 goto out_cleanup_after_capture;
886 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
888 goto out_cleanup_after_capture;
892 wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
895 goto out_destroy_config;
896 out_cleanup_after_capture:
899 do_ntfs_umount(ntfs_vol);
902 destroy_capture_config(&config);
908 execute_delete_command(struct update_command_journal *j,
910 const struct wimlib_update_command *delete_cmd)
913 const tchar *wim_path;
914 struct wim_dentry *tree;
916 flags = delete_cmd->delete_.delete_flags;
917 wim_path = delete_cmd->delete_.wim_path;
919 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
921 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
923 /* Path to delete does not exist in the WIM. */
924 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
927 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
928 wim_path, wim->current_image);
929 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
933 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
934 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
935 "but a recursive delete was not requested",
936 wim_path, wim->current_image);
937 return WIMLIB_ERR_IS_DIRECTORY;
940 return journaled_unlink(j, tree);
944 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
946 FREE(dentry->_full_path);
947 dentry->_full_path = NULL;
951 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
953 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
958 if (dentry_is_root(d2))
964 /* Rename a file or directory in the WIM.
966 * This returns a -errno value.
968 * The journal @j is optional.
971 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
972 CASE_SENSITIVITY_TYPE case_type,
973 struct update_command_journal *j)
975 struct wim_dentry *src;
976 struct wim_dentry *dst;
977 struct wim_dentry *parent_of_dst;
980 /* This rename() implementation currently only supports actual files
981 * (not alternate data streams) */
983 src = get_dentry(wim, from, case_type);
987 dst = get_dentry(wim, to, case_type);
990 /* Destination file exists */
992 if (src == dst) /* Same file */
995 if (!dentry_is_directory(src)) {
996 /* Cannot rename non-directory to directory. */
997 if (dentry_is_directory(dst))
1000 /* Cannot rename directory to a non-directory or a non-empty
1002 if (!dentry_is_directory(dst))
1004 if (dentry_has_children(dst))
1007 parent_of_dst = dst->d_parent;
1009 /* Destination does not exist */
1010 parent_of_dst = get_parent_dentry(wim, to, case_type);
1014 if (!dentry_is_directory(parent_of_dst))
1018 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
1019 * from the tree and creating a loop... */
1020 if (is_ancestor(src, parent_of_dst))
1025 if (journaled_unlink(j, dst))
1027 if (journaled_unlink(j, src))
1029 if (journaled_change_name(j, src, path_basename(to)))
1031 if (journaled_link(j, src, parent_of_dst))
1034 ret = dentry_set_name(src, path_basename(to));
1039 free_dentry_tree(dst, wim->blob_table);
1042 dentry_add_child(parent_of_dst, src);
1044 if (src->_full_path)
1045 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1051 execute_rename_command(struct update_command_journal *j,
1053 const struct wimlib_update_command *rename_cmd)
1057 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1058 rename_cmd->rename.wim_target_path,
1059 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1063 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1064 rename_cmd->rename.wim_source_path,
1065 rename_cmd->rename.wim_target_path);
1068 ret = WIMLIB_ERR_NOMEM;
1071 ret = WIMLIB_ERR_NOTDIR;
1075 /* XXX: EBUSY is returned when the rename would create a
1076 * loop. It maybe should have its own error code. */
1077 ret = WIMLIB_ERR_NOTEMPTY;
1080 ret = WIMLIB_ERR_IS_DIRECTORY;
1084 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1091 static inline const tchar *
1092 update_op_to_str(int op)
1095 case WIMLIB_UPDATE_OP_ADD:
1097 case WIMLIB_UPDATE_OP_DELETE:
1099 case WIMLIB_UPDATE_OP_RENAME:
1108 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1109 enum wimlib_update_op op)
1111 for (size_t i = 0; i < num_cmds; i++)
1112 if (cmds[i].op == op)
1118 execute_update_commands(WIMStruct *wim,
1119 const struct wimlib_update_command *cmds,
1123 struct wim_inode_table *inode_table;
1124 struct wim_sd_set *sd_set;
1125 struct list_head unhashed_blobs;
1126 struct update_command_journal *j;
1127 union wimlib_progress_info info;
1130 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1131 /* If we have at least one "add" command, create the inode and
1132 * security descriptor tables to index new inodes and new
1133 * security descriptors, respectively. */
1134 inode_table = alloca(sizeof(struct wim_inode_table));
1135 sd_set = alloca(sizeof(struct wim_sd_set));
1137 ret = init_inode_table(inode_table, 9001);
1141 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1143 goto out_destroy_inode_table;
1145 INIT_LIST_HEAD(&unhashed_blobs);
1151 /* Start an in-memory journal to allow rollback if something goes wrong
1153 j = new_update_command_journal(num_cmds,
1154 &wim_get_current_image_metadata(wim)->root_dentry,
1157 ret = WIMLIB_ERR_NOMEM;
1158 goto out_destroy_sd_set;
1161 info.update.completed_commands = 0;
1162 info.update.total_commands = num_cmds;
1164 for (size_t i = 0; i < num_cmds; i++) {
1165 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1166 i + 1, num_cmds, update_op_to_str(cmds[i].op));
1167 info.update.command = &cmds[i];
1168 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1169 ret = call_progress(wim->progfunc,
1170 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1171 &info, wim->progctx);
1176 switch (cmds[i].op) {
1177 case WIMLIB_UPDATE_OP_ADD:
1178 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1179 sd_set, &unhashed_blobs);
1181 case WIMLIB_UPDATE_OP_DELETE:
1182 ret = execute_delete_command(j, wim, &cmds[i]);
1184 case WIMLIB_UPDATE_OP_RENAME:
1185 ret = execute_rename_command(j, wim, &cmds[i]);
1190 info.update.completed_commands++;
1191 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1192 ret = call_progress(wim->progfunc,
1193 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1194 &info, wim->progctx);
1203 struct wim_image_metadata *imd;
1205 imd = wim_get_current_image_metadata(wim);
1207 list_splice_tail(&unhashed_blobs, &imd->unhashed_blobs);
1208 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1210 goto out_destroy_sd_set;
1214 rollback_new_security_descriptors(sd_set);
1218 destroy_sd_set(sd_set);
1219 out_destroy_inode_table:
1221 destroy_inode_table(inode_table);
1228 check_add_command(struct wimlib_update_command *cmd,
1229 const struct wim_header *hdr)
1231 int add_flags = cmd->add.add_flags;
1233 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1234 WIMLIB_ADD_FLAG_DEREFERENCE |
1235 WIMLIB_ADD_FLAG_VERBOSE |
1236 /* BOOT doesn't make sense for wimlib_update_image(). */
1237 /*WIMLIB_ADD_FLAG_BOOT |*/
1238 WIMLIB_ADD_FLAG_UNIX_DATA |
1239 WIMLIB_ADD_FLAG_NO_ACLS |
1240 WIMLIB_ADD_FLAG_STRICT_ACLS |
1241 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1242 WIMLIB_ADD_FLAG_RPFIX |
1243 WIMLIB_ADD_FLAG_NORPFIX |
1244 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1245 WIMLIB_ADD_FLAG_WINCONFIG |
1246 WIMLIB_ADD_FLAG_WIMBOOT |
1247 WIMLIB_ADD_FLAG_NO_REPLACE |
1248 WIMLIB_ADD_FLAG_TEST_FILE_EXCLUSION))
1249 return WIMLIB_ERR_INVALID_PARAM;
1251 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1253 #ifndef WITH_NTFS_3G
1254 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1255 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1256 " we cannot capture a WIM image directly "
1257 "from an NTFS volume");
1258 return WIMLIB_ERR_UNSUPPORTED;
1263 /* Check for flags not supported on Windows */
1264 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1265 ERROR("Capturing UNIX-specific data is not supported on Windows");
1266 return WIMLIB_ERR_UNSUPPORTED;
1268 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1269 ERROR("Dereferencing symbolic links is not supported on Windows");
1270 return WIMLIB_ERR_UNSUPPORTED;
1274 /* VERBOSE implies EXCLUDE_VERBOSE */
1275 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1276 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1278 /* Check for contradictory reparse point fixup flags */
1279 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1280 WIMLIB_ADD_FLAG_NORPFIX)) ==
1281 (WIMLIB_ADD_FLAG_RPFIX |
1282 WIMLIB_ADD_FLAG_NORPFIX))
1284 ERROR("Cannot specify RPFIX and NORPFIX flags "
1285 "at the same time!");
1286 return WIMLIB_ERR_INVALID_PARAM;
1289 /* Set default behavior on reparse point fixups if requested */
1290 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1291 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1293 /* Do reparse-point fixups by default if we are capturing an
1294 * entire image and either the header flag is set from previous
1295 * images, or if this is the first image being added. */
1296 if (is_entire_image &&
1297 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1298 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1301 if (!is_entire_image) {
1302 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1303 ERROR("Cannot add directly from an NTFS volume "
1304 "when not capturing a full image!");
1305 return WIMLIB_ERR_INVALID_PARAM;
1308 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1309 ERROR("Cannot do reparse point fixups when "
1310 "not capturing a full image!");
1311 return WIMLIB_ERR_INVALID_PARAM;
1314 /* We may have modified the add flags. */
1315 cmd->add.add_flags = add_flags;
1320 check_delete_command(const struct wimlib_update_command *cmd)
1322 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1323 WIMLIB_DELETE_FLAG_RECURSIVE))
1324 return WIMLIB_ERR_INVALID_PARAM;
1329 check_rename_command(const struct wimlib_update_command *cmd)
1331 if (cmd->rename.rename_flags != 0)
1332 return WIMLIB_ERR_INVALID_PARAM;
1337 check_update_command(struct wimlib_update_command *cmd,
1338 const struct wim_header *hdr)
1341 case WIMLIB_UPDATE_OP_ADD:
1342 return check_add_command(cmd, hdr);
1343 case WIMLIB_UPDATE_OP_DELETE:
1344 return check_delete_command(cmd);
1345 case WIMLIB_UPDATE_OP_RENAME:
1346 return check_rename_command(cmd);
1352 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1353 const struct wim_header *hdr)
1356 for (size_t i = 0; i < num_cmds; i++) {
1357 ret = check_update_command(&cmds[i], hdr);
1366 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1369 for (size_t i = 0; i < num_cmds; i++) {
1370 switch (cmds[i].op) {
1371 case WIMLIB_UPDATE_OP_ADD:
1372 FREE(cmds[i].add.wim_target_path);
1374 case WIMLIB_UPDATE_OP_DELETE:
1375 FREE(cmds[i].delete_.wim_path);
1377 case WIMLIB_UPDATE_OP_RENAME:
1378 FREE(cmds[i].rename.wim_source_path);
1379 FREE(cmds[i].rename.wim_target_path);
1388 copy_update_commands(const struct wimlib_update_command *cmds,
1390 struct wimlib_update_command **cmds_copy_ret)
1393 struct wimlib_update_command *cmds_copy;
1395 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1399 for (size_t i = 0; i < num_cmds; i++) {
1400 cmds_copy[i].op = cmds[i].op;
1401 switch (cmds[i].op) {
1402 case WIMLIB_UPDATE_OP_ADD:
1403 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1404 cmds_copy[i].add.wim_target_path =
1405 canonicalize_wim_path(cmds[i].add.wim_target_path);
1406 if (!cmds_copy[i].add.wim_target_path)
1408 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1409 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1411 case WIMLIB_UPDATE_OP_DELETE:
1412 cmds_copy[i].delete_.wim_path =
1413 canonicalize_wim_path(cmds[i].delete_.wim_path);
1414 if (!cmds_copy[i].delete_.wim_path)
1416 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1418 case WIMLIB_UPDATE_OP_RENAME:
1419 cmds_copy[i].rename.wim_source_path =
1420 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1421 cmds_copy[i].rename.wim_target_path =
1422 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1423 if (!cmds_copy[i].rename.wim_source_path ||
1424 !cmds_copy[i].rename.wim_target_path)
1428 ERROR("Unknown update operation %u", cmds[i].op);
1429 ret = WIMLIB_ERR_INVALID_PARAM;
1433 *cmds_copy_ret = cmds_copy;
1438 ret = WIMLIB_ERR_NOMEM;
1440 free_update_commands(cmds_copy, num_cmds);
1444 /* API function documented in wimlib.h */
1446 wimlib_update_image(WIMStruct *wim,
1448 const struct wimlib_update_command *cmds,
1453 struct wimlib_update_command *cmds_copy;
1455 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1456 return WIMLIB_ERR_INVALID_PARAM;
1458 DEBUG("Updating image %d with %zu commands", image, num_cmds);
1460 /* Load the metadata for the image to modify (if not loaded already) */
1461 ret = select_wim_image(wim, image);
1465 DEBUG("Preparing %zu update commands", num_cmds);
1467 /* Make a copy of the update commands, in the process doing certain
1468 * canonicalizations on paths (e.g. translating backslashes to forward
1469 * slashes). This is done to avoid modifying the caller's copy of the
1471 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1475 /* Perform additional checks on the update commands before we execute
1477 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1479 goto out_free_cmds_copy;
1481 /* Actually execute the update commands. */
1482 DEBUG("Executing %zu update commands", num_cmds);
1483 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1485 goto out_free_cmds_copy;
1487 wim->image_metadata[image - 1]->modified = 1;
1489 /* Statistics about the WIM image, such as the numbers of files and
1490 * directories, may have changed. Call xml_update_image_info() to
1491 * recalculate these statistics. */
1492 xml_update_image_info(wim, image);
1494 for (size_t i = 0; i < num_cmds; i++)
1495 if (cmds_copy[i].op == WIMLIB_UPDATE_OP_ADD &&
1496 cmds_copy[i].add.add_flags & WIMLIB_ADD_FLAG_RPFIX)
1497 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
1499 free_update_commands(cmds_copy, num_cmds);
1505 wimlib_delete_path(WIMStruct *wim, int image,
1506 const tchar *path, int delete_flags)
1508 struct wimlib_update_command cmd;
1510 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1511 cmd.delete_.wim_path = (tchar *)path;
1512 cmd.delete_.delete_flags = delete_flags;
1514 return wimlib_update_image(wim, image, &cmd, 1, 0);
1518 wimlib_rename_path(WIMStruct *wim, int image,
1519 const tchar *source_path, const tchar *dest_path)
1521 struct wimlib_update_command cmd;
1523 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1524 cmd.rename.wim_source_path = (tchar *)source_path;
1525 cmd.rename.wim_target_path = (tchar *)dest_path;
1526 cmd.rename.rename_flags = 0;
1528 return wimlib_update_image(wim, image, &cmd, 1, 0);
1532 wimlib_add_tree(WIMStruct *wim, int image,
1533 const tchar *fs_source_path, const tchar *wim_target_path,
1536 struct wimlib_update_command cmd;
1538 cmd.op = WIMLIB_UPDATE_OP_ADD;
1539 cmd.add.fs_source_path = (tchar *)fs_source_path;
1540 cmd.add.wim_target_path = (tchar *)wim_target_path;
1541 cmd.add.add_flags = add_flags;
1542 cmd.add.config_file = NULL;
1544 return wimlib_update_image(wim, image, &cmd, 1, 0);