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 */
152 entry = readdir(dir);
155 ret = WIMLIB_ERR_READ;
156 ERROR_WITH_ERRNO("Error reading the "
157 "directory `%s'", path);
162 if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0'
163 || (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
166 size_t name_len = strlen(entry->d_name);
168 path[path_len] = '/';
169 memcpy(&path[path_len + 1], entry->d_name, name_len + 1);
170 ret = unix_build_dentry_tree_recursive(&child,
172 path_len + 1 + name_len,
177 dentry_add_child(dir_dentry, child);
184 unix_capture_symlink(struct wim_dentry **root_p,
186 struct wim_inode *inode,
187 struct add_image_params *params)
189 char deref_name_buf[4096];
190 ssize_t deref_name_len;
193 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
194 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
196 /* The idea here is to call readlink() to get the UNIX target of
197 * the symbolic link, then turn the target into a reparse point
198 * data buffer that contains a relative or absolute symbolic
199 * link (NOT a junction point or *full* path symbolic link with
202 deref_name_len = readlink(path, deref_name_buf,
203 sizeof(deref_name_buf) - 1);
204 if (deref_name_len >= 0) {
205 char *dest = deref_name_buf;
207 dest[deref_name_len] = '\0';
208 DEBUG("Read symlink `%s'", dest);
210 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX) &&
213 dest = capture_fixup_absolute_symlink(dest,
214 params->capture_root_ino,
215 params->capture_root_dev);
217 WARNING("Ignoring out of tree absolute symlink "
219 " (Use --norpfix to capture "
220 "absolute symlinks as-is)",
221 path, deref_name_buf);
222 free_dentry(*root_p);
226 inode->i_not_rpfixed = 0;
228 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
230 /* Unfortunately, Windows seems to have the concept of
231 * "file" symbolic links as being different from
232 * "directory" symbolic links... so
233 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
234 * symbolic link if the *target* of the symbolic link is
237 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
238 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
241 ERROR_WITH_ERRNO("Failed to read target of "
242 "symbolic link `%s'", path);
243 ret = WIMLIB_ERR_READLINK;
249 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
252 struct add_image_params *params)
254 struct wim_dentry *root = NULL;
256 struct wim_inode *inode;
258 if (exclude_path(path, path_len, params->config, true)) {
259 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
260 && params->progress_func)
262 union wimlib_progress_info info;
263 info.scan.cur_path = path;
264 info.scan.excluded = true;
265 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
270 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
271 && params->progress_func)
273 union wimlib_progress_info info;
274 info.scan.cur_path = path;
275 info.scan.excluded = false;
276 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
280 int (*stat_fn)(const char *restrict, struct stat *restrict);
281 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) ||
282 (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT))
287 ret = (*stat_fn)(path, &stbuf);
289 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
292 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
293 && !S_ISLNK(stbuf.st_mode)) {
294 ERROR("`%s' is not a regular file, directory, or symbolic link.",
296 ret = WIMLIB_ERR_SPECIAL_FILE;
300 ret = inode_table_new_dentry(params->inode_table,
301 path_basename_with_len(path, path_len),
302 stbuf.st_ino, stbuf.st_dev, false, &root);
306 inode = root->d_inode;
308 if (inode->i_nlink > 1) /* Already captured this inode? */
311 #ifdef HAVE_STAT_NANOSECOND_PRECISION
312 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
313 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
314 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
316 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
317 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
318 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
320 inode->i_resolved = 1;
321 if (params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
322 ret = inode_set_unix_data(inode, stbuf.st_uid,
325 params->lookup_table,
326 UNIX_DATA_ALL | UNIX_DATA_CREATE);
330 params->add_image_flags &=
331 ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
332 if (S_ISREG(stbuf.st_mode))
333 ret = unix_capture_regular_file(path, stbuf.st_size,
334 inode, params->lookup_table);
335 else if (S_ISDIR(stbuf.st_mode))
336 ret = unix_capture_directory(root, path, path_len, params);
338 ret = unix_capture_symlink(&root, path, inode, params);
343 free_dentry_tree(root, params->lookup_table);
348 * unix_build_dentry_tree():
349 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
350 * version; no NTFS-specific data is captured).
352 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
353 * modified if successful. Set to NULL if the file or directory was
354 * excluded from capture.
356 * @root_disk_path: The path to the root of the directory tree on disk.
358 * @params: See doc for `struct add_image_params'.
360 * @return: 0 on success, nonzero on failure. It is a failure if any of
361 * the files cannot be `stat'ed, or if any of the needed
362 * directories cannot be opened or read. Failure to add the files
363 * to the WIM may still occur later when trying to actually read
364 * the on-disk files during a call to wimlib_write() or
365 * wimlib_overwrite().
368 unix_build_dentry_tree(struct wim_dentry **root_ret,
369 const char *root_disk_path,
370 struct add_image_params *params)
378 struct stat root_stbuf;
379 if (stat(root_disk_path, &root_stbuf)) {
380 ERROR_WITH_ERRNO("Failed to stat \"%s\"", root_disk_path);
381 return WIMLIB_ERR_STAT;
384 if ((params->add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
385 !S_ISDIR(root_stbuf.st_mode))
387 ERROR("Root of capture \"%s\" is not a directory",
389 return WIMLIB_ERR_NOTDIR;
391 params->capture_root_ino = root_stbuf.st_ino;
392 params->capture_root_dev = root_stbuf.st_dev;
395 path_bufsz = min(32790, PATH_MAX + 1);
396 path_len = strlen(root_disk_path);
398 if (path_len >= path_bufsz)
399 return WIMLIB_ERR_INVALID_PARAM;
401 path_buf = MALLOC(path_bufsz);
403 return WIMLIB_ERR_NOMEM;
404 memcpy(path_buf, root_disk_path, path_len + 1);
406 ret = unix_build_dentry_tree_recursive(root_ret, path_buf,
411 #endif /* !__WIN32__ */
414 match_pattern(const tchar *path,
415 const tchar *path_basename,
416 const struct wimlib_pattern_list *list)
418 for (size_t i = 0; i < list->num_pats; i++) {
420 const tchar *pat = list->pats[i];
423 if (*pat == T('/')) {
424 /* Absolute path from root of capture */
427 if (tstrchr(pat, T('/')))
428 /* Relative path from root of capture */
431 /* A file name pattern */
432 string = path_basename;
435 /* Warning: on Windows native builds, fnmatch() calls the
436 * replacement function in win32.c. */
437 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
443 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
447 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
454 /* Return true if the image capture configuration file indicates we should
455 * exclude the filename @path from capture.
457 * If @exclude_prefix is %true, the part of the path up and including the name
458 * of the directory being captured is not included in the path for matching
459 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
460 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
464 exclude_path(const tchar *path, size_t path_len,
465 const struct wimlib_capture_config *config, bool exclude_prefix)
467 const tchar *basename = path_basename_with_len(path, path_len);
468 if (exclude_prefix) {
469 wimlib_assert(path_len >= config->_prefix_num_tchars);
470 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
471 path[config->_prefix_num_tchars] == T('/'))
473 path += config->_prefix_num_tchars;
476 return match_pattern(path, basename, &config->exclusion_pats) &&
477 !match_pattern(path, basename, &config->exclusion_exception_pats);
481 /* Strip leading and trailing slashes from the target paths, and translate all
482 * backslashes in the source and target paths into forward slashes. */
484 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
487 while (num_sources--) {
488 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
489 sources->fs_source_path,
490 sources->wim_target_path);
492 /* The Windows API can handle forward slashes. Just get rid of
493 * backslashes to avoid confusing other parts of the library
495 sources->fs_source_path = canonicalize_fs_path(sources->fs_source_path);
496 sources->wim_target_path = canonicalize_wim_path(sources->wim_target_path);
497 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
503 capture_source_cmp(const void *p1, const void *p2)
505 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
506 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
509 /* Sorts the capture sources lexicographically by target path. This occurs
510 * after leading and trailing forward slashes are stripped.
512 * One purpose of this is to make sure that target paths that are inside other
513 * target paths are added after the containing target paths. */
515 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
517 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
521 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
524 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
525 if (num_sources != 1) {
526 ERROR("Must specify exactly 1 capture source "
527 "(the NTFS volume) in NTFS mode!");
528 return WIMLIB_ERR_INVALID_PARAM;
530 if (sources[0].wim_target_path[0] != T('\0')) {
531 ERROR("In NTFS capture mode the target path inside "
532 "the image must be the root directory!");
533 return WIMLIB_ERR_INVALID_PARAM;
535 } else if (num_sources != 0) {
536 /* This code is disabled because the current code
537 * unconditionally attempts to do overlays. So, duplicate
538 * target paths are OK. */
540 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
541 ERROR("Cannot specify root target when using multiple "
543 return WIMLIB_ERR_INVALID_PARAM;
545 for (size_t i = 0; i < num_sources - 1; i++) {
546 size_t len = strlen(sources[i].wim_target_path);
548 const char *target1 = sources[i].wim_target_path;
550 const char *target2 = sources[j].wim_target_path;
551 DEBUG("target1=%s, target2=%s",
553 if (strncmp(target1, target2, len) ||
556 if (target2[len] == '/') {
557 ERROR("Invalid target `%s': is a prefix of `%s'",
559 return WIMLIB_ERR_INVALID_PARAM;
561 if (target2[len] == '\0') {
562 ERROR("Invalid target `%s': is a duplicate of `%s'",
564 return WIMLIB_ERR_INVALID_PARAM;
566 } while (++j != num_sources);
574 /* Creates a new directory to place in the WIM image. This is to create parent
575 * directories that are not part of any target as needed. */
577 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
580 struct wim_dentry *dentry;
582 DEBUG("Creating filler directory \"%"TS"\"", name);
583 ret = new_dentry_with_inode(name, &dentry);
585 /* Leave the inode number as 0; this is allowed for non
586 * hard-linked files. */
587 dentry->d_inode->i_resolved = 1;
588 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
589 *dentry_ret = dentry;
594 /* Overlays @branch onto @target, both of which must be directories. */
596 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
598 struct rb_root *rb_root;
600 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
601 branch->file_name, target->file_name);
603 if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
604 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
605 "is not directory-on-directory!", branch->file_name);
606 return WIMLIB_ERR_INVALID_OVERLAY;
609 rb_root = &branch->d_inode->i_children;
610 while (rb_root->rb_node) { /* While @branch has children... */
611 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
612 struct wim_dentry *existing;
614 /* Move @child to the directory @target */
615 unlink_dentry(child);
616 existing = dentry_add_child(target, child);
618 /* File or directory with same name already exists */
621 ret = do_overlay(existing, child);
623 /* Overlay failed. Revert the change to avoid
624 * leaking the directory tree rooted at @child.
626 dentry_add_child(branch, child);
635 /* Attach or overlay a branch onto the WIM image.
638 * Pointer to the root of the WIM image, or pointer to NULL if it has not
643 * Path in the WIM image to add the branch, with leading and trailing
647 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
651 struct wim_dentry *dentry, *parent, *target;
654 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
655 branch->file_name, target_path);
657 if (*target_path == T('\0')) {
658 /* Target: root directory */
660 /* Overlay on existing root */
661 return do_overlay(*root_p, branch);
669 /* Adding a non-root branch. Create root if it hasn't been created
672 ret = new_filler_directory(T(""), root_p);
677 /* Walk the path to the branch, creating filler directories as needed.
680 while ((slash = tstrchr(target_path, T('/')))) {
682 dentry = get_dentry_child_with_name(parent, target_path);
684 ret = new_filler_directory(target_path, &dentry);
687 dentry_add_child(parent, dentry);
691 /* Skip over slashes. Note: this cannot overrun the length of
692 * the string because the last character cannot be a slash, as
693 * trailing slashes were tripped. */
696 } while (*target_path == T('/'));
699 /* If the target path already existed, overlay the branch onto it.
700 * Otherwise, set the branch as the target path. */
701 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
702 branch->file_name_nbytes);
704 return do_overlay(target, branch);
706 dentry_add_child(parent, branch);
712 canonicalize_pat(tchar **pat_p)
716 /* Turn all backslashes in the pattern into forward slashes. */
717 zap_backslashes(pat);
719 if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
720 /* Pattern begins with drive letter */
721 if (*(pat + 2) != T('/')) {
722 /* Something like c:file, which is actually a path
723 * relative to the current working directory on the c:
724 * drive. We require paths with drive letters to be
726 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
727 "must be absolute!", pat);
728 ERROR("Maybe try \"%"TC":/%"TS"\"?",
730 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
733 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
734 "being removed.", pat);
735 /* Strip the drive letter */
743 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
746 for (size_t i = 0; i < pat_list->num_pats; i++) {
747 ret = canonicalize_pat(&pat_list->pats[i]);
755 canonicalize_capture_config(struct wimlib_capture_config *config)
757 int ret = canonicalize_pat_list(&config->exclusion_pats);
760 return canonicalize_pat_list(&config->exclusion_exception_pats);
764 wimlib_add_image_multisource(WIMStruct *w,
765 struct wimlib_capture_source *sources,
768 struct wimlib_capture_config *config,
770 wimlib_progress_func_t progress_func)
772 int (*capture_tree)(struct wim_dentry **,
774 struct add_image_params *);
776 struct wim_dentry *root_dentry;
777 struct wim_dentry *branch;
778 struct wim_security_data *sd;
779 struct wim_image_metadata *imd;
780 struct wim_inode_table inode_table;
781 struct list_head unhashed_streams;
782 struct add_image_params params;
784 struct sd_set sd_set;
786 struct _ntfs_volume *ntfs_vol = NULL;
789 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
791 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
792 ERROR("Cannot dereference files when capturing directly from NTFS");
793 return WIMLIB_ERR_INVALID_PARAM;
795 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
796 ERROR("Capturing UNIX owner and mode not supported "
797 "when capturing directly from NTFS");
798 return WIMLIB_ERR_INVALID_PARAM;
800 capture_tree = build_dentry_tree_ntfs;
801 extra_arg = &ntfs_vol;
803 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
804 " cannot capture a WIM image directly from a NTFS volume!");
805 return WIMLIB_ERR_UNSUPPORTED;
809 capture_tree = win32_build_dentry_tree;
811 capture_tree = unix_build_dentry_tree;
817 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
818 ERROR("Capturing UNIX-specific data is not supported on Windows");
819 return WIMLIB_ERR_INVALID_PARAM;
821 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
822 ERROR("Dereferencing symbolic links is not supported on Windows");
823 return WIMLIB_ERR_INVALID_PARAM;
827 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
828 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
830 if ((add_image_flags & (WIMLIB_ADD_IMAGE_FLAG_RPFIX |
831 WIMLIB_ADD_IMAGE_FLAG_RPFIX)) ==
832 (WIMLIB_ADD_IMAGE_FLAG_RPFIX | WIMLIB_ADD_IMAGE_FLAG_NORPFIX))
834 ERROR("Cannot specify RPFIX and NORPFIX flags at the same time!");
835 return WIMLIB_ERR_INVALID_PARAM;
838 if ((add_image_flags & (WIMLIB_ADD_IMAGE_FLAG_RPFIX |
839 WIMLIB_ADD_IMAGE_FLAG_NORPFIX)) == 0)
841 /* Do reparse-point fixups by default if the header flag is set
842 * from previous images, or if this is the first image being
844 if ((w->hdr.flags & WIM_HDR_FLAG_RP_FIX) || w->hdr.image_count == 0)
845 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
848 if (!name || !*name) {
849 ERROR("Must specify a non-empty string for the image name");
850 return WIMLIB_ERR_INVALID_PARAM;
853 if (w->hdr.total_parts != 1) {
854 ERROR("Cannot add an image to a split WIM");
855 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
858 if (wimlib_image_name_in_use(w, name)) {
859 ERROR("There is already an image named \"%"TS"\" in the WIM!",
861 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
865 DEBUG("Capture config not provided; using empty config");
866 config = alloca(sizeof(*config));
867 memset(config, 0, sizeof(*config));
870 ret = canonicalize_capture_config(config);
874 ret = init_inode_table(&inode_table, 9001);
878 DEBUG("Allocating security data");
879 sd = CALLOC(1, sizeof(struct wim_security_data));
881 ret = WIMLIB_ERR_NOMEM;
882 goto out_destroy_inode_table;
884 sd->total_length = 8;
887 sd_set.rb_root.rb_node = NULL;
890 DEBUG("Using %zu capture sources", num_sources);
891 canonicalize_sources_and_targets(sources, num_sources);
892 sort_sources(sources, num_sources);
893 ret = check_sorted_sources(sources, num_sources, add_image_flags);
895 ret = WIMLIB_ERR_INVALID_PARAM;
896 goto out_free_security_data;
899 INIT_LIST_HEAD(&unhashed_streams);
900 w->lookup_table->unhashed_streams = &unhashed_streams;
903 params.lookup_table = w->lookup_table;
904 params.inode_table = &inode_table;
905 params.sd_set = &sd_set;
906 params.config = config;
907 params.add_image_flags = add_image_flags;
908 params.progress_func = progress_func;
909 params.extra_arg = extra_arg;
910 for (size_t i = 0; i < num_sources; i++) {
912 union wimlib_progress_info progress;
914 DEBUG("Building dentry tree for source %zu of %zu "
915 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
916 sources[i].fs_source_path,
917 sources[i].wim_target_path);
919 memset(&progress, 0, sizeof(progress));
920 progress.scan.source = sources[i].fs_source_path;
921 progress.scan.wim_target_path = sources[i].wim_target_path;
922 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
924 config->_prefix = sources[i].fs_source_path;
925 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
926 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
927 if (!*sources[i].wim_target_path)
928 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
929 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
932 ERROR("Failed to build dentry tree for `%"TS"'",
933 sources[i].fs_source_path);
934 goto out_free_dentry_tree;
937 /* Use the target name, not the source name, for
938 * the root of each branch from a capture
939 * source. (This will also set the root dentry
940 * of the entire image to be unnamed.) */
941 ret = set_dentry_name(branch,
942 path_basename(sources[i].wim_target_path));
944 goto out_free_branch;
946 ret = attach_branch(&root_dentry, branch,
947 sources[i].wim_target_path);
949 goto out_free_branch;
952 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
955 if (root_dentry == NULL) {
956 ret = new_filler_directory(T(""), &root_dentry);
958 goto out_free_dentry_tree;
961 ret = add_new_dentry_tree(w, root_dentry, sd);
966 do_ntfs_umount(ntfs_vol);
968 goto out_free_dentry_tree;
971 imd = w->image_metadata[w->hdr.image_count - 1];
972 list_transfer(&unhashed_streams, &imd->unhashed_streams);
975 imd->ntfs_vol = ntfs_vol;
978 DEBUG("Assigning hard link group IDs");
979 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
981 ret = xml_add_image(w, name);
985 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
986 wimlib_set_boot_idx(w, w->hdr.image_count);
988 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_RPFIX)
989 w->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
992 goto out_destroy_inode_table;
994 put_image_metadata(w->image_metadata[--w->hdr.image_count],
996 goto out_destroy_inode_table;
998 free_dentry_tree(branch, w->lookup_table);
999 out_free_dentry_tree:
1000 free_dentry_tree(root_dentry, w->lookup_table);
1001 out_free_security_data:
1002 free_security_data(sd);
1003 out_destroy_inode_table:
1004 destroy_inode_table(&inode_table);
1005 destroy_sd_set(&sd_set);
1011 wimlib_add_image(WIMStruct *w,
1012 const tchar *source,
1014 struct wimlib_capture_config *config,
1015 int add_image_flags,
1016 wimlib_progress_func_t progress_func)
1018 if (!source || !*source)
1019 return WIMLIB_ERR_INVALID_PARAM;
1021 tchar *fs_source_path = TSTRDUP(source);
1023 struct wimlib_capture_source capture_src = {
1024 .fs_source_path = fs_source_path,
1025 .wim_target_path = NULL,
1028 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1029 config, add_image_flags,
1031 FREE(fs_source_path);