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(struct capture_config *config,
452 const char *_config_str, size_t config_len)
458 size_t bytes_remaining;
459 enum pattern_type type = NONE;
461 unsigned long line_no = 0;
463 DEBUG("config_len = %zu", config_len);
464 bytes_remaining = config_len;
465 memset(config, 0, sizeof(*config));
466 config_str = MALLOC(config_len);
468 ERROR("Could not duplicate capture config string");
469 return WIMLIB_ERR_NOMEM;
472 memcpy(config_str, _config_str, config_len);
474 config->config_str = config_str;
475 while (bytes_remaining) {
478 eol = memchr(p, '\n', bytes_remaining);
480 ERROR("Expected end-of-line in capture config file on "
481 "line %lu", line_no);
482 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
487 bytes_remaining -= (next_p - p);
491 if (*(eol - 1) == '\r')
495 /* Translate backslash to forward slash */
496 for (char *pp = p; pp != eol; pp++)
500 /* Remove drive letter */
501 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
505 if (strcmp(p, "[ExclusionList]") == 0)
506 type = EXCLUSION_LIST;
507 else if (strcmp(p, "[ExclusionException]") == 0)
508 type = EXCLUSION_EXCEPTION;
509 else if (strcmp(p, "[CompressionExclusionList]") == 0)
510 type = COMPRESSION_EXCLUSION_LIST;
511 else if (strcmp(p, "[AlignmentList]") == 0)
512 type = ALIGNMENT_LIST;
513 else if (p[0] == '[' && strrchr(p, ']')) {
514 ERROR("Unknown capture configuration section `%s'", p);
515 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
516 } else switch (type) {
518 DEBUG("Adding pattern \"%s\" to exclusion list", p);
519 ret = pattern_list_add_pattern(&config->exclusion_list, p);
521 case EXCLUSION_EXCEPTION:
522 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
523 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
525 case COMPRESSION_EXCLUSION_LIST:
526 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
527 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
530 DEBUG("Adding pattern \"%s\" to alignment list", p);
531 ret = pattern_list_add_pattern(&config->alignment_list, p);
534 ERROR("Line %lu of capture configuration is not "
535 "in a block (such as [ExclusionList])",
537 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
545 destroy_capture_config(config);
549 static int capture_config_set_prefix(struct capture_config *config,
552 char *prefix = STRDUP(_prefix);
555 return WIMLIB_ERR_NOMEM;
556 FREE(config->prefix);
557 config->prefix = prefix;
558 config->prefix_len = strlen(prefix);
562 static bool match_pattern(const char *path, const char *path_basename,
563 const struct pattern_list *list)
565 for (size_t i = 0; i < list->num_pats; i++) {
566 const char *pat = list->pats[i];
569 /* Absolute path from root of capture */
572 if (strchr(pat, '/'))
573 /* Relative path from root of capture */
576 /* A file name pattern */
577 string = path_basename;
579 if (fnmatch(pat, string, FNM_PATHNAME
585 DEBUG("`%s' matches the pattern \"%s\"",
593 /* Return true if the image capture configuration file indicates we should
594 * exclude the filename @path from capture.
596 * If @exclude_prefix is %true, the part of the path up and including the name
597 * of the directory being captured is not included in the path for matching
598 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
599 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
602 bool exclude_path(const char *path, const struct capture_config *config,
605 const char *basename = path_basename(path);
606 if (exclude_prefix) {
607 wimlib_assert(strlen(path) >= config->prefix_len);
608 if (memcmp(config->prefix, path, config->prefix_len) == 0
609 && path[config->prefix_len] == '/')
610 path += config->prefix_len;
612 return match_pattern(path, basename, &config->exclusion_list) &&
613 !match_pattern(path, basename, &config->exclusion_exception);
617 /* Strip leading and trailing forward slashes from a string. Modifies it in
618 * place and returns the stripped string. */
619 static const char *canonicalize_target_path(char *target_path)
622 if (target_path == NULL)
625 if (*target_path == '\0')
627 else if (*target_path == '/')
633 p = target_path + strlen(target_path) - 1;
639 /* Strip leading and trailing slashes from the target paths */
640 static void canonicalize_targets(struct wimlib_capture_source *sources,
643 while (num_sources--) {
644 DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
645 sources->fs_source_path,
646 sources->wim_target_path);
647 sources->wim_target_path =
648 (char*)canonicalize_target_path(sources->wim_target_path);
649 DEBUG("Canonical target: \"%s\"", sources->wim_target_path);
654 static int capture_source_cmp(const void *p1, const void *p2)
656 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
657 return strcmp(s1->wim_target_path, s2->wim_target_path);
660 /* Sorts the capture sources lexicographically by target path. This occurs
661 * after leading and trailing forward slashes are stripped.
663 * One purpose of this is to make sure that target paths that are inside other
664 * target paths are extracted after the containing target paths. */
665 static void sort_sources(struct wimlib_capture_source *sources,
668 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
671 static int check_sorted_sources(struct wimlib_capture_source *sources,
672 size_t num_sources, int add_image_flags)
674 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
675 if (num_sources != 1) {
676 ERROR("Must specify exactly 1 capture source "
677 "(the NTFS volume) in NTFS mode!");
678 return WIMLIB_ERR_INVALID_PARAM;
680 if (sources[0].wim_target_path[0] != '\0') {
681 ERROR("In NTFS capture mode the target path inside "
682 "the image must be the root directory!");
683 return WIMLIB_ERR_INVALID_PARAM;
685 } else if (num_sources != 0) {
686 /* This code is disabled because the current code
687 * unconditionally attempts to do overlays. So, duplicate
688 * target paths are OK. */
690 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
691 ERROR("Cannot specify root target when using multiple "
693 return WIMLIB_ERR_INVALID_PARAM;
695 for (size_t i = 0; i < num_sources - 1; i++) {
696 size_t len = strlen(sources[i].wim_target_path);
698 const char *target1 = sources[i].wim_target_path;
700 const char *target2 = sources[j].wim_target_path;
701 DEBUG("target1=%s, target2=%s",
703 if (strncmp(target1, target2, len) ||
706 if (target2[len] == '/') {
707 ERROR("Invalid target `%s': is a prefix of `%s'",
709 return WIMLIB_ERR_INVALID_PARAM;
711 if (target2[len] == '\0') {
712 ERROR("Invalid target `%s': is a duplicate of `%s'",
714 return WIMLIB_ERR_INVALID_PARAM;
716 } while (++j != num_sources);
724 /* Creates a new directory to place in the WIM image. This is to create parent
725 * directories that are not part of any target as needed. */
726 static struct wim_dentry *
727 new_filler_directory(const char *name)
729 struct wim_dentry *dentry;
730 DEBUG("Creating filler directory \"%s\"", name);
731 dentry = new_dentry_with_inode(name);
733 /* Set the inode number to 0 for now. The final inode number
734 * will be assigned later by assign_inode_numbers(). */
735 dentry->d_inode->i_ino = 0;
736 dentry->d_inode->i_resolved = 1;
737 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
742 /* Transfers the children of @branch to @target. It is an error if @target is
743 * not a directory or if both @branch and @target contain a child dentry with
745 static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
747 struct rb_root *rb_root;
749 if (!dentry_is_directory(target)) {
750 ERROR("Cannot overlay directory `%s' over non-directory",
751 branch->file_name_utf8);
752 return WIMLIB_ERR_INVALID_OVERLAY;
755 rb_root = &branch->d_inode->i_children;
756 while (rb_root->rb_node) { /* While @branch has children... */
757 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
758 /* Move @child to the directory @target */
759 unlink_dentry(child);
760 if (!dentry_add_child(target, child)) {
761 /* Revert the change to avoid leaking the directory tree
762 * rooted at @child */
763 dentry_add_child(branch, child);
764 ERROR("Overlay error: file `%s' already exists "
765 "as a child of `%s'",
766 child->file_name_utf8, target->file_name_utf8);
767 return WIMLIB_ERR_INVALID_OVERLAY;
774 /* Attach or overlay a branch onto the WIM image.
777 * Pointer to the root of the WIM image, or pointer to NULL if it has not
782 * Path in the WIM image to add the branch, with leading and trailing
785 static int attach_branch(struct wim_dentry **root_p,
786 struct wim_dentry *branch,
790 struct wim_dentry *dentry, *parent, *target;
792 if (*target_path == '\0') {
793 /* Target: root directory */
795 /* Overlay on existing root */
796 return do_overlay(*root_p, branch);
804 /* Adding a non-root branch. Create root if it hasn't been created
807 *root_p = new_filler_directory("");
809 return WIMLIB_ERR_NOMEM;
812 /* Walk the path to the branch, creating filler directories as needed.
815 while ((slash = strchr(target_path, '/'))) {
817 dentry = get_dentry_child_with_name(parent, target_path);
819 dentry = new_filler_directory(target_path);
821 return WIMLIB_ERR_NOMEM;
822 dentry_add_child(parent, dentry);
826 /* Skip over slashes. Note: this cannot overrun the length of
827 * the string because the last character cannot be a slash, as
828 * trailing slashes were tripped. */
831 } while (*target_path == '/');
834 /* If the target path already existed, overlay the branch onto it.
835 * Otherwise, set the branch as the target path. */
836 target = get_dentry_child_with_name(parent, branch->file_name_utf8);
838 return do_overlay(target, branch);
840 dentry_add_child(parent, branch);
845 WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
846 struct wimlib_capture_source *sources,
849 const char *config_str,
852 wimlib_progress_func_t progress_func)
854 int (*capture_tree)(struct wim_dentry **, const char *,
855 struct wim_lookup_table *,
856 struct wim_security_data *,
857 const struct capture_config *,
858 int, wimlib_progress_func_t, void *);
860 struct wim_dentry *root_dentry;
861 struct wim_dentry *branch;
862 struct wim_security_data *sd;
863 struct capture_config config;
864 struct wim_image_metadata *imd;
867 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
869 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
870 ERROR("Cannot dereference files when capturing directly from NTFS");
871 return WIMLIB_ERR_INVALID_PARAM;
873 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
874 ERROR("Capturing UNIX owner and mode not supported "
875 "when capturing directly from NTFS");
876 return WIMLIB_ERR_INVALID_PARAM;
878 capture_tree = build_dentry_tree_ntfs;
879 extra_arg = &w->ntfs_vol;
881 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
882 " cannot capture a WIM image directly from a NTFS volume!");
883 return WIMLIB_ERR_UNSUPPORTED;
886 capture_tree = build_dentry_tree;
890 if (!name || !*name) {
891 ERROR("Must specify a non-empty string for the image name");
892 return WIMLIB_ERR_INVALID_PARAM;
895 if (w->hdr.total_parts != 1) {
896 ERROR("Cannot add an image to a split WIM");
897 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
900 if (wimlib_image_name_in_use(w, name)) {
901 ERROR("There is already an image named \"%s\" in `%s'",
903 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
907 DEBUG("Using default capture configuration");
908 config_str = default_config;
909 config_len = strlen(default_config);
911 ret = init_capture_config(&config, config_str, config_len);
915 DEBUG("Allocating security data");
916 sd = CALLOC(1, sizeof(struct wim_security_data));
918 ret = WIMLIB_ERR_NOMEM;
919 goto out_destroy_capture_config;
921 sd->total_length = 8;
924 DEBUG("Using %zu capture sources", num_sources);
925 canonicalize_targets(sources, num_sources);
926 sort_sources(sources, num_sources);
927 ret = check_sorted_sources(sources, num_sources, add_image_flags);
929 ret = WIMLIB_ERR_INVALID_PARAM;
930 goto out_free_security_data;
933 DEBUG("Building dentry tree.");
934 if (num_sources == 0) {
935 root_dentry = new_filler_directory("");
937 goto out_free_security_data;
945 union wimlib_progress_info progress;
947 DEBUG("Building dentry tree for source %zu of %zu "
948 "(\"%s\" => \"%s\")", i + 1, num_sources,
949 sources[i].fs_source_path,
950 sources[i].wim_target_path);
952 memset(&progress, 0, sizeof(progress));
953 progress.scan.source = sources[i].fs_source_path;
954 progress.scan.wim_target_path = sources[i].wim_target_path;
955 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
957 ret = capture_config_set_prefix(&config,
958 sources[i].fs_source_path);
960 goto out_free_dentry_tree;
961 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
962 if (!*sources[i].wim_target_path)
963 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
964 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
968 progress_func, extra_arg);
970 ERROR("Failed to build dentry tree for `%s'",
971 sources[i].fs_source_path);
972 goto out_free_dentry_tree;
975 /* Use the target name, not the source name, for
976 * the root of each branch from a capture
977 * source. (This will also set the root dentry
978 * of the entire image to be unnamed.) */
979 ret = set_dentry_name(branch,
980 path_basename(sources[i].wim_target_path));
982 goto out_free_branch;
984 ret = attach_branch(&root_dentry, branch,
985 sources[i].wim_target_path);
987 goto out_free_branch;
990 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
991 } while (++i != num_sources);
994 DEBUG("Calculating full paths of dentries.");
995 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
997 goto out_free_dentry_tree;
999 ret = add_new_dentry_tree(w, root_dentry, sd);
1001 goto out_free_dentry_tree;
1003 imd = &w->image_metadata[w->hdr.image_count - 1];
1005 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1007 goto out_destroy_imd;
1009 DEBUG("Assigning hard link group IDs");
1010 assign_inode_numbers(&imd->inode_list);
1012 ret = xml_add_image(w, name);
1014 goto out_destroy_imd;
1016 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1017 wimlib_set_boot_idx(w, w->hdr.image_count);
1021 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1023 w->hdr.image_count--;
1026 free_dentry_tree(branch, w->lookup_table);
1027 out_free_dentry_tree:
1028 free_dentry_tree(root_dentry, w->lookup_table);
1029 out_free_security_data:
1030 free_security_data(sd);
1031 out_destroy_capture_config:
1032 destroy_capture_config(&config);
1037 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
1038 const char *name, const char *config_str,
1039 size_t config_len, int add_image_flags,
1040 wimlib_progress_func_t progress_func)
1042 if (!source || !*source)
1043 return WIMLIB_ERR_INVALID_PARAM;
1045 char *fs_source_path = STRDUP(source);
1047 struct wimlib_capture_source capture_src = {
1048 .fs_source_path = fs_source_path,
1049 .wim_target_path = NULL,
1052 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1053 config_str, config_len,
1054 add_image_flags, progress_func);
1055 FREE(fs_source_path);