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;
63 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->i_lte);
122 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
125 struct wim_lookup_table *lookup_table,
126 struct wim_inode_table *inode_table,
127 const struct wimlib_capture_config *config,
129 wimlib_progress_func_t progress_func);
132 unix_capture_directory(struct wim_dentry *dir_dentry,
135 struct wim_lookup_table *lookup_table,
136 struct wim_inode_table *inode_table,
137 const struct wimlib_capture_config *config,
139 wimlib_progress_func_t progress_func)
143 struct dirent entry, *result;
144 struct wim_dentry *child;
147 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
150 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
152 return WIMLIB_ERR_OPEN;
155 /* Recurse on directory contents */
158 ret = readdir_r(dir, &entry, &result);
160 ret = WIMLIB_ERR_READ;
161 ERROR_WITH_ERRNO("Error reading the "
168 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
169 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
172 size_t name_len = strlen(result->d_name);
174 path[path_len] = '/';
175 memcpy(&path[path_len + 1], result->d_name, name_len + 1);
176 ret = unix_build_dentry_tree_recursive(&child,
178 path_len + 1 + name_len,
187 dentry_add_child(dir_dentry, child);
194 unix_capture_symlink(const char *path,
195 struct wim_inode *inode,
196 struct wim_lookup_table *lookup_table)
198 char deref_name_buf[4096];
199 ssize_t deref_name_len;
202 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
203 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
205 /* The idea here is to call readlink() to get the UNIX target of
206 * the symbolic link, then turn the target into a reparse point
207 * data buffer that contains a relative or absolute symbolic
208 * link (NOT a junction point or *full* path symbolic link with
211 deref_name_len = readlink(path, deref_name_buf,
212 sizeof(deref_name_buf) - 1);
213 if (deref_name_len >= 0) {
214 deref_name_buf[deref_name_len] = '\0';
215 DEBUG("Read symlink `%s'", deref_name_buf);
216 ret = inode_set_symlink(inode, deref_name_buf,
219 /* Unfortunately, Windows seems to have the concept of
220 * "file" symbolic links as being different from
221 * "directory" symbolic links... so
222 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
223 * symbolic link if the *target* of the symbolic link is
226 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
227 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
230 ERROR_WITH_ERRNO("Failed to read target of "
231 "symbolic link `%s'", path);
232 ret = WIMLIB_ERR_READLINK;
238 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
241 struct wim_lookup_table *lookup_table,
242 struct wim_inode_table *inode_table,
243 const struct wimlib_capture_config *config,
245 wimlib_progress_func_t progress_func)
247 struct wim_dentry *root = NULL;
249 struct wim_inode *inode;
251 if (exclude_path(path, path_len, config, true)) {
252 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
253 ERROR("Cannot exclude the root directory from capture");
254 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
257 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
260 union wimlib_progress_info info;
261 info.scan.cur_path = path;
262 info.scan.excluded = true;
263 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
268 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
271 union wimlib_progress_info info;
272 info.scan.cur_path = path;
273 info.scan.excluded = false;
274 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
277 /* UNIX version of capturing a directory tree */
279 int (*stat_fn)(const char *restrict, struct stat *restrict);
280 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
285 ret = (*stat_fn)(path, &stbuf);
287 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
291 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
292 !S_ISDIR(stbuf.st_mode))
294 /* Do a dereference-stat in case the root is a symbolic link.
295 * This case is allowed, provided that the symbolic link points
297 ret = stat(path, &stbuf);
299 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
300 ret = WIMLIB_ERR_STAT;
303 if (!S_ISDIR(stbuf.st_mode)) {
304 ERROR("`%s' is not a directory", path);
305 ret = WIMLIB_ERR_NOTDIR;
309 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
310 && !S_ISLNK(stbuf.st_mode)) {
311 ERROR("`%s' is not a regular file, directory, or symbolic link.",
313 ret = WIMLIB_ERR_SPECIAL_FILE;
317 ret = inode_table_new_dentry(inode_table,
318 path_basename_with_len(path, path_len),
325 inode = root->d_inode;
327 if (inode->i_nlink > 1) /* Already captured this inode? */
330 #ifdef HAVE_STAT_NANOSECOND_PRECISION
331 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
332 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
333 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
335 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
336 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
337 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
339 inode->i_resolved = 1;
340 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
341 ret = inode_set_unix_data(inode, stbuf.st_uid,
345 UNIX_DATA_ALL | UNIX_DATA_CREATE);
349 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
350 if (S_ISREG(stbuf.st_mode))
351 ret = unix_capture_regular_file(path, stbuf.st_size,
352 inode, lookup_table);
353 else if (S_ISDIR(stbuf.st_mode))
354 ret = unix_capture_directory(root, path, path_len,
355 lookup_table, inode_table, config,
356 add_image_flags, progress_func);
358 ret = unix_capture_symlink(path, inode, lookup_table);
363 free_dentry_tree(root, lookup_table);
368 * unix_build_dentry_tree():
369 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
370 * version; no NTFS-specific data is captured).
372 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
373 * modified if successful. Set to NULL if the file or directory was
374 * excluded from capture.
376 * @root_disk_path: The path to the root of the directory tree on disk.
378 * @lookup_table: The lookup table for the WIM file. For each file added to the
379 * dentry tree being built, an entry is added to the lookup table,
380 * unless an identical stream is already in the lookup table.
381 * These lookup table entries that are added point to the path of
384 * @sd_set: Ignored. (Security data only captured in NTFS mode.)
387 * Configuration for files to be excluded from capture.
389 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
391 * @extra_arg: Ignored
393 * @return: 0 on success, nonzero on failure. It is a failure if any of
394 * the files cannot be `stat'ed, or if any of the needed
395 * directories cannot be opened or read. Failure to add the files
396 * to the WIM may still occur later when trying to actually read
397 * the on-disk files during a call to wimlib_write() or
398 * wimlib_overwrite().
401 unix_build_dentry_tree(struct wim_dentry **root_ret,
402 const char *root_disk_path,
403 struct wim_lookup_table *lookup_table,
404 struct wim_inode_table *inode_table,
405 struct sd_set *sd_set,
406 const struct wimlib_capture_config *config,
408 wimlib_progress_func_t progress_func,
416 path_bufsz = min(32790, PATH_MAX + 1);
417 path_len = strlen(root_disk_path);
419 if (path_len >= path_bufsz)
420 return WIMLIB_ERR_INVALID_PARAM;
422 path_buf = MALLOC(path_bufsz);
424 return WIMLIB_ERR_NOMEM;
425 memcpy(path_buf, root_disk_path, path_len + 1);
426 ret = unix_build_dentry_tree_recursive(root_ret,
437 #endif /* !__WIN32__ */
440 match_pattern(const tchar *path,
441 const tchar *path_basename,
442 const struct wimlib_pattern_list *list)
444 for (size_t i = 0; i < list->num_pats; i++) {
446 const tchar *pat = list->pats[i];
449 if (*pat == T('/')) {
450 /* Absolute path from root of capture */
453 if (tstrchr(pat, T('/')))
454 /* Relative path from root of capture */
457 /* A file name pattern */
458 string = path_basename;
461 /* Warning: on Windows native builds, fnmatch() calls the
462 * replacement function in win32.c. */
463 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
469 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
473 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
480 /* Return true if the image capture configuration file indicates we should
481 * exclude the filename @path from capture.
483 * If @exclude_prefix is %true, the part of the path up and including the name
484 * of the directory being captured is not included in the path for matching
485 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
486 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
490 exclude_path(const tchar *path, size_t path_len,
491 const struct wimlib_capture_config *config, bool exclude_prefix)
493 const tchar *basename = path_basename_with_len(path, path_len);
494 if (exclude_prefix) {
495 wimlib_assert(path_len >= config->_prefix_num_tchars);
496 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
497 path[config->_prefix_num_tchars] == T('/'))
499 path += config->_prefix_num_tchars;
502 return match_pattern(path, basename, &config->exclusion_pats) &&
503 !match_pattern(path, basename, &config->exclusion_exception_pats);
507 /* Strip leading and trailing forward slashes from a string. Modifies it in
508 * place and returns the stripped string. */
510 canonicalize_target_path(tchar *target_path)
513 if (target_path == NULL)
516 if (*target_path == T('\0'))
518 else if (*target_path == T('/'))
524 p = tstrchr(target_path, T('\0')) - 1;
530 /* Strip leading and trailing slashes from the target paths, and translate all
531 * backslashes in the source and target paths into forward slashes. */
533 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
536 while (num_sources--) {
537 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
538 sources->fs_source_path,
539 sources->wim_target_path);
541 /* The Windows API can handle forward slashes. Just get rid of
542 * backslashes to avoid confusing other parts of the library
544 zap_backslashes(sources->fs_source_path);
545 if (sources->wim_target_path)
546 zap_backslashes(sources->wim_target_path);
548 sources->wim_target_path =
549 (tchar*)canonicalize_target_path(sources->wim_target_path);
550 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
556 capture_source_cmp(const void *p1, const void *p2)
558 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
559 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
562 /* Sorts the capture sources lexicographically by target path. This occurs
563 * after leading and trailing forward slashes are stripped.
565 * One purpose of this is to make sure that target paths that are inside other
566 * target paths are added after the containing target paths. */
568 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
570 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
574 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
577 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
578 if (num_sources != 1) {
579 ERROR("Must specify exactly 1 capture source "
580 "(the NTFS volume) in NTFS mode!");
581 return WIMLIB_ERR_INVALID_PARAM;
583 if (sources[0].wim_target_path[0] != T('\0')) {
584 ERROR("In NTFS capture mode the target path inside "
585 "the image must be the root directory!");
586 return WIMLIB_ERR_INVALID_PARAM;
588 } else if (num_sources != 0) {
589 /* This code is disabled because the current code
590 * unconditionally attempts to do overlays. So, duplicate
591 * target paths are OK. */
593 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
594 ERROR("Cannot specify root target when using multiple "
596 return WIMLIB_ERR_INVALID_PARAM;
598 for (size_t i = 0; i < num_sources - 1; i++) {
599 size_t len = strlen(sources[i].wim_target_path);
601 const char *target1 = sources[i].wim_target_path;
603 const char *target2 = sources[j].wim_target_path;
604 DEBUG("target1=%s, target2=%s",
606 if (strncmp(target1, target2, len) ||
609 if (target2[len] == '/') {
610 ERROR("Invalid target `%s': is a prefix of `%s'",
612 return WIMLIB_ERR_INVALID_PARAM;
614 if (target2[len] == '\0') {
615 ERROR("Invalid target `%s': is a duplicate of `%s'",
617 return WIMLIB_ERR_INVALID_PARAM;
619 } while (++j != num_sources);
627 /* Creates a new directory to place in the WIM image. This is to create parent
628 * directories that are not part of any target as needed. */
630 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
633 struct wim_dentry *dentry;
635 DEBUG("Creating filler directory \"%"TS"\"", name);
636 ret = new_dentry_with_inode(name, &dentry);
638 /* Leave the inode number as 0; this is allowed for non
639 * hard-linked files. */
640 dentry->d_inode->i_resolved = 1;
641 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
642 *dentry_ret = dentry;
647 /* Transfers the children of @branch to @target. It is an error if @target is
648 * not a directory or if both @branch and @target contain a child dentry with
651 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
653 struct rb_root *rb_root;
655 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
656 branch->file_name, target->file_name);
658 if (!dentry_is_directory(target)) {
659 ERROR("Cannot overlay directory \"%"WS"\" "
660 "over non-directory", branch->file_name);
661 return WIMLIB_ERR_INVALID_OVERLAY;
664 rb_root = &branch->d_inode->i_children;
665 while (rb_root->rb_node) { /* While @branch has children... */
666 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
667 /* Move @child to the directory @target */
668 unlink_dentry(child);
669 if (!dentry_add_child(target, child)) {
670 /* Revert the change to avoid leaking the directory tree
671 * rooted at @child */
672 dentry_add_child(branch, child);
673 ERROR("Overlay error: file \"%"WS"\" already exists "
674 "as a child of \"%"WS"\"",
675 child->file_name, target->file_name);
676 return WIMLIB_ERR_INVALID_OVERLAY;
684 /* Attach or overlay a branch onto the WIM image.
687 * Pointer to the root of the WIM image, or pointer to NULL if it has not
692 * Path in the WIM image to add the branch, with leading and trailing
696 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
700 struct wim_dentry *dentry, *parent, *target;
703 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
704 branch->file_name, target_path);
706 if (*target_path == T('\0')) {
707 /* Target: root directory */
709 /* Overlay on existing root */
710 return do_overlay(*root_p, branch);
718 /* Adding a non-root branch. Create root if it hasn't been created
721 ret = new_filler_directory(T(""), root_p);
726 /* Walk the path to the branch, creating filler directories as needed.
729 while ((slash = tstrchr(target_path, T('/')))) {
731 dentry = get_dentry_child_with_name(parent, target_path);
733 ret = new_filler_directory(target_path, &dentry);
736 dentry_add_child(parent, dentry);
740 /* Skip over slashes. Note: this cannot overrun the length of
741 * the string because the last character cannot be a slash, as
742 * trailing slashes were tripped. */
745 } while (*target_path == T('/'));
748 /* If the target path already existed, overlay the branch onto it.
749 * Otherwise, set the branch as the target path. */
750 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
751 branch->file_name_nbytes);
753 return do_overlay(target, branch);
755 dentry_add_child(parent, branch);
761 canonicalize_pat(tchar **pat_p)
765 /* Turn all backslashes in the pattern into forward slashes. */
766 zap_backslashes(pat);
768 if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
769 /* Pattern begins with drive letter */
770 if (*(pat + 2) != T('/')) {
771 /* Something like c:file, which is actually a path
772 * relative to the current working directory on the c:
773 * drive. We require paths with drive letters to be
775 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
776 "must be absolute!", pat);
777 ERROR("Maybe try \"%"TC":/%"TS"\"?",
779 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
782 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
783 "being removed.", pat);
784 /* Strip the drive letter */
792 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
795 for (size_t i = 0; i < pat_list->num_pats; i++) {
796 ret = canonicalize_pat(&pat_list->pats[i]);
804 canonicalize_capture_config(struct wimlib_capture_config *config)
806 int ret = canonicalize_pat_list(&config->exclusion_pats);
809 return canonicalize_pat_list(&config->exclusion_exception_pats);
813 wimlib_add_image_multisource(WIMStruct *w,
814 struct wimlib_capture_source *sources,
817 struct wimlib_capture_config *config,
819 wimlib_progress_func_t progress_func)
821 int (*capture_tree)(struct wim_dentry **,
823 struct wim_lookup_table *,
824 struct wim_inode_table *,
826 const struct wimlib_capture_config *,
828 wimlib_progress_func_t,
831 struct wim_dentry *root_dentry;
832 struct wim_dentry *branch;
833 struct wim_security_data *sd;
834 struct wim_image_metadata *imd;
835 struct wim_inode_table inode_table;
836 struct list_head unhashed_streams;
838 struct sd_set sd_set;
840 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
842 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
843 ERROR("Cannot dereference files when capturing directly from NTFS");
844 return WIMLIB_ERR_INVALID_PARAM;
846 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
847 ERROR("Capturing UNIX owner and mode not supported "
848 "when capturing directly from NTFS");
849 return WIMLIB_ERR_INVALID_PARAM;
851 capture_tree = build_dentry_tree_ntfs;
852 extra_arg = &w->ntfs_vol;
854 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
855 " cannot capture a WIM image directly from a NTFS volume!");
856 return WIMLIB_ERR_UNSUPPORTED;
860 capture_tree = win32_build_dentry_tree;
862 capture_tree = unix_build_dentry_tree;
868 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
869 ERROR("Capturing UNIX-specific data is not supported on Windows");
870 return WIMLIB_ERR_INVALID_PARAM;
872 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
873 ERROR("Dereferencing symbolic links is not supported on Windows");
874 return WIMLIB_ERR_INVALID_PARAM;
878 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
879 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
881 if (!name || !*name) {
882 ERROR("Must specify a non-empty string for the image name");
883 return WIMLIB_ERR_INVALID_PARAM;
886 if (w->hdr.total_parts != 1) {
887 ERROR("Cannot add an image to a split WIM");
888 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
891 if (wimlib_image_name_in_use(w, name)) {
892 ERROR("There is already an image named \"%"TS"\" in the WIM!",
894 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
898 DEBUG("Capture config not provided; using empty config");
899 config = alloca(sizeof(*config));
900 memset(config, 0, sizeof(*config));
903 ret = canonicalize_capture_config(config);
907 ret = init_inode_table(&inode_table, 9001);
911 DEBUG("Allocating security data");
912 sd = CALLOC(1, sizeof(struct wim_security_data));
914 ret = WIMLIB_ERR_NOMEM;
915 goto out_destroy_inode_table;
917 sd->total_length = 8;
920 sd_set.rb_root.rb_node = NULL;
923 DEBUG("Using %zu capture sources", num_sources);
924 canonicalize_sources_and_targets(sources, num_sources);
925 sort_sources(sources, num_sources);
926 ret = check_sorted_sources(sources, num_sources, add_image_flags);
928 ret = WIMLIB_ERR_INVALID_PARAM;
929 goto out_free_security_data;
932 INIT_LIST_HEAD(&unhashed_streams);
933 w->lookup_table->unhashed_streams = &unhashed_streams;
935 for (size_t i = 0; i < num_sources; i++) {
937 union wimlib_progress_info progress;
939 DEBUG("Building dentry tree for source %zu of %zu "
940 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
941 sources[i].fs_source_path,
942 sources[i].wim_target_path);
944 memset(&progress, 0, sizeof(progress));
945 progress.scan.source = sources[i].fs_source_path;
946 progress.scan.wim_target_path = sources[i].wim_target_path;
947 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
949 config->_prefix = sources[i].fs_source_path;
950 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
951 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
952 if (!*sources[i].wim_target_path)
953 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
954 ret = (*capture_tree)(&branch,
955 sources[i].fs_source_path,
961 progress_func, extra_arg);
963 ERROR("Failed to build dentry tree for `%"TS"'",
964 sources[i].fs_source_path);
965 goto out_free_dentry_tree;
968 /* Use the target name, not the source name, for
969 * the root of each branch from a capture
970 * source. (This will also set the root dentry
971 * of the entire image to be unnamed.) */
972 ret = set_dentry_name(branch,
973 path_basename(sources[i].wim_target_path));
975 goto out_free_branch;
977 ret = attach_branch(&root_dentry, branch,
978 sources[i].wim_target_path);
980 goto out_free_branch;
983 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
986 if (root_dentry == NULL) {
987 ret = new_filler_directory(T(""), &root_dentry);
989 goto out_free_dentry_tree;
992 ret = add_new_dentry_tree(w, root_dentry, sd);
994 goto out_free_dentry_tree;
996 imd = w->image_metadata[w->hdr.image_count - 1];
997 INIT_LIST_HEAD(&imd->unhashed_streams);
998 list_splice(&unhashed_streams, &imd->unhashed_streams);
1000 DEBUG("Assigning hard link group IDs");
1001 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
1003 ret = xml_add_image(w, name);
1007 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1008 wimlib_set_boot_idx(w, w->hdr.image_count);
1011 goto out_destroy_inode_table;
1013 put_image_metadata(w->image_metadata[--w->hdr.image_count],
1015 goto out_destroy_inode_table;
1017 free_dentry_tree(branch, w->lookup_table);
1018 out_free_dentry_tree:
1019 free_dentry_tree(root_dentry, w->lookup_table);
1020 out_free_security_data:
1021 free_security_data(sd);
1022 out_destroy_inode_table:
1023 destroy_inode_table(&inode_table);
1024 destroy_sd_set(&sd_set);
1030 wimlib_add_image(WIMStruct *w,
1031 const tchar *source,
1033 struct wimlib_capture_config *config,
1034 int add_image_flags,
1035 wimlib_progress_func_t progress_func)
1037 if (!source || !*source)
1038 return WIMLIB_ERR_INVALID_PARAM;
1040 tchar *fs_source_path = TSTRDUP(source);
1042 struct wimlib_capture_source capture_src = {
1043 .fs_source_path = fs_source_path,
1044 .wim_target_path = NULL,
1047 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1048 config, add_image_flags,
1050 FREE(fs_source_path);