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"
31 /* Overlays @branch onto @target, both of which must be directories. */
33 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
35 struct rb_root *rb_root;
37 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
38 branch->file_name, target->file_name);
40 if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
41 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
42 "is not directory-on-directory!", branch->file_name);
43 return WIMLIB_ERR_INVALID_OVERLAY;
46 rb_root = &branch->d_inode->i_children;
47 while (rb_root->rb_node) { /* While @branch has children... */
48 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
49 struct wim_dentry *existing;
51 /* Move @child to the directory @target */
53 existing = dentry_add_child(target, child);
55 /* File or directory with same name already exists */
58 ret = do_overlay(existing, child);
60 /* Overlay failed. Revert the change to avoid
61 * leaking the directory tree rooted at @child.
63 dentry_add_child(branch, child);
72 /* Attach or overlay a branch onto the WIM image.
75 * Pointer to the root of the WIM image, or pointer to NULL if it has not
80 * Path in the WIM image to add the branch, with leading and trailing
84 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
88 struct wim_dentry *dentry, *parent, *target;
91 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
92 branch->file_name, target_path);
94 if (*target_path == T('\0')) {
95 /* Target: root directory */
97 /* Overlay on existing root */
98 return do_overlay(*root_p, branch);
100 if (!dentry_is_directory(branch)) {
101 ERROR("Cannot set non-directory as root of WIM image");
102 return WIMLIB_ERR_NOTDIR;
110 /* Adding a non-root branch. Create root if it hasn't been created
113 ret = new_filler_directory(T(""), root_p);
118 /* Walk the path to the branch, creating filler directories as needed.
121 while ((slash = tstrchr(target_path, T('/')))) {
123 dentry = get_dentry_child_with_name(parent, target_path);
125 ret = new_filler_directory(target_path, &dentry);
128 dentry_add_child(parent, dentry);
132 /* Skip over slashes. Note: this cannot overrun the length of
133 * the string because the last character cannot be a slash, as
134 * trailing slashes were tripped. */
137 } while (*target_path == T('/'));
140 /* If the target path already existed, overlay the branch onto it.
141 * Otherwise, set the branch as the target path. */
142 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
143 branch->file_name_nbytes);
145 return do_overlay(target, branch);
147 dentry_add_child(parent, branch);
153 execute_add_command(WIMStruct *wim,
154 const struct wimlib_update_command *add_cmd,
155 wimlib_progress_func_t progress_func)
159 tchar *fs_source_path;
160 tchar *wim_target_path;
161 struct wim_inode_table inode_table;
162 struct sd_set sd_set;
163 struct wim_image_metadata *imd;
164 struct list_head unhashed_streams;
165 struct add_image_params params;
166 int (*capture_tree)(struct wim_dentry **,
168 struct add_image_params *);
169 union wimlib_progress_info progress;
170 struct wimlib_capture_config *config;
172 struct _ntfs_volume *ntfs_vol = NULL;
175 struct wim_dentry *branch;
176 bool rollback_sd = true;
178 wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
179 add_flags = add_cmd->add.add_flags;
180 fs_source_path = add_cmd->add.fs_source_path;
181 wim_target_path = add_cmd->add.wim_target_path;
182 config = add_cmd->add.config;
183 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
184 fs_source_path, wim_target_path, add_flags);
186 imd = wim->image_metadata[wim->current_image - 1];
188 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
190 capture_tree = build_dentry_tree_ntfs;
191 extra_arg = &ntfs_vol;
192 if (imd->ntfs_vol != NULL) {
193 ERROR("NTFS volume already set");
194 ret = WIMLIB_ERR_INVALID_PARAM;
198 ret = WIMLIB_ERR_INVALID_PARAM;
203 capture_tree = win32_build_dentry_tree;
205 capture_tree = unix_build_dentry_tree;
210 ret = init_inode_table(&inode_table, 9001);
214 ret = init_sd_set(&sd_set, imd->security_data);
216 goto out_destroy_inode_table;
218 INIT_LIST_HEAD(&unhashed_streams);
219 wim->lookup_table->unhashed_streams = &unhashed_streams;
220 params.lookup_table = wim->lookup_table;
221 params.inode_table = &inode_table;
222 params.sd_set = &sd_set;
223 params.config = config;
224 params.add_flags = add_flags;
225 params.progress_func = progress_func;
226 params.extra_arg = extra_arg;
229 memset(&progress, 0, sizeof(progress));
230 progress.scan.source = fs_source_path;
231 progress.scan.wim_target_path = wim_target_path;
232 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
234 config->_prefix = fs_source_path;
235 config->_prefix_num_tchars = tstrlen(fs_source_path);
237 if (wim_target_path[0] == T('\0'))
238 add_flags |= WIMLIB_ADD_FLAG_ROOT;
239 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
241 ERROR("Failed to build dentry tree for \"%"TS"\"",
243 goto out_destroy_sd_set;
246 /* Use the target name, not the source name, for
247 * the root of each branch from a capture
248 * source. (This will also set the root dentry
249 * of the entire image to be unnamed.) */
250 ret = set_dentry_name(branch,
251 path_basename(wim_target_path));
253 goto out_ntfs_umount;
255 ret = attach_branch(&imd->root_dentry, branch, wim_target_path);
257 goto out_ntfs_umount;
260 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
261 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
263 imd->ntfs_vol = ntfs_vol;
265 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
268 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
269 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
270 goto out_destroy_sd_set;
274 do_ntfs_umount(ntfs_vol);
276 free_dentry_tree(branch, wim->lookup_table);
278 destroy_sd_set(&sd_set, rollback_sd);
279 out_destroy_inode_table:
280 destroy_inode_table(&inode_table);
286 execute_delete_command(WIMStruct *wim,
287 const struct wimlib_update_command *delete_cmd)
290 const tchar *wim_path;
291 struct wim_dentry *tree;
294 wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
295 flags = delete_cmd->delete.delete_flags;
296 wim_path = delete_cmd->delete.wim_path;
298 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
300 tree = get_dentry(wim, wim_path);
302 /* Path to delete does not exist in the WIM. */
303 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
306 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
307 wim_path, wim->current_image);
308 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
312 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
313 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
314 "but a recursive delete was not requested",
315 wim_path, wim->current_image);
316 return WIMLIB_ERR_IS_DIRECTORY;
319 is_root = dentry_is_root(tree);
321 free_dentry_tree(tree, wim->lookup_table);
323 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
328 * Rename a file or directory in the WIM.
330 * This is also called from wimfs_rename() in the FUSE mount code.
333 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to)
335 struct wim_dentry *src;
336 struct wim_dentry *dst;
337 struct wim_dentry *parent_of_dst;
340 /* This rename() implementation currently only supports actual files
341 * (not alternate data streams) */
343 src = get_dentry(wim, from);
347 dst = get_dentry(wim, to);
350 /* Destination file exists */
352 if (src == dst) /* Same file */
355 if (!dentry_is_directory(src)) {
356 /* Cannot rename non-directory to directory. */
357 if (dentry_is_directory(dst))
360 /* Cannot rename directory to a non-directory or a non-empty
362 if (!dentry_is_directory(dst))
364 if (inode_has_children(dst->d_inode))
367 parent_of_dst = dst->parent;
369 /* Destination does not exist */
370 parent_of_dst = get_parent_dentry(wim, to);
374 if (!dentry_is_directory(parent_of_dst))
378 ret = set_dentry_name(src, path_basename(to));
383 free_dentry_tree(dst, wim->lookup_table);
386 dentry_add_child(parent_of_dst, src);
392 execute_rename_command(WIMStruct *wim,
393 const struct wimlib_update_command *rename_cmd)
397 wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
399 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
400 rename_cmd->rename.wim_target_path);
404 ret = WIMLIB_ERR_NOMEM;
407 ret = WIMLIB_ERR_NOTDIR;
410 ret = WIMLIB_ERR_NOTEMPTY;
413 ret = WIMLIB_ERR_IS_DIRECTORY;
417 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
424 static inline const tchar *
425 update_op_to_str(int op)
428 case WIMLIB_UPDATE_OP_ADD:
430 case WIMLIB_UPDATE_OP_DELETE:
432 case WIMLIB_UPDATE_OP_RENAME:
440 execute_update_commands(WIMStruct *wim,
441 const struct wimlib_update_command *cmds,
443 wimlib_progress_func_t progress_func)
446 for (size_t i = 0; i < num_cmds; i++) {
447 DEBUG("Executing update command %zu of %zu (op=%"TS")",
448 i + 1, num_cmds, update_op_to_str(cmds[i].op));
449 switch (cmds[i].op) {
450 case WIMLIB_UPDATE_OP_ADD:
451 ret = execute_add_command(wim, &cmds[i], progress_func);
453 case WIMLIB_UPDATE_OP_DELETE:
454 ret = execute_delete_command(wim, &cmds[i]);
456 wim->deletion_occurred = 1;
458 case WIMLIB_UPDATE_OP_RENAME:
459 ret = execute_rename_command(wim, &cmds[i]);
472 check_add_command(struct wimlib_update_command *cmd,
473 const struct wim_header *hdr)
475 int add_flags = cmd->add.add_flags;
477 /* Are we adding the entire image or not? An empty wim_target_path
478 * indicates that the tree we're adding is to be placed in the root of
479 * the image. We consider this to be capturing the entire image,
480 * although it could potentially be an overlay on an existing root as
482 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
485 /* Check for flags not supported on Windows */
486 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
487 ERROR("wimlib was compiled without support for NTFS-3g, so");
488 ERROR("we cannot capture a WIM image directly from a NTFS volume");
489 return WIMLIB_ERR_UNSUPPORTED;
491 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
492 ERROR("Capturing UNIX-specific data is not supported on Windows");
493 return WIMLIB_ERR_UNSUPPORTED;
495 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
496 ERROR("Dereferencing symbolic links is not supported on Windows");
497 return WIMLIB_ERR_UNSUPPORTED;
501 /* VERBOSE implies EXCLUDE_VERBOSE */
502 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
503 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
505 /* Check for contradictory reparse point fixup flags */
506 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
507 WIMLIB_ADD_FLAG_NORPFIX)) ==
508 (WIMLIB_ADD_FLAG_RPFIX |
509 WIMLIB_ADD_FLAG_NORPFIX))
511 ERROR("Cannot specify RPFIX and NORPFIX flags "
512 "at the same time!");
513 return WIMLIB_ERR_INVALID_PARAM;
516 /* Set default behavior on reparse point fixups if requested */
517 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
518 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
520 /* Do reparse-point fixups by default if we are capturing an
521 * entire image and either the header flag is set from previous
522 * images, or if this is the first image being added. */
523 if (is_entire_image &&
524 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
525 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
528 if (!is_entire_image) {
529 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
530 ERROR("Cannot add directly from a NTFS volume "
531 "when not capturing a full image!");
532 return WIMLIB_ERR_INVALID_PARAM;
535 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
536 ERROR("Cannot do reparse point fixups when "
537 "not capturing a full image!");
538 return WIMLIB_ERR_INVALID_PARAM;
541 /* We may have modified the add flags. */
542 cmd->add.add_flags = add_flags;
547 check_update_command(struct wimlib_update_command *cmd,
548 const struct wim_header *hdr)
551 case WIMLIB_UPDATE_OP_ADD:
552 return check_add_command(cmd, hdr);
553 case WIMLIB_UPDATE_OP_DELETE:
554 case WIMLIB_UPDATE_OP_RENAME:
561 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
562 const struct wim_header *hdr)
565 for (size_t i = 0; i < num_cmds; i++) {
566 ret = check_update_command(&cmds[i], hdr);
575 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
578 for (size_t i = 0; i < num_cmds; i++) {
579 switch (cmds[i].op) {
580 case WIMLIB_UPDATE_OP_ADD:
581 FREE(cmds[i].add.fs_source_path);
582 FREE(cmds[i].add.wim_target_path);
583 free_capture_config(cmds[i].add.config);
585 case WIMLIB_UPDATE_OP_DELETE:
586 FREE(cmds[i].delete.wim_path);
588 case WIMLIB_UPDATE_OP_RENAME:
589 FREE(cmds[i].rename.wim_source_path);
590 FREE(cmds[i].rename.wim_target_path);
601 copy_update_commands(const struct wimlib_update_command *cmds,
603 struct wimlib_update_command **cmds_copy_ret)
606 struct wimlib_update_command *cmds_copy;
608 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
612 for (size_t i = 0; i < num_cmds; i++) {
613 cmds_copy[i].op = cmds[i].op;
614 switch (cmds[i].op) {
615 case WIMLIB_UPDATE_OP_ADD:
616 cmds_copy[i].add.fs_source_path =
617 canonicalize_fs_path(cmds[i].add.fs_source_path);
618 cmds_copy[i].add.wim_target_path =
619 canonicalize_wim_path(cmds[i].add.wim_target_path);
620 if (!cmds_copy[i].add.fs_source_path ||
621 !cmds_copy[i].add.wim_target_path)
623 if (cmds[i].add.config) {
624 ret = copy_and_canonicalize_capture_config(cmds[i].add.config,
625 &cmds_copy[i].add.config);
629 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
631 case WIMLIB_UPDATE_OP_DELETE:
632 cmds_copy[i].delete.wim_path =
633 canonicalize_wim_path(cmds[i].delete.wim_path);
634 if (!cmds_copy[i].delete.wim_path)
636 cmds_copy[i].delete.delete_flags = cmds[i].delete.delete_flags;
638 case WIMLIB_UPDATE_OP_RENAME:
639 cmds_copy[i].rename.wim_source_path =
640 canonicalize_wim_path(cmds[i].rename.wim_source_path);
641 cmds_copy[i].rename.wim_target_path =
642 canonicalize_wim_path(cmds[i].rename.wim_target_path);
643 if (!cmds_copy[i].rename.wim_source_path ||
644 !cmds_copy[i].rename.wim_target_path)
648 ERROR("Unknown update operation %u", cmds[i].op);
649 ret = WIMLIB_ERR_INVALID_PARAM;
653 *cmds_copy_ret = cmds_copy;
658 ret = WIMLIB_ERR_NOMEM;
660 free_update_commands(cmds_copy, num_cmds);
665 * Entry point for making a series of updates to a WIM image.
668 wimlib_update_image(WIMStruct *wim,
670 const struct wimlib_update_command *cmds,
673 wimlib_progress_func_t progress_func)
676 struct wimlib_update_command *cmds_copy;
678 DEBUG("Updating image %d with %zu commands", image, num_cmds);
680 /* Refuse to update a split WIM. */
681 if (wim->hdr.total_parts != 1) {
682 ERROR("Cannot update a split WIM!");
683 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
687 /* Load the metadata for the image to modify (if not loaded already) */
688 ret = select_wim_image(wim, image);
692 /* Short circuit a successful return if no commands were specified.
693 * Avoids problems with trying to allocate 0 bytes of memory. */
697 DEBUG("Preparing %zu update commands", num_cmds);
699 /* Make a copy of the update commands, in the process doing certain
700 * canonicalizations on paths (e.g. translating backslashes to forward
701 * slashes). This is done to avoid modifying the caller's copy of the
703 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
707 /* Perform additional checks on the update commands before we execute
709 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
711 goto out_free_cmds_copy;
713 /* Actually execute the update commands. */
714 DEBUG("Executing %zu update commands", num_cmds);
715 ret = execute_update_commands(wim, cmds_copy, num_cmds, progress_func);
717 goto out_free_cmds_copy;
719 wim->image_metadata[image - 1]->modified = 1;
721 /* Statistics about the WIM image, such as the numbers of files and
722 * directories, may have changed. Call xml_update_image_info() to
723 * recalculate these statistics. */
724 xml_update_image_info(wim, image);
726 free_update_commands(cmds_copy, num_cmds);