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 static const char wincfg[] =
173 "/System Volume Information\n"
178 get_capture_config(const tchar *config_file, struct capture_config *config,
179 int add_flags, const tchar *fs_source_path)
182 tchar *tmp_config_file = NULL;
184 memset(config, 0, sizeof(*config));
186 /* For WIMBoot capture, check for default capture configuration file
187 * unless one was explicitly specified. */
188 if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
190 /* XXX: Handle loading file correctly when in NTFS volume. */
192 const tchar *wimboot_cfgfile =
193 T("/Windows/System32/WimBootCompress.ini");
194 size_t len = tstrlen(fs_source_path) +
195 tstrlen(wimboot_cfgfile);
196 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
199 tsprintf(tmp_config_file, T("%"TS"%"TS),
200 fs_source_path, wimboot_cfgfile);
201 if (!tstat(tmp_config_file, &st)) {
202 config_file = tmp_config_file;
203 add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
205 WARNING("\"%"TS"\" does not exist.\n"
206 " Using default capture configuration!",
211 if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
212 /* Use Windows default. */
214 return WIMLIB_ERR_INVALID_PARAM;
215 ret = do_read_capture_config_file(T("wincfg"), wincfg,
216 sizeof(wincfg) - 1, config);
217 } else if (config_file) {
218 /* Use the specified configuration file. */
219 ret = do_read_capture_config_file(config_file, NULL, 0, config);
221 /* ... Or don't use any configuration file at all. No files
222 * will be excluded from capture, all files will be compressed,
226 FREE(tmp_config_file);
231 execute_add_command(WIMStruct *wim,
232 const struct wimlib_update_command *add_cmd,
233 wimlib_progress_func_t progress_func)
237 tchar *fs_source_path;
238 tchar *wim_target_path;
239 struct wim_image_metadata *imd;
240 struct list_head unhashed_streams;
241 struct add_image_params params;
242 int (*capture_tree)(struct wim_dentry **,
244 struct add_image_params *);
245 const tchar *config_file;
246 struct capture_config config;
248 struct _ntfs_volume *ntfs_vol = NULL;
251 struct wim_dentry *branch;
252 bool rollback_sd = true;
254 wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
256 add_flags = add_cmd->add.add_flags;
257 fs_source_path = add_cmd->add.fs_source_path;
258 wim_target_path = add_cmd->add.wim_target_path;
259 config_file = add_cmd->add.config_file;
261 DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
262 fs_source_path, wim_target_path, add_flags);
264 memset(¶ms, 0, sizeof(params));
266 imd = wim->image_metadata[wim->current_image - 1];
268 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
270 capture_tree = build_dentry_tree_ntfs;
271 extra_arg = &ntfs_vol;
272 if (imd->ntfs_vol != NULL) {
273 ERROR("NTFS volume already set");
274 ret = WIMLIB_ERR_INVALID_PARAM;
278 ret = WIMLIB_ERR_INVALID_PARAM;
283 capture_tree = win32_build_dentry_tree;
285 capture_tree = unix_build_dentry_tree;
290 ret = get_capture_config(config_file, &config,
291 add_flags, fs_source_path);
295 ret = init_inode_table(¶ms.inode_table, 9001);
297 goto out_destroy_config;
299 ret = init_sd_set(¶ms.sd_set, imd->security_data);
301 goto out_destroy_inode_table;
303 INIT_LIST_HEAD(&unhashed_streams);
304 params.lookup_table = wim->lookup_table;
305 params.unhashed_streams = &unhashed_streams;
306 params.config = &config;
307 params.add_flags = add_flags;
308 params.extra_arg = extra_arg;
310 params.progress_func = progress_func;
311 params.progress.scan.source = fs_source_path;
312 params.progress.scan.wim_target_path = wim_target_path;
314 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, ¶ms.progress);
316 config.prefix = fs_source_path;
317 config.prefix_num_tchars = tstrlen(fs_source_path);
319 if (wim_target_path[0] == T('\0'))
320 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
321 ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
323 goto out_destroy_sd_set;
326 /* Use the target name, not the source name, for
327 * the root of each branch from a capture
328 * source. (This will also set the root dentry
329 * of the entire image to be unnamed.) */
330 ret = dentry_set_name(branch,
331 path_basename(wim_target_path));
333 goto out_ntfs_umount;
335 ret = attach_branch(&imd->root_dentry, branch, wim_target_path,
336 WIMLIB_CASE_PLATFORM_DEFAULT);
338 goto out_ntfs_umount;
341 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, ¶ms.progress);
342 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
344 imd->ntfs_vol = ntfs_vol;
346 inode_table_prepare_inode_list(¶ms.inode_table, &imd->inode_list);
349 if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
350 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
351 goto out_destroy_sd_set;
355 do_ntfs_umount(ntfs_vol);
357 free_dentry_tree(branch, wim->lookup_table);
359 destroy_sd_set(¶ms.sd_set, rollback_sd);
360 out_destroy_inode_table:
361 destroy_inode_table(¶ms.inode_table);
363 destroy_capture_config(&config);
369 execute_delete_command(WIMStruct *wim,
370 const struct wimlib_update_command *delete_cmd)
373 const tchar *wim_path;
374 struct wim_dentry *tree;
377 wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
378 flags = delete_cmd->delete_.delete_flags;
379 wim_path = delete_cmd->delete_.wim_path;
381 DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
383 tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
385 /* Path to delete does not exist in the WIM. */
386 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
389 ERROR("Path \"%"TS"\" does not exist in WIM image %d",
390 wim_path, wim->current_image);
391 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
395 if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
396 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
397 "but a recursive delete was not requested",
398 wim_path, wim->current_image);
399 return WIMLIB_ERR_IS_DIRECTORY;
402 is_root = dentry_is_root(tree);
404 free_dentry_tree(tree, wim->lookup_table);
406 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
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,
420 WIMLIB_CASE_PLATFORM_DEFAULT);
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:
466 execute_update_commands(WIMStruct *wim,
467 const struct wimlib_update_command *cmds,
470 wimlib_progress_func_t progress_func)
473 union wimlib_progress_info info;
474 info.update.completed_commands = 0;
475 info.update.total_commands = num_cmds;
476 for (size_t i = 0; i < num_cmds; i++) {
477 DEBUG("Executing update command %zu of %zu (op=%"TS")",
478 i + 1, num_cmds, update_op_to_str(cmds[i].op));
479 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
482 info.update.command = &cmds[i];
483 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
486 switch (cmds[i].op) {
487 case WIMLIB_UPDATE_OP_ADD:
488 ret = execute_add_command(wim, &cmds[i], progress_func);
490 case WIMLIB_UPDATE_OP_DELETE:
491 ret = execute_delete_command(wim, &cmds[i]);
493 case WIMLIB_UPDATE_OP_RENAME:
494 ret = execute_rename_command(wim, &cmds[i]);
501 info.update.completed_commands++;
502 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
505 (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
514 check_add_command(struct wimlib_update_command *cmd,
515 const struct wim_header *hdr)
517 int add_flags = cmd->add.add_flags;
519 if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
520 WIMLIB_ADD_FLAG_DEREFERENCE |
521 WIMLIB_ADD_FLAG_VERBOSE |
522 /* BOOT doesn't make sense for wimlib_update_image(). */
523 /*WIMLIB_ADD_FLAG_BOOT |*/
524 WIMLIB_ADD_FLAG_WIMBOOT |
525 WIMLIB_ADD_FLAG_UNIX_DATA |
526 WIMLIB_ADD_FLAG_NO_ACLS |
527 WIMLIB_ADD_FLAG_STRICT_ACLS |
528 WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
529 WIMLIB_ADD_FLAG_RPFIX |
530 WIMLIB_ADD_FLAG_NORPFIX |
531 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
532 WIMLIB_ADD_FLAG_WINCONFIG))
533 return WIMLIB_ERR_INVALID_PARAM;
535 /* Are we adding the entire image or not? An empty wim_target_path
536 * indicates that the tree we're adding is to be placed in the root of
537 * the image. We consider this to be capturing the entire image,
538 * although it could potentially be an overlay on an existing root as
540 bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
543 /* Check for flags not supported on Windows */
544 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
545 ERROR("wimlib was compiled without support for NTFS-3g, so");
546 ERROR("we cannot capture a WIM image directly from a NTFS volume");
547 return WIMLIB_ERR_UNSUPPORTED;
549 if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
550 ERROR("Capturing UNIX-specific data is not supported on Windows");
551 return WIMLIB_ERR_UNSUPPORTED;
553 if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
554 ERROR("Dereferencing symbolic links is not supported on Windows");
555 return WIMLIB_ERR_UNSUPPORTED;
559 /* VERBOSE implies EXCLUDE_VERBOSE */
560 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
561 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
563 /* Check for contradictory reparse point fixup flags */
564 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
565 WIMLIB_ADD_FLAG_NORPFIX)) ==
566 (WIMLIB_ADD_FLAG_RPFIX |
567 WIMLIB_ADD_FLAG_NORPFIX))
569 ERROR("Cannot specify RPFIX and NORPFIX flags "
570 "at the same time!");
571 return WIMLIB_ERR_INVALID_PARAM;
574 /* Set default behavior on reparse point fixups if requested */
575 if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
576 WIMLIB_ADD_FLAG_NORPFIX)) == 0)
578 /* Do reparse-point fixups by default if we are capturing an
579 * entire image and either the header flag is set from previous
580 * images, or if this is the first image being added. */
581 if (is_entire_image &&
582 ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
583 add_flags |= WIMLIB_ADD_FLAG_RPFIX;
586 if (!is_entire_image) {
587 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
588 ERROR("Cannot add directly from a NTFS volume "
589 "when not capturing a full image!");
590 return WIMLIB_ERR_INVALID_PARAM;
593 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
594 ERROR("Cannot do reparse point fixups when "
595 "not capturing a full image!");
596 return WIMLIB_ERR_INVALID_PARAM;
599 /* We may have modified the add flags. */
600 cmd->add.add_flags = add_flags;
605 check_delete_command(const struct wimlib_update_command *cmd)
607 if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
608 WIMLIB_DELETE_FLAG_RECURSIVE))
609 return WIMLIB_ERR_INVALID_PARAM;
614 check_rename_command(const struct wimlib_update_command *cmd)
616 if (cmd->rename.rename_flags != 0)
617 return WIMLIB_ERR_INVALID_PARAM;
622 check_update_command(struct wimlib_update_command *cmd,
623 const struct wim_header *hdr)
626 case WIMLIB_UPDATE_OP_ADD:
627 return check_add_command(cmd, hdr);
628 case WIMLIB_UPDATE_OP_DELETE:
629 return check_delete_command(cmd);
630 case WIMLIB_UPDATE_OP_RENAME:
631 return check_rename_command(cmd);
637 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
638 const struct wim_header *hdr)
641 for (size_t i = 0; i < num_cmds; i++) {
642 ret = check_update_command(&cmds[i], hdr);
651 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
654 for (size_t i = 0; i < num_cmds; i++) {
655 switch (cmds[i].op) {
656 case WIMLIB_UPDATE_OP_ADD:
657 FREE(cmds[i].add.fs_source_path);
658 FREE(cmds[i].add.wim_target_path);
659 FREE(cmds[i].add.config_file);
661 case WIMLIB_UPDATE_OP_DELETE:
662 FREE(cmds[i].delete_.wim_path);
664 case WIMLIB_UPDATE_OP_RENAME:
665 FREE(cmds[i].rename.wim_source_path);
666 FREE(cmds[i].rename.wim_target_path);
675 copy_update_commands(const struct wimlib_update_command *cmds,
677 struct wimlib_update_command **cmds_copy_ret)
680 struct wimlib_update_command *cmds_copy;
682 cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
686 for (size_t i = 0; i < num_cmds; i++) {
687 cmds_copy[i].op = cmds[i].op;
688 switch (cmds[i].op) {
689 case WIMLIB_UPDATE_OP_ADD:
690 cmds_copy[i].add.fs_source_path =
691 canonicalize_fs_path(cmds[i].add.fs_source_path);
692 cmds_copy[i].add.wim_target_path =
693 canonicalize_wim_path(cmds[i].add.wim_target_path);
694 if (!cmds_copy[i].add.fs_source_path ||
695 !cmds_copy[i].add.wim_target_path)
697 if (cmds[i].add.config_file) {
698 cmds_copy[i].add.config_file = TSTRDUP(cmds[i].add.config_file);
699 if (!cmds_copy[i].add.config_file)
702 cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
704 case WIMLIB_UPDATE_OP_DELETE:
705 cmds_copy[i].delete_.wim_path =
706 canonicalize_wim_path(cmds[i].delete_.wim_path);
707 if (!cmds_copy[i].delete_.wim_path)
709 cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
711 case WIMLIB_UPDATE_OP_RENAME:
712 cmds_copy[i].rename.wim_source_path =
713 canonicalize_wim_path(cmds[i].rename.wim_source_path);
714 cmds_copy[i].rename.wim_target_path =
715 canonicalize_wim_path(cmds[i].rename.wim_target_path);
716 if (!cmds_copy[i].rename.wim_source_path ||
717 !cmds_copy[i].rename.wim_target_path)
721 ERROR("Unknown update operation %u", cmds[i].op);
722 ret = WIMLIB_ERR_INVALID_PARAM;
726 *cmds_copy_ret = cmds_copy;
731 ret = WIMLIB_ERR_NOMEM;
733 free_update_commands(cmds_copy, num_cmds);
737 /* API function documented in wimlib.h */
739 wimlib_update_image(WIMStruct *wim,
741 const struct wimlib_update_command *cmds,
744 wimlib_progress_func_t progress_func)
747 struct wimlib_update_command *cmds_copy;
748 bool deletion_requested = false;
750 if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
751 return WIMLIB_ERR_INVALID_PARAM;
753 DEBUG("Updating image %d with %zu commands", image, num_cmds);
755 for (size_t i = 0; i < num_cmds; i++)
756 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
757 deletion_requested = true;
759 if (deletion_requested)
760 ret = can_delete_from_wim(wim);
762 ret = can_modify_wim(wim);
767 /* Load the metadata for the image to modify (if not loaded already) */
768 ret = select_wim_image(wim, image);
772 /* Short circuit a successful return if no commands were specified.
773 * Avoids problems with trying to allocate 0 bytes of memory. */
777 DEBUG("Preparing %zu update commands", num_cmds);
779 /* Make a copy of the update commands, in the process doing certain
780 * canonicalizations on paths (e.g. translating backslashes to forward
781 * slashes). This is done to avoid modifying the caller's copy of the
783 ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
787 /* Perform additional checks on the update commands before we execute
789 ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
791 goto out_free_cmds_copy;
793 /* Actually execute the update commands. */
794 DEBUG("Executing %zu update commands", num_cmds);
795 ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
798 goto out_free_cmds_copy;
800 wim->image_metadata[image - 1]->modified = 1;
802 /* Statistics about the WIM image, such as the numbers of files and
803 * directories, may have changed. Call xml_update_image_info() to
804 * recalculate these statistics. */
805 xml_update_image_info(wim, image);
807 free_update_commands(cmds_copy, num_cmds);