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"
51 * Adds the dentry tree and security data for a new image to the image metadata
52 * array of the WIMStruct.
54 int add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
55 struct wim_security_data *sd)
57 struct wim_lookup_table_entry *metadata_lte;
58 struct wim_image_metadata *imd;
59 struct wim_image_metadata *new_imd;
61 wimlib_assert(root_dentry != NULL);
63 DEBUG("Reallocating image metadata array for image_count = %u",
64 w->hdr.image_count + 1);
65 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
68 ERROR("Failed to allocate memory for new image metadata array");
72 memcpy(imd, w->image_metadata,
73 w->hdr.image_count * sizeof(struct wim_image_metadata));
75 metadata_lte = new_lookup_table_entry();
79 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
80 random_hash(metadata_lte->hash);
81 lookup_table_insert(w->lookup_table, metadata_lte);
83 new_imd = &imd[w->hdr.image_count];
85 new_imd->root_dentry = root_dentry;
86 new_imd->metadata_lte = metadata_lte;
87 new_imd->security_data = sd;
88 new_imd->modified = 1;
90 FREE(w->image_metadata);
91 w->image_metadata = imd;
97 return WIMLIB_ERR_NOMEM;
103 * build_dentry_tree():
104 * Recursively builds a tree of WIM dentries from an on-disk directory
107 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
108 * modified if successful. Set to NULL if the file or directory was
109 * excluded from capture.
111 * @root_disk_path: The path to the root of the directory tree on disk (UTF-8).
113 * @lookup_table: The lookup table for the WIM file. For each file added to the
114 * dentry tree being built, an entry is added to the lookup table,
115 * unless an identical stream is already in the lookup table.
116 * These lookup table entries that are added point to the path of
119 * @sd: Ignored. (Security data only captured in NTFS mode.)
122 * Configuration for files to be excluded from capture.
124 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
126 * @extra_arg: Ignored in UNIX builds; used to pass sd_set pointer in Windows
129 * @return: 0 on success, nonzero on failure. It is a failure if any of
130 * the files cannot be `stat'ed, or if any of the needed
131 * directories cannot be opened or read. Failure to add the files
132 * to the WIM may still occur later when trying to actually read
133 * the on-disk files during a call to wimlib_write() or
134 * wimlib_overwrite().
136 static int unix_build_dentry_tree(struct wim_dentry **root_ret,
137 const char *root_disk_path,
138 struct wim_lookup_table *lookup_table,
139 struct wim_security_data *sd,
140 const struct capture_config *config,
142 wimlib_progress_func_t progress_func,
145 struct wim_dentry *root = NULL;
147 struct wim_inode *inode;
149 if (exclude_path(root_disk_path, config, true)) {
150 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
151 ERROR("Cannot exclude the root directory from capture");
152 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
155 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
158 union wimlib_progress_info info;
159 info.scan.cur_path = root_disk_path;
160 info.scan.excluded = true;
161 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
166 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
169 union wimlib_progress_info info;
170 info.scan.cur_path = root_disk_path;
171 info.scan.excluded = false;
172 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
175 /* UNIX version of capturing a directory tree */
176 struct stat root_stbuf;
177 int (*stat_fn)(const char *restrict, struct stat *restrict);
178 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
183 ret = (*stat_fn)(root_disk_path, &root_stbuf);
185 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
189 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
190 !S_ISDIR(root_stbuf.st_mode))
192 /* Do a dereference-stat in case the root is a symbolic link.
193 * This case is allowed, provided that the symbolic link points
195 ret = stat(root_disk_path, &root_stbuf);
197 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
198 ret = WIMLIB_ERR_STAT;
201 if (!S_ISDIR(root_stbuf.st_mode)) {
202 ERROR("`%s' is not a directory", root_disk_path);
203 ret = WIMLIB_ERR_NOTDIR;
207 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
208 && !S_ISLNK(root_stbuf.st_mode)) {
209 ERROR("`%s' is not a regular file, directory, or symbolic link.",
211 ret = WIMLIB_ERR_SPECIAL_FILE;
215 root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
218 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
219 else if (errno == ENOMEM)
220 ret = WIMLIB_ERR_NOMEM;
222 ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
226 inode = root->d_inode;
228 #ifdef HAVE_STAT_NANOSECOND_PRECISION
229 inode->i_creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
230 inode->i_last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
231 inode->i_last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
233 inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
234 inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
235 inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
237 /* Leave the inode number at 0 for directories. */
238 if (!S_ISDIR(root_stbuf.st_mode)) {
239 if (sizeof(ino_t) >= 8)
240 inode->i_ino = (u64)root_stbuf.st_ino;
242 inode->i_ino = (u64)root_stbuf.st_ino |
243 ((u64)root_stbuf.st_dev <<
244 ((sizeof(ino_t) * 8) & 63));
246 inode->i_resolved = 1;
247 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
248 ret = inode_set_unix_data(inode, root_stbuf.st_uid,
252 UNIX_DATA_ALL | UNIX_DATA_CREATE);
256 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
257 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
259 struct wim_lookup_table_entry *lte;
260 u8 hash[SHA1_HASH_SIZE];
262 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
264 /* Empty files do not have to have a lookup table entry. */
265 if (root_stbuf.st_size == 0)
268 /* For each regular file, we must check to see if the file is in
269 * the lookup table already; if it is, we increment its refcnt;
270 * otherwise, we create a new lookup table entry and insert it.
273 ret = sha1sum(root_disk_path, hash);
277 lte = __lookup_resource(lookup_table, hash);
280 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
283 char *file_on_disk = STRDUP(root_disk_path);
285 ERROR("Failed to allocate memory for file path");
286 ret = WIMLIB_ERR_NOMEM;
289 lte = new_lookup_table_entry();
292 ret = WIMLIB_ERR_NOMEM;
295 lte->file_on_disk = file_on_disk;
296 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
297 lte->resource_entry.original_size = root_stbuf.st_size;
298 lte->resource_entry.size = root_stbuf.st_size;
299 copy_hash(lte->hash, hash);
300 lookup_table_insert(lookup_table, lte);
302 root->d_inode->i_lte = lte;
303 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
305 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
308 struct dirent entry, *result;
309 struct wim_dentry *child;
311 dir = opendir(root_disk_path);
313 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
315 ret = WIMLIB_ERR_OPEN;
319 /* Buffer for names of files in directory. */
320 size_t len = strlen(root_disk_path);
321 char name[len + 1 + FILENAME_MAX + 1];
322 memcpy(name, root_disk_path, len);
325 /* Create a dentry for each entry in the directory on disk, and recurse
326 * to any subdirectories. */
329 ret = readdir_r(dir, &entry, &result);
331 ret = WIMLIB_ERR_READ;
332 ERROR_WITH_ERRNO("Error reading the "
339 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
340 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
342 strcpy(name + len + 1, result->d_name);
343 ret = unix_build_dentry_tree(&child, name,
347 progress_func, NULL);
351 dentry_add_child(root, child);
354 } else { /* Archiving a symbolic link */
355 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
356 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
358 /* The idea here is to call readlink() to get the UNIX target of
359 * the symbolic link, then turn the target into a reparse point
360 * data buffer that contains a relative or absolute symbolic
361 * link (NOT a junction point or *full* path symbolic link with
365 char deref_name_buf[4096];
366 ssize_t deref_name_len;
368 deref_name_len = readlink(root_disk_path, deref_name_buf,
369 sizeof(deref_name_buf) - 1);
370 if (deref_name_len >= 0) {
371 deref_name_buf[deref_name_len] = '\0';
372 DEBUG("Read symlink `%s'", deref_name_buf);
373 ret = inode_set_symlink(root->d_inode, deref_name_buf,
377 * Unfortunately, Windows seems to have the
378 * concept of "file" symbolic links as being
379 * different from "directory" symbolic links...
380 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
381 * on the symbolic link if the *target* of the
382 * symbolic link is a directory.
385 if (stat(root_disk_path, &stbuf) == 0 &&
386 S_ISDIR(stbuf.st_mode))
388 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
392 ERROR_WITH_ERRNO("Failed to read target of "
393 "symbolic link `%s'", root_disk_path);
394 ret = WIMLIB_ERR_READLINK;
401 free_dentry_tree(root, lookup_table);
404 #endif /* !__WIN32__ */
410 COMPRESSION_EXCLUSION_LIST,
414 #define COMPAT_DEFAULT_CONFIG
416 /* Default capture configuration file when none is specified. */
417 static const char *default_config =
418 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
419 users. The next ABI-incompatible library
420 version will default to the empty string here. */
425 "\\System Volume Information\n"
429 "[CompressionExclusionList]\n"
433 "\\WINDOWS\\inf\\*.pnf\n";
438 static void destroy_pattern_list(struct pattern_list *list)
443 static void destroy_capture_config(struct capture_config *config)
445 destroy_pattern_list(&config->exclusion_list);
446 destroy_pattern_list(&config->exclusion_exception);
447 destroy_pattern_list(&config->compression_exclusion_list);
448 destroy_pattern_list(&config->alignment_list);
449 FREE(config->config_str);
450 FREE(config->prefix);
451 memset(config, 0, sizeof(*config));
454 static int pattern_list_add_pattern(struct pattern_list *list,
458 if (list->num_pats >= list->num_allocated_pats) {
459 pats = REALLOC(list->pats,
460 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
462 return WIMLIB_ERR_NOMEM;
463 list->num_allocated_pats += 8;
466 list->pats[list->num_pats++] = pattern;
470 /* Parses the contents of the image capture configuration file and fills in a
471 * `struct capture_config'. */
472 static int init_capture_config(struct capture_config *config,
473 const char *_config_str, size_t config_len)
479 size_t bytes_remaining;
480 enum pattern_type type = NONE;
482 unsigned long line_no = 0;
484 DEBUG("config_len = %zu", config_len);
485 bytes_remaining = config_len;
486 memset(config, 0, sizeof(*config));
487 config_str = MALLOC(config_len);
489 ERROR("Could not duplicate capture config string");
490 return WIMLIB_ERR_NOMEM;
493 memcpy(config_str, _config_str, config_len);
495 config->config_str = config_str;
496 while (bytes_remaining) {
499 eol = memchr(p, '\n', bytes_remaining);
501 ERROR("Expected end-of-line in capture config file on "
502 "line %lu", line_no);
503 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
508 bytes_remaining -= (next_p - p);
512 if (*(eol - 1) == '\r')
516 /* Translate backslash to forward slash */
517 for (char *pp = p; pp != eol; pp++)
521 /* Remove drive letter */
522 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
526 if (strcmp(p, "[ExclusionList]") == 0)
527 type = EXCLUSION_LIST;
528 else if (strcmp(p, "[ExclusionException]") == 0)
529 type = EXCLUSION_EXCEPTION;
530 else if (strcmp(p, "[CompressionExclusionList]") == 0)
531 type = COMPRESSION_EXCLUSION_LIST;
532 else if (strcmp(p, "[AlignmentList]") == 0)
533 type = ALIGNMENT_LIST;
534 else if (p[0] == '[' && strrchr(p, ']')) {
535 ERROR("Unknown capture configuration section `%s'", p);
536 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
537 } else switch (type) {
539 DEBUG("Adding pattern \"%s\" to exclusion list", p);
540 ret = pattern_list_add_pattern(&config->exclusion_list, p);
542 case EXCLUSION_EXCEPTION:
543 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
544 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
546 case COMPRESSION_EXCLUSION_LIST:
547 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
548 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
551 DEBUG("Adding pattern \"%s\" to alignment list", p);
552 ret = pattern_list_add_pattern(&config->alignment_list, p);
555 ERROR("Line %lu of capture configuration is not "
556 "in a block (such as [ExclusionList])",
558 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
566 destroy_capture_config(config);
570 static int capture_config_set_prefix(struct capture_config *config,
573 char *prefix = STRDUP(_prefix);
576 return WIMLIB_ERR_NOMEM;
577 FREE(config->prefix);
578 config->prefix = prefix;
579 config->prefix_len = strlen(prefix);
583 static bool match_pattern(const char *path, const char *path_basename,
584 const struct pattern_list *list)
586 for (size_t i = 0; i < list->num_pats; i++) {
587 const char *pat = list->pats[i];
590 /* Absolute path from root of capture */
593 if (strchr(pat, '/'))
594 /* Relative path from root of capture */
597 /* A file name pattern */
598 string = path_basename;
601 if (fnmatch(pat, string, FNM_PATHNAME
607 DEBUG("`%s' matches the pattern \"%s\"",
615 /* Return true if the image capture configuration file indicates we should
616 * exclude the filename @path from capture.
618 * If @exclude_prefix is %true, the part of the path up and including the name
619 * of the directory being captured is not included in the path for matching
620 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
621 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
624 bool exclude_path(const char *path, const struct capture_config *config,
627 const char *basename = path_basename(path);
628 if (exclude_prefix) {
629 wimlib_assert(strlen(path) >= config->prefix_len);
630 if (memcmp(config->prefix, path, config->prefix_len) == 0
631 && path[config->prefix_len] == '/')
632 path += config->prefix_len;
634 return match_pattern(path, basename, &config->exclusion_list) &&
635 !match_pattern(path, basename, &config->exclusion_exception);
639 /* Strip leading and trailing forward slashes from a string. Modifies it in
640 * place and returns the stripped string. */
641 static const char *canonicalize_target_path(char *target_path)
644 if (target_path == NULL)
647 if (*target_path == '\0')
649 else if (*target_path == '/')
655 p = target_path + strlen(target_path) - 1;
662 static void zap_backslashes(char *s)
672 /* Strip leading and trailing slashes from the target paths */
673 static void canonicalize_targets(struct wimlib_capture_source *sources,
676 while (num_sources--) {
677 DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
678 sources->fs_source_path,
679 sources->wim_target_path);
681 /* The Windows API can handle forward slashes. Just get rid of
682 * backslashes to avoid confusing other parts of the library
684 zap_backslashes(sources->fs_source_path);
685 if (sources->wim_target_path)
686 zap_backslashes(sources->wim_target_path);
688 sources->wim_target_path =
689 (char*)canonicalize_target_path(sources->wim_target_path);
690 DEBUG("Canonical target: \"%s\"", sources->wim_target_path);
695 static int capture_source_cmp(const void *p1, const void *p2)
697 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
698 return strcmp(s1->wim_target_path, s2->wim_target_path);
701 /* Sorts the capture sources lexicographically by target path. This occurs
702 * after leading and trailing forward slashes are stripped.
704 * One purpose of this is to make sure that target paths that are inside other
705 * target paths are added after the containing target paths. */
706 static void sort_sources(struct wimlib_capture_source *sources,
709 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
712 static int check_sorted_sources(struct wimlib_capture_source *sources,
713 size_t num_sources, int add_image_flags)
715 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
716 if (num_sources != 1) {
717 ERROR("Must specify exactly 1 capture source "
718 "(the NTFS volume) in NTFS mode!");
719 return WIMLIB_ERR_INVALID_PARAM;
721 if (sources[0].wim_target_path[0] != '\0') {
722 ERROR("In NTFS capture mode the target path inside "
723 "the image must be the root directory!");
724 return WIMLIB_ERR_INVALID_PARAM;
726 } else if (num_sources != 0) {
727 /* This code is disabled because the current code
728 * unconditionally attempts to do overlays. So, duplicate
729 * target paths are OK. */
731 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
732 ERROR("Cannot specify root target when using multiple "
734 return WIMLIB_ERR_INVALID_PARAM;
736 for (size_t i = 0; i < num_sources - 1; i++) {
737 size_t len = strlen(sources[i].wim_target_path);
739 const char *target1 = sources[i].wim_target_path;
741 const char *target2 = sources[j].wim_target_path;
742 DEBUG("target1=%s, target2=%s",
744 if (strncmp(target1, target2, len) ||
747 if (target2[len] == '/') {
748 ERROR("Invalid target `%s': is a prefix of `%s'",
750 return WIMLIB_ERR_INVALID_PARAM;
752 if (target2[len] == '\0') {
753 ERROR("Invalid target `%s': is a duplicate of `%s'",
755 return WIMLIB_ERR_INVALID_PARAM;
757 } while (++j != num_sources);
765 /* Creates a new directory to place in the WIM image. This is to create parent
766 * directories that are not part of any target as needed. */
767 static struct wim_dentry *
768 new_filler_directory(const char *name)
770 struct wim_dentry *dentry;
771 DEBUG("Creating filler directory \"%s\"", name);
772 dentry = new_dentry_with_inode(name);
774 /* Leave the inode number as 0 for now. The final inode number
775 * will be assigned later by assign_inode_numbers(). */
776 dentry->d_inode->i_resolved = 1;
777 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
782 /* Transfers the children of @branch to @target. It is an error if @target is
783 * not a directory or if both @branch and @target contain a child dentry with
785 static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
787 struct rb_root *rb_root;
789 DEBUG("Doing overlay %s => %s",
790 branch->file_name_utf8, target->file_name_utf8);
792 if (!dentry_is_directory(target)) {
793 ERROR("Cannot overlay directory `%s' over non-directory",
794 branch->file_name_utf8);
795 return WIMLIB_ERR_INVALID_OVERLAY;
798 rb_root = &branch->d_inode->i_children;
799 while (rb_root->rb_node) { /* While @branch has children... */
800 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
801 /* Move @child to the directory @target */
802 unlink_dentry(child);
803 if (!dentry_add_child(target, child)) {
804 /* Revert the change to avoid leaking the directory tree
805 * rooted at @child */
806 dentry_add_child(branch, child);
807 ERROR("Overlay error: file `%s' already exists "
808 "as a child of `%s'",
809 child->file_name_utf8, target->file_name_utf8);
810 return WIMLIB_ERR_INVALID_OVERLAY;
818 /* Attach or overlay a branch onto the WIM image.
821 * Pointer to the root of the WIM image, or pointer to NULL if it has not
826 * Path in the WIM image to add the branch, with leading and trailing
829 static int attach_branch(struct wim_dentry **root_p,
830 struct wim_dentry *branch,
834 struct wim_dentry *dentry, *parent, *target;
836 DEBUG("Attaching branch \"%s\" => \"%s\"",
837 branch->file_name_utf8, target_path);
839 if (*target_path == '\0') {
840 /* Target: root directory */
842 /* Overlay on existing root */
843 return do_overlay(*root_p, branch);
851 /* Adding a non-root branch. Create root if it hasn't been created
854 *root_p = new_filler_directory("");
856 return WIMLIB_ERR_NOMEM;
859 /* Walk the path to the branch, creating filler directories as needed.
862 while ((slash = strchr(target_path, '/'))) {
864 dentry = get_dentry_child_with_name(parent, target_path);
866 dentry = new_filler_directory(target_path);
868 return WIMLIB_ERR_NOMEM;
869 dentry_add_child(parent, dentry);
873 /* Skip over slashes. Note: this cannot overrun the length of
874 * the string because the last character cannot be a slash, as
875 * trailing slashes were tripped. */
878 } while (*target_path == '/');
881 /* If the target path already existed, overlay the branch onto it.
882 * Otherwise, set the branch as the target path. */
883 target = get_dentry_child_with_name(parent, branch->file_name_utf8);
885 return do_overlay(target, branch);
887 dentry_add_child(parent, branch);
892 WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
893 struct wimlib_capture_source *sources,
896 const char *config_str,
899 wimlib_progress_func_t progress_func)
901 int (*capture_tree)(struct wim_dentry **, const char *,
902 struct wim_lookup_table *,
903 struct wim_security_data *,
904 const struct capture_config *,
905 int, wimlib_progress_func_t, void *);
907 struct wim_dentry *root_dentry;
908 struct wim_dentry *branch;
909 struct wim_security_data *sd;
910 struct capture_config config;
911 struct wim_image_metadata *imd;
914 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
916 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
917 ERROR("Cannot dereference files when capturing directly from NTFS");
918 return WIMLIB_ERR_INVALID_PARAM;
920 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
921 ERROR("Capturing UNIX owner and mode not supported "
922 "when capturing directly from NTFS");
923 return WIMLIB_ERR_INVALID_PARAM;
925 capture_tree = build_dentry_tree_ntfs;
926 extra_arg = &w->ntfs_vol;
928 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
929 " cannot capture a WIM image directly from a NTFS volume!");
930 return WIMLIB_ERR_UNSUPPORTED;
934 capture_tree = win32_build_dentry_tree;
936 capture_tree = unix_build_dentry_tree;
942 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
943 ERROR("Capturing UNIX-specific data is not supported on Windows");
944 return WIMLIB_ERR_INVALID_PARAM;
946 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
947 ERROR("Dereferencing symbolic links is not supported on Windows");
948 return WIMLIB_ERR_INVALID_PARAM;
952 if (!name || !*name) {
953 ERROR("Must specify a non-empty string for the image name");
954 return WIMLIB_ERR_INVALID_PARAM;
957 if (w->hdr.total_parts != 1) {
958 ERROR("Cannot add an image to a split WIM");
959 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
962 if (wimlib_image_name_in_use(w, name)) {
963 ERROR("There is already an image named \"%s\" in `%s'",
965 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
969 DEBUG("Using default capture configuration");
970 config_str = default_config;
971 config_len = strlen(default_config);
973 ret = init_capture_config(&config, config_str, config_len);
977 DEBUG("Allocating security data");
978 sd = CALLOC(1, sizeof(struct wim_security_data));
980 ret = WIMLIB_ERR_NOMEM;
981 goto out_destroy_capture_config;
983 sd->total_length = 8;
986 DEBUG("Using %zu capture sources", num_sources);
987 canonicalize_targets(sources, num_sources);
988 sort_sources(sources, num_sources);
989 ret = check_sorted_sources(sources, num_sources, add_image_flags);
991 ret = WIMLIB_ERR_INVALID_PARAM;
992 goto out_free_security_data;
995 DEBUG("Building dentry tree.");
996 if (num_sources == 0) {
997 root_dentry = new_filler_directory("");
999 ret = WIMLIB_ERR_NOMEM;
1000 goto out_free_security_data;
1006 win32_acquire_capture_privileges();
1013 union wimlib_progress_info progress;
1015 DEBUG("Building dentry tree for source %zu of %zu "
1016 "(\"%s\" => \"%s\")", i + 1, num_sources,
1017 sources[i].fs_source_path,
1018 sources[i].wim_target_path);
1019 if (progress_func) {
1020 memset(&progress, 0, sizeof(progress));
1021 progress.scan.source = sources[i].fs_source_path;
1022 progress.scan.wim_target_path = sources[i].wim_target_path;
1023 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1025 ret = capture_config_set_prefix(&config,
1026 sources[i].fs_source_path);
1028 goto out_free_dentry_tree;
1029 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
1030 if (!*sources[i].wim_target_path)
1031 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
1032 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
1033 w->lookup_table, sd,
1036 progress_func, extra_arg);
1038 ERROR("Failed to build dentry tree for `%s'",
1039 sources[i].fs_source_path);
1040 goto out_free_dentry_tree;
1043 /* Use the target name, not the source name, for
1044 * the root of each branch from a capture
1045 * source. (This will also set the root dentry
1046 * of the entire image to be unnamed.) */
1047 ret = set_dentry_name(branch,
1048 path_basename(sources[i].wim_target_path));
1050 goto out_free_branch;
1052 ret = attach_branch(&root_dentry, branch,
1053 sources[i].wim_target_path);
1055 goto out_free_branch;
1058 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1059 } while (++i != num_sources);
1062 DEBUG("Calculating full paths of dentries.");
1063 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1065 goto out_free_dentry_tree;
1067 ret = add_new_dentry_tree(w, root_dentry, sd);
1069 goto out_free_dentry_tree;
1071 imd = &w->image_metadata[w->hdr.image_count - 1];
1073 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1075 goto out_destroy_imd;
1077 DEBUG("Assigning hard link group IDs");
1078 assign_inode_numbers(&imd->inode_list);
1080 ret = xml_add_image(w, name);
1082 goto out_destroy_imd;
1084 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1085 wimlib_set_boot_idx(w, w->hdr.image_count);
1087 goto out_destroy_capture_config;
1089 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1091 w->hdr.image_count--;
1094 free_dentry_tree(branch, w->lookup_table);
1095 out_free_dentry_tree:
1096 free_dentry_tree(root_dentry, w->lookup_table);
1097 out_free_security_data:
1098 free_security_data(sd);
1099 out_destroy_capture_config:
1100 destroy_capture_config(&config);
1103 win32_release_capture_privileges();
1108 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
1109 const char *name, const char *config_str,
1110 size_t config_len, int add_image_flags,
1111 wimlib_progress_func_t progress_func)
1113 if (!source || !*source)
1114 return WIMLIB_ERR_INVALID_PARAM;
1116 char *fs_source_path = STRDUP(source);
1118 struct wimlib_capture_source capture_src = {
1119 .fs_source_path = fs_source_path,
1120 .wim_target_path = NULL,
1123 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1124 config_str, config_len,
1125 add_image_flags, progress_func);
1126 FREE(fs_source_path);