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, 0);
123 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
126 struct wim_lookup_table *lookup_table,
127 struct wim_inode_table *inode_table,
128 const struct wimlib_capture_config *config,
130 wimlib_progress_func_t progress_func);
133 unix_capture_directory(struct wim_dentry *dir_dentry,
136 struct wim_lookup_table *lookup_table,
137 struct wim_inode_table *inode_table,
138 const struct wimlib_capture_config *config,
140 wimlib_progress_func_t progress_func)
144 struct dirent entry, *result;
145 struct wim_dentry *child;
148 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
151 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
153 return WIMLIB_ERR_OPEN;
156 /* Recurse on directory contents */
159 ret = readdir_r(dir, &entry, &result);
161 ret = WIMLIB_ERR_READ;
162 ERROR_WITH_ERRNO("Error reading the "
169 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
170 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
173 size_t name_len = strlen(result->d_name);
175 path[path_len] = '/';
176 memcpy(&path[path_len + 1], result->d_name, name_len + 1);
177 ret = unix_build_dentry_tree_recursive(&child,
179 path_len + 1 + name_len,
188 dentry_add_child(dir_dentry, child);
195 unix_capture_symlink(const char *path,
196 struct wim_inode *inode,
197 struct wim_lookup_table *lookup_table)
199 char deref_name_buf[4096];
200 ssize_t deref_name_len;
203 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
204 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
206 /* The idea here is to call readlink() to get the UNIX target of
207 * the symbolic link, then turn the target into a reparse point
208 * data buffer that contains a relative or absolute symbolic
209 * link (NOT a junction point or *full* path symbolic link with
212 deref_name_len = readlink(path, deref_name_buf,
213 sizeof(deref_name_buf) - 1);
214 if (deref_name_len >= 0) {
215 deref_name_buf[deref_name_len] = '\0';
216 DEBUG("Read symlink `%s'", deref_name_buf);
217 ret = inode_set_symlink(inode, deref_name_buf,
220 /* Unfortunately, Windows seems to have the concept of
221 * "file" symbolic links as being different from
222 * "directory" symbolic links... so
223 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
224 * symbolic link if the *target* of the symbolic link is
227 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
228 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
231 ERROR_WITH_ERRNO("Failed to read target of "
232 "symbolic link `%s'", path);
233 ret = WIMLIB_ERR_READLINK;
239 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
242 struct wim_lookup_table *lookup_table,
243 struct wim_inode_table *inode_table,
244 const struct wimlib_capture_config *config,
246 wimlib_progress_func_t progress_func)
248 struct wim_dentry *root = NULL;
250 struct wim_inode *inode;
252 if (exclude_path(path, path_len, config, true)) {
253 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
254 ERROR("Cannot exclude the root directory from capture");
255 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
258 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
261 union wimlib_progress_info info;
262 info.scan.cur_path = path;
263 info.scan.excluded = true;
264 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
269 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
272 union wimlib_progress_info info;
273 info.scan.cur_path = path;
274 info.scan.excluded = false;
275 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
278 /* UNIX version of capturing a directory tree */
280 int (*stat_fn)(const char *restrict, struct stat *restrict);
281 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
286 ret = (*stat_fn)(path, &stbuf);
288 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
292 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
293 !S_ISDIR(stbuf.st_mode))
295 /* Do a dereference-stat in case the root is a symbolic link.
296 * This case is allowed, provided that the symbolic link points
298 ret = stat(path, &stbuf);
300 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
301 ret = WIMLIB_ERR_STAT;
304 if (!S_ISDIR(stbuf.st_mode)) {
305 ERROR("`%s' is not a directory", path);
306 ret = WIMLIB_ERR_NOTDIR;
310 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
311 && !S_ISLNK(stbuf.st_mode)) {
312 ERROR("`%s' is not a regular file, directory, or symbolic link.",
314 ret = WIMLIB_ERR_SPECIAL_FILE;
318 ret = inode_table_new_dentry(inode_table,
319 path_basename_with_len(path, path_len),
326 inode = root->d_inode;
328 if (inode->i_nlink > 1) /* Already captured this inode? */
331 #ifdef HAVE_STAT_NANOSECOND_PRECISION
332 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
333 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
334 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
336 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
337 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
338 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
340 inode->i_resolved = 1;
341 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
342 ret = inode_set_unix_data(inode, stbuf.st_uid,
346 UNIX_DATA_ALL | UNIX_DATA_CREATE);
350 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
351 if (S_ISREG(stbuf.st_mode))
352 ret = unix_capture_regular_file(path, stbuf.st_size,
353 inode, lookup_table);
354 else if (S_ISDIR(stbuf.st_mode))
355 ret = unix_capture_directory(root, path, path_len,
356 lookup_table, inode_table, config,
357 add_image_flags, progress_func);
359 ret = unix_capture_symlink(path, inode, lookup_table);
364 free_dentry_tree(root, lookup_table);
369 * unix_build_dentry_tree():
370 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
371 * version; no NTFS-specific data is captured).
373 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
374 * modified if successful. Set to NULL if the file or directory was
375 * excluded from capture.
377 * @root_disk_path: The path to the root of the directory tree on disk.
379 * @lookup_table: The lookup table for the WIM file. For each file added to the
380 * dentry tree being built, an entry is added to the lookup table,
381 * unless an identical stream is already in the lookup table.
382 * These lookup table entries that are added point to the path of
385 * @sd_set: Ignored. (Security data only captured in NTFS mode.)
388 * Configuration for files to be excluded from capture.
390 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
392 * @extra_arg: Ignored
394 * @return: 0 on success, nonzero on failure. It is a failure if any of
395 * the files cannot be `stat'ed, or if any of the needed
396 * directories cannot be opened or read. Failure to add the files
397 * to the WIM may still occur later when trying to actually read
398 * the on-disk files during a call to wimlib_write() or
399 * wimlib_overwrite().
402 unix_build_dentry_tree(struct wim_dentry **root_ret,
403 const char *root_disk_path,
404 struct wim_lookup_table *lookup_table,
405 struct wim_inode_table *inode_table,
406 struct sd_set *sd_set,
407 const struct wimlib_capture_config *config,
409 wimlib_progress_func_t progress_func,
417 path_bufsz = min(32790, PATH_MAX + 1);
418 path_len = strlen(root_disk_path);
420 if (path_len >= path_bufsz)
421 return WIMLIB_ERR_INVALID_PARAM;
423 path_buf = MALLOC(path_bufsz);
425 return WIMLIB_ERR_NOMEM;
426 memcpy(path_buf, root_disk_path, path_len + 1);
427 ret = unix_build_dentry_tree_recursive(root_ret,
438 #endif /* !__WIN32__ */
441 match_pattern(const tchar *path,
442 const tchar *path_basename,
443 const struct wimlib_pattern_list *list)
445 for (size_t i = 0; i < list->num_pats; i++) {
447 const tchar *pat = list->pats[i];
450 if (*pat == T('/')) {
451 /* Absolute path from root of capture */
454 if (tstrchr(pat, T('/')))
455 /* Relative path from root of capture */
458 /* A file name pattern */
459 string = path_basename;
462 /* Warning: on Windows native builds, fnmatch() calls the
463 * replacement function in win32.c. */
464 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
470 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
474 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
481 /* Return true if the image capture configuration file indicates we should
482 * exclude the filename @path from capture.
484 * If @exclude_prefix is %true, the part of the path up and including the name
485 * of the directory being captured is not included in the path for matching
486 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
487 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
491 exclude_path(const tchar *path, size_t path_len,
492 const struct wimlib_capture_config *config, bool exclude_prefix)
494 const tchar *basename = path_basename_with_len(path, path_len);
495 if (exclude_prefix) {
496 wimlib_assert(path_len >= config->_prefix_num_tchars);
497 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
498 path[config->_prefix_num_tchars] == T('/'))
500 path += config->_prefix_num_tchars;
503 return match_pattern(path, basename, &config->exclusion_pats) &&
504 !match_pattern(path, basename, &config->exclusion_exception_pats);
508 /* Strip leading and trailing forward slashes from a string. Modifies it in
509 * place and returns the stripped string. */
511 canonicalize_target_path(tchar *target_path)
514 if (target_path == NULL)
517 if (*target_path == T('\0'))
519 else if (*target_path == T('/'))
525 p = tstrchr(target_path, T('\0')) - 1;
531 /* Strip leading and trailing slashes from the target paths, and translate all
532 * backslashes in the source and target paths into forward slashes. */
534 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
537 while (num_sources--) {
538 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
539 sources->fs_source_path,
540 sources->wim_target_path);
542 /* The Windows API can handle forward slashes. Just get rid of
543 * backslashes to avoid confusing other parts of the library
545 zap_backslashes(sources->fs_source_path);
546 if (sources->wim_target_path)
547 zap_backslashes(sources->wim_target_path);
549 sources->wim_target_path =
550 (tchar*)canonicalize_target_path(sources->wim_target_path);
551 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
557 capture_source_cmp(const void *p1, const void *p2)
559 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
560 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
563 /* Sorts the capture sources lexicographically by target path. This occurs
564 * after leading and trailing forward slashes are stripped.
566 * One purpose of this is to make sure that target paths that are inside other
567 * target paths are added after the containing target paths. */
569 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
571 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
575 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
578 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
579 if (num_sources != 1) {
580 ERROR("Must specify exactly 1 capture source "
581 "(the NTFS volume) in NTFS mode!");
582 return WIMLIB_ERR_INVALID_PARAM;
584 if (sources[0].wim_target_path[0] != T('\0')) {
585 ERROR("In NTFS capture mode the target path inside "
586 "the image must be the root directory!");
587 return WIMLIB_ERR_INVALID_PARAM;
589 } else if (num_sources != 0) {
590 /* This code is disabled because the current code
591 * unconditionally attempts to do overlays. So, duplicate
592 * target paths are OK. */
594 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
595 ERROR("Cannot specify root target when using multiple "
597 return WIMLIB_ERR_INVALID_PARAM;
599 for (size_t i = 0; i < num_sources - 1; i++) {
600 size_t len = strlen(sources[i].wim_target_path);
602 const char *target1 = sources[i].wim_target_path;
604 const char *target2 = sources[j].wim_target_path;
605 DEBUG("target1=%s, target2=%s",
607 if (strncmp(target1, target2, len) ||
610 if (target2[len] == '/') {
611 ERROR("Invalid target `%s': is a prefix of `%s'",
613 return WIMLIB_ERR_INVALID_PARAM;
615 if (target2[len] == '\0') {
616 ERROR("Invalid target `%s': is a duplicate of `%s'",
618 return WIMLIB_ERR_INVALID_PARAM;
620 } while (++j != num_sources);
628 /* Creates a new directory to place in the WIM image. This is to create parent
629 * directories that are not part of any target as needed. */
631 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
634 struct wim_dentry *dentry;
636 DEBUG("Creating filler directory \"%"TS"\"", name);
637 ret = new_dentry_with_inode(name, &dentry);
639 /* Leave the inode number as 0; this is allowed for non
640 * hard-linked files. */
641 dentry->d_inode->i_resolved = 1;
642 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
643 *dentry_ret = dentry;
648 /* Transfers the children of @branch to @target. It is an error if @target is
649 * not a directory or if both @branch and @target contain a child dentry with
652 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
654 struct rb_root *rb_root;
656 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
657 branch->file_name, target->file_name);
659 if (!dentry_is_directory(target)) {
660 ERROR("Cannot overlay directory \"%"WS"\" "
661 "over non-directory", branch->file_name);
662 return WIMLIB_ERR_INVALID_OVERLAY;
665 rb_root = &branch->d_inode->i_children;
666 while (rb_root->rb_node) { /* While @branch has children... */
667 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
668 /* Move @child to the directory @target */
669 unlink_dentry(child);
670 if (!dentry_add_child(target, child)) {
671 /* Revert the change to avoid leaking the directory tree
672 * rooted at @child */
673 dentry_add_child(branch, child);
674 ERROR("Overlay error: file \"%"WS"\" already exists "
675 "as a child of \"%"WS"\"",
676 child->file_name, target->file_name);
677 return WIMLIB_ERR_INVALID_OVERLAY;
685 /* Attach or overlay a branch onto the WIM image.
688 * Pointer to the root of the WIM image, or pointer to NULL if it has not
693 * Path in the WIM image to add the branch, with leading and trailing
697 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
701 struct wim_dentry *dentry, *parent, *target;
704 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
705 branch->file_name, target_path);
707 if (*target_path == T('\0')) {
708 /* Target: root directory */
710 /* Overlay on existing root */
711 return do_overlay(*root_p, branch);
719 /* Adding a non-root branch. Create root if it hasn't been created
722 ret = new_filler_directory(T(""), root_p);
727 /* Walk the path to the branch, creating filler directories as needed.
730 while ((slash = tstrchr(target_path, T('/')))) {
732 dentry = get_dentry_child_with_name(parent, target_path);
734 ret = new_filler_directory(target_path, &dentry);
737 dentry_add_child(parent, dentry);
741 /* Skip over slashes. Note: this cannot overrun the length of
742 * the string because the last character cannot be a slash, as
743 * trailing slashes were tripped. */
746 } while (*target_path == T('/'));
749 /* If the target path already existed, overlay the branch onto it.
750 * Otherwise, set the branch as the target path. */
751 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
752 branch->file_name_nbytes);
754 return do_overlay(target, branch);
756 dentry_add_child(parent, branch);
762 canonicalize_pat(tchar **pat_p)
766 /* Turn all backslashes in the pattern into forward slashes. */
767 zap_backslashes(pat);
769 if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
770 /* Pattern begins with drive letter */
771 if (*(pat + 2) != T('/')) {
772 /* Something like c:file, which is actually a path
773 * relative to the current working directory on the c:
774 * drive. We require paths with drive letters to be
776 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
777 "must be absolute!", pat);
778 ERROR("Maybe try \"%"TC":/%"TS"\"?",
780 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
783 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
784 "being removed.", pat);
785 /* Strip the drive letter */
793 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
796 for (size_t i = 0; i < pat_list->num_pats; i++) {
797 ret = canonicalize_pat(&pat_list->pats[i]);
805 canonicalize_capture_config(struct wimlib_capture_config *config)
807 int ret = canonicalize_pat_list(&config->exclusion_pats);
810 return canonicalize_pat_list(&config->exclusion_exception_pats);
814 wimlib_add_image_multisource(WIMStruct *w,
815 struct wimlib_capture_source *sources,
818 struct wimlib_capture_config *config,
820 wimlib_progress_func_t progress_func)
822 int (*capture_tree)(struct wim_dentry **,
824 struct wim_lookup_table *,
825 struct wim_inode_table *,
827 const struct wimlib_capture_config *,
829 wimlib_progress_func_t,
832 struct wim_dentry *root_dentry;
833 struct wim_dentry *branch;
834 struct wim_security_data *sd;
835 struct wim_image_metadata *imd;
836 struct wim_inode_table inode_table;
837 struct list_head unhashed_streams;
839 struct sd_set sd_set;
841 struct _ntfs_volume *ntfs_vol = NULL;
844 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
846 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
847 ERROR("Cannot dereference files when capturing directly from NTFS");
848 return WIMLIB_ERR_INVALID_PARAM;
850 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
851 ERROR("Capturing UNIX owner and mode not supported "
852 "when capturing directly from NTFS");
853 return WIMLIB_ERR_INVALID_PARAM;
855 capture_tree = build_dentry_tree_ntfs;
856 extra_arg = &ntfs_vol;
858 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
859 " cannot capture a WIM image directly from a NTFS volume!");
860 return WIMLIB_ERR_UNSUPPORTED;
864 capture_tree = win32_build_dentry_tree;
866 capture_tree = unix_build_dentry_tree;
872 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
873 ERROR("Capturing UNIX-specific data is not supported on Windows");
874 return WIMLIB_ERR_INVALID_PARAM;
876 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
877 ERROR("Dereferencing symbolic links is not supported on Windows");
878 return WIMLIB_ERR_INVALID_PARAM;
882 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
883 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
885 if (!name || !*name) {
886 ERROR("Must specify a non-empty string for the image name");
887 return WIMLIB_ERR_INVALID_PARAM;
890 if (w->hdr.total_parts != 1) {
891 ERROR("Cannot add an image to a split WIM");
892 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
895 if (wimlib_image_name_in_use(w, name)) {
896 ERROR("There is already an image named \"%"TS"\" in the WIM!",
898 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
902 DEBUG("Capture config not provided; using empty config");
903 config = alloca(sizeof(*config));
904 memset(config, 0, sizeof(*config));
907 ret = canonicalize_capture_config(config);
911 ret = init_inode_table(&inode_table, 9001);
915 DEBUG("Allocating security data");
916 sd = CALLOC(1, sizeof(struct wim_security_data));
918 ret = WIMLIB_ERR_NOMEM;
919 goto out_destroy_inode_table;
921 sd->total_length = 8;
924 sd_set.rb_root.rb_node = NULL;
927 DEBUG("Using %zu capture sources", num_sources);
928 canonicalize_sources_and_targets(sources, num_sources);
929 sort_sources(sources, num_sources);
930 ret = check_sorted_sources(sources, num_sources, add_image_flags);
932 ret = WIMLIB_ERR_INVALID_PARAM;
933 goto out_free_security_data;
936 INIT_LIST_HEAD(&unhashed_streams);
937 w->lookup_table->unhashed_streams = &unhashed_streams;
939 for (size_t i = 0; i < num_sources; i++) {
941 union wimlib_progress_info progress;
943 DEBUG("Building dentry tree for source %zu of %zu "
944 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
945 sources[i].fs_source_path,
946 sources[i].wim_target_path);
948 memset(&progress, 0, sizeof(progress));
949 progress.scan.source = sources[i].fs_source_path;
950 progress.scan.wim_target_path = sources[i].wim_target_path;
951 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
953 config->_prefix = sources[i].fs_source_path;
954 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
955 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
956 if (!*sources[i].wim_target_path)
957 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
958 ret = (*capture_tree)(&branch,
959 sources[i].fs_source_path,
965 progress_func, extra_arg);
967 ERROR("Failed to build dentry tree for `%"TS"'",
968 sources[i].fs_source_path);
969 goto out_free_dentry_tree;
972 /* Use the target name, not the source name, for
973 * the root of each branch from a capture
974 * source. (This will also set the root dentry
975 * of the entire image to be unnamed.) */
976 ret = set_dentry_name(branch,
977 path_basename(sources[i].wim_target_path));
979 goto out_free_branch;
981 ret = attach_branch(&root_dentry, branch,
982 sources[i].wim_target_path);
984 goto out_free_branch;
987 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
990 if (root_dentry == NULL) {
991 ret = new_filler_directory(T(""), &root_dentry);
993 goto out_free_dentry_tree;
996 ret = add_new_dentry_tree(w, root_dentry, sd);
1001 do_ntfs_umount(ntfs_vol);
1003 goto out_free_dentry_tree;
1006 imd = w->image_metadata[w->hdr.image_count - 1];
1007 INIT_LIST_HEAD(&imd->unhashed_streams);
1008 list_splice(&unhashed_streams, &imd->unhashed_streams);
1011 imd->ntfs_vol = ntfs_vol;
1014 DEBUG("Assigning hard link group IDs");
1015 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
1017 ret = xml_add_image(w, name);
1021 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1022 wimlib_set_boot_idx(w, w->hdr.image_count);
1025 goto out_destroy_inode_table;
1027 put_image_metadata(w->image_metadata[--w->hdr.image_count],
1029 goto out_destroy_inode_table;
1031 free_dentry_tree(branch, w->lookup_table);
1032 out_free_dentry_tree:
1033 free_dentry_tree(root_dentry, w->lookup_table);
1034 out_free_security_data:
1035 free_security_data(sd);
1036 out_destroy_inode_table:
1037 destroy_inode_table(&inode_table);
1038 destroy_sd_set(&sd_set);
1044 wimlib_add_image(WIMStruct *w,
1045 const tchar *source,
1047 struct wimlib_capture_config *config,
1048 int add_image_flags,
1049 wimlib_progress_func_t progress_func)
1051 if (!source || !*source)
1052 return WIMLIB_ERR_INVALID_PARAM;
1054 tchar *fs_source_path = TSTRDUP(source);
1056 struct wimlib_capture_source capture_src = {
1057 .fs_source_path = fs_source_path,
1058 .wim_target_path = NULL,
1061 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1062 config, add_image_flags,
1064 FREE(fs_source_path);