6 * Copyright (C) 2012, 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/.
30 # include <sys/stat.h>
32 # include "timestamp.h"
35 #include "wimlib_internal.h"
37 #include "lookup_table.h"
54 * Adds the dentry tree and security data for a new image to the image metadata
55 * array of the WIMStruct.
58 add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
59 struct wim_security_data *sd)
61 struct wim_image_metadata *new_imd;
62 struct wim_lookup_table_entry *metadata_lte;
65 metadata_lte = new_lookup_table_entry();
67 return WIMLIB_ERR_NOMEM;
69 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
70 metadata_lte->unhashed = 1;
72 new_imd = new_image_metadata();
74 free_lookup_table_entry(metadata_lte);
75 return WIMLIB_ERR_NOMEM;
78 new_imd->root_dentry = root_dentry;
79 new_imd->metadata_lte = metadata_lte;
80 new_imd->security_data = sd;
81 new_imd->modified = 1;
83 ret = append_image_metadata(w, new_imd);
85 put_image_metadata(new_imd, NULL);
93 unix_capture_regular_file(const char *path,
95 struct wim_inode *inode,
96 struct wim_lookup_table *lookup_table)
98 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
100 /* Empty files do not have to have a lookup table entry. */
102 struct wim_lookup_table_entry *lte;
105 file_on_disk = STRDUP(path);
107 return WIMLIB_ERR_NOMEM;
108 lte = new_lookup_table_entry();
111 return WIMLIB_ERR_NOMEM;
113 lte->file_on_disk = file_on_disk;
114 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
115 lte->resource_entry.original_size = size;
116 lookup_table_insert_unhashed(lookup_table, lte, inode, 0);
123 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
126 struct add_image_params *params);
129 unix_capture_directory(struct wim_dentry *dir_dentry,
132 struct add_image_params *params)
136 struct dirent *entry;
137 struct wim_dentry *child;
140 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
143 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
145 return WIMLIB_ERR_OPEN;
148 /* Recurse on directory contents */
151 entry = readdir(dir);
154 ret = WIMLIB_ERR_READ;
155 ERROR_WITH_ERRNO("Error reading the "
156 "directory `%s'", path);
161 if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0'
162 || (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
165 size_t name_len = strlen(entry->d_name);
167 path[path_len] = '/';
168 memcpy(&path[path_len + 1], entry->d_name, name_len + 1);
169 ret = unix_build_dentry_tree_recursive(&child,
171 path_len + 1 + name_len,
176 dentry_add_child(dir_dentry, child);
183 fixup_symlink(char *dest, ino_t capture_root_ino, dev_t capture_root_dev)
197 if (stat(dest, &stbuf)) {
198 WARNING_WITH_ERRNO("Failed to stat \"%s\": %m", dest);
200 /* Treat as a link pointing outside the capture root (it
201 * most likely is). */
206 if (stbuf.st_ino == capture_root_ino &&
207 stbuf.st_dev == capture_root_dev)
209 /* Link points inside capture root. Return abbreviated
213 while (p - 1 >= dest && *(p - 1) == '/')
219 /* Link points outside capture root. */
225 } while (*p != '/' && *p != '\0');
230 unix_capture_symlink(struct wim_dentry **root_p,
232 struct wim_inode *inode,
233 struct add_image_params *params)
235 char deref_name_buf[4096];
236 ssize_t deref_name_len;
239 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
240 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
242 /* The idea here is to call readlink() to get the UNIX target of
243 * the symbolic link, then turn the target into a reparse point
244 * data buffer that contains a relative or absolute symbolic
245 * link (NOT a junction point or *full* path symbolic link with
248 deref_name_len = readlink(path, deref_name_buf,
249 sizeof(deref_name_buf) - 1);
250 if (deref_name_len >= 0) {
251 char *dest = deref_name_buf;
253 dest[deref_name_len] = '\0';
254 DEBUG("Read symlink `%s'", dest);
256 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX) &&
259 dest = fixup_symlink(dest,
260 params->capture_root_ino,
261 params->capture_root_dev);
263 WARNING("Ignoring out of tree absolute symlink "
265 " (Use --norpfix to capture "
266 "absolute symlinks as-is)",
267 path, deref_name_buf);
268 free_dentry(*root_p);
273 ret = inode_set_symlink(inode, dest,
274 params->lookup_table, NULL);
276 /* Unfortunately, Windows seems to have the concept of
277 * "file" symbolic links as being different from
278 * "directory" symbolic links... so
279 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
280 * symbolic link if the *target* of the symbolic link is
283 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
284 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
287 ERROR_WITH_ERRNO("Failed to read target of "
288 "symbolic link `%s'", path);
289 ret = WIMLIB_ERR_READLINK;
295 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
298 struct add_image_params *params)
300 struct wim_dentry *root = NULL;
302 struct wim_inode *inode;
304 if (exclude_path(path, path_len, params->config, true)) {
305 if (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
306 ERROR("Cannot exclude the root directory from capture");
307 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
310 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
311 && params->progress_func)
313 union wimlib_progress_info info;
314 info.scan.cur_path = path;
315 info.scan.excluded = true;
316 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
321 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
322 && params->progress_func)
324 union wimlib_progress_info info;
325 info.scan.cur_path = path;
326 info.scan.excluded = false;
327 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
331 int (*stat_fn)(const char *restrict, struct stat *restrict);
332 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) ||
333 (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT))
338 ret = (*stat_fn)(path, &stbuf);
340 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
343 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
344 && !S_ISLNK(stbuf.st_mode)) {
345 ERROR("`%s' is not a regular file, directory, or symbolic link.",
347 ret = WIMLIB_ERR_SPECIAL_FILE;
351 ret = inode_table_new_dentry(params->inode_table,
352 path_basename_with_len(path, path_len),
359 inode = root->d_inode;
361 if (inode->i_nlink > 1) /* Already captured this inode? */
364 #ifdef HAVE_STAT_NANOSECOND_PRECISION
365 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
366 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
367 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
369 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
370 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
371 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
373 inode->i_resolved = 1;
374 if (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
375 ret = inode_set_unix_data(inode, stbuf.st_uid,
378 params->lookup_table,
379 UNIX_DATA_ALL | UNIX_DATA_CREATE);
383 params->add_image_flags &=
384 ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
385 if (S_ISREG(stbuf.st_mode))
386 ret = unix_capture_regular_file(path, stbuf.st_size,
387 inode, params->lookup_table);
388 else if (S_ISDIR(stbuf.st_mode))
389 ret = unix_capture_directory(root, path, path_len, params);
391 ret = unix_capture_symlink(&root, path, inode, params);
396 free_dentry_tree(root, params->lookup_table);
401 * unix_build_dentry_tree():
402 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
403 * version; no NTFS-specific data is captured).
405 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
406 * modified if successful. Set to NULL if the file or directory was
407 * excluded from capture.
409 * @root_disk_path: The path to the root of the directory tree on disk.
411 * @params: See doc for `struct add_image_params'.
413 * @return: 0 on success, nonzero on failure. It is a failure if any of
414 * the files cannot be `stat'ed, or if any of the needed
415 * directories cannot be opened or read. Failure to add the files
416 * to the WIM may still occur later when trying to actually read
417 * the on-disk files during a call to wimlib_write() or
418 * wimlib_overwrite().
421 unix_build_dentry_tree(struct wim_dentry **root_ret,
422 const char *root_disk_path,
423 struct add_image_params *params)
431 struct stat root_stbuf;
432 if (stat(root_disk_path, &root_stbuf)) {
433 ERROR_WITH_ERRNO("Failed to stat \"%s\"", root_disk_path);
434 return WIMLIB_ERR_STAT;
437 if (!S_ISDIR(root_stbuf.st_mode)) {
438 ERROR("Root of capture \"%s\" is not a directory",
440 return WIMLIB_ERR_NOTDIR;
442 params->capture_root_ino = root_stbuf.st_ino;
443 params->capture_root_dev = root_stbuf.st_dev;
446 path_bufsz = min(32790, PATH_MAX + 1);
447 path_len = strlen(root_disk_path);
449 if (path_len >= path_bufsz)
450 return WIMLIB_ERR_INVALID_PARAM;
452 path_buf = MALLOC(path_bufsz);
454 return WIMLIB_ERR_NOMEM;
455 memcpy(path_buf, root_disk_path, path_len + 1);
457 ret = unix_build_dentry_tree_recursive(root_ret, path_buf,
462 #endif /* !__WIN32__ */
465 match_pattern(const tchar *path,
466 const tchar *path_basename,
467 const struct wimlib_pattern_list *list)
469 for (size_t i = 0; i < list->num_pats; i++) {
471 const tchar *pat = list->pats[i];
474 if (*pat == T('/')) {
475 /* Absolute path from root of capture */
478 if (tstrchr(pat, T('/')))
479 /* Relative path from root of capture */
482 /* A file name pattern */
483 string = path_basename;
486 /* Warning: on Windows native builds, fnmatch() calls the
487 * replacement function in win32.c. */
488 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
494 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
498 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
505 /* Return true if the image capture configuration file indicates we should
506 * exclude the filename @path from capture.
508 * If @exclude_prefix is %true, the part of the path up and including the name
509 * of the directory being captured is not included in the path for matching
510 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
511 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
515 exclude_path(const tchar *path, size_t path_len,
516 const struct wimlib_capture_config *config, bool exclude_prefix)
518 const tchar *basename = path_basename_with_len(path, path_len);
519 if (exclude_prefix) {
520 wimlib_assert(path_len >= config->_prefix_num_tchars);
521 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
522 path[config->_prefix_num_tchars] == T('/'))
524 path += config->_prefix_num_tchars;
527 return match_pattern(path, basename, &config->exclusion_pats) &&
528 !match_pattern(path, basename, &config->exclusion_exception_pats);
532 /* Strip leading and trailing forward slashes from a string. Modifies it in
533 * place and returns the stripped string. */
535 canonicalize_target_path(tchar *target_path)
538 if (target_path == NULL)
541 if (*target_path == T('\0'))
543 else if (*target_path == T('/'))
549 p = tstrchr(target_path, T('\0')) - 1;
555 /* Strip leading and trailing slashes from the target paths, and translate all
556 * backslashes in the source and target paths into forward slashes. */
558 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
561 while (num_sources--) {
562 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
563 sources->fs_source_path,
564 sources->wim_target_path);
566 /* The Windows API can handle forward slashes. Just get rid of
567 * backslashes to avoid confusing other parts of the library
569 zap_backslashes(sources->fs_source_path);
570 if (sources->wim_target_path)
571 zap_backslashes(sources->wim_target_path);
573 sources->wim_target_path =
574 (tchar*)canonicalize_target_path(sources->wim_target_path);
575 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
581 capture_source_cmp(const void *p1, const void *p2)
583 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
584 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
587 /* Sorts the capture sources lexicographically by target path. This occurs
588 * after leading and trailing forward slashes are stripped.
590 * One purpose of this is to make sure that target paths that are inside other
591 * target paths are added after the containing target paths. */
593 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
595 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
599 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
602 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
603 if (num_sources != 1) {
604 ERROR("Must specify exactly 1 capture source "
605 "(the NTFS volume) in NTFS mode!");
606 return WIMLIB_ERR_INVALID_PARAM;
608 if (sources[0].wim_target_path[0] != T('\0')) {
609 ERROR("In NTFS capture mode the target path inside "
610 "the image must be the root directory!");
611 return WIMLIB_ERR_INVALID_PARAM;
613 } else if (num_sources != 0) {
614 /* This code is disabled because the current code
615 * unconditionally attempts to do overlays. So, duplicate
616 * target paths are OK. */
618 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
619 ERROR("Cannot specify root target when using multiple "
621 return WIMLIB_ERR_INVALID_PARAM;
623 for (size_t i = 0; i < num_sources - 1; i++) {
624 size_t len = strlen(sources[i].wim_target_path);
626 const char *target1 = sources[i].wim_target_path;
628 const char *target2 = sources[j].wim_target_path;
629 DEBUG("target1=%s, target2=%s",
631 if (strncmp(target1, target2, len) ||
634 if (target2[len] == '/') {
635 ERROR("Invalid target `%s': is a prefix of `%s'",
637 return WIMLIB_ERR_INVALID_PARAM;
639 if (target2[len] == '\0') {
640 ERROR("Invalid target `%s': is a duplicate of `%s'",
642 return WIMLIB_ERR_INVALID_PARAM;
644 } while (++j != num_sources);
652 /* Creates a new directory to place in the WIM image. This is to create parent
653 * directories that are not part of any target as needed. */
655 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
658 struct wim_dentry *dentry;
660 DEBUG("Creating filler directory \"%"TS"\"", name);
661 ret = new_dentry_with_inode(name, &dentry);
663 /* Leave the inode number as 0; this is allowed for non
664 * hard-linked files. */
665 dentry->d_inode->i_resolved = 1;
666 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
667 *dentry_ret = dentry;
672 /* Transfers the children of @branch to @target. It is an error if @target is
673 * not a directory or if both @branch and @target contain a child dentry with
676 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
678 struct rb_root *rb_root;
680 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
681 branch->file_name, target->file_name);
683 if (!dentry_is_directory(target)) {
684 ERROR("Cannot overlay directory \"%"WS"\" "
685 "over non-directory", branch->file_name);
686 return WIMLIB_ERR_INVALID_OVERLAY;
689 rb_root = &branch->d_inode->i_children;
690 while (rb_root->rb_node) { /* While @branch has children... */
691 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
692 /* Move @child to the directory @target */
693 unlink_dentry(child);
694 if (!dentry_add_child(target, child)) {
695 /* Revert the change to avoid leaking the directory tree
696 * rooted at @child */
697 dentry_add_child(branch, child);
698 ERROR("Overlay error: file \"%"WS"\" already exists "
699 "as a child of \"%"WS"\"",
700 child->file_name, target->file_name);
701 return WIMLIB_ERR_INVALID_OVERLAY;
709 /* Attach or overlay a branch onto the WIM image.
712 * Pointer to the root of the WIM image, or pointer to NULL if it has not
717 * Path in the WIM image to add the branch, with leading and trailing
721 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
725 struct wim_dentry *dentry, *parent, *target;
728 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
729 branch->file_name, target_path);
731 if (*target_path == T('\0')) {
732 /* Target: root directory */
734 /* Overlay on existing root */
735 return do_overlay(*root_p, branch);
743 /* Adding a non-root branch. Create root if it hasn't been created
746 ret = new_filler_directory(T(""), root_p);
751 /* Walk the path to the branch, creating filler directories as needed.
754 while ((slash = tstrchr(target_path, T('/')))) {
756 dentry = get_dentry_child_with_name(parent, target_path);
758 ret = new_filler_directory(target_path, &dentry);
761 dentry_add_child(parent, dentry);
765 /* Skip over slashes. Note: this cannot overrun the length of
766 * the string because the last character cannot be a slash, as
767 * trailing slashes were tripped. */
770 } while (*target_path == T('/'));
773 /* If the target path already existed, overlay the branch onto it.
774 * Otherwise, set the branch as the target path. */
775 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
776 branch->file_name_nbytes);
778 return do_overlay(target, branch);
780 dentry_add_child(parent, branch);
786 canonicalize_pat(tchar **pat_p)
790 /* Turn all backslashes in the pattern into forward slashes. */
791 zap_backslashes(pat);
793 if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
794 /* Pattern begins with drive letter */
795 if (*(pat + 2) != T('/')) {
796 /* Something like c:file, which is actually a path
797 * relative to the current working directory on the c:
798 * drive. We require paths with drive letters to be
800 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
801 "must be absolute!", pat);
802 ERROR("Maybe try \"%"TC":/%"TS"\"?",
804 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
807 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
808 "being removed.", pat);
809 /* Strip the drive letter */
817 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
820 for (size_t i = 0; i < pat_list->num_pats; i++) {
821 ret = canonicalize_pat(&pat_list->pats[i]);
829 canonicalize_capture_config(struct wimlib_capture_config *config)
831 int ret = canonicalize_pat_list(&config->exclusion_pats);
834 return canonicalize_pat_list(&config->exclusion_exception_pats);
838 wimlib_add_image_multisource(WIMStruct *w,
839 struct wimlib_capture_source *sources,
842 struct wimlib_capture_config *config,
844 wimlib_progress_func_t progress_func)
846 int (*capture_tree)(struct wim_dentry **,
848 struct add_image_params *);
850 struct wim_dentry *root_dentry;
851 struct wim_dentry *branch;
852 struct wim_security_data *sd;
853 struct wim_image_metadata *imd;
854 struct wim_inode_table inode_table;
855 struct list_head unhashed_streams;
856 struct add_image_params params;
858 struct sd_set sd_set;
860 struct _ntfs_volume *ntfs_vol = NULL;
863 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
865 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
866 ERROR("Cannot dereference files when capturing directly from NTFS");
867 return WIMLIB_ERR_INVALID_PARAM;
869 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
870 ERROR("Capturing UNIX owner and mode not supported "
871 "when capturing directly from NTFS");
872 return WIMLIB_ERR_INVALID_PARAM;
874 capture_tree = build_dentry_tree_ntfs;
875 extra_arg = &ntfs_vol;
877 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
878 " cannot capture a WIM image directly from a NTFS volume!");
879 return WIMLIB_ERR_UNSUPPORTED;
883 capture_tree = win32_build_dentry_tree;
885 capture_tree = unix_build_dentry_tree;
891 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
892 ERROR("Capturing UNIX-specific data is not supported on Windows");
893 return WIMLIB_ERR_INVALID_PARAM;
895 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
896 ERROR("Dereferencing symbolic links is not supported on Windows");
897 return WIMLIB_ERR_INVALID_PARAM;
901 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
902 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
904 if ((add_image_flags & (WIMLIB_ADD_IMAGE_FLAG_RPFIX |
905 WIMLIB_ADD_IMAGE_FLAG_RPFIX)) ==
906 (WIMLIB_ADD_IMAGE_FLAG_RPFIX | WIMLIB_ADD_IMAGE_FLAG_NORPFIX))
908 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
909 return WIMLIB_ERR_INVALID_PARAM;
912 if ((add_image_flags & (WIMLIB_ADD_IMAGE_FLAG_RPFIX |
913 WIMLIB_ADD_IMAGE_FLAG_NORPFIX)) == 0)
914 if (w->hdr.flags & WIM_HDR_FLAG_RP_FIX)
915 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
917 if (!name || !*name) {
918 ERROR("Must specify a non-empty string for the image name");
919 return WIMLIB_ERR_INVALID_PARAM;
922 if (w->hdr.total_parts != 1) {
923 ERROR("Cannot add an image to a split WIM");
924 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
927 if (wimlib_image_name_in_use(w, name)) {
928 ERROR("There is already an image named \"%"TS"\" in the WIM!",
930 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
934 DEBUG("Capture config not provided; using empty config");
935 config = alloca(sizeof(*config));
936 memset(config, 0, sizeof(*config));
939 ret = canonicalize_capture_config(config);
943 ret = init_inode_table(&inode_table, 9001);
947 DEBUG("Allocating security data");
948 sd = CALLOC(1, sizeof(struct wim_security_data));
950 ret = WIMLIB_ERR_NOMEM;
951 goto out_destroy_inode_table;
953 sd->total_length = 8;
956 sd_set.rb_root.rb_node = NULL;
959 DEBUG("Using %zu capture sources", num_sources);
960 canonicalize_sources_and_targets(sources, num_sources);
961 sort_sources(sources, num_sources);
962 ret = check_sorted_sources(sources, num_sources, add_image_flags);
964 ret = WIMLIB_ERR_INVALID_PARAM;
965 goto out_free_security_data;
968 INIT_LIST_HEAD(&unhashed_streams);
969 w->lookup_table->unhashed_streams = &unhashed_streams;
972 params.lookup_table = w->lookup_table;
973 params.inode_table = &inode_table;
974 params.sd_set = &sd_set;
975 params.config = config;
976 params.add_image_flags = add_image_flags;
977 params.progress_func = progress_func;
978 params.extra_arg = extra_arg;
979 for (size_t i = 0; i < num_sources; i++) {
981 union wimlib_progress_info progress;
983 DEBUG("Building dentry tree for source %zu of %zu "
984 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
985 sources[i].fs_source_path,
986 sources[i].wim_target_path);
988 memset(&progress, 0, sizeof(progress));
989 progress.scan.source = sources[i].fs_source_path;
990 progress.scan.wim_target_path = sources[i].wim_target_path;
991 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
993 config->_prefix = sources[i].fs_source_path;
994 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
995 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
996 if (!*sources[i].wim_target_path)
997 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
998 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
1001 ERROR("Failed to build dentry tree for `%"TS"'",
1002 sources[i].fs_source_path);
1003 goto out_free_dentry_tree;
1006 /* Use the target name, not the source name, for
1007 * the root of each branch from a capture
1008 * source. (This will also set the root dentry
1009 * of the entire image to be unnamed.) */
1010 ret = set_dentry_name(branch,
1011 path_basename(sources[i].wim_target_path));
1013 goto out_free_branch;
1015 ret = attach_branch(&root_dentry, branch,
1016 sources[i].wim_target_path);
1018 goto out_free_branch;
1021 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1024 if (root_dentry == NULL) {
1025 ret = new_filler_directory(T(""), &root_dentry);
1027 goto out_free_dentry_tree;
1030 ret = add_new_dentry_tree(w, root_dentry, sd);
1035 do_ntfs_umount(ntfs_vol);
1037 goto out_free_dentry_tree;
1040 imd = w->image_metadata[w->hdr.image_count - 1];
1041 list_transfer(&unhashed_streams, &imd->unhashed_streams);
1044 imd->ntfs_vol = ntfs_vol;
1047 DEBUG("Assigning hard link group IDs");
1048 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
1050 ret = xml_add_image(w, name);
1054 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1055 wimlib_set_boot_idx(w, w->hdr.image_count);
1057 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX)
1058 w->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
1061 goto out_destroy_inode_table;
1063 put_image_metadata(w->image_metadata[--w->hdr.image_count],
1065 goto out_destroy_inode_table;
1067 free_dentry_tree(branch, w->lookup_table);
1068 out_free_dentry_tree:
1069 free_dentry_tree(root_dentry, w->lookup_table);
1070 out_free_security_data:
1071 free_security_data(sd);
1072 out_destroy_inode_table:
1073 destroy_inode_table(&inode_table);
1074 destroy_sd_set(&sd_set);
1080 wimlib_add_image(WIMStruct *w,
1081 const tchar *source,
1083 struct wimlib_capture_config *config,
1084 int add_image_flags,
1085 wimlib_progress_func_t progress_func)
1087 if (!source || !*source)
1088 return WIMLIB_ERR_INVALID_PARAM;
1090 tchar *fs_source_path = TSTRDUP(source);
1092 struct wimlib_capture_source capture_src = {
1093 .fs_source_path = fs_source_path,
1094 .wim_target_path = NULL,
1097 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1098 config, add_image_flags,
1100 FREE(fs_source_path);