2 * update_image.c - Update a WIM image.
6 * Copyright (C) 2013, 2014 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"
42 /* Overlays @branch onto @target, both of which must be directories. */
44 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
46 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
47 branch->file_name, target->file_name);
49 if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
50 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
51 "is not directory-on-directory!", branch->file_name);
52 return WIMLIB_ERR_INVALID_OVERLAY;
55 LIST_HEAD(moved_children);
56 while (dentry_has_children(branch)) {
57 struct wim_dentry *child = dentry_any_child(branch);
58 struct wim_dentry *existing;
60 /* Move @child to the directory @target */
62 existing = dentry_add_child(target, child);
64 /* File or directory with same name already exists */
67 ret = do_overlay(existing, child);
69 /* Overlay failed. Revert the changes. */
70 dentry_add_child(branch, child);
71 list_for_each_entry(child, &moved_children, tmp_list)
74 dentry_add_child(branch, child);
79 list_add(&child->tmp_list, &moved_children);
86 /* Attach or overlay a branch onto the WIM image.
89 * Pointer to the root of the WIM image, or pointer to NULL if it has not
94 * Path in the WIM image to add the branch, with leading and trailing
98 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
99 tchar *target_path, CASE_SENSITIVITY_TYPE case_type)
102 struct wim_dentry *dentry, *parent, *target;
105 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
106 branch->file_name, target_path);
108 if (*target_path == T('\0')) {
109 /* Target: root directory */
111 /* Overlay on existing root */
112 return do_overlay(*root_p, branch);
114 if (!dentry_is_directory(branch)) {
115 ERROR("Cannot set non-directory as root of WIM image");
116 return WIMLIB_ERR_NOTDIR;
124 /* Adding a non-root branch. Create root if it hasn't been created
127 ret = new_filler_directory(T(""), root_p);
132 /* Walk the path to the branch, creating filler directories as needed.
135 while ((slash = tstrchr(target_path, WIM_PATH_SEPARATOR))) {
137 dentry = get_dentry_child_with_name(parent, target_path,
140 ret = new_filler_directory(target_path, &dentry);
143 dentry_add_child(parent, dentry);
147 /* Skip over slashes. Note: this cannot overrun the length of
148 * the string because the last character cannot be a slash, as
149 * trailing slashes were tripped. */
152 } while (*target_path == WIM_PATH_SEPARATOR);
155 /* If the target path already existed, overlay the branch onto it.
156 * Otherwise, set the branch as the target path. */
157 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
158 branch->file_name_nbytes,
161 return do_overlay(target, branch);
163 dentry_add_child(parent, branch);
168 const tchar wincfg[] =
174 "/System Volume Information\n"
180 get_capture_config(const tchar *config_file, struct capture_config *config,
181 int add_flags, const tchar *fs_source_path)
184 tchar *tmp_config_file = NULL;
186 memset(config, 0, sizeof(*config));
188 /* For WIMBoot capture, check for default capture configuration file
189 * unless one was explicitly specified. */
190 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
192 /* XXX: Handle loading file correctly when in NTFS volume. */
194 const tchar *wimboot_cfgfile =
195 T("/Windows/System32/WimBootCompress.ini");
196 size_t len = tstrlen(fs_source_path) +
197 tstrlen(wimboot_cfgfile);
198 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
201 tsprintf(tmp_config_file, T("%"TS"%"TS),
202 fs_source_path, wimboot_cfgfile);
203 if (!tstat(tmp_config_file, &st)) {
204 config_file = tmp_config_file;
205 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
207 WARNING("\"%"TS"\" does not exist.\n"
208 " Using default capture configuration!",
213 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
215 /* Use Windows default. */
218 const size_t wincfg_len = ARRAY_LEN(wincfg) - 1;
221 return WIMLIB_ERR_INVALID_PARAM;
223 wincfg_copy = memdup(wincfg, wincfg_len * sizeof(wincfg[0]));
225 return WIMLIB_ERR_NOMEM;
227 ret = do_read_capture_config_file(T("wincfg"), wincfg_copy,
231 } else if (config_file) {
232 /* Use the specified configuration file. */
233 ret = do_read_capture_config_file(config_file, NULL, 0, config);
235 /* ... Or don't use any configuration file at all. No files
236 * will be excluded from capture, all files will be compressed,
240 FREE(tmp_config_file);
245 execute_add_command(WIMStruct *wim,
246 const struct wimlib_update_command *add_cmd,
247 wimlib_progress_func_t progress_func)
251 tchar *fs_source_path;
252 tchar *wim_target_path;
253 struct wim_image_metadata *imd;
254 struct list_head unhashed_streams;
255 struct add_image_params params;
256 int (*capture_tree)(struct wim_dentry **,
258 struct add_image_params *);
259 const tchar *config_file;
260 struct capture_config config;
262 struct _ntfs_volume *ntfs_vol = NULL;
265 struct wim_dentry *branch;
266 bool rollback_sd = true;
268 wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
270 add_flags = add_cmd->add.add_flags;
271 fs_source_path = add_cmd->add.fs_source_path;
272 wim_target_path = add_cmd->add.wim_target_path;
273 config_file = add_cmd->add.config_file;
275 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
276 fs_source_path, wim_target_path, add_flags);
278 memset(¶ms, 0, sizeof(params));
280 imd = wim->image_metadata[wim->current_image - 1];
282 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
284 capture_tree = build_dentry_tree_ntfs;
285 extra_arg = &ntfs_vol;
286 if (imd->ntfs_vol != NULL) {
287 ERROR("NTFS volume already set");
288 ret = WIMLIB_ERR_INVALID_PARAM;
292 ret = WIMLIB_ERR_INVALID_PARAM;
297 capture_tree = win32_build_dentry_tree;
299 capture_tree = unix_build_dentry_tree;
304 ret = get_capture_config(config_file, &config,
305 add_flags, fs_source_path);
309 ret = init_inode_table(¶ms.inode_table, 9001);
311 goto out_destroy_config;
313 ret = init_sd_set(¶ms.sd_set, imd->security_data);
315 goto out_destroy_inode_table;
317 INIT_LIST_HEAD(&unhashed_streams);
318 params.lookup_table = wim->lookup_table;
319 params.unhashed_streams = &unhashed_streams;
320 params.config = &config;
321 params.add_flags = add_flags;
322 params.extra_arg = extra_arg;
324 params.progress_func = progress_func;
325 params.progress.scan.source = fs_source_path;
326 params.progress.scan.wim_target_path = wim_target_path;
328 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, ¶ms.progress);
330 config.prefix = fs_source_path;
331 config.prefix_num_tchars = tstrlen(fs_source_path);
333 if (wim_target_path[0] == T('\0'))
334 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
335 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
337 goto out_destroy_sd_set;
340 /* Use the target name, not the source name, for
341 * the root of each branch from a capture
342 * source. (This will also set the root dentry
343 * of the entire image to be unnamed.) */
344 ret = dentry_set_name(branch,
345 path_basename(wim_target_path));
347 goto out_ntfs_umount;
349 ret = attach_branch(&imd->root_dentry, branch, wim_target_path,
350 WIMLIB_CASE_PLATFORM_DEFAULT);
352 goto out_ntfs_umount;
355 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, ¶ms.progress);
356 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
358 imd->ntfs_vol = ntfs_vol;
360 inode_table_prepare_inode_list(¶ms.inode_table, &imd->inode_list);
363 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
364 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
365 goto out_destroy_sd_set;
369 do_ntfs_umount(ntfs_vol);
371 free_dentry_tree(branch, wim->lookup_table);
373 destroy_sd_set(¶ms.sd_set, rollback_sd);
374 out_destroy_inode_table:
375 destroy_inode_table(¶ms.inode_table);
377 destroy_capture_config(&config);
383 execute_delete_command(WIMStruct *wim,
384 const struct wimlib_update_command *delete_cmd)
387 const tchar *wim_path;
388 struct wim_dentry *tree;
391 wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
392 flags = delete_cmd->delete_.delete_flags;
393 wim_path = delete_cmd->delete_.wim_path;
395 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
397 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
399 /* Path to delete does not exist in the WIM. */
400 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
403 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
404 wim_path, wim->current_image);
405 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
409 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
410 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
411 "but a recursive delete was not requested",
412 wim_path, wim->current_image);
413 return WIMLIB_ERR_IS_DIRECTORY;
416 is_root = dentry_is_root(tree);
418 free_dentry_tree(tree, wim->lookup_table);
420 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
425 execute_rename_command(WIMStruct *wim,
426 const struct wimlib_update_command *rename_cmd)
430 wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
432 ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
433 rename_cmd->rename.wim_target_path,
434 WIMLIB_CASE_PLATFORM_DEFAULT);
438 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
439 rename_cmd->rename.wim_source_path,
440 rename_cmd->rename.wim_target_path);
443 ret = WIMLIB_ERR_NOMEM;
446 ret = WIMLIB_ERR_NOTDIR;
449 ret = WIMLIB_ERR_NOTEMPTY;
452 ret = WIMLIB_ERR_IS_DIRECTORY;
456 ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
463 static inline const tchar *
464 update_op_to_str(int op)
467 case WIMLIB_UPDATE_OP_ADD:
469 case WIMLIB_UPDATE_OP_DELETE:
471 case WIMLIB_UPDATE_OP_RENAME:
480 execute_update_commands(WIMStruct *wim,
481 const struct wimlib_update_command *cmds,
484 wimlib_progress_func_t progress_func)
487 union wimlib_progress_info info;
488 info.update.completed_commands = 0;
489 info.update.total_commands = num_cmds;
490 for (size_t i = 0; i < num_cmds; i++) {
491 DEBUG("Executing update command %zu of %zu (op=%"TS")",
492 i + 1, num_cmds, update_op_to_str(cmds[i].op));
493 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
496 info.update.command = &cmds[i];
497 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
500 switch (cmds[i].op) {
501 case WIMLIB_UPDATE_OP_ADD:
502 ret = execute_add_command(wim, &cmds[i], progress_func);
504 case WIMLIB_UPDATE_OP_DELETE:
505 ret = execute_delete_command(wim, &cmds[i]);
507 case WIMLIB_UPDATE_OP_RENAME:
508 ret = execute_rename_command(wim, &cmds[i]);
515 info.update.completed_commands++;
516 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
519 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
528 check_add_command(struct wimlib_update_command *cmd,
529 const struct wim_header *hdr)
531 int add_flags = cmd->add.add_flags;
533 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
534 WIMLIB_ADD_FLAG_DEREFERENCE |
535 WIMLIB_ADD_FLAG_VERBOSE |
536 /* BOOT doesn't make sense for wimlib_update_image(). */
537 /*WIMLIB_ADD_FLAG_BOOT |*/
538 WIMLIB_ADD_FLAG_WIMBOOT |
539 WIMLIB_ADD_FLAG_UNIX_DATA |
540 WIMLIB_ADD_FLAG_NO_ACLS |
541 WIMLIB_ADD_FLAG_STRICT_ACLS |
542 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
543 WIMLIB_ADD_FLAG_RPFIX |
544 WIMLIB_ADD_FLAG_NORPFIX |
545 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
546 WIMLIB_ADD_FLAG_WINCONFIG))
547 return WIMLIB_ERR_INVALID_PARAM;
549 /* Are we adding the entire image or not? An empty wim_target_path
550 * indicates that the tree we're adding is to be placed in the root of
551 * the image. We consider this to be capturing the entire image,
552 * although it could potentially be an overlay on an existing root as
554 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
557 /* Check for flags not supported on Windows */
558 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
559 ERROR("wimlib was compiled without support for NTFS-3g, so");
560 ERROR("we cannot capture a WIM image directly from a NTFS volume");
561 return WIMLIB_ERR_UNSUPPORTED;
563 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
564 ERROR("Capturing UNIX-specific data is not supported on Windows");
565 return WIMLIB_ERR_UNSUPPORTED;
567 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
568 ERROR("Dereferencing symbolic links is not supported on Windows");
569 return WIMLIB_ERR_UNSUPPORTED;
573 /* VERBOSE implies EXCLUDE_VERBOSE */
574 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
575 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
577 /* Check for contradictory reparse point fixup flags */
578 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
579 WIMLIB_ADD_FLAG_NORPFIX)) ==
580 (WIMLIB_ADD_FLAG_RPFIX |
581 WIMLIB_ADD_FLAG_NORPFIX))
583 ERROR("Cannot specify RPFIX and NORPFIX flags "
584 "at the same time!");
585 return WIMLIB_ERR_INVALID_PARAM;
588 /* Set default behavior on reparse point fixups if requested */
589 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
590 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
592 /* Do reparse-point fixups by default if we are capturing an
593 * entire image and either the header flag is set from previous
594 * images, or if this is the first image being added. */
595 if (is_entire_image &&
596 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
597 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
600 if (!is_entire_image) {
601 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
602 ERROR("Cannot add directly from a NTFS volume "
603 "when not capturing a full image!");
604 return WIMLIB_ERR_INVALID_PARAM;
607 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
608 ERROR("Cannot do reparse point fixups when "
609 "not capturing a full image!");
610 return WIMLIB_ERR_INVALID_PARAM;
613 /* We may have modified the add flags. */
614 cmd->add.add_flags = add_flags;
619 check_delete_command(const struct wimlib_update_command *cmd)
621 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
622 WIMLIB_DELETE_FLAG_RECURSIVE))
623 return WIMLIB_ERR_INVALID_PARAM;
628 check_rename_command(const struct wimlib_update_command *cmd)
630 if (cmd->rename.rename_flags != 0)
631 return WIMLIB_ERR_INVALID_PARAM;
636 check_update_command(struct wimlib_update_command *cmd,
637 const struct wim_header *hdr)
640 case WIMLIB_UPDATE_OP_ADD:
641 return check_add_command(cmd, hdr);
642 case WIMLIB_UPDATE_OP_DELETE:
643 return check_delete_command(cmd);
644 case WIMLIB_UPDATE_OP_RENAME:
645 return check_rename_command(cmd);
651 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
652 const struct wim_header *hdr)
655 for (size_t i = 0; i < num_cmds; i++) {
656 ret = check_update_command(&cmds[i], hdr);
665 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
668 for (size_t i = 0; i < num_cmds; i++) {
669 switch (cmds[i].op) {
670 case WIMLIB_UPDATE_OP_ADD:
671 FREE(cmds[i].add.fs_source_path);
672 FREE(cmds[i].add.wim_target_path);
673 FREE(cmds[i].add.config_file);
675 case WIMLIB_UPDATE_OP_DELETE:
676 FREE(cmds[i].delete_.wim_path);
678 case WIMLIB_UPDATE_OP_RENAME:
679 FREE(cmds[i].rename.wim_source_path);
680 FREE(cmds[i].rename.wim_target_path);
689 copy_update_commands(const struct wimlib_update_command *cmds,
691 struct wimlib_update_command **cmds_copy_ret)
694 struct wimlib_update_command *cmds_copy;
696 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
700 for (size_t i = 0; i < num_cmds; i++) {
701 cmds_copy[i].op = cmds[i].op;
702 switch (cmds[i].op) {
703 case WIMLIB_UPDATE_OP_ADD:
704 cmds_copy[i].add.fs_source_path =
705 canonicalize_fs_path(cmds[i].add.fs_source_path);
706 cmds_copy[i].add.wim_target_path =
707 canonicalize_wim_path(cmds[i].add.wim_target_path);
708 if (!cmds_copy[i].add.fs_source_path ||
709 !cmds_copy[i].add.wim_target_path)
711 if (cmds[i].add.config_file) {
712 cmds_copy[i].add.config_file = TSTRDUP(cmds[i].add.config_file);
713 if (!cmds_copy[i].add.config_file)
716 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
718 case WIMLIB_UPDATE_OP_DELETE:
719 cmds_copy[i].delete_.wim_path =
720 canonicalize_wim_path(cmds[i].delete_.wim_path);
721 if (!cmds_copy[i].delete_.wim_path)
723 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
725 case WIMLIB_UPDATE_OP_RENAME:
726 cmds_copy[i].rename.wim_source_path =
727 canonicalize_wim_path(cmds[i].rename.wim_source_path);
728 cmds_copy[i].rename.wim_target_path =
729 canonicalize_wim_path(cmds[i].rename.wim_target_path);
730 if (!cmds_copy[i].rename.wim_source_path ||
731 !cmds_copy[i].rename.wim_target_path)
735 ERROR("Unknown update operation %u", cmds[i].op);
736 ret = WIMLIB_ERR_INVALID_PARAM;
740 *cmds_copy_ret = cmds_copy;
745 ret = WIMLIB_ERR_NOMEM;
747 free_update_commands(cmds_copy, num_cmds);
751 /* API function documented in wimlib.h */
753 wimlib_update_image(WIMStruct *wim,
755 const struct wimlib_update_command *cmds,
758 wimlib_progress_func_t progress_func)
761 struct wimlib_update_command *cmds_copy;
762 bool deletion_requested = false;
764 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
765 return WIMLIB_ERR_INVALID_PARAM;
767 DEBUG("Updating image %d with %zu commands", image, num_cmds);
769 for (size_t i = 0; i < num_cmds; i++)
770 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
771 deletion_requested = true;
773 if (deletion_requested)
774 ret = can_delete_from_wim(wim);
776 ret = can_modify_wim(wim);
781 /* Load the metadata for the image to modify (if not loaded already) */
782 ret = select_wim_image(wim, image);
786 /* Short circuit a successful return if no commands were specified.
787 * Avoids problems with trying to allocate 0 bytes of memory. */
791 DEBUG("Preparing %zu update commands", num_cmds);
793 /* Make a copy of the update commands, in the process doing certain
794 * canonicalizations on paths (e.g. translating backslashes to forward
795 * slashes). This is done to avoid modifying the caller's copy of the
797 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
801 /* Perform additional checks on the update commands before we execute
803 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
805 goto out_free_cmds_copy;
807 /* Actually execute the update commands. */
808 DEBUG("Executing %zu update commands", num_cmds);
809 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
812 goto out_free_cmds_copy;
814 wim->image_metadata[image - 1]->modified = 1;
816 /* Statistics about the WIM image, such as the numbers of files and
817 * directories, may have changed. Call xml_update_image_info() to
818 * recalculate these statistics. */
819 xml_update_image_info(wim, image);
821 free_update_commands(cmds_copy, num_cmds);