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"
65 #include "wimlib/paths.h"
66 #include "wimlib/progress.h"
67 #include "wimlib/xml_windows.h"
69 /* Saved specification of a "primitive" update operation that was performed. */
70 struct update_primitive {
72 /* Unlinked a dentry from its parent directory. */
75 /* Linked a dentry into its parent directory. */
78 /* Changed the file name of a dentry. */
81 /* Changed the short name of a dentry. */
86 /* For UNLINK_DENTRY and LINK_DENTRY operations */
88 /* Dentry that was linked or unlinked. */
89 struct wim_dentry *subject;
91 /* For link operations, the directory into which
92 * @subject was linked, or NULL if @subject was set as
93 * the root of the image.
95 * For unlink operations, the directory from which
96 * @subject was unlinked, or NULL if @subject was unset
97 * as the root of the image. */
98 struct wim_dentry *parent;
101 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations */
103 /* Dentry that had its name changed. */
104 struct wim_dentry *subject;
107 utf16lechar *old_name;
112 /* Chronological list of primitive operations that were executed for a single
113 * logical update command, such as 'add', 'delete', or 'rename'. */
114 struct update_primitive_list {
115 struct update_primitive *entries;
116 struct update_primitive inline_entries[4];
118 size_t num_alloc_entries;
121 /* Journal for managing the executing of zero or more logical update commands,
122 * such as 'add', 'delete', or 'rename'. This allows either committing or
123 * rolling back the commands. */
124 struct update_command_journal {
125 /* Number of update commands this journal contains. */
128 /* Index of currently executing update command. */
131 /* Location of the WIM image's root pointer. */
132 struct wim_dentry **root_p;
134 /* Pointer to the blob table of the WIM (may needed for rollback) */
135 struct blob_table *blob_table;
137 /* List of dentries that are currently unlinked from the WIM image.
138 * These must be freed when no longer needed for commit or rollback. */
139 struct list_head orphans;
141 /* Per-command logs. */
142 struct update_primitive_list cmd_prims[];
146 init_update_primitive_list(struct update_primitive_list *l)
148 l->entries = l->inline_entries;
150 l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
153 /* Allocates a new journal for managing the execution of up to @num_cmds update
155 static struct update_command_journal *
156 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
157 struct blob_table *blob_table)
159 struct update_command_journal *j;
161 j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
163 j->num_cmds = num_cmds;
166 j->blob_table = blob_table;
167 INIT_LIST_HEAD(&j->orphans);
168 for (size_t i = 0; i < num_cmds; i++)
169 init_update_primitive_list(&j->cmd_prims[i]);
174 /* Don't call this directly; use commit_update() or rollback_update() instead.
177 free_update_command_journal(struct update_command_journal *j)
179 struct wim_dentry *orphan;
181 /* Free orphaned dentry trees */
182 while (!list_empty(&j->orphans)) {
183 orphan = list_first_entry(&j->orphans,
184 struct wim_dentry, d_tmp_list);
185 list_del(&orphan->d_tmp_list);
186 free_dentry_tree(orphan, j->blob_table);
189 for (size_t i = 0; i < j->num_cmds; i++)
190 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
191 FREE(j->cmd_prims[i].entries);
195 /* Add the entry @prim to the update command journal @j. */
197 record_update_primitive(struct update_command_journal *j,
198 struct update_primitive prim)
200 struct update_primitive_list *l;
202 l = &j->cmd_prims[j->cur_cmd];
204 if (l->num_entries == l->num_alloc_entries) {
205 struct update_primitive *new_entries;
206 size_t new_num_alloc_entries;
209 new_num_alloc_entries = l->num_alloc_entries * 2;
210 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
211 if (l->entries == l->inline_entries) {
212 new_entries = MALLOC(new_size);
214 return WIMLIB_ERR_NOMEM;
215 memcpy(new_entries, l->inline_entries,
216 sizeof(l->inline_entries));
218 new_entries = REALLOC(l->entries, new_size);
220 return WIMLIB_ERR_NOMEM;
222 l->entries = new_entries;
223 l->num_alloc_entries = new_num_alloc_entries;
225 l->entries[l->num_entries++] = prim;
230 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
231 struct wim_dentry **root_p)
234 /* Unlink @subject from its @parent. */
235 wimlib_assert(subject->d_parent == parent);
236 unlink_dentry(subject);
238 /* Unset @subject as the root of the image. */
241 subject->d_parent = subject;
245 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
246 struct wim_dentry **root_p)
249 /* Link @subject to its @parent */
250 struct wim_dentry *existing;
252 existing = dentry_add_child(parent, subject);
253 wimlib_assert(!existing);
255 /* Set @subject as root of the image */
260 /* Undo a link operation. */
262 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
263 struct wim_dentry **root_p, struct list_head *orphans)
265 /* Unlink is the opposite of link */
266 do_unlink(subject, parent, root_p);
268 /* @subject is now unlinked. Add it to orphans. */
269 list_add(&subject->d_tmp_list, orphans);
270 subject->d_is_orphan = 1;
273 /* Undo an unlink operation. */
275 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
276 struct wim_dentry **root_p)
278 /* Link is the opposite of unlink */
279 do_link(subject, parent, root_p);
281 /* @subject is no longer unlinked. Delete it from orphans. */
282 list_del(&subject->d_tmp_list);
283 subject->d_is_orphan = 0;
286 /* Rollback a name change operation. */
288 rollback_name_change(utf16lechar *old_name,
289 utf16lechar **name_ptr, u16 *name_nbytes_ptr)
291 /* Free the new name, then replace it with the old name. */
294 *name_ptr = old_name;
295 *name_nbytes_ptr = utf16le_len_bytes(old_name);
298 *name_nbytes_ptr = 0;
302 /* Rollback a primitive update operation. */
304 rollback_update_primitive(const struct update_primitive *prim,
305 struct wim_dentry **root_p,
306 struct list_head *orphans)
308 switch (prim->type) {
310 rollback_link(prim->link.subject, prim->link.parent, root_p,
314 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
316 case CHANGE_FILE_NAME:
317 rollback_name_change(prim->name.old_name,
318 &prim->name.subject->d_name,
319 &prim->name.subject->d_name_nbytes);
321 case CHANGE_SHORT_NAME:
322 rollback_name_change(prim->name.old_name,
323 &prim->name.subject->d_short_name,
324 &prim->name.subject->d_short_name_nbytes);
329 /* Rollback a logical update command */
331 rollback_update_command(const struct update_primitive_list *l,
332 struct wim_dentry **root_p,
333 struct list_head *orphans)
335 size_t i = l->num_entries;
337 /* Rollback each primitive operation, in reverse order. */
339 rollback_update_primitive(&l->entries[i], root_p, orphans);
342 /****************************************************************************/
344 /* Link @subject into the directory @parent; or, if @parent is NULL, set
345 * @subject as the root of the WIM image.
347 * This is the journaled version, so it can be rolled back. */
349 journaled_link(struct update_command_journal *j,
350 struct wim_dentry *subject, struct wim_dentry *parent)
352 struct update_primitive prim;
355 prim.type = LINK_DENTRY;
356 prim.link.subject = subject;
357 prim.link.parent = parent;
359 ret = record_update_primitive(j, prim);
363 do_link(subject, parent, j->root_p);
365 if (subject->d_is_orphan) {
366 list_del(&subject->d_tmp_list);
367 subject->d_is_orphan = 0;
372 /* Unlink @subject from the WIM image.
374 * This is the journaled version, so it can be rolled back. */
376 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
378 struct wim_dentry *parent;
379 struct update_primitive prim;
382 if (dentry_is_root(subject))
385 parent = subject->d_parent;
387 prim.type = UNLINK_DENTRY;
388 prim.link.subject = subject;
389 prim.link.parent = parent;
391 ret = record_update_primitive(j, prim);
395 do_unlink(subject, parent, j->root_p);
397 list_add(&subject->d_tmp_list, &j->orphans);
398 subject->d_is_orphan = 1;
402 /* Change the name of @dentry to @new_name_tstr.
404 * This is the journaled version, so it can be rolled back. */
406 journaled_change_name(struct update_command_journal *j,
407 struct wim_dentry *dentry, const tchar *new_name_tstr)
410 utf16lechar *new_name;
411 size_t new_name_nbytes;
412 struct update_primitive prim;
414 /* Set the long name. */
415 ret = tstr_to_utf16le(new_name_tstr,
416 tstrlen(new_name_tstr) * sizeof(tchar),
417 &new_name, &new_name_nbytes);
421 prim.type = CHANGE_FILE_NAME;
422 prim.name.subject = dentry;
423 prim.name.old_name = dentry->d_name;
424 ret = record_update_primitive(j, prim);
430 dentry->d_name = new_name;
431 dentry->d_name_nbytes = new_name_nbytes;
433 /* Clear the short name. */
434 prim.type = CHANGE_SHORT_NAME;
435 prim.name.subject = dentry;
436 prim.name.old_name = dentry->d_short_name;
437 ret = record_update_primitive(j, prim);
441 dentry->d_short_name = NULL;
442 dentry->d_short_name_nbytes = 0;
447 next_command(struct update_command_journal *j)
453 commit_update(struct update_command_journal *j)
455 for (size_t i = 0; i < j->num_cmds; i++)
457 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
459 if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
460 j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
462 FREE(j->cmd_prims[i].entries[k].name.old_name);
466 free_update_command_journal(j);
470 rollback_update(struct update_command_journal *j)
472 /* Rollback each logical update command, in reverse order. */
473 size_t i = j->cur_cmd;
477 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
478 free_update_command_journal(j);
482 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
483 struct update_command_journal *j,
485 wimlib_progress_func_t progfunc, void *progctx)
487 bool branch_is_dir = dentry_is_directory(branch);
488 bool existing_is_dir = dentry_is_directory(existing);
490 if (branch_is_dir != existing_is_dir) {
491 if (existing_is_dir) {
492 ERROR("\"%"TS"\" is a directory!\n"
493 " Specify the path at which "
494 "to place the file inside this directory.",
495 dentry_full_path(existing));
496 return WIMLIB_ERR_IS_DIRECTORY;
498 ERROR("Can't place directory at \"%"TS"\" because "
499 "a nondirectory file already exists there!",
500 dentry_full_path(existing));
501 return WIMLIB_ERR_NOTDIR;
506 /* Directory overlay */
507 while (dentry_has_children(branch)) {
508 struct wim_dentry *new_child;
509 struct wim_dentry *existing_child;
512 new_child = dentry_any_child(branch);
515 get_dentry_child_with_utf16le_name(existing,
517 new_child->d_name_nbytes,
518 WIMLIB_CASE_PLATFORM_DEFAULT);
519 unlink_dentry(new_child);
520 if (existing_child) {
521 ret = handle_conflict(new_child, existing_child,
525 ret = journaled_link(j, new_child, existing);
528 dentry_add_child(branch, new_child);
532 free_dentry_tree(branch, j->blob_table);
534 } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
535 /* Can't replace nondirectory file */
536 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
537 dentry_full_path(existing));
538 return WIMLIB_ERR_INVALID_OVERLAY;
540 /* Replace nondirectory file */
541 struct wim_dentry *parent;
544 parent = existing->d_parent;
546 ret = calculate_dentry_full_path(existing);
550 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
551 union wimlib_progress_info info;
553 info.replace.path_in_wim = existing->d_full_path;
554 ret = call_progress(progfunc,
555 WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
561 ret = journaled_unlink(j, existing);
565 return journaled_link(j, branch, parent);
570 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
571 struct update_command_journal *j,
572 int add_flags, wimlib_progress_func_t progfunc, void *progctx)
574 struct wim_dentry *parent;
575 struct wim_dentry *existing;
576 const utf16lechar empty_name[1] = {0};
577 const utf16lechar *cur_component_name;
578 size_t cur_component_nbytes;
579 const utf16lechar *next_component_name;
582 /* Attempt to create root directory before proceeding to the "real"
585 existing = *j->root_p;
586 cur_component_name = empty_name;
587 cur_component_nbytes = 0;
589 /* Skip leading slashes */
590 next_component_name = target;
591 while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
592 next_component_name++;
594 while (*next_component_name) { /* While not the last component ... */
595 const utf16lechar *end;
598 /* Descend into existing directory */
599 if (!dentry_is_directory(existing)) {
600 ERROR("\"%"TS"\" in the WIM image "
601 "is not a directory!",
602 dentry_full_path(existing));
603 return WIMLIB_ERR_NOTDIR;
606 /* A parent directory of the target didn't exist. Make
607 * the way by creating a filler directory. */
608 struct wim_dentry *filler;
610 ret = new_filler_directory(&filler);
613 ret = dentry_set_name_utf16le(filler,
615 cur_component_nbytes);
620 ret = journaled_link(j, filler, parent);
628 /* Advance to next component */
630 cur_component_name = next_component_name;
631 end = cur_component_name + 1;
632 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
635 next_component_name = end;
637 /* There will still be more components after this. */
639 } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
640 wimlib_assert(*next_component_name); /* No trailing slashes */
642 /* This will be the last component */
643 next_component_name = end;
646 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
647 existing = get_dentry_child_with_utf16le_name(
650 cur_component_nbytes,
651 WIMLIB_CASE_PLATFORM_DEFAULT);
656 return handle_conflict(branch, existing, j, add_flags,
659 return journaled_link(j, branch, parent);
664 * Place the directory entry tree @branch at the path @target_tstr in the WIM
667 * @target_tstr cannot contain trailing slashes, and all path separators must be
668 * WIM_PATH_SEPARATOR.
670 * On success, @branch is committed to the journal @j.
671 * Otherwise @branch is freed.
673 * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
674 * WIMLIB_ADD_FLAG_VERBOSE.
677 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
678 struct update_command_journal *j, int add_flags,
679 wimlib_progress_func_t progfunc, void *progctx)
682 const utf16lechar *target;
685 if (unlikely(!branch))
688 ret = tstr_get_utf16le(target_tstr, &target);
690 goto out_free_branch;
692 STATIC_ASSERT(WIM_PATH_SEPARATOR == OS_PREFERRED_PATH_SEPARATOR);
693 ret = dentry_set_name(branch, path_basename(target_tstr));
695 goto out_free_target;
697 ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
699 goto out_free_target;
700 /* branch was successfully committed to the journal */
703 tstr_put_utf16le(target);
705 free_dentry_tree(branch, j->blob_table);
710 static const char wincfg[] =
716 "/System Volume Information\n"
720 static const tchar *wimboot_cfgfile =
721 WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
722 WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
723 WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
726 get_capture_config(const tchar *config_file, struct capture_config *config,
727 int add_flags, const tchar *fs_source_path)
730 tchar *tmp_config_file = NULL;
732 memset(config, 0, sizeof(*config));
734 /* For WIMBoot capture, check for default capture configuration file
735 * unless one was explicitly specified. */
736 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
738 /* XXX: Handle loading file correctly when in NTFS volume. */
740 size_t len = tstrlen(fs_source_path) +
741 tstrlen(wimboot_cfgfile);
742 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
745 tsprintf(tmp_config_file, T("%"TS"%"TS),
746 fs_source_path, wimboot_cfgfile);
747 if (!tstat(tmp_config_file, &st)) {
748 config_file = tmp_config_file;
749 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
751 WARNING("\"%"TS"\" does not exist.\n"
752 " Using default capture configuration!",
757 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
758 /* Use Windows default. */
760 return WIMLIB_ERR_INVALID_PARAM;
761 ret = read_capture_config(T("wincfg"), wincfg,
762 sizeof(wincfg) - 1, config);
763 } else if (config_file) {
764 /* Use the specified configuration file. */
765 ret = read_capture_config(config_file, NULL, 0, config);
767 /* ... Or don't use any configuration file at all. No files
768 * will be excluded from capture, all files will be compressed,
772 FREE(tmp_config_file);
777 execute_add_command(struct update_command_journal *j,
779 const struct wimlib_update_command *add_cmd,
780 struct wim_inode_table *inode_table,
781 struct wim_sd_set *sd_set,
782 struct list_head *unhashed_blobs)
786 tchar *fs_source_path;
787 tchar *wim_target_path;
788 const tchar *config_file;
789 struct capture_params params;
790 struct capture_config config;
791 capture_tree_t capture_tree = platform_default_capture_tree;
792 struct wim_dentry *branch;
794 add_flags = add_cmd->add.add_flags;
795 fs_source_path = add_cmd->add.fs_source_path;
796 wim_target_path = add_cmd->add.wim_target_path;
797 config_file = add_cmd->add.config_file;
799 memset(¶ms, 0, sizeof(params));
802 if (add_flags & WIMLIB_ADD_FLAG_NTFS)
803 capture_tree = ntfs_3g_build_dentry_tree;
806 ret = get_capture_config(config_file, &config,
807 add_flags, fs_source_path);
811 params.blob_table = wim->blob_table;
812 params.unhashed_blobs = unhashed_blobs;
813 params.inode_table = inode_table;
814 params.sd_set = sd_set;
815 params.config = &config;
816 params.add_flags = add_flags;
818 params.progfunc = wim->progfunc;
819 params.progctx = wim->progctx;
820 params.progress.scan.source = fs_source_path;
821 params.progress.scan.wim_target_path = wim_target_path;
822 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
823 ¶ms.progress, params.progctx);
825 goto out_destroy_config;
827 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
828 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
829 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
831 goto out_destroy_config;
833 ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
834 ¶ms.progress, params.progctx);
836 free_dentry_tree(branch, wim->blob_table);
837 goto out_destroy_config;
840 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
841 branch && !dentry_is_directory(branch))
843 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
844 ret = WIMLIB_ERR_NOTDIR;
845 free_dentry_tree(branch, wim->blob_table);
846 goto out_destroy_config;
849 ret = attach_branch(branch, wim_target_path, j,
850 add_flags, params.progfunc, params.progctx);
852 goto out_destroy_config;
854 if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
855 WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
857 params.add_flags = 0;
858 params.progfunc = NULL;
859 params.config = NULL;
861 /* If a capture configuration file was explicitly specified when
862 * capturing an image in WIMBoot mode, save it as
863 * /Windows/System32/WimBootCompress.ini in the WIM image. */
864 ret = platform_default_capture_tree(&branch, config_file, ¶ms);
866 goto out_destroy_config;
868 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
870 goto out_destroy_config;
873 if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path)) {
874 ret = set_windows_specific_info(wim);
876 goto out_destroy_config;
881 destroy_capture_config(&config);
887 execute_delete_command(struct update_command_journal *j,
889 const struct wimlib_update_command *delete_cmd)
892 const tchar *wim_path;
893 struct wim_dentry *tree;
895 flags = delete_cmd->delete_.delete_flags;
896 wim_path = delete_cmd->delete_.wim_path;
898 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
900 /* Path to delete does not exist in the WIM. */
901 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
904 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
905 wim_path, wim->current_image);
906 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
910 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
911 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
912 "but a recursive delete was not requested",
913 wim_path, wim->current_image);
914 return WIMLIB_ERR_IS_DIRECTORY;
917 return journaled_unlink(j, tree);
921 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
923 FREE(dentry->d_full_path);
924 dentry->d_full_path = NULL;
928 /* Is @d1 a (possibly nonproper) ancestor of @d2? */
930 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
935 if (dentry_is_root(d2))
941 /* Rename a file or directory in the WIM.
943 * This returns a -errno value.
945 * The journal @j is optional.
948 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
949 CASE_SENSITIVITY_TYPE case_type,
950 struct update_command_journal *j)
952 struct wim_dentry *src;
953 struct wim_dentry *dst;
954 struct wim_dentry *parent_of_dst;
957 /* This rename() implementation currently only supports actual files
958 * (not alternate data streams) */
960 src = get_dentry(wim, from, case_type);
964 dst = get_dentry(wim, to, case_type);
967 /* Destination file exists */
969 if (src == dst) /* Same file */
972 if (!dentry_is_directory(src)) {
973 /* Cannot rename non-directory to directory. */
974 if (dentry_is_directory(dst))
977 /* Cannot rename directory to a non-directory or a non-empty
979 if (!dentry_is_directory(dst))
981 if (dentry_has_children(dst))
984 parent_of_dst = dst->d_parent;
986 /* Destination does not exist */
987 parent_of_dst = get_parent_dentry(wim, to, case_type);
991 if (!dentry_is_directory(parent_of_dst))
995 /* @src can't be an ancestor of @dst. Otherwise we're unlinking @src
996 * from the tree and creating a loop... */
997 if (is_ancestor(src, parent_of_dst))
1002 if (journaled_unlink(j, dst))
1004 if (journaled_unlink(j, src))
1006 if (journaled_change_name(j, src, path_basename(to)))
1008 if (journaled_link(j, src, parent_of_dst))
1011 ret = dentry_set_name(src, path_basename(to));
1016 free_dentry_tree(dst, wim->blob_table);
1019 dentry_add_child(parent_of_dst, src);
1021 if (src->d_full_path)
1022 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1028 execute_rename_command(struct update_command_journal *j,
1030 const struct wimlib_update_command *rename_cmd)
1034 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1035 rename_cmd->rename.wim_target_path,
1036 WIMLIB_CASE_PLATFORM_DEFAULT, j);
1040 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1041 rename_cmd->rename.wim_source_path,
1042 rename_cmd->rename.wim_target_path);
1045 ret = WIMLIB_ERR_NOMEM;
1048 ret = WIMLIB_ERR_NOTDIR;
1052 /* XXX: EBUSY is returned when the rename would create a
1053 * loop. It maybe should have its own error code. */
1054 ret = WIMLIB_ERR_NOTEMPTY;
1057 ret = WIMLIB_ERR_IS_DIRECTORY;
1061 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1069 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1070 enum wimlib_update_op op)
1072 for (size_t i = 0; i < num_cmds; i++)
1073 if (cmds[i].op == op)
1079 execute_update_commands(WIMStruct *wim,
1080 const struct wimlib_update_command *cmds,
1084 struct wim_inode_table *inode_table;
1085 struct wim_sd_set *sd_set;
1086 struct list_head unhashed_blobs;
1087 struct update_command_journal *j;
1088 union wimlib_progress_info info;
1091 if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1092 /* If we have at least one "add" command, create the inode and
1093 * security descriptor tables to index new inodes and new
1094 * security descriptors, respectively. */
1095 inode_table = alloca(sizeof(struct wim_inode_table));
1096 sd_set = alloca(sizeof(struct wim_sd_set));
1098 ret = init_inode_table(inode_table, 9001);
1102 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1104 goto out_destroy_inode_table;
1106 INIT_LIST_HEAD(&unhashed_blobs);
1112 /* Start an in-memory journal to allow rollback if something goes wrong
1114 j = new_update_command_journal(num_cmds,
1115 &wim_get_current_image_metadata(wim)->root_dentry,
1118 ret = WIMLIB_ERR_NOMEM;
1119 goto out_destroy_sd_set;
1122 info.update.completed_commands = 0;
1123 info.update.total_commands = num_cmds;
1125 for (size_t i = 0; i < num_cmds; i++) {
1126 info.update.command = &cmds[i];
1127 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1128 ret = call_progress(wim->progfunc,
1129 WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1130 &info, wim->progctx);
1135 switch (cmds[i].op) {
1136 case WIMLIB_UPDATE_OP_ADD:
1137 ret = execute_add_command(j, wim, &cmds[i], inode_table,
1138 sd_set, &unhashed_blobs);
1140 case WIMLIB_UPDATE_OP_DELETE:
1141 ret = execute_delete_command(j, wim, &cmds[i]);
1143 case WIMLIB_UPDATE_OP_RENAME:
1144 ret = execute_rename_command(j, wim, &cmds[i]);
1149 info.update.completed_commands++;
1150 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1151 ret = call_progress(wim->progfunc,
1152 WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1153 &info, wim->progctx);
1162 struct wim_image_metadata *imd;
1164 imd = wim_get_current_image_metadata(wim);
1166 list_splice_tail(&unhashed_blobs, &imd->unhashed_blobs);
1167 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1169 goto out_destroy_sd_set;
1173 rollback_new_security_descriptors(sd_set);
1177 destroy_sd_set(sd_set);
1178 out_destroy_inode_table:
1180 destroy_inode_table(inode_table);
1187 check_add_command(struct wimlib_update_command *cmd,
1188 const struct wim_header *hdr)
1190 int add_flags = cmd->add.add_flags;
1192 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1193 WIMLIB_ADD_FLAG_DEREFERENCE |
1194 WIMLIB_ADD_FLAG_VERBOSE |
1195 /* BOOT doesn't make sense for wimlib_update_image(). */
1196 /*WIMLIB_ADD_FLAG_BOOT |*/
1197 WIMLIB_ADD_FLAG_UNIX_DATA |
1198 WIMLIB_ADD_FLAG_NO_ACLS |
1199 WIMLIB_ADD_FLAG_STRICT_ACLS |
1200 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1201 WIMLIB_ADD_FLAG_RPFIX |
1202 WIMLIB_ADD_FLAG_NORPFIX |
1203 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1204 WIMLIB_ADD_FLAG_WINCONFIG |
1205 WIMLIB_ADD_FLAG_WIMBOOT |
1206 WIMLIB_ADD_FLAG_NO_REPLACE |
1207 WIMLIB_ADD_FLAG_TEST_FILE_EXCLUSION |
1208 WIMLIB_ADD_FLAG_SNAPSHOT))
1209 return WIMLIB_ERR_INVALID_PARAM;
1211 bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1213 #ifndef WITH_NTFS_3G
1214 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1215 ERROR("NTFS-3g capture mode is unsupported because wimlib "
1216 "was compiled --without-ntfs-3g");
1217 return WIMLIB_ERR_UNSUPPORTED;
1222 /* Check for flags not supported on Windows. */
1223 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1224 ERROR("Capturing UNIX-specific data is not supported on Windows");
1225 return WIMLIB_ERR_UNSUPPORTED;
1227 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1228 ERROR("Dereferencing symbolic links is not supported on Windows");
1229 return WIMLIB_ERR_UNSUPPORTED;
1232 /* Check for flags only supported on Windows. */
1234 /* Currently, SNAPSHOT means Windows VSS. In the future, it perhaps
1235 * could be implemented for other types of snapshots, such as btrfs. */
1236 if (add_flags & WIMLIB_ADD_FLAG_SNAPSHOT) {
1237 ERROR("Snapshot mode is only supported on Windows (VSS)");
1238 return WIMLIB_ERR_UNSUPPORTED;
1242 /* VERBOSE implies EXCLUDE_VERBOSE */
1243 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1244 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1246 /* Check for contradictory reparse point fixup flags */
1247 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1248 WIMLIB_ADD_FLAG_NORPFIX)) ==
1249 (WIMLIB_ADD_FLAG_RPFIX |
1250 WIMLIB_ADD_FLAG_NORPFIX))
1252 ERROR("Cannot specify RPFIX and NORPFIX flags "
1253 "at the same time!");
1254 return WIMLIB_ERR_INVALID_PARAM;
1257 /* Set default behavior on reparse point fixups if requested */
1258 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1259 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1261 /* Do reparse-point fixups by default if we are capturing an
1262 * entire image and either the header flag is set from previous
1263 * images, or if this is the first image being added. */
1264 if (is_entire_image &&
1265 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1266 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1269 if (!is_entire_image) {
1270 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1271 ERROR("Cannot do reparse point fixups when "
1272 "not capturing a full image!");
1273 return WIMLIB_ERR_INVALID_PARAM;
1276 /* We may have modified the add flags. */
1277 cmd->add.add_flags = add_flags;
1282 check_delete_command(const struct wimlib_update_command *cmd)
1284 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1285 WIMLIB_DELETE_FLAG_RECURSIVE))
1286 return WIMLIB_ERR_INVALID_PARAM;
1291 check_rename_command(const struct wimlib_update_command *cmd)
1293 if (cmd->rename.rename_flags != 0)
1294 return WIMLIB_ERR_INVALID_PARAM;
1299 check_update_command(struct wimlib_update_command *cmd,
1300 const struct wim_header *hdr)
1303 case WIMLIB_UPDATE_OP_ADD:
1304 return check_add_command(cmd, hdr);
1305 case WIMLIB_UPDATE_OP_DELETE:
1306 return check_delete_command(cmd);
1307 case WIMLIB_UPDATE_OP_RENAME:
1308 return check_rename_command(cmd);
1314 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1315 const struct wim_header *hdr)
1318 for (size_t i = 0; i < num_cmds; i++) {
1319 ret = check_update_command(&cmds[i], hdr);
1328 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1331 for (size_t i = 0; i < num_cmds; i++) {
1332 switch (cmds[i].op) {
1333 case WIMLIB_UPDATE_OP_ADD:
1334 FREE(cmds[i].add.wim_target_path);
1336 case WIMLIB_UPDATE_OP_DELETE:
1337 FREE(cmds[i].delete_.wim_path);
1339 case WIMLIB_UPDATE_OP_RENAME:
1340 FREE(cmds[i].rename.wim_source_path);
1341 FREE(cmds[i].rename.wim_target_path);
1350 copy_update_commands(const struct wimlib_update_command *cmds,
1352 struct wimlib_update_command **cmds_copy_ret)
1355 struct wimlib_update_command *cmds_copy;
1357 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1361 for (size_t i = 0; i < num_cmds; i++) {
1362 cmds_copy[i].op = cmds[i].op;
1363 switch (cmds[i].op) {
1364 case WIMLIB_UPDATE_OP_ADD:
1365 cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1366 cmds_copy[i].add.wim_target_path =
1367 canonicalize_wim_path(cmds[i].add.wim_target_path);
1368 if (!cmds_copy[i].add.wim_target_path)
1370 cmds_copy[i].add.config_file = cmds[i].add.config_file;
1371 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1373 case WIMLIB_UPDATE_OP_DELETE:
1374 cmds_copy[i].delete_.wim_path =
1375 canonicalize_wim_path(cmds[i].delete_.wim_path);
1376 if (!cmds_copy[i].delete_.wim_path)
1378 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1380 case WIMLIB_UPDATE_OP_RENAME:
1381 cmds_copy[i].rename.wim_source_path =
1382 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1383 cmds_copy[i].rename.wim_target_path =
1384 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1385 if (!cmds_copy[i].rename.wim_source_path ||
1386 !cmds_copy[i].rename.wim_target_path)
1390 ERROR("Unknown update operation %u", cmds[i].op);
1391 ret = WIMLIB_ERR_INVALID_PARAM;
1395 *cmds_copy_ret = cmds_copy;
1400 ret = WIMLIB_ERR_NOMEM;
1402 free_update_commands(cmds_copy, num_cmds);
1406 /* API function documented in wimlib.h */
1408 wimlib_update_image(WIMStruct *wim,
1410 const struct wimlib_update_command *cmds,
1415 struct wim_image_metadata *imd;
1416 struct wimlib_update_command *cmds_copy;
1418 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1419 return WIMLIB_ERR_INVALID_PARAM;
1421 /* Load the metadata for the image to modify (if not loaded already) */
1422 ret = select_wim_image(wim, image);
1426 imd = wim->image_metadata[image - 1];
1428 /* Don't allow updating an image currently being shared by multiple
1429 * WIMStructs (as a result of an export) */
1430 if (imd->refcnt > 1)
1431 return WIMLIB_ERR_IMAGE_HAS_MULTIPLE_REFERENCES;
1433 /* Make a copy of the update commands, in the process doing certain
1434 * canonicalizations on paths (e.g. translating backslashes to forward
1435 * slashes). This is done to avoid modifying the caller's copy of the
1437 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1441 /* Perform additional checks on the update commands before we execute
1443 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1445 goto out_free_cmds_copy;
1447 /* Actually execute the update commands. */
1448 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1450 goto out_free_cmds_copy;
1452 mark_image_dirty(imd);
1454 for (size_t i = 0; i < num_cmds; i++)
1455 if (cmds_copy[i].op == WIMLIB_UPDATE_OP_ADD &&
1456 cmds_copy[i].add.add_flags & WIMLIB_ADD_FLAG_RPFIX)
1457 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
1459 free_update_commands(cmds_copy, num_cmds);
1464 wimlib_delete_path(WIMStruct *wim, int image,
1465 const tchar *path, int delete_flags)
1467 struct wimlib_update_command cmd;
1469 cmd.op = WIMLIB_UPDATE_OP_DELETE;
1470 cmd.delete_.wim_path = (tchar *)path;
1471 cmd.delete_.delete_flags = delete_flags;
1473 return wimlib_update_image(wim, image, &cmd, 1, 0);
1477 wimlib_rename_path(WIMStruct *wim, int image,
1478 const tchar *source_path, const tchar *dest_path)
1480 struct wimlib_update_command cmd;
1482 cmd.op = WIMLIB_UPDATE_OP_RENAME;
1483 cmd.rename.wim_source_path = (tchar *)source_path;
1484 cmd.rename.wim_target_path = (tchar *)dest_path;
1485 cmd.rename.rename_flags = 0;
1487 return wimlib_update_image(wim, image, &cmd, 1, 0);
1491 wimlib_add_tree(WIMStruct *wim, int image,
1492 const tchar *fs_source_path, const tchar *wim_target_path,
1495 struct wimlib_update_command cmd;
1497 cmd.op = WIMLIB_UPDATE_OP_ADD;
1498 cmd.add.fs_source_path = (tchar *)fs_source_path;
1499 cmd.add.wim_target_path = (tchar *)wim_target_path;
1500 cmd.add.add_flags = add_flags;
1501 cmd.add.config_file = NULL;
1503 return wimlib_update_image(wim, image, &cmd, 1, 0);