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 struct rb_root *rb_root;
47 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
48 branch->file_name, target->file_name);
50 if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
51 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
52 "is not directory-on-directory!", branch->file_name);
53 return WIMLIB_ERR_INVALID_OVERLAY;
56 rb_root = &branch->d_inode->i_children;
57 LIST_HEAD(moved_children);
58 while (rb_root->rb_node) { /* While @branch has children... */
59 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
60 struct wim_dentry *existing;
62 /* Move @child to the directory @target */
64 existing = dentry_add_child(target, child);
66 /* File or directory with same name already exists */
69 ret = do_overlay(existing, child);
71 /* Overlay failed. Revert the changes. */
72 dentry_add_child(branch, child);
73 list_for_each_entry(child, &moved_children, tmp_list)
76 dentry_add_child(branch, child);
81 list_add(&child->tmp_list, &moved_children);
88 /* Attach or overlay a branch onto the WIM image.
91 * Pointer to the root of the WIM image, or pointer to NULL if it has not
96 * Path in the WIM image to add the branch, with leading and trailing
100 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
104 struct wim_dentry *dentry, *parent, *target;
107 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
108 branch->file_name, target_path);
110 if (*target_path == T('\0')) {
111 /* Target: root directory */
113 /* Overlay on existing root */
114 return do_overlay(*root_p, branch);
116 if (!dentry_is_directory(branch)) {
117 ERROR("Cannot set non-directory as root of WIM image");
118 return WIMLIB_ERR_NOTDIR;
126 /* Adding a non-root branch. Create root if it hasn't been created
129 ret = new_filler_directory(T(""), root_p);
134 /* Walk the path to the branch, creating filler directories as needed.
137 while ((slash = tstrchr(target_path, T('/')))) {
139 dentry = get_dentry_child_with_name(parent, target_path);
141 ret = new_filler_directory(target_path, &dentry);
144 dentry_add_child(parent, dentry);
148 /* Skip over slashes. Note: this cannot overrun the length of
149 * the string because the last character cannot be a slash, as
150 * trailing slashes were tripped. */
153 } while (*target_path == T('/'));
156 /* If the target path already existed, overlay the branch onto it.
157 * Otherwise, set the branch as the target path. */
158 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
159 branch->file_name_nbytes);
161 return do_overlay(target, branch);
163 dentry_add_child(parent, branch);
169 execute_add_command(WIMStruct *wim,
170 const struct wimlib_update_command *add_cmd,
171 wimlib_progress_func_t progress_func)
175 tchar *fs_source_path;
176 tchar *wim_target_path;
177 struct wim_image_metadata *imd;
178 struct list_head unhashed_streams;
179 struct add_image_params params;
180 int (*capture_tree)(struct wim_dentry **,
182 struct add_image_params *);
183 union wimlib_progress_info progress;
184 struct wimlib_capture_config *config;
186 struct _ntfs_volume *ntfs_vol = NULL;
189 struct wim_dentry *branch;
190 bool rollback_sd = true;
192 wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
193 add_flags = add_cmd->add.add_flags;
194 fs_source_path = add_cmd->add.fs_source_path;
195 wim_target_path = add_cmd->add.wim_target_path;
196 config = add_cmd->add.config;
197 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
198 fs_source_path, wim_target_path, add_flags);
200 imd = wim->image_metadata[wim->current_image - 1];
202 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
204 capture_tree = build_dentry_tree_ntfs;
205 extra_arg = &ntfs_vol;
206 if (imd->ntfs_vol != NULL) {
207 ERROR("NTFS volume already set");
208 ret = WIMLIB_ERR_INVALID_PARAM;
212 ret = WIMLIB_ERR_INVALID_PARAM;
217 capture_tree = win32_build_dentry_tree;
219 capture_tree = unix_build_dentry_tree;
224 ret = init_inode_table(¶ms.inode_table, 9001);
228 ret = init_sd_set(¶ms.sd_set, imd->security_data);
230 goto out_destroy_inode_table;
232 INIT_LIST_HEAD(&unhashed_streams);
233 wim->lookup_table->unhashed_streams = &unhashed_streams;
234 params.lookup_table = wim->lookup_table;
235 params.config = config;
236 params.add_flags = add_flags;
237 params.progress_func = progress_func;
238 params.extra_arg = extra_arg;
241 memset(&progress, 0, sizeof(progress));
242 progress.scan.source = fs_source_path;
243 progress.scan.wim_target_path = wim_target_path;
244 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
246 config->_prefix = fs_source_path;
247 config->_prefix_num_tchars = tstrlen(fs_source_path);
249 if (wim_target_path[0] == T('\0'))
250 add_flags |= WIMLIB_ADD_FLAG_ROOT;
251 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
253 ERROR("Failed to build dentry tree for \"%"TS"\"",
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 = set_dentry_name(branch,
263 path_basename(wim_target_path));
265 goto out_ntfs_umount;
267 ret = attach_branch(&imd->root_dentry, branch, wim_target_path);
269 goto out_ntfs_umount;
272 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
273 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
275 imd->ntfs_vol = ntfs_vol;
277 inode_table_prepare_inode_list(¶ms.inode_table, &imd->inode_list);
280 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
281 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
282 goto out_destroy_sd_set;
286 do_ntfs_umount(ntfs_vol);
288 free_dentry_tree(branch, wim->lookup_table);
290 destroy_sd_set(¶ms.sd_set, rollback_sd);
291 out_destroy_inode_table:
292 destroy_inode_table(¶ms.inode_table);
298 execute_delete_command(WIMStruct *wim,
299 const struct wimlib_update_command *delete_cmd)
302 const tchar *wim_path;
303 struct wim_dentry *tree;
306 wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
307 flags = delete_cmd->delete.delete_flags;
308 wim_path = delete_cmd->delete.wim_path;
310 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
312 tree = get_dentry(wim, wim_path);
314 /* Path to delete does not exist in the WIM. */
315 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
318 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
319 wim_path, wim->current_image);
320 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
324 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
325 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
326 "but a recursive delete was not requested",
327 wim_path, wim->current_image);
328 return WIMLIB_ERR_IS_DIRECTORY;
331 is_root = dentry_is_root(tree);
333 free_dentry_tree(tree, wim->lookup_table);
335 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
340 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
342 FREE(dentry->_full_path);
343 dentry->_full_path = NULL;
348 * Rename a file or directory in the WIM.
350 * This is also called from wimfs_rename() in the FUSE mount code.
353 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to)
355 struct wim_dentry *src;
356 struct wim_dentry *dst;
357 struct wim_dentry *parent_of_dst;
360 /* This rename() implementation currently only supports actual files
361 * (not alternate data streams) */
363 src = get_dentry(wim, from);
367 dst = get_dentry(wim, to);
370 /* Destination file exists */
372 if (src == dst) /* Same file */
375 if (!dentry_is_directory(src)) {
376 /* Cannot rename non-directory to directory. */
377 if (dentry_is_directory(dst))
380 /* Cannot rename directory to a non-directory or a non-empty
382 if (!dentry_is_directory(dst))
384 if (inode_has_children(dst->d_inode))
387 parent_of_dst = dst->parent;
389 /* Destination does not exist */
390 parent_of_dst = get_parent_dentry(wim, to);
394 if (!dentry_is_directory(parent_of_dst))
398 ret = set_dentry_name(src, path_basename(to));
403 free_dentry_tree(dst, wim->lookup_table);
406 dentry_add_child(parent_of_dst, src);
408 for_dentry_in_tree(src, free_dentry_full_path, NULL);
414 execute_rename_command(WIMStruct *wim,
415 const struct wimlib_update_command *rename_cmd)
419 wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
421 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
422 rename_cmd->rename.wim_target_path);
426 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
427 rename_cmd->rename.wim_source_path,
428 rename_cmd->rename.wim_target_path);
431 ret = WIMLIB_ERR_NOMEM;
434 ret = WIMLIB_ERR_NOTDIR;
437 ret = WIMLIB_ERR_NOTEMPTY;
440 ret = WIMLIB_ERR_IS_DIRECTORY;
444 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
451 static inline const tchar *
452 update_op_to_str(int op)
455 case WIMLIB_UPDATE_OP_ADD:
457 case WIMLIB_UPDATE_OP_DELETE:
459 case WIMLIB_UPDATE_OP_RENAME:
468 execute_update_commands(WIMStruct *wim,
469 const struct wimlib_update_command *cmds,
471 wimlib_progress_func_t progress_func)
474 for (size_t i = 0; i < num_cmds; i++) {
475 DEBUG("Executing update command %zu of %zu (op=%"TS")",
476 i + 1, num_cmds, update_op_to_str(cmds[i].op));
477 switch (cmds[i].op) {
478 case WIMLIB_UPDATE_OP_ADD:
479 ret = execute_add_command(wim, &cmds[i], progress_func);
481 case WIMLIB_UPDATE_OP_DELETE:
482 ret = execute_delete_command(wim, &cmds[i]);
484 case WIMLIB_UPDATE_OP_RENAME:
485 ret = execute_rename_command(wim, &cmds[i]);
497 check_add_command(struct wimlib_update_command *cmd,
498 const struct wim_header *hdr)
500 int add_flags = cmd->add.add_flags;
502 /* Are we adding the entire image or not? An empty wim_target_path
503 * indicates that the tree we're adding is to be placed in the root of
504 * the image. We consider this to be capturing the entire image,
505 * although it could potentially be an overlay on an existing root as
507 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
510 /* Check for flags not supported on Windows */
511 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
512 ERROR("wimlib was compiled without support for NTFS-3g, so");
513 ERROR("we cannot capture a WIM image directly from a NTFS volume");
514 return WIMLIB_ERR_UNSUPPORTED;
516 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
517 ERROR("Capturing UNIX-specific data is not supported on Windows");
518 return WIMLIB_ERR_UNSUPPORTED;
520 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
521 ERROR("Dereferencing symbolic links is not supported on Windows");
522 return WIMLIB_ERR_UNSUPPORTED;
526 /* VERBOSE implies EXCLUDE_VERBOSE */
527 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
528 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
530 /* Check for contradictory reparse point fixup flags */
531 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
532 WIMLIB_ADD_FLAG_NORPFIX)) ==
533 (WIMLIB_ADD_FLAG_RPFIX |
534 WIMLIB_ADD_FLAG_NORPFIX))
536 ERROR("Cannot specify RPFIX and NORPFIX flags "
537 "at the same time!");
538 return WIMLIB_ERR_INVALID_PARAM;
541 /* Set default behavior on reparse point fixups if requested */
542 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
543 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
545 /* Do reparse-point fixups by default if we are capturing an
546 * entire image and either the header flag is set from previous
547 * images, or if this is the first image being added. */
548 if (is_entire_image &&
549 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
550 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
553 if (!is_entire_image) {
554 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
555 ERROR("Cannot add directly from a NTFS volume "
556 "when not capturing a full image!");
557 return WIMLIB_ERR_INVALID_PARAM;
560 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
561 ERROR("Cannot do reparse point fixups when "
562 "not capturing a full image!");
563 return WIMLIB_ERR_INVALID_PARAM;
566 /* We may have modified the add flags. */
567 cmd->add.add_flags = add_flags;
572 check_update_command(struct wimlib_update_command *cmd,
573 const struct wim_header *hdr)
576 case WIMLIB_UPDATE_OP_ADD:
577 return check_add_command(cmd, hdr);
578 case WIMLIB_UPDATE_OP_DELETE:
579 case WIMLIB_UPDATE_OP_RENAME:
586 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
587 const struct wim_header *hdr)
590 for (size_t i = 0; i < num_cmds; i++) {
591 ret = check_update_command(&cmds[i], hdr);
600 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
603 for (size_t i = 0; i < num_cmds; i++) {
604 switch (cmds[i].op) {
605 case WIMLIB_UPDATE_OP_ADD:
606 FREE(cmds[i].add.fs_source_path);
607 FREE(cmds[i].add.wim_target_path);
608 free_capture_config(cmds[i].add.config);
610 case WIMLIB_UPDATE_OP_DELETE:
611 FREE(cmds[i].delete.wim_path);
613 case WIMLIB_UPDATE_OP_RENAME:
614 FREE(cmds[i].rename.wim_source_path);
615 FREE(cmds[i].rename.wim_target_path);
624 copy_update_commands(const struct wimlib_update_command *cmds,
626 struct wimlib_update_command **cmds_copy_ret)
629 struct wimlib_update_command *cmds_copy;
631 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
635 for (size_t i = 0; i < num_cmds; i++) {
636 cmds_copy[i].op = cmds[i].op;
637 switch (cmds[i].op) {
638 case WIMLIB_UPDATE_OP_ADD:
639 cmds_copy[i].add.fs_source_path =
640 canonicalize_fs_path(cmds[i].add.fs_source_path);
641 cmds_copy[i].add.wim_target_path =
642 canonicalize_wim_path(cmds[i].add.wim_target_path);
643 if (!cmds_copy[i].add.fs_source_path ||
644 !cmds_copy[i].add.wim_target_path)
646 if (cmds[i].add.config) {
647 ret = copy_and_canonicalize_capture_config(cmds[i].add.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);
688 * Entry point for making a series of updates to a WIM image.
691 wimlib_update_image(WIMStruct *wim,
693 const struct wimlib_update_command *cmds,
696 wimlib_progress_func_t progress_func)
699 struct wimlib_update_command *cmds_copy;
701 DEBUG("Updating image %d with %zu commands", image, num_cmds);
703 /* Refuse to update a split WIM. */
704 if (wim->hdr.total_parts != 1) {
705 ERROR("Cannot update a split WIM!");
706 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
710 /* Load the metadata for the image to modify (if not loaded already) */
711 ret = select_wim_image(wim, image);
715 /* Short circuit a successful return if no commands were specified.
716 * Avoids problems with trying to allocate 0 bytes of memory. */
720 DEBUG("Preparing %zu update commands", num_cmds);
722 /* Make a copy of the update commands, in the process doing certain
723 * canonicalizations on paths (e.g. translating backslashes to forward
724 * slashes). This is done to avoid modifying the caller's copy of the
726 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
730 /* Perform additional checks on the update commands before we execute
732 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
734 goto out_free_cmds_copy;
736 /* Actually execute the update commands. */
737 DEBUG("Executing %zu update commands", num_cmds);
738 ret = execute_update_commands(wim, cmds_copy, num_cmds, progress_func);
740 goto out_free_cmds_copy;
742 wim->image_metadata[image - 1]->modified = 1;
744 /* Statistics about the WIM image, such as the numbers of files and
745 * directories, may have changed. Call xml_update_image_info() to
746 * recalculate these statistics. */
747 xml_update_image_info(wim, image);
749 free_update_commands(cmds_copy, num_cmds);