2 * update_image.c - Update a WIM image.
6 * Copyright (C) 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/capture.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/error.h"
31 #include "wimlib/lookup_table.h"
32 #include "wimlib/metadata.h"
34 # include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
36 #include "wimlib/paths.h"
37 #include "wimlib/xml.h"
41 /* Overlays @branch onto @target, both of which must be directories. */
43 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
45 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
46 branch->file_name, target->file_name);
48 if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
49 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
50 "is not directory-on-directory!", branch->file_name);
51 return WIMLIB_ERR_INVALID_OVERLAY;
54 LIST_HEAD(moved_children);
55 while (dentry_has_children(branch)) {
56 struct wim_dentry *child = dentry_any_child(branch);
57 struct wim_dentry *existing;
59 /* Move @child to the directory @target */
61 existing = dentry_add_child(target, child);
63 /* File or directory with same name already exists */
66 ret = do_overlay(existing, child);
68 /* Overlay failed. Revert the changes. */
69 dentry_add_child(branch, child);
70 list_for_each_entry(child, &moved_children, tmp_list)
73 dentry_add_child(branch, child);
78 list_add(&child->tmp_list, &moved_children);
85 /* Attach or overlay a branch onto the WIM image.
88 * Pointer to the root of the WIM image, or pointer to NULL if it has not
93 * Path in the WIM image to add the branch, with leading and trailing
97 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
98 tchar *target_path, CASE_SENSITIVITY_TYPE case_type)
101 struct wim_dentry *dentry, *parent, *target;
104 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
105 branch->file_name, target_path);
107 if (*target_path == T('\0')) {
108 /* Target: root directory */
110 /* Overlay on existing root */
111 return do_overlay(*root_p, branch);
113 if (!dentry_is_directory(branch)) {
114 ERROR("Cannot set non-directory as root of WIM image");
115 return WIMLIB_ERR_NOTDIR;
123 /* Adding a non-root branch. Create root if it hasn't been created
126 ret = new_filler_directory(T(""), root_p);
131 /* Walk the path to the branch, creating filler directories as needed.
134 while ((slash = tstrchr(target_path, WIM_PATH_SEPARATOR))) {
136 dentry = get_dentry_child_with_name(parent, target_path,
139 ret = new_filler_directory(target_path, &dentry);
142 dentry_add_child(parent, dentry);
146 /* Skip over slashes. Note: this cannot overrun the length of
147 * the string because the last character cannot be a slash, as
148 * trailing slashes were tripped. */
151 } while (*target_path == WIM_PATH_SEPARATOR);
154 /* If the target path already existed, overlay the branch onto it.
155 * Otherwise, set the branch as the target path. */
156 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
157 branch->file_name_nbytes,
160 return do_overlay(target, branch);
162 dentry_add_child(parent, branch);
168 execute_add_command(WIMStruct *wim,
169 const struct wimlib_update_command *add_cmd,
170 wimlib_progress_func_t progress_func)
174 tchar *fs_source_path;
175 tchar *wim_target_path;
176 struct wim_image_metadata *imd;
177 struct list_head unhashed_streams;
178 struct add_image_params params;
179 int (*capture_tree)(struct wim_dentry **,
181 struct add_image_params *);
182 struct wimlib_capture_config *config;
184 struct _ntfs_volume *ntfs_vol = NULL;
187 struct wim_dentry *branch;
188 bool rollback_sd = true;
190 wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
192 add_flags = add_cmd->add.add_flags;
193 fs_source_path = add_cmd->add.fs_source_path;
194 wim_target_path = add_cmd->add.wim_target_path;
195 config = add_cmd->add.config;
196 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
197 fs_source_path, wim_target_path, add_flags);
199 memset(¶ms, 0, sizeof(params));
201 imd = wim->image_metadata[wim->current_image - 1];
203 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
205 capture_tree = build_dentry_tree_ntfs;
206 extra_arg = &ntfs_vol;
207 if (imd->ntfs_vol != NULL) {
208 ERROR("NTFS volume already set");
209 ret = WIMLIB_ERR_INVALID_PARAM;
213 ret = WIMLIB_ERR_INVALID_PARAM;
218 capture_tree = win32_build_dentry_tree;
220 capture_tree = unix_build_dentry_tree;
226 ret = init_inode_table(¶ms.inode_table, 9001);
230 ret = init_sd_set(¶ms.sd_set, imd->security_data);
232 goto out_destroy_inode_table;
234 INIT_LIST_HEAD(&unhashed_streams);
235 params.lookup_table = wim->lookup_table;
236 params.unhashed_streams = &unhashed_streams;
237 params.config = config;
238 params.add_flags = add_flags;
239 params.extra_arg = extra_arg;
241 params.progress_func = progress_func;
242 params.progress.scan.source = fs_source_path;
243 params.progress.scan.wim_target_path = wim_target_path;
245 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, ¶ms.progress);
247 config->_prefix = fs_source_path;
248 config->_prefix_num_tchars = tstrlen(fs_source_path);
251 if (wim_target_path[0] == T('\0'))
252 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
253 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
255 goto out_destroy_sd_set;
258 /* Use the target name, not the source name, for
259 * the root of each branch from a capture
260 * source. (This will also set the root dentry
261 * of the entire image to be unnamed.) */
262 ret = dentry_set_name(branch,
263 path_basename(wim_target_path));
265 goto out_ntfs_umount;
267 ret = attach_branch(&imd->root_dentry, branch, wim_target_path,
268 WIMLIB_CASE_PLATFORM_DEFAULT);
270 goto out_ntfs_umount;
273 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, ¶ms.progress);
274 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
276 imd->ntfs_vol = ntfs_vol;
278 inode_table_prepare_inode_list(¶ms.inode_table, &imd->inode_list);
281 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
282 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
283 goto out_destroy_sd_set;
287 do_ntfs_umount(ntfs_vol);
289 free_dentry_tree(branch, wim->lookup_table);
291 destroy_sd_set(¶ms.sd_set, rollback_sd);
292 out_destroy_inode_table:
293 destroy_inode_table(¶ms.inode_table);
299 execute_delete_command(WIMStruct *wim,
300 const struct wimlib_update_command *delete_cmd)
303 const tchar *wim_path;
304 struct wim_dentry *tree;
307 wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
308 flags = delete_cmd->delete_.delete_flags;
309 wim_path = delete_cmd->delete_.wim_path;
311 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
313 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
315 /* Path to delete does not exist in the WIM. */
316 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
319 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
320 wim_path, wim->current_image);
321 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
325 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
326 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
327 "but a recursive delete was not requested",
328 wim_path, wim->current_image);
329 return WIMLIB_ERR_IS_DIRECTORY;
332 is_root = dentry_is_root(tree);
334 free_dentry_tree(tree, wim->lookup_table);
336 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
341 execute_rename_command(WIMStruct *wim,
342 const struct wimlib_update_command *rename_cmd)
346 wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
348 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
349 rename_cmd->rename.wim_target_path,
350 WIMLIB_CASE_PLATFORM_DEFAULT);
354 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
355 rename_cmd->rename.wim_source_path,
356 rename_cmd->rename.wim_target_path);
359 ret = WIMLIB_ERR_NOMEM;
362 ret = WIMLIB_ERR_NOTDIR;
365 ret = WIMLIB_ERR_NOTEMPTY;
368 ret = WIMLIB_ERR_IS_DIRECTORY;
372 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
379 static inline const tchar *
380 update_op_to_str(int op)
383 case WIMLIB_UPDATE_OP_ADD:
385 case WIMLIB_UPDATE_OP_DELETE:
387 case WIMLIB_UPDATE_OP_RENAME:
396 execute_update_commands(WIMStruct *wim,
397 const struct wimlib_update_command *cmds,
400 wimlib_progress_func_t progress_func)
403 union wimlib_progress_info info;
404 info.update.completed_commands = 0;
405 info.update.total_commands = num_cmds;
406 for (size_t i = 0; i < num_cmds; i++) {
407 DEBUG("Executing update command %zu of %zu (op=%"TS")",
408 i + 1, num_cmds, update_op_to_str(cmds[i].op));
409 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
412 info.update.command = &cmds[i];
413 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
416 switch (cmds[i].op) {
417 case WIMLIB_UPDATE_OP_ADD:
418 ret = execute_add_command(wim, &cmds[i], progress_func);
420 case WIMLIB_UPDATE_OP_DELETE:
421 ret = execute_delete_command(wim, &cmds[i]);
423 case WIMLIB_UPDATE_OP_RENAME:
424 ret = execute_rename_command(wim, &cmds[i]);
431 info.update.completed_commands++;
432 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
435 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
447 T("/System Volume Information"),
452 static const struct wimlib_capture_config winconfig = {
454 .num_pats = ARRAY_LEN(winpats),
460 check_add_command(struct wimlib_update_command *cmd,
461 const struct wim_header *hdr)
463 int add_flags = cmd->add.add_flags;
465 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
466 WIMLIB_ADD_FLAG_DEREFERENCE |
467 WIMLIB_ADD_FLAG_VERBOSE |
468 /* BOOT doesn't make sense for wimlib_update_image() */
469 /*WIMLIB_ADD_FLAG_BOOT |*/
470 WIMLIB_ADD_FLAG_UNIX_DATA |
471 WIMLIB_ADD_FLAG_NO_ACLS |
472 WIMLIB_ADD_FLAG_STRICT_ACLS |
473 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
474 WIMLIB_ADD_FLAG_RPFIX |
475 WIMLIB_ADD_FLAG_NORPFIX |
476 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
477 WIMLIB_ADD_FLAG_WINCONFIG))
478 return WIMLIB_ERR_INVALID_PARAM;
480 /* Are we adding the entire image or not? An empty wim_target_path
481 * indicates that the tree we're adding is to be placed in the root of
482 * the image. We consider this to be capturing the entire image,
483 * although it could potentially be an overlay on an existing root as
485 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
488 /* Check for flags not supported on Windows */
489 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
490 ERROR("wimlib was compiled without support for NTFS-3g, so");
491 ERROR("we cannot capture a WIM image directly from a NTFS volume");
492 return WIMLIB_ERR_UNSUPPORTED;
494 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
495 ERROR("Capturing UNIX-specific data is not supported on Windows");
496 return WIMLIB_ERR_UNSUPPORTED;
498 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
499 ERROR("Dereferencing symbolic links is not supported on Windows");
500 return WIMLIB_ERR_UNSUPPORTED;
504 /* VERBOSE implies EXCLUDE_VERBOSE */
505 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
506 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
508 /* Check for contradictory reparse point fixup flags */
509 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
510 WIMLIB_ADD_FLAG_NORPFIX)) ==
511 (WIMLIB_ADD_FLAG_RPFIX |
512 WIMLIB_ADD_FLAG_NORPFIX))
514 ERROR("Cannot specify RPFIX and NORPFIX flags "
515 "at the same time!");
516 return WIMLIB_ERR_INVALID_PARAM;
519 /* Set default behavior on reparse point fixups if requested */
520 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
521 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
523 /* Do reparse-point fixups by default if we are capturing an
524 * entire image and either the header flag is set from previous
525 * images, or if this is the first image being added. */
526 if (is_entire_image &&
527 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
528 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
531 if (!is_entire_image) {
532 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
533 ERROR("Cannot add directly from a NTFS volume "
534 "when not capturing a full image!");
535 return WIMLIB_ERR_INVALID_PARAM;
538 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
539 ERROR("Cannot do reparse point fixups when "
540 "not capturing a full image!");
541 return WIMLIB_ERR_INVALID_PARAM;
544 /* We may have modified the add flags. */
545 cmd->add.add_flags = add_flags;
550 check_delete_command(const struct wimlib_update_command *cmd)
552 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
553 WIMLIB_DELETE_FLAG_RECURSIVE))
554 return WIMLIB_ERR_INVALID_PARAM;
559 check_rename_command(const struct wimlib_update_command *cmd)
561 if (cmd->rename.rename_flags != 0)
562 return WIMLIB_ERR_INVALID_PARAM;
567 check_update_command(struct wimlib_update_command *cmd,
568 const struct wim_header *hdr)
571 case WIMLIB_UPDATE_OP_ADD:
572 return check_add_command(cmd, hdr);
573 case WIMLIB_UPDATE_OP_DELETE:
574 return check_delete_command(cmd);
575 case WIMLIB_UPDATE_OP_RENAME:
576 return check_rename_command(cmd);
582 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
583 const struct wim_header *hdr)
586 for (size_t i = 0; i < num_cmds; i++) {
587 ret = check_update_command(&cmds[i], hdr);
596 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
599 for (size_t i = 0; i < num_cmds; i++) {
600 switch (cmds[i].op) {
601 case WIMLIB_UPDATE_OP_ADD:
602 FREE(cmds[i].add.fs_source_path);
603 FREE(cmds[i].add.wim_target_path);
604 free_capture_config(cmds[i].add.config);
606 case WIMLIB_UPDATE_OP_DELETE:
607 FREE(cmds[i].delete_.wim_path);
609 case WIMLIB_UPDATE_OP_RENAME:
610 FREE(cmds[i].rename.wim_source_path);
611 FREE(cmds[i].rename.wim_target_path);
620 copy_update_commands(const struct wimlib_update_command *cmds,
622 struct wimlib_update_command **cmds_copy_ret)
625 struct wimlib_update_command *cmds_copy;
626 const struct wimlib_capture_config *config;
628 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
632 for (size_t i = 0; i < num_cmds; i++) {
633 cmds_copy[i].op = cmds[i].op;
634 switch (cmds[i].op) {
635 case WIMLIB_UPDATE_OP_ADD:
636 cmds_copy[i].add.fs_source_path =
637 canonicalize_fs_path(cmds[i].add.fs_source_path);
638 cmds_copy[i].add.wim_target_path =
639 canonicalize_wim_path(cmds[i].add.wim_target_path);
640 if (!cmds_copy[i].add.fs_source_path ||
641 !cmds_copy[i].add.wim_target_path)
643 config = cmds[i].add.config;
644 if (cmds[i].add.add_flags & WIMLIB_ADD_FLAG_WINCONFIG)
647 ret = copy_and_canonicalize_capture_config(config,
648 &cmds_copy[i].add.config);
652 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
654 case WIMLIB_UPDATE_OP_DELETE:
655 cmds_copy[i].delete_.wim_path =
656 canonicalize_wim_path(cmds[i].delete_.wim_path);
657 if (!cmds_copy[i].delete_.wim_path)
659 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
661 case WIMLIB_UPDATE_OP_RENAME:
662 cmds_copy[i].rename.wim_source_path =
663 canonicalize_wim_path(cmds[i].rename.wim_source_path);
664 cmds_copy[i].rename.wim_target_path =
665 canonicalize_wim_path(cmds[i].rename.wim_target_path);
666 if (!cmds_copy[i].rename.wim_source_path ||
667 !cmds_copy[i].rename.wim_target_path)
671 ERROR("Unknown update operation %u", cmds[i].op);
672 ret = WIMLIB_ERR_INVALID_PARAM;
676 *cmds_copy_ret = cmds_copy;
681 ret = WIMLIB_ERR_NOMEM;
683 free_update_commands(cmds_copy, num_cmds);
687 /* API function documented in wimlib.h */
689 wimlib_update_image(WIMStruct *wim,
691 const struct wimlib_update_command *cmds,
694 wimlib_progress_func_t progress_func)
697 struct wimlib_update_command *cmds_copy;
698 bool deletion_requested = false;
700 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
701 return WIMLIB_ERR_INVALID_PARAM;
703 DEBUG("Updating image %d with %zu commands", image, num_cmds);
705 for (size_t i = 0; i < num_cmds; i++)
706 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
707 deletion_requested = true;
709 if (deletion_requested)
710 ret = can_delete_from_wim(wim);
712 ret = can_modify_wim(wim);
717 /* Load the metadata for the image to modify (if not loaded already) */
718 ret = select_wim_image(wim, image);
722 /* Short circuit a successful return if no commands were specified.
723 * Avoids problems with trying to allocate 0 bytes of memory. */
727 DEBUG("Preparing %zu update commands", num_cmds);
729 /* Make a copy of the update commands, in the process doing certain
730 * canonicalizations on paths (e.g. translating backslashes to forward
731 * slashes). This is done to avoid modifying the caller's copy of the
733 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
737 /* Perform additional checks on the update commands before we execute
739 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
741 goto out_free_cmds_copy;
743 /* Actually execute the update commands. */
744 DEBUG("Executing %zu update commands", num_cmds);
745 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
748 goto out_free_cmds_copy;
750 wim->image_metadata[image - 1]->modified = 1;
752 /* Statistics about the WIM image, such as the numbers of files and
753 * directories, may have changed. Call xml_update_image_info() to
754 * recalculate these statistics. */
755 xml_update_image_info(wim, image);
757 free_update_commands(cmds_copy, num_cmds);