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_lookup_table_entry *metadata_lte;
62 struct wim_image_metadata *imd;
63 struct wim_image_metadata *new_imd;
65 wimlib_assert(root_dentry != NULL);
67 DEBUG("Reallocating image metadata array for image_count = %u",
68 w->hdr.image_count + 1);
69 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
72 ERROR("Failed to allocate memory for new image metadata array");
76 memcpy(imd, w->image_metadata,
77 w->hdr.image_count * sizeof(struct wim_image_metadata));
79 metadata_lte = new_lookup_table_entry();
83 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
85 new_imd = &imd[w->hdr.image_count];
87 new_imd->root_dentry = root_dentry;
88 new_imd->metadata_lte = metadata_lte;
89 new_imd->security_data = sd;
90 new_imd->modified = 1;
92 FREE(w->image_metadata);
93 w->image_metadata = imd;
99 return WIMLIB_ERR_NOMEM;
106 unix_capture_regular_file(const char *path,
108 struct wim_inode *inode,
109 struct wim_lookup_table *lookup_table)
111 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
113 /* Empty files do not have to have a lookup table entry. */
115 struct wim_lookup_table_entry *lte;
118 file_on_disk = STRDUP(path);
120 return WIMLIB_ERR_NOMEM;
121 lte = new_lookup_table_entry();
124 return WIMLIB_ERR_NOMEM;
126 lte->file_on_disk = file_on_disk;
127 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
128 lte->resource_entry.original_size = size;
129 lookup_table_insert_unhashed(lookup_table, lte);
136 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
139 struct wim_lookup_table *lookup_table,
140 struct wim_inode_table *inode_table,
141 const struct wimlib_capture_config *config,
143 wimlib_progress_func_t progress_func);
146 unix_capture_directory(struct wim_dentry *dir_dentry,
149 struct wim_lookup_table *lookup_table,
150 struct wim_inode_table *inode_table,
151 const struct wimlib_capture_config *config,
153 wimlib_progress_func_t progress_func)
157 struct dirent entry, *result;
158 struct wim_dentry *child;
161 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
164 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
166 return WIMLIB_ERR_OPEN;
169 /* Recurse on directory contents */
172 ret = readdir_r(dir, &entry, &result);
174 ret = WIMLIB_ERR_READ;
175 ERROR_WITH_ERRNO("Error reading the "
182 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
183 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
186 size_t name_len = strlen(result->d_name);
188 path[path_len] = '/';
189 memcpy(&path[path_len + 1], result->d_name, name_len + 1);
190 ret = unix_build_dentry_tree_recursive(&child,
192 path_len + 1 + name_len,
201 dentry_add_child(dir_dentry, child);
208 unix_capture_symlink(const char *path,
209 struct wim_inode *inode,
210 struct wim_lookup_table *lookup_table)
212 char deref_name_buf[4096];
213 ssize_t deref_name_len;
216 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
217 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
219 /* The idea here is to call readlink() to get the UNIX target of
220 * the symbolic link, then turn the target into a reparse point
221 * data buffer that contains a relative or absolute symbolic
222 * link (NOT a junction point or *full* path symbolic link with
225 deref_name_len = readlink(path, deref_name_buf,
226 sizeof(deref_name_buf) - 1);
227 if (deref_name_len >= 0) {
228 deref_name_buf[deref_name_len] = '\0';
229 DEBUG("Read symlink `%s'", deref_name_buf);
230 ret = inode_set_symlink(inode, deref_name_buf,
233 /* Unfortunately, Windows seems to have the concept of
234 * "file" symbolic links as being different from
235 * "directory" symbolic links... so
236 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
237 * symbolic link if the *target* of the symbolic link is
240 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
241 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
244 ERROR_WITH_ERRNO("Failed to read target of "
245 "symbolic link `%s'", path);
246 ret = WIMLIB_ERR_READLINK;
252 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
255 struct wim_lookup_table *lookup_table,
256 struct wim_inode_table *inode_table,
257 const struct wimlib_capture_config *config,
259 wimlib_progress_func_t progress_func)
261 struct wim_dentry *root = NULL;
263 struct wim_inode *inode;
265 if (exclude_path(path, path_len, config, true)) {
266 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
267 ERROR("Cannot exclude the root directory from capture");
268 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
271 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
274 union wimlib_progress_info info;
275 info.scan.cur_path = path;
276 info.scan.excluded = true;
277 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
282 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
285 union wimlib_progress_info info;
286 info.scan.cur_path = path;
287 info.scan.excluded = false;
288 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
291 /* UNIX version of capturing a directory tree */
293 int (*stat_fn)(const char *restrict, struct stat *restrict);
294 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
299 ret = (*stat_fn)(path, &stbuf);
301 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
305 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
306 !S_ISDIR(stbuf.st_mode))
308 /* Do a dereference-stat in case the root is a symbolic link.
309 * This case is allowed, provided that the symbolic link points
311 ret = stat(path, &stbuf);
313 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
314 ret = WIMLIB_ERR_STAT;
317 if (!S_ISDIR(stbuf.st_mode)) {
318 ERROR("`%s' is not a directory", path);
319 ret = WIMLIB_ERR_NOTDIR;
323 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
324 && !S_ISLNK(stbuf.st_mode)) {
325 ERROR("`%s' is not a regular file, directory, or symbolic link.",
327 ret = WIMLIB_ERR_SPECIAL_FILE;
331 ret = inode_table_new_dentry(inode_table,
332 path_basename_with_len(path, path_len),
339 inode = root->d_inode;
341 if (inode->i_nlink > 1) /* Already captured this inode? */
344 #ifdef HAVE_STAT_NANOSECOND_PRECISION
345 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
346 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
347 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
349 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
350 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
351 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
353 inode->i_resolved = 1;
354 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
355 ret = inode_set_unix_data(inode, stbuf.st_uid,
359 UNIX_DATA_ALL | UNIX_DATA_CREATE);
363 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
364 if (S_ISREG(stbuf.st_mode))
365 ret = unix_capture_regular_file(path, stbuf.st_size,
366 inode, lookup_table);
367 else if (S_ISDIR(stbuf.st_mode))
368 ret = unix_capture_directory(root, path, path_len,
369 lookup_table, inode_table, config,
370 add_image_flags, progress_func);
372 ret = unix_capture_symlink(path, inode, lookup_table);
377 free_dentry_tree(root, lookup_table);
378 lookup_table_free_unhashed_streams(lookup_table);
384 * unix_build_dentry_tree():
385 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
386 * version; no NTFS-specific data is captured).
388 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
389 * modified if successful. Set to NULL if the file or directory was
390 * excluded from capture.
392 * @root_disk_path: The path to the root of the directory tree on disk.
394 * @lookup_table: The lookup table for the WIM file. For each file added to the
395 * dentry tree being built, an entry is added to the lookup table,
396 * unless an identical stream is already in the lookup table.
397 * These lookup table entries that are added point to the path of
400 * @sd_set: Ignored. (Security data only captured in NTFS mode.)
403 * Configuration for files to be excluded from capture.
405 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
407 * @extra_arg: Ignored
409 * @return: 0 on success, nonzero on failure. It is a failure if any of
410 * the files cannot be `stat'ed, or if any of the needed
411 * directories cannot be opened or read. Failure to add the files
412 * to the WIM may still occur later when trying to actually read
413 * the on-disk files during a call to wimlib_write() or
414 * wimlib_overwrite().
417 unix_build_dentry_tree(struct wim_dentry **root_ret,
418 const char *root_disk_path,
419 struct wim_lookup_table *lookup_table,
420 struct wim_inode_table *inode_table,
421 struct sd_set *sd_set,
422 const struct wimlib_capture_config *config,
424 wimlib_progress_func_t progress_func,
432 path_bufsz = min(32790, PATH_MAX + 1);
433 path_len = strlen(root_disk_path);
435 if (path_len >= path_bufsz)
436 return WIMLIB_ERR_INVALID_PARAM;
438 path_buf = MALLOC(path_bufsz);
440 return WIMLIB_ERR_NOMEM;
441 memcpy(path_buf, root_disk_path, path_len + 1);
442 ret = unix_build_dentry_tree_recursive(root_ret,
453 #endif /* !__WIN32__ */
456 match_pattern(const tchar *path,
457 const tchar *path_basename,
458 const struct wimlib_pattern_list *list)
460 for (size_t i = 0; i < list->num_pats; i++) {
462 const tchar *pat = list->pats[i];
465 if (*pat == T('/')) {
466 /* Absolute path from root of capture */
469 if (tstrchr(pat, T('/')))
470 /* Relative path from root of capture */
473 /* A file name pattern */
474 string = path_basename;
477 /* Warning: on Windows native builds, fnmatch() calls the
478 * replacement function in win32.c. */
479 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
485 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
489 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
496 /* Return true if the image capture configuration file indicates we should
497 * exclude the filename @path from capture.
499 * If @exclude_prefix is %true, the part of the path up and including the name
500 * of the directory being captured is not included in the path for matching
501 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
502 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
506 exclude_path(const tchar *path, size_t path_len,
507 const struct wimlib_capture_config *config, bool exclude_prefix)
509 const tchar *basename = path_basename_with_len(path, path_len);
510 if (exclude_prefix) {
511 wimlib_assert(path_len >= config->_prefix_num_tchars);
512 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
513 path[config->_prefix_num_tchars] == T('/'))
515 path += config->_prefix_num_tchars;
518 return match_pattern(path, basename, &config->exclusion_pats) &&
519 !match_pattern(path, basename, &config->exclusion_exception_pats);
523 /* Strip leading and trailing forward slashes from a string. Modifies it in
524 * place and returns the stripped string. */
526 canonicalize_target_path(tchar *target_path)
529 if (target_path == NULL)
532 if (*target_path == T('\0'))
534 else if (*target_path == T('/'))
540 p = tstrchr(target_path, T('\0')) - 1;
546 /* Strip leading and trailing slashes from the target paths, and translate all
547 * backslashes in the source and target paths into forward slashes. */
549 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
552 while (num_sources--) {
553 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
554 sources->fs_source_path,
555 sources->wim_target_path);
557 /* The Windows API can handle forward slashes. Just get rid of
558 * backslashes to avoid confusing other parts of the library
560 zap_backslashes(sources->fs_source_path);
561 if (sources->wim_target_path)
562 zap_backslashes(sources->wim_target_path);
564 sources->wim_target_path =
565 (tchar*)canonicalize_target_path(sources->wim_target_path);
566 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
572 capture_source_cmp(const void *p1, const void *p2)
574 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
575 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
578 /* Sorts the capture sources lexicographically by target path. This occurs
579 * after leading and trailing forward slashes are stripped.
581 * One purpose of this is to make sure that target paths that are inside other
582 * target paths are added after the containing target paths. */
584 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
586 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
590 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
593 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
594 if (num_sources != 1) {
595 ERROR("Must specify exactly 1 capture source "
596 "(the NTFS volume) in NTFS mode!");
597 return WIMLIB_ERR_INVALID_PARAM;
599 if (sources[0].wim_target_path[0] != T('\0')) {
600 ERROR("In NTFS capture mode the target path inside "
601 "the image must be the root directory!");
602 return WIMLIB_ERR_INVALID_PARAM;
604 } else if (num_sources != 0) {
605 /* This code is disabled because the current code
606 * unconditionally attempts to do overlays. So, duplicate
607 * target paths are OK. */
609 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
610 ERROR("Cannot specify root target when using multiple "
612 return WIMLIB_ERR_INVALID_PARAM;
614 for (size_t i = 0; i < num_sources - 1; i++) {
615 size_t len = strlen(sources[i].wim_target_path);
617 const char *target1 = sources[i].wim_target_path;
619 const char *target2 = sources[j].wim_target_path;
620 DEBUG("target1=%s, target2=%s",
622 if (strncmp(target1, target2, len) ||
625 if (target2[len] == '/') {
626 ERROR("Invalid target `%s': is a prefix of `%s'",
628 return WIMLIB_ERR_INVALID_PARAM;
630 if (target2[len] == '\0') {
631 ERROR("Invalid target `%s': is a duplicate of `%s'",
633 return WIMLIB_ERR_INVALID_PARAM;
635 } while (++j != num_sources);
643 /* Creates a new directory to place in the WIM image. This is to create parent
644 * directories that are not part of any target as needed. */
646 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
649 struct wim_dentry *dentry;
651 DEBUG("Creating filler directory \"%"TS"\"", name);
652 ret = new_dentry_with_inode(name, &dentry);
654 /* Leave the inode number as 0; this is allowed for non
655 * hard-linked files. */
656 dentry->d_inode->i_resolved = 1;
657 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
658 *dentry_ret = dentry;
663 /* Transfers the children of @branch to @target. It is an error if @target is
664 * not a directory or if both @branch and @target contain a child dentry with
667 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
669 struct rb_root *rb_root;
671 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
672 branch->file_name, target->file_name);
674 if (!dentry_is_directory(target)) {
675 ERROR("Cannot overlay directory \"%"WS"\" "
676 "over non-directory", branch->file_name);
677 return WIMLIB_ERR_INVALID_OVERLAY;
680 rb_root = &branch->d_inode->i_children;
681 while (rb_root->rb_node) { /* While @branch has children... */
682 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
683 /* Move @child to the directory @target */
684 unlink_dentry(child);
685 if (!dentry_add_child(target, child)) {
686 /* Revert the change to avoid leaking the directory tree
687 * rooted at @child */
688 dentry_add_child(branch, child);
689 ERROR("Overlay error: file \"%"WS"\" already exists "
690 "as a child of \"%"WS"\"",
691 child->file_name, target->file_name);
692 return WIMLIB_ERR_INVALID_OVERLAY;
700 /* Attach or overlay a branch onto the WIM image.
703 * Pointer to the root of the WIM image, or pointer to NULL if it has not
708 * Path in the WIM image to add the branch, with leading and trailing
712 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
716 struct wim_dentry *dentry, *parent, *target;
719 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
720 branch->file_name, target_path);
722 if (*target_path == T('\0')) {
723 /* Target: root directory */
725 /* Overlay on existing root */
726 return do_overlay(*root_p, branch);
734 /* Adding a non-root branch. Create root if it hasn't been created
737 ret = new_filler_directory(T(""), root_p);
742 /* Walk the path to the branch, creating filler directories as needed.
745 while ((slash = tstrchr(target_path, T('/')))) {
747 dentry = get_dentry_child_with_name(parent, target_path);
749 ret = new_filler_directory(target_path, &dentry);
752 dentry_add_child(parent, dentry);
756 /* Skip over slashes. Note: this cannot overrun the length of
757 * the string because the last character cannot be a slash, as
758 * trailing slashes were tripped. */
761 } while (*target_path == T('/'));
764 /* If the target path already existed, overlay the branch onto it.
765 * Otherwise, set the branch as the target path. */
766 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
767 branch->file_name_nbytes);
769 return do_overlay(target, branch);
771 dentry_add_child(parent, branch);
777 canonicalize_pat(tchar **pat_p)
781 /* Turn all backslashes in the pattern into forward slashes. */
782 zap_backslashes(pat);
784 if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
785 /* Pattern begins with drive letter */
786 if (*(pat + 2) != T('/')) {
787 /* Something like c:file, which is actually a path
788 * relative to the current working directory on the c:
789 * drive. We require paths with drive letters to be
791 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
792 "must be absolute!", pat);
793 ERROR("Maybe try \"%"TC":/%"TS"\"?",
795 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
798 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
799 "being removed.", pat);
800 /* Strip the drive letter */
808 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
811 for (size_t i = 0; i < pat_list->num_pats; i++) {
812 ret = canonicalize_pat(&pat_list->pats[i]);
820 canonicalize_capture_config(struct wimlib_capture_config *config)
822 int ret = canonicalize_pat_list(&config->exclusion_pats);
825 return canonicalize_pat_list(&config->exclusion_exception_pats);
829 wimlib_add_image_multisource(WIMStruct *w,
830 struct wimlib_capture_source *sources,
833 struct wimlib_capture_config *config,
835 wimlib_progress_func_t progress_func)
837 int (*capture_tree)(struct wim_dentry **,
839 struct wim_lookup_table *,
840 struct wim_inode_table *,
842 const struct wimlib_capture_config *,
844 wimlib_progress_func_t,
847 struct wim_dentry *root_dentry;
848 struct wim_dentry *branch;
849 struct wim_security_data *sd;
850 struct wim_image_metadata *imd;
851 struct wim_inode_table inode_table;
853 struct sd_set sd_set;
855 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
857 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
858 ERROR("Cannot dereference files when capturing directly from NTFS");
859 return WIMLIB_ERR_INVALID_PARAM;
861 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
862 ERROR("Capturing UNIX owner and mode not supported "
863 "when capturing directly from NTFS");
864 return WIMLIB_ERR_INVALID_PARAM;
866 capture_tree = build_dentry_tree_ntfs;
867 extra_arg = &w->ntfs_vol;
869 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
870 " cannot capture a WIM image directly from a NTFS volume!");
871 return WIMLIB_ERR_UNSUPPORTED;
875 capture_tree = win32_build_dentry_tree;
877 capture_tree = unix_build_dentry_tree;
883 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
884 ERROR("Capturing UNIX-specific data is not supported on Windows");
885 return WIMLIB_ERR_INVALID_PARAM;
887 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
888 ERROR("Dereferencing symbolic links is not supported on Windows");
889 return WIMLIB_ERR_INVALID_PARAM;
893 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
894 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
896 if (!name || !*name) {
897 ERROR("Must specify a non-empty string for the image name");
898 return WIMLIB_ERR_INVALID_PARAM;
901 if (w->hdr.total_parts != 1) {
902 ERROR("Cannot add an image to a split WIM");
903 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
906 if (wimlib_image_name_in_use(w, name)) {
907 ERROR("There is already an image named \"%"TS"\" in the WIM!",
909 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
913 DEBUG("Capture config not provided; using empty config");
914 config = alloca(sizeof(*config));
915 memset(config, 0, sizeof(*config));
918 ret = canonicalize_capture_config(config);
922 ret = init_inode_table(&inode_table, 9001);
926 DEBUG("Allocating security data");
927 sd = CALLOC(1, sizeof(struct wim_security_data));
929 ret = WIMLIB_ERR_NOMEM;
930 goto out_destroy_inode_table;
932 sd->total_length = 8;
936 sd_set.rb_root.rb_node = NULL;
939 DEBUG("Using %zu capture sources", num_sources);
940 canonicalize_sources_and_targets(sources, num_sources);
941 sort_sources(sources, num_sources);
942 ret = check_sorted_sources(sources, num_sources, add_image_flags);
944 ret = WIMLIB_ERR_INVALID_PARAM;
945 goto out_free_security_data;
949 DEBUG("Building dentry tree.");
952 for (size_t i = 0; i < num_sources; i++) {
954 union wimlib_progress_info progress;
956 DEBUG("Building dentry tree for source %zu of %zu "
957 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
958 sources[i].fs_source_path,
959 sources[i].wim_target_path);
961 memset(&progress, 0, sizeof(progress));
962 progress.scan.source = sources[i].fs_source_path;
963 progress.scan.wim_target_path = sources[i].wim_target_path;
964 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
966 config->_prefix = sources[i].fs_source_path;
967 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
968 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
969 if (!*sources[i].wim_target_path)
970 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
971 ret = (*capture_tree)(&branch,
972 sources[i].fs_source_path,
978 progress_func, extra_arg);
980 ERROR("Failed to build dentry tree for `%"TS"'",
981 sources[i].fs_source_path);
982 goto out_free_dentry_tree;
985 /* Use the target name, not the source name, for
986 * the root of each branch from a capture
987 * source. (This will also set the root dentry
988 * of the entire image to be unnamed.) */
989 ret = set_dentry_name(branch,
990 path_basename(sources[i].wim_target_path));
992 goto out_free_branch;
994 ret = attach_branch(&root_dentry, branch,
995 sources[i].wim_target_path);
997 goto out_free_branch;
1000 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1003 if (root_dentry == NULL) {
1004 ret = new_filler_directory(T(""), &root_dentry);
1006 goto out_free_dentry_tree;
1009 DEBUG("Calculating full paths of dentries.");
1010 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1012 goto out_free_dentry_tree;
1014 ret = add_new_dentry_tree(w, root_dentry, sd);
1016 goto out_free_dentry_tree;
1018 imd = &w->image_metadata[w->hdr.image_count - 1];
1020 DEBUG("Assigning hard link group IDs");
1021 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
1023 ret = xml_add_image(w, name);
1025 goto out_destroy_imd;
1027 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1028 wimlib_set_boot_idx(w, w->hdr.image_count);
1030 goto out_destroy_inode_table;
1032 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1034 w->hdr.image_count--;
1035 goto out_destroy_inode_table;
1037 free_dentry_tree(branch, w->lookup_table);
1038 out_free_dentry_tree:
1039 free_dentry_tree(root_dentry, w->lookup_table);
1040 out_free_security_data:
1041 free_security_data(sd);
1042 out_destroy_inode_table:
1043 destroy_inode_table(&inode_table);
1044 destroy_sd_set(&sd_set);
1050 wimlib_add_image(WIMStruct *w,
1051 const tchar *source,
1053 struct wimlib_capture_config *config,
1054 int add_image_flags,
1055 wimlib_progress_func_t progress_func)
1057 if (!source || !*source)
1058 return WIMLIB_ERR_INVALID_PARAM;
1060 tchar *fs_source_path = TSTRDUP(source);
1062 struct wimlib_capture_source capture_src = {
1063 .fs_source_path = fs_source_path,
1064 .wim_target_path = NULL,
1067 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1068 config, add_image_flags,
1070 FREE(fs_source_path);