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/.
24 #include "wimlib_internal.h"
26 #include "timestamp.h"
27 #include "lookup_table.h"
38 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
39 #define WIMLIB_ADD_IMAGE_FLAG_SOURCE 0x40000000
42 * Adds the dentry tree and security data for a new image to the image metadata
43 * array of the WIMStruct.
45 int add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
46 struct wim_security_data *sd)
48 struct wim_lookup_table_entry *metadata_lte;
49 struct wim_image_metadata *imd;
50 struct wim_image_metadata *new_imd;
52 wimlib_assert(root_dentry != NULL);
54 DEBUG("Reallocating image metadata array for image_count = %u",
55 w->hdr.image_count + 1);
56 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
59 ERROR("Failed to allocate memory for new image metadata array");
63 memcpy(imd, w->image_metadata,
64 w->hdr.image_count * sizeof(struct wim_image_metadata));
66 metadata_lte = new_lookup_table_entry();
70 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
71 random_hash(metadata_lte->hash);
72 lookup_table_insert(w->lookup_table, metadata_lte);
74 new_imd = &imd[w->hdr.image_count];
76 new_imd->root_dentry = root_dentry;
77 new_imd->metadata_lte = metadata_lte;
78 new_imd->security_data = sd;
79 new_imd->modified = 1;
81 FREE(w->image_metadata);
82 w->image_metadata = imd;
88 return WIMLIB_ERR_NOMEM;
94 * build_dentry_tree():
95 * Recursively builds a tree of WIM dentries from an on-disk directory
98 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
99 * modified if successful. Set to NULL if the file or directory was
100 * excluded from capture.
102 * @root_disk_path: The path to the root of the directory tree on disk.
104 * @lookup_table: The lookup table for the WIM file. For each file added to the
105 * dentry tree being built, an entry is added to the lookup table,
106 * unless an identical stream is already in the lookup table.
107 * These lookup table entries that are added point to the path of
110 * @sd: Ignored. (Security data only captured in NTFS mode.)
113 * Configuration for files to be excluded from capture.
115 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
117 * @extra_arg: Ignored. (Only used in NTFS mode.)
119 * @return: 0 on success, nonzero on failure. It is a failure if any of
120 * the files cannot be `stat'ed, or if any of the needed
121 * directories cannot be opened or read. Failure to add the files
122 * to the WIM may still occur later when trying to actually read
123 * the on-disk files during a call to wimlib_write() or
124 * wimlib_overwrite().
126 static int build_dentry_tree(struct wim_dentry **root_ret,
127 const char *root_disk_path,
128 struct wim_lookup_table *lookup_table,
129 struct wim_security_data *sd,
130 const struct capture_config *config,
132 wimlib_progress_func_t progress_func,
135 struct stat root_stbuf;
137 int (*stat_fn)(const char *restrict, struct stat *restrict);
138 struct wim_dentry *root;
139 struct wim_inode *inode;
141 if (exclude_path(root_disk_path, config, true)) {
142 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
143 ERROR("Cannot exclude the root directory from capture");
144 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
146 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
149 union wimlib_progress_info info;
150 info.scan.cur_path = root_disk_path;
151 info.scan.excluded = true;
152 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
158 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
161 union wimlib_progress_info info;
162 info.scan.cur_path = root_disk_path;
163 info.scan.excluded = false;
164 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
167 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
172 ret = (*stat_fn)(root_disk_path, &root_stbuf);
174 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
175 return WIMLIB_ERR_STAT;
178 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
179 !S_ISDIR(root_stbuf.st_mode))
181 /* Do a dereference-stat in case the root is a symbolic link.
182 * This case is allowed, provided that the symbolic link points
184 ret = stat(root_disk_path, &root_stbuf);
186 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
187 return WIMLIB_ERR_STAT;
189 if (!S_ISDIR(root_stbuf.st_mode)) {
190 ERROR("`%s' is not a directory", root_disk_path);
191 return WIMLIB_ERR_NOTDIR;
194 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
195 && !S_ISLNK(root_stbuf.st_mode)) {
196 ERROR("`%s' is not a regular file, directory, or symbolic link.",
198 return WIMLIB_ERR_SPECIAL_FILE;
201 root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
204 return WIMLIB_ERR_INVALID_UTF8_STRING;
205 else if (errno == ENOMEM)
206 return WIMLIB_ERR_NOMEM;
208 return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
211 inode = root->d_inode;
213 #ifdef HAVE_STAT_NANOSECOND_PRECISION
214 inode->i_creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
215 inode->i_last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
216 inode->i_last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
218 inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
219 inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
220 inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
222 if (sizeof(ino_t) >= 8)
223 inode->i_ino = (u64)root_stbuf.st_ino;
225 inode->i_ino = (u64)root_stbuf.st_ino |
226 ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
227 inode->i_resolved = 1;
228 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
229 ret = inode_set_unix_data(inode, root_stbuf.st_uid,
233 UNIX_DATA_ALL | UNIX_DATA_CREATE);
237 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
238 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
240 struct wim_lookup_table_entry *lte;
241 u8 hash[SHA1_HASH_SIZE];
243 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
245 /* Empty files do not have to have a lookup table entry. */
246 if (root_stbuf.st_size == 0)
249 /* For each regular file, we must check to see if the file is in
250 * the lookup table already; if it is, we increment its refcnt;
251 * otherwise, we create a new lookup table entry and insert it.
254 ret = sha1sum(root_disk_path, hash);
258 lte = __lookup_resource(lookup_table, hash);
261 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
264 char *file_on_disk = STRDUP(root_disk_path);
266 ERROR("Failed to allocate memory for file path");
267 ret = WIMLIB_ERR_NOMEM;
270 lte = new_lookup_table_entry();
273 ret = WIMLIB_ERR_NOMEM;
276 lte->file_on_disk = file_on_disk;
277 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
278 lte->resource_entry.original_size = root_stbuf.st_size;
279 lte->resource_entry.size = root_stbuf.st_size;
280 copy_hash(lte->hash, hash);
281 lookup_table_insert(lookup_table, lte);
283 root->d_inode->i_lte = lte;
284 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
286 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
289 struct dirent entry, *result;
290 struct wim_dentry *child;
292 dir = opendir(root_disk_path);
294 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
296 ret = WIMLIB_ERR_OPEN;
300 /* Buffer for names of files in directory. */
301 size_t len = strlen(root_disk_path);
302 char name[len + 1 + FILENAME_MAX + 1];
303 memcpy(name, root_disk_path, len);
306 /* Create a dentry for each entry in the directory on disk, and recurse
307 * to any subdirectories. */
310 ret = readdir_r(dir, &entry, &result);
312 ret = WIMLIB_ERR_READ;
313 ERROR_WITH_ERRNO("Error reading the "
320 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
321 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
323 strcpy(name + len + 1, result->d_name);
324 ret = build_dentry_tree(&child, name, lookup_table,
325 NULL, config, add_image_flags,
326 progress_func, NULL);
330 dentry_add_child(root, child);
333 } else { /* Archiving a symbolic link */
334 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
335 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
337 /* The idea here is to call readlink() to get the UNIX target of
338 * the symbolic link, then turn the target into a reparse point
339 * data buffer that contains a relative or absolute symbolic
340 * link (NOT a junction point or *full* path symbolic link with
344 char deref_name_buf[4096];
345 ssize_t deref_name_len;
347 deref_name_len = readlink(root_disk_path, deref_name_buf,
348 sizeof(deref_name_buf) - 1);
349 if (deref_name_len >= 0) {
350 deref_name_buf[deref_name_len] = '\0';
351 DEBUG("Read symlink `%s'", deref_name_buf);
352 ret = inode_set_symlink(root->d_inode, deref_name_buf,
356 * Unfortunately, Windows seems to have the
357 * concept of "file" symbolic links as being
358 * different from "directory" symbolic links...
359 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
360 * on the symbolic link if the *target* of the
361 * symbolic link is a directory.
364 if (stat(root_disk_path, &stbuf) == 0 &&
365 S_ISDIR(stbuf.st_mode))
367 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
371 ERROR_WITH_ERRNO("Failed to read target of "
372 "symbolic link `%s'", root_disk_path);
373 ret = WIMLIB_ERR_READLINK;
380 free_dentry_tree(root, lookup_table);
389 COMPRESSION_EXCLUSION_LIST,
393 #define COMPAT_DEFAULT_CONFIG
395 /* Default capture configuration file when none is specified. */
396 static const char *default_config =
397 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
398 users. The next ABI-incompatible library
399 version will default to the empty string here. */
404 "\\System Volume Information\n"
408 "[CompressionExclusionList]\n"
412 "\\WINDOWS\\inf\\*.pnf\n";
417 static void destroy_pattern_list(struct pattern_list *list)
422 static void destroy_capture_config(struct capture_config *config)
424 destroy_pattern_list(&config->exclusion_list);
425 destroy_pattern_list(&config->exclusion_exception);
426 destroy_pattern_list(&config->compression_exclusion_list);
427 destroy_pattern_list(&config->alignment_list);
428 FREE(config->config_str);
429 FREE(config->prefix);
430 memset(config, 0, sizeof(*config));
433 static int pattern_list_add_pattern(struct pattern_list *list,
437 if (list->num_pats >= list->num_allocated_pats) {
438 pats = REALLOC(list->pats,
439 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
441 return WIMLIB_ERR_NOMEM;
442 list->num_allocated_pats += 8;
445 list->pats[list->num_pats++] = pattern;
449 /* Parses the contents of the image capture configuration file and fills in a
450 * `struct capture_config'. */
451 static int init_capture_config(const char *_config_str, size_t config_len,
452 const char *_prefix, struct capture_config *config)
459 size_t bytes_remaining;
460 enum pattern_type type = NONE;
462 unsigned long line_no = 0;
464 DEBUG("config_len = %zu", config_len);
465 bytes_remaining = config_len;
466 memset(config, 0, sizeof(*config));
467 config_str = MALLOC(config_len);
469 ERROR("Could not duplicate capture config string");
470 return WIMLIB_ERR_NOMEM;
472 prefix = STRDUP(_prefix);
475 return WIMLIB_ERR_NOMEM;
478 memcpy(config_str, _config_str, config_len);
480 config->config_str = config_str;
481 config->prefix = prefix;
482 config->prefix_len = strlen(prefix);
483 while (bytes_remaining) {
486 eol = memchr(p, '\n', bytes_remaining);
488 ERROR("Expected end-of-line in capture config file on "
489 "line %lu", line_no);
490 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
495 bytes_remaining -= (next_p - p);
499 if (*(eol - 1) == '\r')
503 /* Translate backslash to forward slash */
504 for (char *pp = p; pp != eol; pp++)
508 /* Remove drive letter */
509 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
513 if (strcmp(p, "[ExclusionList]") == 0)
514 type = EXCLUSION_LIST;
515 else if (strcmp(p, "[ExclusionException]") == 0)
516 type = EXCLUSION_EXCEPTION;
517 else if (strcmp(p, "[CompressionExclusionList]") == 0)
518 type = COMPRESSION_EXCLUSION_LIST;
519 else if (strcmp(p, "[AlignmentList]") == 0)
520 type = ALIGNMENT_LIST;
521 else if (p[0] == '[' && strrchr(p, ']')) {
522 ERROR("Unknown capture configuration section `%s'", p);
523 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
524 } else switch (type) {
526 DEBUG("Adding pattern \"%s\" to exclusion list", p);
527 ret = pattern_list_add_pattern(&config->exclusion_list, p);
529 case EXCLUSION_EXCEPTION:
530 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
531 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
533 case COMPRESSION_EXCLUSION_LIST:
534 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
535 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
538 DEBUG("Adding pattern \"%s\" to alignment list", p);
539 ret = pattern_list_add_pattern(&config->alignment_list, p);
542 ERROR("Line %lu of capture configuration is not "
543 "in a block (such as [ExclusionList])",
545 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
553 destroy_capture_config(config);
557 static bool match_pattern(const char *path, const char *path_basename,
558 const struct pattern_list *list)
560 for (size_t i = 0; i < list->num_pats; i++) {
561 const char *pat = list->pats[i];
564 /* Absolute path from root of capture */
567 if (strchr(pat, '/'))
568 /* Relative path from root of capture */
571 /* A file name pattern */
572 string = path_basename;
574 if (fnmatch(pat, string, FNM_PATHNAME
580 DEBUG("`%s' matches the pattern \"%s\"",
588 /* Return true if the image capture configuration file indicates we should
589 * exclude the filename @path from capture.
591 * If @exclude_prefix is %true, the part of the path up and including the name
592 * of the directory being captured is not included in the path for matching
593 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
594 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
597 bool exclude_path(const char *path, const struct capture_config *config,
600 const char *basename = path_basename(path);
601 if (exclude_prefix) {
602 wimlib_assert(strlen(path) >= config->prefix_len);
603 if (memcmp(config->prefix, path, config->prefix_len) == 0
604 && path[config->prefix_len] == '/')
605 path += config->prefix_len;
607 return match_pattern(path, basename, &config->exclusion_list) &&
608 !match_pattern(path, basename, &config->exclusion_exception);
612 /* Strip leading and trailing forward slashes from a string. Modifies it in
613 * place and returns the stripped string. */
614 static const char *canonicalize_target_path(char *target_path)
617 if (target_path == NULL)
620 if (*target_path == '\0')
622 else if (*target_path == '/')
628 p = target_path + strlen(target_path) - 1;
634 /* Strip leading and trailing slashes from the target paths */
635 static void canonicalize_targets(struct wimlib_capture_source *sources,
638 while (num_sources--) {
639 DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
640 sources->fs_source_path,
641 sources->wim_target_path);
642 sources->wim_target_path =
643 (char*)canonicalize_target_path(sources->wim_target_path);
644 DEBUG("\"%s\"", sources->wim_target_path);
649 static int capture_source_cmp(const void *p1, const void *p2)
651 const struct wimlib_capture_source *s1, *s2;
655 return strcmp(s1->wim_target_path, s2->wim_target_path);
658 /* Sorts the capture sources lexicographically by target path. This occurs
659 * after leading and trailing forward slashes are stripped.
661 * One purpose of this is to make sure that target paths that are inside other
662 * target paths are extracted after the containing target paths. */
663 static void sort_sources(struct wimlib_capture_source *sources,
666 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
669 static int check_sorted_sources(struct wimlib_capture_source *sources,
670 size_t num_sources, int add_image_flags)
672 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
673 if (num_sources != 1) {
674 ERROR("Must specify exactly 1 capture source "
675 "(the NTFS volume) in NTFS mode!");
676 return WIMLIB_ERR_INVALID_PARAM;
678 if (sources[0].wim_target_path[0] != '\0') {
679 ERROR("In NTFS capture mode the target path inside "
680 "the image must be the root directory!");
681 return WIMLIB_ERR_INVALID_PARAM;
683 } else if (num_sources != 0) {
684 /* This code is disabled because the current code
685 * unconditionally attempts to do overlays. So, duplicate
686 * target paths are OK. */
688 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
689 ERROR("Cannot specify root target when using multiple "
691 return WIMLIB_ERR_INVALID_PARAM;
693 for (size_t i = 0; i < num_sources - 1; i++) {
694 size_t len = strlen(sources[i].wim_target_path);
696 const char *target1 = sources[i].wim_target_path;
698 const char *target2 = sources[j].wim_target_path;
699 DEBUG("target1=%s, target2=%s",
701 if (strncmp(target1, target2, len) ||
704 if (target2[len] == '/') {
705 ERROR("Invalid target `%s': is a prefix of `%s'",
707 return WIMLIB_ERR_INVALID_PARAM;
709 if (target2[len] == '\0') {
710 ERROR("Invalid target `%s': is a duplicate of `%s'",
712 return WIMLIB_ERR_INVALID_PARAM;
714 } while (++j != num_sources);
722 /* Creates a new directory to place in the WIM image. This is to create parent
723 * directories that are not part of any target as needed. */
724 static struct wim_dentry *
725 new_filler_directory(const char *name)
727 struct wim_dentry *dentry;
728 DEBUG("Creating filler directory \"%s\"", name);
729 dentry = new_dentry_with_inode(name);
731 dentry->d_inode->i_ino = 0;
732 dentry->d_inode->i_resolved = 1;
733 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
738 /* Transfers the children of @branch to @target. It is an error if @target is
739 * not a directory or if both @branch and @target contain a child dentry with
741 static int do_overlay(struct wim_dentry *target,
742 struct wim_dentry *branch)
744 struct rb_root *rb_root;
746 if (!dentry_is_directory(target)) {
747 ERROR("Cannot overlay directory `%s' over non-directory",
748 branch->file_name_utf8);
749 /* XXX Use a different error code */
750 return WIMLIB_ERR_INVALID_DENTRY;
753 rb_root = &branch->d_inode->i_children;
754 while (rb_root->rb_node) { /* While @branch has children... */
755 struct wim_dentry *child;
757 child = container_of(rb_root->rb_node, struct wim_dentry, rb_node);
758 unlink_dentry(child);
759 if (!dentry_add_child(target, child)) {
760 dentry_add_child(branch, child);
761 ERROR("Overlay error: file `%s' already exists as child of `%s'",
762 child->file_name_utf8, target->file_name_utf8);
763 return WIMLIB_ERR_INVALID_DENTRY;
770 /* Attach or overlay a branch onto the WIM image.
773 * Pointer to the root of the WIM image, or pointer to NULL if it has not
778 * Path in the WIM image to add the branch.
780 static int attach_branch(struct wim_dentry **root_p,
781 struct wim_dentry *branch,
785 struct wim_dentry *dentry, *parent, *target;
787 if (*target_path == '\0') {
788 /* Target: root directory */
790 /* Overlay on existing root */
791 return do_overlay(*root_p, branch);
799 /* Adding a non-root branch. Create root if it hasn't been created
802 *root_p = new_filler_directory("");
804 return WIMLIB_ERR_NOMEM;
807 /* Walk the path to the branch, creating filler directories as needed.
810 while ((slash = strchr(target_path, '/'))) {
812 dentry = get_dentry_child_with_name(parent, target_path);
814 dentry = new_filler_directory(target_path);
816 return WIMLIB_ERR_NOMEM;
817 dentry_add_child(parent, dentry);
823 wimlib_assert(*target_path != '\0');
824 } while (*target_path == '/');
827 /* If the target path already existed, overlay the branch onto it.
828 * Otherwise, set the branch as the target path. */
829 target = get_dentry_child_with_name(parent, branch->file_name_utf8);
831 return do_overlay(target, branch);
833 dentry_add_child(parent, branch);
838 WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
839 struct wimlib_capture_source *sources,
842 const char *config_str,
845 wimlib_progress_func_t progress_func)
847 int (*capture_tree)(struct wim_dentry **, const char *,
848 struct wim_lookup_table *,
849 struct wim_security_data *,
850 const struct capture_config *,
851 int, wimlib_progress_func_t, void *);
853 struct wim_dentry *root_dentry = NULL;
854 struct wim_security_data *sd;
855 struct capture_config config;
856 struct wim_image_metadata *imd;
859 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
861 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
862 ERROR("Cannot dereference files when capturing directly from NTFS");
863 return WIMLIB_ERR_INVALID_PARAM;
865 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
866 ERROR("Capturing UNIX owner and mode not supported "
867 "when capturing directly from NTFS");
868 return WIMLIB_ERR_INVALID_PARAM;
870 capture_tree = build_dentry_tree_ntfs;
871 extra_arg = &w->ntfs_vol;
873 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
874 " cannot capture a WIM image directly from a NTFS volume!");
875 return WIMLIB_ERR_UNSUPPORTED;
878 capture_tree = build_dentry_tree;
882 if (!name || !*name) {
883 ERROR("Must specify a non-empty string for the image name");
884 return WIMLIB_ERR_INVALID_PARAM;
887 if (w->hdr.total_parts != 1) {
888 ERROR("Cannot add an image to a split WIM");
889 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
892 if (wimlib_image_name_in_use(w, name)) {
893 ERROR("There is already an image named \"%s\" in `%s'",
895 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
899 DEBUG("Using default capture configuration");
900 config_str = default_config;
901 config_len = strlen(default_config);
903 memset(&config, 0, sizeof(struct capture_config));
905 DEBUG("Allocating security data");
907 sd = CALLOC(1, sizeof(struct wim_security_data));
909 ret = WIMLIB_ERR_NOMEM;
910 goto out_destroy_config;
912 sd->total_length = 8;
915 DEBUG("Using %zu capture sources", num_sources);
916 canonicalize_targets(sources, num_sources);
917 sort_sources(sources, num_sources);
918 ret = check_sorted_sources(sources, num_sources, add_image_flags);
920 ret = WIMLIB_ERR_INVALID_PARAM;
921 goto out_free_security_data;
924 DEBUG("Building dentry tree.");
925 if (num_sources == 0) {
926 root_dentry = new_filler_directory("");
928 goto out_free_security_data;
931 struct wim_dentry *branch;
934 DEBUG("Building dentry tree for source %zu of %zu "
935 "(\"%s\" => \"%s\")", i + 1, num_sources,
936 sources[i].fs_source_path,
937 sources[i].wim_target_path);
938 union wimlib_progress_info progress;
940 memset(&progress, 0, sizeof(progress));
941 progress.scan.source = sources[i].fs_source_path;
942 progress.scan.wim_target_path = sources[i].wim_target_path;
943 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
945 ret = init_capture_config(config_str, config_len,
946 sources[i].fs_source_path,
949 goto out_free_dentry_tree;
950 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
951 if (!*sources[i].wim_target_path)
952 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
953 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
957 progress_func, extra_arg);
959 ERROR("Failed to build dentry tree for `%s'",
960 sources[i].fs_source_path);
961 goto out_free_dentry_tree;
964 ret = set_dentry_name(branch,
965 path_basename(sources[i].wim_target_path));
967 free_dentry_tree(branch, w->lookup_table);
968 goto out_free_dentry_tree;
971 ret = attach_branch(&root_dentry, branch,
972 sources[i].wim_target_path);
974 free_dentry_tree(branch, w->lookup_table);
975 goto out_free_dentry_tree;
978 destroy_capture_config(&config);
980 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
981 } while (++i != num_sources);
984 DEBUG("Calculating full paths of dentries.");
985 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
987 goto out_free_dentry_tree;
989 ret = add_new_dentry_tree(w, root_dentry, sd);
991 goto out_free_dentry_tree;
993 imd = &w->image_metadata[w->hdr.image_count - 1];
995 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
997 goto out_destroy_imd;
999 DEBUG("Assigning hard link group IDs");
1000 assign_inode_numbers(&imd->inode_list);
1002 ret = xml_add_image(w, name);
1004 goto out_destroy_imd;
1006 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1007 wimlib_set_boot_idx(w, w->hdr.image_count);
1011 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1013 w->hdr.image_count--;
1015 out_free_dentry_tree:
1016 free_dentry_tree(root_dentry, w->lookup_table);
1017 out_free_security_data:
1018 free_security_data(sd);
1020 destroy_capture_config(&config);
1025 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
1026 const char *name, const char *config_str,
1027 size_t config_len, int add_image_flags,
1028 wimlib_progress_func_t progress_func)
1030 if (!source || !*source)
1031 return WIMLIB_ERR_INVALID_PARAM;
1033 char *fs_source_path = STRDUP(source);
1035 struct wimlib_capture_source capture_src = {
1036 .fs_source_path = fs_source_path,
1037 .wim_target_path = NULL,
1040 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1041 config_str, config_len,
1042 add_image_flags, progress_func);
1043 FREE(fs_source_path);