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 /* Warning: on Windows native builds, fnmatch() calls the
602 * replacement function in win32.c. */
603 if (fnmatch(pat, string, FNM_PATHNAME
609 DEBUG("`%s' matches the pattern \"%s\"",
617 /* Return true if the image capture configuration file indicates we should
618 * exclude the filename @path from capture.
620 * If @exclude_prefix is %true, the part of the path up and including the name
621 * of the directory being captured is not included in the path for matching
622 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
623 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
626 bool exclude_path(const char *path, const struct capture_config *config,
629 const char *basename = path_basename(path);
630 if (exclude_prefix) {
631 wimlib_assert(strlen(path) >= config->prefix_len);
632 if (memcmp(config->prefix, path, config->prefix_len) == 0
633 && path[config->prefix_len] == '/')
634 path += config->prefix_len;
636 return match_pattern(path, basename, &config->exclusion_list) &&
637 !match_pattern(path, basename, &config->exclusion_exception);
641 /* Strip leading and trailing forward slashes from a string. Modifies it in
642 * place and returns the stripped string. */
643 static const char *canonicalize_target_path(char *target_path)
646 if (target_path == NULL)
649 if (*target_path == '\0')
651 else if (*target_path == '/')
657 p = target_path + strlen(target_path) - 1;
664 static void zap_backslashes(char *s)
674 /* Strip leading and trailing slashes from the target paths */
675 static void canonicalize_targets(struct wimlib_capture_source *sources,
678 while (num_sources--) {
679 DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
680 sources->fs_source_path,
681 sources->wim_target_path);
683 /* The Windows API can handle forward slashes. Just get rid of
684 * backslashes to avoid confusing other parts of the library
686 zap_backslashes(sources->fs_source_path);
687 if (sources->wim_target_path)
688 zap_backslashes(sources->wim_target_path);
690 sources->wim_target_path =
691 (char*)canonicalize_target_path(sources->wim_target_path);
692 DEBUG("Canonical target: \"%s\"", sources->wim_target_path);
697 static int capture_source_cmp(const void *p1, const void *p2)
699 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
700 return strcmp(s1->wim_target_path, s2->wim_target_path);
703 /* Sorts the capture sources lexicographically by target path. This occurs
704 * after leading and trailing forward slashes are stripped.
706 * One purpose of this is to make sure that target paths that are inside other
707 * target paths are added after the containing target paths. */
708 static void sort_sources(struct wimlib_capture_source *sources,
711 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
714 static int check_sorted_sources(struct wimlib_capture_source *sources,
715 size_t num_sources, int add_image_flags)
717 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
718 if (num_sources != 1) {
719 ERROR("Must specify exactly 1 capture source "
720 "(the NTFS volume) in NTFS mode!");
721 return WIMLIB_ERR_INVALID_PARAM;
723 if (sources[0].wim_target_path[0] != '\0') {
724 ERROR("In NTFS capture mode the target path inside "
725 "the image must be the root directory!");
726 return WIMLIB_ERR_INVALID_PARAM;
728 } else if (num_sources != 0) {
729 /* This code is disabled because the current code
730 * unconditionally attempts to do overlays. So, duplicate
731 * target paths are OK. */
733 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
734 ERROR("Cannot specify root target when using multiple "
736 return WIMLIB_ERR_INVALID_PARAM;
738 for (size_t i = 0; i < num_sources - 1; i++) {
739 size_t len = strlen(sources[i].wim_target_path);
741 const char *target1 = sources[i].wim_target_path;
743 const char *target2 = sources[j].wim_target_path;
744 DEBUG("target1=%s, target2=%s",
746 if (strncmp(target1, target2, len) ||
749 if (target2[len] == '/') {
750 ERROR("Invalid target `%s': is a prefix of `%s'",
752 return WIMLIB_ERR_INVALID_PARAM;
754 if (target2[len] == '\0') {
755 ERROR("Invalid target `%s': is a duplicate of `%s'",
757 return WIMLIB_ERR_INVALID_PARAM;
759 } while (++j != num_sources);
767 /* Creates a new directory to place in the WIM image. This is to create parent
768 * directories that are not part of any target as needed. */
769 static struct wim_dentry *
770 new_filler_directory(const char *name)
772 struct wim_dentry *dentry;
773 DEBUG("Creating filler directory \"%s\"", name);
774 dentry = new_dentry_with_inode(name);
776 /* Leave the inode number as 0 for now. The final inode number
777 * will be assigned later by assign_inode_numbers(). */
778 dentry->d_inode->i_resolved = 1;
779 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
784 /* Transfers the children of @branch to @target. It is an error if @target is
785 * not a directory or if both @branch and @target contain a child dentry with
787 static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
789 struct rb_root *rb_root;
791 DEBUG("Doing overlay %s => %s",
792 branch->file_name_utf8, target->file_name_utf8);
794 if (!dentry_is_directory(target)) {
795 ERROR("Cannot overlay directory `%s' over non-directory",
796 branch->file_name_utf8);
797 return WIMLIB_ERR_INVALID_OVERLAY;
800 rb_root = &branch->d_inode->i_children;
801 while (rb_root->rb_node) { /* While @branch has children... */
802 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
803 /* Move @child to the directory @target */
804 unlink_dentry(child);
805 if (!dentry_add_child(target, child)) {
806 /* Revert the change to avoid leaking the directory tree
807 * rooted at @child */
808 dentry_add_child(branch, child);
809 ERROR("Overlay error: file `%s' already exists "
810 "as a child of `%s'",
811 child->file_name_utf8, target->file_name_utf8);
812 return WIMLIB_ERR_INVALID_OVERLAY;
820 /* Attach or overlay a branch onto the WIM image.
823 * Pointer to the root of the WIM image, or pointer to NULL if it has not
828 * Path in the WIM image to add the branch, with leading and trailing
831 static int attach_branch(struct wim_dentry **root_p,
832 struct wim_dentry *branch,
836 struct wim_dentry *dentry, *parent, *target;
838 DEBUG("Attaching branch \"%s\" => \"%s\"",
839 branch->file_name_utf8, target_path);
841 if (*target_path == '\0') {
842 /* Target: root directory */
844 /* Overlay on existing root */
845 return do_overlay(*root_p, branch);
853 /* Adding a non-root branch. Create root if it hasn't been created
856 *root_p = new_filler_directory("");
858 return WIMLIB_ERR_NOMEM;
861 /* Walk the path to the branch, creating filler directories as needed.
864 while ((slash = strchr(target_path, '/'))) {
866 dentry = get_dentry_child_with_name(parent, target_path);
868 dentry = new_filler_directory(target_path);
870 return WIMLIB_ERR_NOMEM;
871 dentry_add_child(parent, dentry);
875 /* Skip over slashes. Note: this cannot overrun the length of
876 * the string because the last character cannot be a slash, as
877 * trailing slashes were tripped. */
880 } while (*target_path == '/');
883 /* If the target path already existed, overlay the branch onto it.
884 * Otherwise, set the branch as the target path. */
885 target = get_dentry_child_with_name(parent, branch->file_name_utf8);
887 return do_overlay(target, branch);
889 dentry_add_child(parent, branch);
894 WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
895 struct wimlib_capture_source *sources,
898 const char *config_str,
901 wimlib_progress_func_t progress_func)
903 int (*capture_tree)(struct wim_dentry **, const char *,
904 struct wim_lookup_table *,
905 struct wim_security_data *,
906 const struct capture_config *,
907 int, wimlib_progress_func_t, void *);
909 struct wim_dentry *root_dentry;
910 struct wim_dentry *branch;
911 struct wim_security_data *sd;
912 struct capture_config config;
913 struct wim_image_metadata *imd;
916 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
918 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
919 ERROR("Cannot dereference files when capturing directly from NTFS");
920 return WIMLIB_ERR_INVALID_PARAM;
922 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
923 ERROR("Capturing UNIX owner and mode not supported "
924 "when capturing directly from NTFS");
925 return WIMLIB_ERR_INVALID_PARAM;
927 capture_tree = build_dentry_tree_ntfs;
928 extra_arg = &w->ntfs_vol;
930 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
931 " cannot capture a WIM image directly from a NTFS volume!");
932 return WIMLIB_ERR_UNSUPPORTED;
936 capture_tree = win32_build_dentry_tree;
938 capture_tree = unix_build_dentry_tree;
944 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
945 ERROR("Capturing UNIX-specific data is not supported on Windows");
946 return WIMLIB_ERR_INVALID_PARAM;
948 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
949 ERROR("Dereferencing symbolic links is not supported on Windows");
950 return WIMLIB_ERR_INVALID_PARAM;
954 if (!name || !*name) {
955 ERROR("Must specify a non-empty string for the image name");
956 return WIMLIB_ERR_INVALID_PARAM;
959 if (w->hdr.total_parts != 1) {
960 ERROR("Cannot add an image to a split WIM");
961 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
964 if (wimlib_image_name_in_use(w, name)) {
965 ERROR("There is already an image named \"%s\" in `%s'",
967 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
971 DEBUG("Using default capture configuration");
972 config_str = default_config;
973 config_len = strlen(default_config);
975 ret = init_capture_config(&config, config_str, config_len);
979 DEBUG("Allocating security data");
980 sd = CALLOC(1, sizeof(struct wim_security_data));
982 ret = WIMLIB_ERR_NOMEM;
983 goto out_destroy_capture_config;
985 sd->total_length = 8;
988 DEBUG("Using %zu capture sources", num_sources);
989 canonicalize_targets(sources, num_sources);
990 sort_sources(sources, num_sources);
991 ret = check_sorted_sources(sources, num_sources, add_image_flags);
993 ret = WIMLIB_ERR_INVALID_PARAM;
994 goto out_free_security_data;
997 DEBUG("Building dentry tree.");
998 if (num_sources == 0) {
999 root_dentry = new_filler_directory("");
1001 ret = WIMLIB_ERR_NOMEM;
1002 goto out_free_security_data;
1008 win32_acquire_capture_privileges();
1015 union wimlib_progress_info progress;
1017 DEBUG("Building dentry tree for source %zu of %zu "
1018 "(\"%s\" => \"%s\")", i + 1, num_sources,
1019 sources[i].fs_source_path,
1020 sources[i].wim_target_path);
1021 if (progress_func) {
1022 memset(&progress, 0, sizeof(progress));
1023 progress.scan.source = sources[i].fs_source_path;
1024 progress.scan.wim_target_path = sources[i].wim_target_path;
1025 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1027 ret = capture_config_set_prefix(&config,
1028 sources[i].fs_source_path);
1030 goto out_free_dentry_tree;
1031 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
1032 if (!*sources[i].wim_target_path)
1033 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
1034 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
1035 w->lookup_table, sd,
1038 progress_func, extra_arg);
1040 ERROR("Failed to build dentry tree for `%s'",
1041 sources[i].fs_source_path);
1042 goto out_free_dentry_tree;
1045 /* Use the target name, not the source name, for
1046 * the root of each branch from a capture
1047 * source. (This will also set the root dentry
1048 * of the entire image to be unnamed.) */
1049 ret = set_dentry_name(branch,
1050 path_basename(sources[i].wim_target_path));
1052 goto out_free_branch;
1054 ret = attach_branch(&root_dentry, branch,
1055 sources[i].wim_target_path);
1057 goto out_free_branch;
1060 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1061 } while (++i != num_sources);
1064 DEBUG("Calculating full paths of dentries.");
1065 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1067 goto out_free_dentry_tree;
1069 ret = add_new_dentry_tree(w, root_dentry, sd);
1071 goto out_free_dentry_tree;
1073 imd = &w->image_metadata[w->hdr.image_count - 1];
1075 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1077 goto out_destroy_imd;
1079 DEBUG("Assigning hard link group IDs");
1080 assign_inode_numbers(&imd->inode_list);
1082 ret = xml_add_image(w, name);
1084 goto out_destroy_imd;
1086 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1087 wimlib_set_boot_idx(w, w->hdr.image_count);
1089 goto out_destroy_capture_config;
1091 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1093 w->hdr.image_count--;
1096 free_dentry_tree(branch, w->lookup_table);
1097 out_free_dentry_tree:
1098 free_dentry_tree(root_dentry, w->lookup_table);
1099 out_free_security_data:
1100 free_security_data(sd);
1101 out_destroy_capture_config:
1102 destroy_capture_config(&config);
1105 win32_release_capture_privileges();
1110 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
1111 const char *name, const char *config_str,
1112 size_t config_len, int add_image_flags,
1113 wimlib_progress_func_t progress_func)
1115 if (!source || !*source)
1116 return WIMLIB_ERR_INVALID_PARAM;
1118 char *fs_source_path = STRDUP(source);
1120 struct wimlib_capture_source capture_src = {
1121 .fs_source_path = fs_source_path,
1122 .wim_target_path = NULL,
1125 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1126 config_str, config_len,
1127 add_image_flags, progress_func);
1128 FREE(fs_source_path);