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/.
24 #include "wimlib_internal.h"
26 #include "lookup_table.h"
35 /* Overlays @branch onto @target, both of which must be directories. */
37 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
39 struct rb_root *rb_root;
41 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
42 branch->file_name, target->file_name);
44 if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
45 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
46 "is not directory-on-directory!", branch->file_name);
47 return WIMLIB_ERR_INVALID_OVERLAY;
50 rb_root = &branch->d_inode->i_children;
51 LIST_HEAD(moved_children);
52 while (rb_root->rb_node) { /* While @branch has children... */
53 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
54 struct wim_dentry *existing;
56 /* Move @child to the directory @target */
58 existing = dentry_add_child(target, child);
60 /* File or directory with same name already exists */
63 ret = do_overlay(existing, child);
65 /* Overlay failed. Revert the changes. */
66 dentry_add_child(branch, child);
67 list_for_each_entry(child, &moved_children, tmp_list)
70 dentry_add_child(branch, child);
75 list_add(&child->tmp_list, &moved_children);
82 /* Attach or overlay a branch onto the WIM image.
85 * Pointer to the root of the WIM image, or pointer to NULL if it has not
90 * Path in the WIM image to add the branch, with leading and trailing
94 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
98 struct wim_dentry *dentry, *parent, *target;
101 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
102 branch->file_name, target_path);
104 if (*target_path == T('\0')) {
105 /* Target: root directory */
107 /* Overlay on existing root */
108 return do_overlay(*root_p, branch);
110 if (!dentry_is_directory(branch)) {
111 ERROR("Cannot set non-directory as root of WIM image");
112 return WIMLIB_ERR_NOTDIR;
120 /* Adding a non-root branch. Create root if it hasn't been created
123 ret = new_filler_directory(T(""), root_p);
128 /* Walk the path to the branch, creating filler directories as needed.
131 while ((slash = tstrchr(target_path, T('/')))) {
133 dentry = get_dentry_child_with_name(parent, target_path);
135 ret = new_filler_directory(target_path, &dentry);
138 dentry_add_child(parent, dentry);
142 /* Skip over slashes. Note: this cannot overrun the length of
143 * the string because the last character cannot be a slash, as
144 * trailing slashes were tripped. */
147 } while (*target_path == T('/'));
150 /* If the target path already existed, overlay the branch onto it.
151 * Otherwise, set the branch as the target path. */
152 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
153 branch->file_name_nbytes);
155 return do_overlay(target, branch);
157 dentry_add_child(parent, branch);
163 execute_add_command(WIMStruct *wim,
164 const struct wimlib_update_command *add_cmd,
165 wimlib_progress_func_t progress_func)
169 tchar *fs_source_path;
170 tchar *wim_target_path;
171 struct wim_inode_table inode_table;
172 struct sd_set sd_set;
173 struct wim_image_metadata *imd;
174 struct list_head unhashed_streams;
175 struct add_image_params params;
176 int (*capture_tree)(struct wim_dentry **,
178 struct add_image_params *);
179 union wimlib_progress_info progress;
180 struct wimlib_capture_config *config;
182 struct _ntfs_volume *ntfs_vol = NULL;
185 struct wim_dentry *branch;
186 bool rollback_sd = true;
188 wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
189 add_flags = add_cmd->add.add_flags;
190 fs_source_path = add_cmd->add.fs_source_path;
191 wim_target_path = add_cmd->add.wim_target_path;
192 config = add_cmd->add.config;
193 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
194 fs_source_path, wim_target_path, add_flags);
196 imd = wim->image_metadata[wim->current_image - 1];
198 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
200 capture_tree = build_dentry_tree_ntfs;
201 extra_arg = &ntfs_vol;
202 if (imd->ntfs_vol != NULL) {
203 ERROR("NTFS volume already set");
204 ret = WIMLIB_ERR_INVALID_PARAM;
208 ret = WIMLIB_ERR_INVALID_PARAM;
213 capture_tree = win32_build_dentry_tree;
215 capture_tree = unix_build_dentry_tree;
220 ret = init_inode_table(&inode_table, 9001);
224 ret = init_sd_set(&sd_set, imd->security_data);
226 goto out_destroy_inode_table;
228 INIT_LIST_HEAD(&unhashed_streams);
229 wim->lookup_table->unhashed_streams = &unhashed_streams;
230 params.lookup_table = wim->lookup_table;
231 params.inode_table = &inode_table;
232 params.sd_set = &sd_set;
233 params.config = config;
234 params.add_flags = add_flags;
235 params.progress_func = progress_func;
236 params.extra_arg = extra_arg;
239 memset(&progress, 0, sizeof(progress));
240 progress.scan.source = fs_source_path;
241 progress.scan.wim_target_path = wim_target_path;
242 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
244 config->_prefix = fs_source_path;
245 config->_prefix_num_tchars = tstrlen(fs_source_path);
247 if (wim_target_path[0] == T('\0'))
248 add_flags |= WIMLIB_ADD_FLAG_ROOT;
249 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
251 ERROR("Failed to build dentry tree for \"%"TS"\"",
253 goto out_destroy_sd_set;
256 /* Use the target name, not the source name, for
257 * the root of each branch from a capture
258 * source. (This will also set the root dentry
259 * of the entire image to be unnamed.) */
260 ret = set_dentry_name(branch,
261 path_basename(wim_target_path));
263 goto out_ntfs_umount;
265 ret = attach_branch(&imd->root_dentry, branch, wim_target_path);
267 goto out_ntfs_umount;
270 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
271 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
273 imd->ntfs_vol = ntfs_vol;
275 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
278 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
279 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
280 goto out_destroy_sd_set;
284 do_ntfs_umount(ntfs_vol);
286 free_dentry_tree(branch, wim->lookup_table);
288 destroy_sd_set(&sd_set, rollback_sd);
289 out_destroy_inode_table:
290 destroy_inode_table(&inode_table);
296 execute_delete_command(WIMStruct *wim,
297 const struct wimlib_update_command *delete_cmd)
300 const tchar *wim_path;
301 struct wim_dentry *tree;
304 wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
305 flags = delete_cmd->delete.delete_flags;
306 wim_path = delete_cmd->delete.wim_path;
308 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
310 tree = get_dentry(wim, wim_path);
312 /* Path to delete does not exist in the WIM. */
313 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
316 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
317 wim_path, wim->current_image);
318 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
322 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
323 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
324 "but a recursive delete was not requested",
325 wim_path, wim->current_image);
326 return WIMLIB_ERR_IS_DIRECTORY;
329 is_root = dentry_is_root(tree);
331 free_dentry_tree(tree, wim->lookup_table);
333 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
338 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
340 FREE(dentry->_full_path);
341 dentry->_full_path = NULL;
346 * Rename a file or directory in the WIM.
348 * This is also called from wimfs_rename() in the FUSE mount code.
351 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to)
353 struct wim_dentry *src;
354 struct wim_dentry *dst;
355 struct wim_dentry *parent_of_dst;
358 /* This rename() implementation currently only supports actual files
359 * (not alternate data streams) */
361 src = get_dentry(wim, from);
365 dst = get_dentry(wim, to);
368 /* Destination file exists */
370 if (src == dst) /* Same file */
373 if (!dentry_is_directory(src)) {
374 /* Cannot rename non-directory to directory. */
375 if (dentry_is_directory(dst))
378 /* Cannot rename directory to a non-directory or a non-empty
380 if (!dentry_is_directory(dst))
382 if (inode_has_children(dst->d_inode))
385 parent_of_dst = dst->parent;
387 /* Destination does not exist */
388 parent_of_dst = get_parent_dentry(wim, to);
392 if (!dentry_is_directory(parent_of_dst))
396 ret = set_dentry_name(src, path_basename(to));
401 free_dentry_tree(dst, wim->lookup_table);
404 dentry_add_child(parent_of_dst, src);
406 for_dentry_in_tree(src, free_dentry_full_path, NULL);
412 execute_rename_command(WIMStruct *wim,
413 const struct wimlib_update_command *rename_cmd)
417 wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
419 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
420 rename_cmd->rename.wim_target_path);
424 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
425 rename_cmd->rename.wim_source_path,
426 rename_cmd->rename.wim_target_path);
429 ret = WIMLIB_ERR_NOMEM;
432 ret = WIMLIB_ERR_NOTDIR;
435 ret = WIMLIB_ERR_NOTEMPTY;
438 ret = WIMLIB_ERR_IS_DIRECTORY;
442 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
449 static inline const tchar *
450 update_op_to_str(int op)
453 case WIMLIB_UPDATE_OP_ADD:
455 case WIMLIB_UPDATE_OP_DELETE:
457 case WIMLIB_UPDATE_OP_RENAME:
465 execute_update_commands(WIMStruct *wim,
466 const struct wimlib_update_command *cmds,
468 wimlib_progress_func_t progress_func)
471 for (size_t i = 0; i < num_cmds; i++) {
472 DEBUG("Executing update command %zu of %zu (op=%"TS")",
473 i + 1, num_cmds, update_op_to_str(cmds[i].op));
474 switch (cmds[i].op) {
475 case WIMLIB_UPDATE_OP_ADD:
476 ret = execute_add_command(wim, &cmds[i], progress_func);
478 case WIMLIB_UPDATE_OP_DELETE:
479 ret = execute_delete_command(wim, &cmds[i]);
481 case WIMLIB_UPDATE_OP_RENAME:
482 ret = execute_rename_command(wim, &cmds[i]);
494 check_add_command(struct wimlib_update_command *cmd,
495 const struct wim_header *hdr)
497 int add_flags = cmd->add.add_flags;
499 /* Are we adding the entire image or not? An empty wim_target_path
500 * indicates that the tree we're adding is to be placed in the root of
501 * the image. We consider this to be capturing the entire image,
502 * although it could potentially be an overlay on an existing root as
504 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
507 /* Check for flags not supported on Windows */
508 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
509 ERROR("wimlib was compiled without support for NTFS-3g, so");
510 ERROR("we cannot capture a WIM image directly from a NTFS volume");
511 return WIMLIB_ERR_UNSUPPORTED;
513 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
514 ERROR("Capturing UNIX-specific data is not supported on Windows");
515 return WIMLIB_ERR_UNSUPPORTED;
517 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
518 ERROR("Dereferencing symbolic links is not supported on Windows");
519 return WIMLIB_ERR_UNSUPPORTED;
523 /* VERBOSE implies EXCLUDE_VERBOSE */
524 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
525 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
527 /* Check for contradictory reparse point fixup flags */
528 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
529 WIMLIB_ADD_FLAG_NORPFIX)) ==
530 (WIMLIB_ADD_FLAG_RPFIX |
531 WIMLIB_ADD_FLAG_NORPFIX))
533 ERROR("Cannot specify RPFIX and NORPFIX flags "
534 "at the same time!");
535 return WIMLIB_ERR_INVALID_PARAM;
538 /* Set default behavior on reparse point fixups if requested */
539 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
540 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
542 /* Do reparse-point fixups by default if we are capturing an
543 * entire image and either the header flag is set from previous
544 * images, or if this is the first image being added. */
545 if (is_entire_image &&
546 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
547 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
550 if (!is_entire_image) {
551 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
552 ERROR("Cannot add directly from a NTFS volume "
553 "when not capturing a full image!");
554 return WIMLIB_ERR_INVALID_PARAM;
557 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
558 ERROR("Cannot do reparse point fixups when "
559 "not capturing a full image!");
560 return WIMLIB_ERR_INVALID_PARAM;
563 /* We may have modified the add flags. */
564 cmd->add.add_flags = add_flags;
569 check_update_command(struct wimlib_update_command *cmd,
570 const struct wim_header *hdr)
573 case WIMLIB_UPDATE_OP_ADD:
574 return check_add_command(cmd, hdr);
575 case WIMLIB_UPDATE_OP_DELETE:
576 case WIMLIB_UPDATE_OP_RENAME:
583 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
584 const struct wim_header *hdr)
587 for (size_t i = 0; i < num_cmds; i++) {
588 ret = check_update_command(&cmds[i], hdr);
597 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
600 for (size_t i = 0; i < num_cmds; i++) {
601 switch (cmds[i].op) {
602 case WIMLIB_UPDATE_OP_ADD:
603 FREE(cmds[i].add.fs_source_path);
604 FREE(cmds[i].add.wim_target_path);
605 free_capture_config(cmds[i].add.config);
607 case WIMLIB_UPDATE_OP_DELETE:
608 FREE(cmds[i].delete.wim_path);
610 case WIMLIB_UPDATE_OP_RENAME:
611 FREE(cmds[i].rename.wim_source_path);
612 FREE(cmds[i].rename.wim_target_path);
621 copy_update_commands(const struct wimlib_update_command *cmds,
623 struct wimlib_update_command **cmds_copy_ret)
626 struct wimlib_update_command *cmds_copy;
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 if (cmds[i].add.config) {
644 ret = copy_and_canonicalize_capture_config(cmds[i].add.config,
645 &cmds_copy[i].add.config);
649 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
651 case WIMLIB_UPDATE_OP_DELETE:
652 cmds_copy[i].delete.wim_path =
653 canonicalize_wim_path(cmds[i].delete.wim_path);
654 if (!cmds_copy[i].delete.wim_path)
656 cmds_copy[i].delete.delete_flags = cmds[i].delete.delete_flags;
658 case WIMLIB_UPDATE_OP_RENAME:
659 cmds_copy[i].rename.wim_source_path =
660 canonicalize_wim_path(cmds[i].rename.wim_source_path);
661 cmds_copy[i].rename.wim_target_path =
662 canonicalize_wim_path(cmds[i].rename.wim_target_path);
663 if (!cmds_copy[i].rename.wim_source_path ||
664 !cmds_copy[i].rename.wim_target_path)
668 ERROR("Unknown update operation %u", cmds[i].op);
669 ret = WIMLIB_ERR_INVALID_PARAM;
673 *cmds_copy_ret = cmds_copy;
678 ret = WIMLIB_ERR_NOMEM;
680 free_update_commands(cmds_copy, num_cmds);
685 * Entry point for making a series of updates to a WIM image.
688 wimlib_update_image(WIMStruct *wim,
690 const struct wimlib_update_command *cmds,
693 wimlib_progress_func_t progress_func)
696 struct wimlib_update_command *cmds_copy;
698 DEBUG("Updating image %d with %zu commands", image, num_cmds);
700 /* Refuse to update a split WIM. */
701 if (wim->hdr.total_parts != 1) {
702 ERROR("Cannot update a split WIM!");
703 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
707 /* Load the metadata for the image to modify (if not loaded already) */
708 ret = select_wim_image(wim, image);
712 /* Short circuit a successful return if no commands were specified.
713 * Avoids problems with trying to allocate 0 bytes of memory. */
717 DEBUG("Preparing %zu update commands", num_cmds);
719 /* Make a copy of the update commands, in the process doing certain
720 * canonicalizations on paths (e.g. translating backslashes to forward
721 * slashes). This is done to avoid modifying the caller's copy of the
723 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
727 /* Perform additional checks on the update commands before we execute
729 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
731 goto out_free_cmds_copy;
733 /* Actually execute the update commands. */
734 DEBUG("Executing %zu update commands", num_cmds);
735 ret = execute_update_commands(wim, cmds_copy, num_cmds, progress_func);
737 goto out_free_cmds_copy;
739 wim->image_metadata[image - 1]->modified = 1;
741 /* Statistics about the WIM image, such as the numbers of files and
742 * directories, may have changed. Call xml_update_image_info() to
743 * recalculate these statistics. */
744 xml_update_image_info(wim, image);
746 free_update_commands(cmds_copy, num_cmds);