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"
30 /* Creates a new directory to place in the WIM image. This is to create parent
31 * directories that are not part of any target as needed. */
33 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
36 struct wim_dentry *dentry;
38 DEBUG("Creating filler directory \"%"TS"\"", name);
39 ret = new_dentry_with_inode(name, &dentry);
42 /* Leave the inode number as 0; this is allowed for non
43 * hard-linked files. */
44 dentry->d_inode->i_resolved = 1;
45 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
52 /* Overlays @branch onto @target, both of which must be directories. */
54 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
56 struct rb_root *rb_root;
58 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
59 branch->file_name, target->file_name);
61 if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
62 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
63 "is not directory-on-directory!", branch->file_name);
64 return WIMLIB_ERR_INVALID_OVERLAY;
67 rb_root = &branch->d_inode->i_children;
68 while (rb_root->rb_node) { /* While @branch has children... */
69 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
70 struct wim_dentry *existing;
72 /* Move @child to the directory @target */
74 existing = dentry_add_child(target, child);
76 /* File or directory with same name already exists */
79 ret = do_overlay(existing, child);
81 /* Overlay failed. Revert the change to avoid
82 * leaking the directory tree rooted at @child.
84 dentry_add_child(branch, child);
93 /* Attach or overlay a branch onto the WIM image.
96 * Pointer to the root of the WIM image, or pointer to NULL if it has not
101 * Path in the WIM image to add the branch, with leading and trailing
105 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
109 struct wim_dentry *dentry, *parent, *target;
112 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
113 branch->file_name, target_path);
115 if (*target_path == T('\0')) {
116 /* Target: root directory */
118 /* Overlay on existing root */
119 return do_overlay(*root_p, branch);
121 if (!dentry_is_directory(branch)) {
122 ERROR("Cannot set non-directory as root of WIM image");
123 return WIMLIB_ERR_NOTDIR;
131 /* Adding a non-root branch. Create root if it hasn't been created
134 ret = new_filler_directory(T(""), root_p);
139 /* Walk the path to the branch, creating filler directories as needed.
142 while ((slash = tstrchr(target_path, T('/')))) {
144 dentry = get_dentry_child_with_name(parent, target_path);
146 ret = new_filler_directory(target_path, &dentry);
149 dentry_add_child(parent, dentry);
153 /* Skip over slashes. Note: this cannot overrun the length of
154 * the string because the last character cannot be a slash, as
155 * trailing slashes were tripped. */
158 } while (*target_path == T('/'));
161 /* If the target path already existed, overlay the branch onto it.
162 * Otherwise, set the branch as the target path. */
163 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
164 branch->file_name_nbytes);
166 return do_overlay(target, branch);
168 dentry_add_child(parent, branch);
174 execute_add_command(WIMStruct *wim,
175 const struct wimlib_update_command *add_cmd,
176 wimlib_progress_func_t progress_func)
180 tchar *fs_source_path;
181 tchar *wim_target_path;
182 struct wim_inode_table inode_table;
183 struct sd_set sd_set;
184 struct wim_image_metadata *imd;
185 struct list_head unhashed_streams;
186 struct add_image_params params;
187 int (*capture_tree)(struct wim_dentry **,
189 struct add_image_params *);
190 union wimlib_progress_info progress;
191 struct wimlib_capture_config *config;
193 struct _ntfs_volume *ntfs_vol = NULL;
196 struct wim_dentry *branch;
197 bool rollback_sd = true;
199 wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
200 add_flags = add_cmd->add.add_flags;
201 fs_source_path = add_cmd->add.fs_source_path;
202 wim_target_path = add_cmd->add.wim_target_path;
203 config = add_cmd->add.config;
204 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
205 fs_source_path, wim_target_path, add_flags);
207 imd = wim->image_metadata[wim->current_image - 1];
209 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
211 capture_tree = build_dentry_tree_ntfs;
212 extra_arg = &ntfs_vol;
213 if (imd->ntfs_vol != NULL) {
214 ERROR("NTFS volume already set");
215 ret = WIMLIB_ERR_INVALID_PARAM;
219 ret = WIMLIB_ERR_INVALID_PARAM;
224 capture_tree = win32_build_dentry_tree;
226 capture_tree = unix_build_dentry_tree;
231 ret = init_inode_table(&inode_table, 9001);
235 ret = init_sd_set(&sd_set, imd->security_data);
237 goto out_destroy_inode_table;
239 INIT_LIST_HEAD(&unhashed_streams);
240 wim->lookup_table->unhashed_streams = &unhashed_streams;
241 params.lookup_table = wim->lookup_table;
242 params.inode_table = &inode_table;
243 params.sd_set = &sd_set;
244 params.config = config;
245 params.add_image_flags = add_flags;
246 params.progress_func = progress_func;
247 params.extra_arg = extra_arg;
250 memset(&progress, 0, sizeof(progress));
251 progress.scan.source = fs_source_path;
252 progress.scan.wim_target_path = wim_target_path;
253 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
255 config->_prefix = fs_source_path;
256 config->_prefix_num_tchars = tstrlen(fs_source_path);
258 if (wim_target_path[0] == T('\0'))
259 add_flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
260 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
262 ERROR("Failed to build dentry tree for \"%"TS"\"",
264 goto out_destroy_sd_set;
267 /* Use the target name, not the source name, for
268 * the root of each branch from a capture
269 * source. (This will also set the root dentry
270 * of the entire image to be unnamed.) */
271 ret = set_dentry_name(branch,
272 path_basename(wim_target_path));
274 goto out_ntfs_umount;
276 ret = attach_branch(&imd->root_dentry, branch, wim_target_path);
278 goto out_ntfs_umount;
281 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
282 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
284 imd->ntfs_vol = ntfs_vol;
286 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
289 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX)
290 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
291 goto out_destroy_sd_set;
295 do_ntfs_umount(ntfs_vol);
297 free_dentry_tree(branch, wim->lookup_table);
299 destroy_sd_set(&sd_set, rollback_sd);
300 out_destroy_inode_table:
301 destroy_inode_table(&inode_table);
307 execute_delete_command(WIMStruct *wim,
308 const struct wimlib_update_command *delete_cmd)
311 const tchar *wim_path;
312 struct wim_dentry *tree;
315 wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
316 flags = delete_cmd->delete.delete_flags;
317 wim_path = delete_cmd->delete.wim_path;
319 tree = get_dentry(wim, wim_path);
321 /* Path to delete does not exist in the WIM. */
322 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
325 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
326 wim_path, wim->current_image);
327 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
331 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
332 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
333 "but a recursive delete was not requested",
334 wim_path, wim->current_image);
335 return WIMLIB_ERR_IS_DIRECTORY;
338 is_root = dentry_is_root(tree);
340 free_dentry_tree(tree, wim->lookup_table);
342 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
347 * Rename a file or directory in the WIM.
349 * This is also called from wimfs_rename() in the FUSE mount code.
352 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to)
354 struct wim_dentry *src;
355 struct wim_dentry *dst;
356 struct wim_dentry *parent_of_dst;
359 /* This rename() implementation currently only supports actual files
360 * (not alternate data streams) */
362 src = get_dentry(wim, from);
366 dst = get_dentry(wim, to);
369 /* Destination file exists */
371 if (src == dst) /* Same file */
374 if (!dentry_is_directory(src)) {
375 /* Cannot rename non-directory to directory. */
376 if (dentry_is_directory(dst))
379 /* Cannot rename directory to a non-directory or a non-empty
381 if (!dentry_is_directory(dst))
383 if (inode_has_children(dst->d_inode))
386 parent_of_dst = dst->parent;
388 /* Destination does not exist */
389 parent_of_dst = get_parent_dentry(wim, to);
393 if (!dentry_is_directory(parent_of_dst))
397 ret = set_dentry_name(src, path_basename(to));
402 free_dentry_tree(dst, wim->lookup_table);
405 dentry_add_child(parent_of_dst, src);
411 execute_rename_command(WIMStruct *wim,
412 const struct wimlib_update_command *rename_cmd)
416 wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
418 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
419 rename_cmd->rename.wim_target_path);
423 ret = WIMLIB_ERR_NOMEM;
426 ret = WIMLIB_ERR_NOTDIR;
430 ret = WIMLIB_ERR_IS_DIRECTORY;
434 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
441 static inline const tchar *
442 update_op_to_str(int op)
445 case WIMLIB_UPDATE_OP_ADD:
447 case WIMLIB_UPDATE_OP_DELETE:
449 case WIMLIB_UPDATE_OP_RENAME:
457 execute_update_commands(WIMStruct *wim,
458 const struct wimlib_update_command *cmds,
460 wimlib_progress_func_t progress_func)
463 for (size_t i = 0; i < num_cmds; i++) {
464 DEBUG("Executing update command %zu of %zu (op=%"TS")",
465 i + 1, num_cmds, update_op_to_str(cmds[i].op));
466 switch (cmds[i].op) {
467 case WIMLIB_UPDATE_OP_ADD:
468 ret = execute_add_command(wim, &cmds[i], progress_func);
470 case WIMLIB_UPDATE_OP_DELETE:
471 ret = execute_delete_command(wim, &cmds[i]);
473 case WIMLIB_UPDATE_OP_RENAME:
474 ret = execute_rename_command(wim, &cmds[i]);
477 ret = WIMLIB_ERR_INVALID_PARAM;
487 check_add_command(struct wimlib_update_command *cmd,
488 const struct wim_header *hdr)
490 int add_flags = cmd->add.add_flags;
491 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
494 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
495 ERROR("wimlib was compiled without support for NTFS-3g, so");
496 ERROR("we cannot capture a WIM image directly from a NTFS volume");
497 return WIMLIB_ERR_UNSUPPORTED;
499 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
500 ERROR("Capturing UNIX-specific data is not supported on Windows");
501 return WIMLIB_ERR_INVALID_PARAM;
503 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
504 ERROR("Dereferencing symbolic links is not supported on Windows");
505 return WIMLIB_ERR_INVALID_PARAM;
509 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
510 add_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
512 if ((add_flags & (WIMLIB_ADD_IMAGE_FLAG_RPFIX |
513 WIMLIB_ADD_IMAGE_FLAG_NORPFIX)) ==
514 (WIMLIB_ADD_IMAGE_FLAG_RPFIX |
515 WIMLIB_ADD_IMAGE_FLAG_NORPFIX))
517 ERROR("Cannot specify RPFIX and NORPFIX flags "
518 "at the same time!");
519 return WIMLIB_ERR_INVALID_PARAM;
522 if ((add_flags & (WIMLIB_ADD_IMAGE_FLAG_RPFIX |
523 WIMLIB_ADD_IMAGE_FLAG_NORPFIX)) == 0)
525 /* Do reparse-point fixups by default if we are capturing an
526 * entire image and either the header flag is set from previous
527 * images, or if this is the first image being added. */
528 if (is_entire_image &&
529 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
530 add_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
533 if (!is_entire_image) {
534 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
535 ERROR("Cannot add directly from a NTFS volume "
536 "when not capturing a full image!");
537 return WIMLIB_ERR_INVALID_PARAM;
540 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX) {
541 ERROR("Cannot do reparse point fixups when "
542 "not capturing a full image!");
543 return WIMLIB_ERR_INVALID_PARAM;
546 ret = canonicalize_capture_config(cmd->add.config);
549 cmd->add.add_flags = add_flags;
554 check_update_command(struct wimlib_update_command *cmd,
555 const struct wim_header *hdr)
558 case WIMLIB_UPDATE_OP_ADD:
559 return check_add_command(cmd, hdr);
560 case WIMLIB_UPDATE_OP_DELETE:
561 case WIMLIB_UPDATE_OP_RENAME:
568 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
569 const struct wim_header *hdr)
572 for (size_t i = 0; i < num_cmds; i++) {
573 ret = check_update_command(&cmds[i], hdr);
582 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
585 for (size_t i = 0; i < num_cmds; i++) {
587 case WIMLIB_UPDATE_OP_ADD:
588 FREE(cmds[i].add.fs_source_path);
589 FREE(cmds[i].add.wim_target_path);
590 free_capture_config(cmds[i].add.config);
592 case WIMLIB_UPDATE_OP_DELETE:
593 FREE(cmds[i].delete.wim_path);
595 case WIMLIB_UPDATE_OP_RENAME:
596 FREE(cmds[i].rename.wim_source_path);
597 FREE(cmds[i].rename.wim_target_path);
606 copy_update_commands(const struct wimlib_update_command *cmds,
608 struct wimlib_update_command **cmds_copy_ret)
611 struct wimlib_update_command *cmds_copy;
613 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
617 for (size_t i = 0; i < num_cmds; i++) {
618 switch (cmds[i].op) {
619 case WIMLIB_UPDATE_OP_ADD:
620 cmds_copy[i].add.fs_source_path =
621 canonicalize_fs_path(cmds[i].add.fs_source_path);
622 cmds_copy[i].add.wim_target_path =
623 canonicalize_wim_path(cmds[i].add.wim_target_path);
624 if (!cmds_copy[i].add.fs_source_path ||
625 !cmds_copy[i].add.wim_target_path)
627 if (cmds[i].add.config) {
628 cmds_copy[i].add.config =
629 copy_capture_config(cmds[i].add.config);
630 if (!cmds_copy[i].add.config)
633 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
635 case WIMLIB_UPDATE_OP_DELETE:
636 cmds_copy[i].delete.wim_path =
637 canonicalize_wim_path(cmds[i].delete.wim_path);
638 if (!cmds_copy[i].delete.wim_path)
640 cmds_copy[i].delete.delete_flags = cmds[i].delete.delete_flags;
642 case WIMLIB_UPDATE_OP_RENAME:
643 cmds_copy[i].rename.wim_source_path =
644 canonicalize_wim_path(cmds[i].rename.wim_source_path);
645 cmds_copy[i].rename.wim_target_path =
646 canonicalize_wim_path(cmds[i].rename.wim_target_path);
647 if (!cmds_copy[i].rename.wim_source_path ||
648 !cmds_copy[i].rename.wim_target_path)
652 ERROR("Unknown update operation %u", cmds[i].op);
653 ret = WIMLIB_ERR_INVALID_PARAM;
657 *cmds_copy_ret = cmds_copy;
662 ret = WIMLIB_ERR_NOMEM;
664 free_update_commands(cmds_copy, num_cmds);
669 wimlib_update_image(WIMStruct *wim,
671 const struct wimlib_update_command *cmds,
674 wimlib_progress_func_t progress_func)
677 struct wimlib_update_command *cmds_copy;
679 DEBUG("Updating image %d with %zu commands", image, num_cmds);
681 if (wim->hdr.total_parts != 1) {
682 ERROR("Cannot update a split WIM!");
683 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
687 ret = select_wim_image(wim, image);
691 DEBUG("Selected image %d for update", image);
696 DEBUG("Preparing update commands");
698 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
702 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
704 goto out_free_cmds_copy;
706 DEBUG("Executing update commands");
708 ret = execute_update_commands(wim, cmds_copy, num_cmds, progress_func);
711 free_update_commands(cmds_copy, num_cmds);