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.
55 add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
56 struct wim_security_data *sd)
58 struct wim_lookup_table_entry *metadata_lte;
59 struct wim_image_metadata *imd;
60 struct wim_image_metadata *new_imd;
62 wimlib_assert(root_dentry != NULL);
64 DEBUG("Reallocating image metadata array for image_count = %u",
65 w->hdr.image_count + 1);
66 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
69 ERROR("Failed to allocate memory for new image metadata array");
73 memcpy(imd, w->image_metadata,
74 w->hdr.image_count * sizeof(struct wim_image_metadata));
76 metadata_lte = new_lookup_table_entry();
80 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
81 random_hash(metadata_lte->hash);
82 lookup_table_insert(w->lookup_table, metadata_lte);
84 new_imd = &imd[w->hdr.image_count];
86 new_imd->root_dentry = root_dentry;
87 new_imd->metadata_lte = metadata_lte;
88 new_imd->security_data = sd;
89 new_imd->modified = 1;
91 FREE(w->image_metadata);
92 w->image_metadata = imd;
98 return WIMLIB_ERR_NOMEM;
104 * build_dentry_tree():
105 * Recursively builds a tree of WIM dentries from an on-disk directory
108 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
109 * modified if successful. Set to NULL if the file or directory was
110 * excluded from capture.
112 * @root_disk_path: The path to the root of the directory tree on disk.
114 * @lookup_table: The lookup table for the WIM file. For each file added to the
115 * dentry tree being built, an entry is added to the lookup table,
116 * unless an identical stream is already in the lookup table.
117 * These lookup table entries that are added point to the path of
120 * @sd: Ignored. (Security data only captured in NTFS mode.)
123 * Configuration for files to be excluded from capture.
125 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
127 * @extra_arg: Ignored in UNIX builds; used to pass sd_set pointer in Windows
130 * @return: 0 on success, nonzero on failure. It is a failure if any of
131 * the files cannot be `stat'ed, or if any of the needed
132 * directories cannot be opened or read. Failure to add the files
133 * to the WIM may still occur later when trying to actually read
134 * the on-disk files during a call to wimlib_write() or
135 * wimlib_overwrite().
138 unix_build_dentry_tree(struct wim_dentry **root_ret,
139 const mbchar *root_disk_path,
140 struct wim_lookup_table *lookup_table,
141 struct wim_security_data *sd,
142 const struct capture_config *config,
144 wimlib_progress_func_t progress_func,
147 struct wim_dentry *root = NULL;
149 struct wim_inode *inode;
151 if (exclude_path(root_disk_path, config, true)) {
152 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
153 ERROR("Cannot exclude the root directory from capture");
154 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
157 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
160 union wimlib_progress_info info;
161 info.scan.cur_path = root_disk_path;
162 info.scan.excluded = true;
163 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
168 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
171 union wimlib_progress_info info;
172 info.scan.cur_path = root_disk_path;
173 info.scan.excluded = false;
174 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
177 /* UNIX version of capturing a directory tree */
178 struct stat root_stbuf;
179 int (*stat_fn)(const char *restrict, struct stat *restrict);
180 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
185 ret = (*stat_fn)(root_disk_path, &root_stbuf);
187 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
191 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
192 !S_ISDIR(root_stbuf.st_mode))
194 /* Do a dereference-stat in case the root is a symbolic link.
195 * This case is allowed, provided that the symbolic link points
197 ret = stat(root_disk_path, &root_stbuf);
199 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
200 ret = WIMLIB_ERR_STAT;
203 if (!S_ISDIR(root_stbuf.st_mode)) {
204 ERROR("`%s' is not a directory", root_disk_path);
205 ret = WIMLIB_ERR_NOTDIR;
209 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
210 && !S_ISLNK(root_stbuf.st_mode)) {
211 ERROR("`%s' is not a regular file, directory, or symbolic link.",
213 ret = WIMLIB_ERR_SPECIAL_FILE;
217 ret = new_dentry_with_timeless_inode(path_basename(root_disk_path),
222 inode = root->d_inode;
224 #ifdef HAVE_STAT_NANOSECOND_PRECISION
225 inode->i_creation_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
226 inode->i_last_write_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
227 inode->i_last_access_time = timespec_to_wim_timestamp(root_stbuf.st_atim);
229 inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
230 inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
231 inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
233 /* Leave the inode number at 0 for directories. */
234 if (!S_ISDIR(root_stbuf.st_mode)) {
235 if (sizeof(ino_t) >= 8)
236 inode->i_ino = (u64)root_stbuf.st_ino;
238 inode->i_ino = (u64)root_stbuf.st_ino |
239 ((u64)root_stbuf.st_dev <<
240 ((sizeof(ino_t) * 8) & 63));
242 inode->i_resolved = 1;
243 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
244 ret = inode_set_unix_data(inode, root_stbuf.st_uid,
248 UNIX_DATA_ALL | UNIX_DATA_CREATE);
252 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
253 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
255 struct wim_lookup_table_entry *lte;
256 u8 hash[SHA1_HASH_SIZE];
258 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
260 /* Empty files do not have to have a lookup table entry. */
261 if (root_stbuf.st_size == 0)
264 /* For each regular file, we must check to see if the file is in
265 * the lookup table already; if it is, we increment its refcnt;
266 * otherwise, we create a new lookup table entry and insert it.
269 ret = sha1sum(root_disk_path, hash);
273 lte = __lookup_resource(lookup_table, hash);
276 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
279 mbchar *file_on_disk = STRDUP(root_disk_path);
281 ERROR("Failed to allocate memory for file path");
282 ret = WIMLIB_ERR_NOMEM;
285 lte = new_lookup_table_entry();
288 ret = WIMLIB_ERR_NOMEM;
291 lte->file_on_disk = file_on_disk;
292 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
293 lte->resource_entry.original_size = root_stbuf.st_size;
294 lte->resource_entry.size = root_stbuf.st_size;
295 copy_hash(lte->hash, hash);
296 lookup_table_insert(lookup_table, lte);
298 root->d_inode->i_lte = lte;
299 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
301 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
304 struct dirent entry, *result;
305 struct wim_dentry *child;
307 dir = opendir(root_disk_path);
309 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
311 ret = WIMLIB_ERR_OPEN;
315 /* Buffer for names of files in directory. */
316 size_t len = strlen(root_disk_path);
317 mbchar name[len + 1 + FILENAME_MAX + 1];
318 memcpy(name, root_disk_path, len);
321 /* Create a dentry for each entry in the directory on disk, and recurse
322 * to any subdirectories. */
325 ret = readdir_r(dir, &entry, &result);
327 ret = WIMLIB_ERR_READ;
328 ERROR_WITH_ERRNO("Error reading the "
335 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
336 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
338 strcpy(name + len + 1, result->d_name);
339 ret = unix_build_dentry_tree(&child, name,
343 progress_func, NULL);
347 dentry_add_child(root, child);
350 } else { /* Archiving a symbolic link */
351 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
352 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
354 /* The idea here is to call readlink() to get the UNIX target of
355 * the symbolic link, then turn the target into a reparse point
356 * data buffer that contains a relative or absolute symbolic
357 * link (NOT a junction point or *full* path symbolic link with
361 mbchar deref_name_buf[4096];
362 ssize_t deref_name_len;
364 deref_name_len = readlink(root_disk_path, deref_name_buf,
365 sizeof(deref_name_buf) - 1);
366 if (deref_name_len >= 0) {
367 deref_name_buf[deref_name_len] = '\0';
368 DEBUG("Read symlink `%s'", deref_name_buf);
369 ret = inode_set_symlink(root->d_inode, deref_name_buf,
373 * Unfortunately, Windows seems to have the
374 * concept of "file" symbolic links as being
375 * different from "directory" symbolic links...
376 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
377 * on the symbolic link if the *target* of the
378 * symbolic link is a directory.
381 if (stat(root_disk_path, &stbuf) == 0 &&
382 S_ISDIR(stbuf.st_mode))
384 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
388 ERROR_WITH_ERRNO("Failed to read target of "
389 "symbolic link `%s'", root_disk_path);
390 ret = WIMLIB_ERR_READLINK;
397 free_dentry_tree(root, lookup_table);
400 #endif /* !__WIN32__ */
406 COMPRESSION_EXCLUSION_LIST,
410 #define COMPAT_DEFAULT_CONFIG
412 /* Default capture configuration file when none is specified. */
413 static const mbchar *default_config =
414 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
415 users. The next ABI-incompatible library
416 version will default to the empty string here. */
421 "\\System Volume Information\n"
425 "[CompressionExclusionList]\n"
429 "\\WINDOWS\\inf\\*.pnf\n";
435 destroy_pattern_list(struct pattern_list *list)
441 destroy_capture_config(struct capture_config *config)
443 destroy_pattern_list(&config->exclusion_list);
444 destroy_pattern_list(&config->exclusion_exception);
445 destroy_pattern_list(&config->compression_exclusion_list);
446 destroy_pattern_list(&config->alignment_list);
447 FREE(config->config_str);
448 FREE(config->prefix);
449 memset(config, 0, sizeof(*config));
453 pattern_list_add_pattern(struct pattern_list *list, const mbchar *pattern)
456 if (list->num_pats >= list->num_allocated_pats) {
457 pats = REALLOC(list->pats,
458 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
460 return WIMLIB_ERR_NOMEM;
461 list->num_allocated_pats += 8;
464 list->pats[list->num_pats++] = pattern;
468 /* Parses the contents of the image capture configuration file and fills in a
469 * `struct capture_config'. */
471 init_capture_config(struct capture_config *config,
472 const mbchar *_config_str, size_t config_len)
478 size_t bytes_remaining;
479 enum pattern_type type = NONE;
481 unsigned long line_no = 0;
483 DEBUG("config_len = %zu", config_len);
484 bytes_remaining = config_len;
485 memset(config, 0, sizeof(*config));
486 config_str = MALLOC(config_len);
488 ERROR("Could not duplicate capture config string");
489 return WIMLIB_ERR_NOMEM;
492 memcpy(config_str, _config_str, config_len);
494 config->config_str = config_str;
495 while (bytes_remaining) {
498 eol = memchr(p, '\n', bytes_remaining);
500 ERROR("Expected end-of-line in capture config file on "
501 "line %lu", line_no);
502 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
507 bytes_remaining -= (next_p - p);
511 if (*(eol - 1) == '\r')
515 /* Translate backslash to forward slash */
516 for (mbchar *pp = p; pp != eol; pp++)
520 /* Remove drive letter */
521 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
525 if (strcmp(p, "[ExclusionList]") == 0)
526 type = EXCLUSION_LIST;
527 else if (strcmp(p, "[ExclusionException]") == 0)
528 type = EXCLUSION_EXCEPTION;
529 else if (strcmp(p, "[CompressionExclusionList]") == 0)
530 type = COMPRESSION_EXCLUSION_LIST;
531 else if (strcmp(p, "[AlignmentList]") == 0)
532 type = ALIGNMENT_LIST;
533 else if (p[0] == '[' && strrchr(p, ']')) {
534 ERROR("Unknown capture configuration section `%s'", p);
535 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
536 } else switch (type) {
538 DEBUG("Adding pattern \"%s\" to exclusion list", p);
539 ret = pattern_list_add_pattern(&config->exclusion_list, p);
541 case EXCLUSION_EXCEPTION:
542 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
543 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
545 case COMPRESSION_EXCLUSION_LIST:
546 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
547 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
550 DEBUG("Adding pattern \"%s\" to alignment list", p);
551 ret = pattern_list_add_pattern(&config->alignment_list, p);
554 ERROR("Line %lu of capture configuration is not "
555 "in a block (such as [ExclusionList])",
557 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
565 destroy_capture_config(config);
569 static int capture_config_set_prefix(struct capture_config *config,
570 const mbchar *_prefix)
572 mbchar *prefix = STRDUP(_prefix);
575 return WIMLIB_ERR_NOMEM;
576 FREE(config->prefix);
577 config->prefix = prefix;
578 config->prefix_len = strlen(prefix);
582 static bool match_pattern(const mbchar *path,
583 const mbchar *path_basename,
584 const struct pattern_list *list)
586 for (size_t i = 0; i < list->num_pats; i++) {
587 const mbchar *pat = list->pats[i];
588 const mbchar *string;
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
627 exclude_path(const mbchar *path, const struct capture_config *config,
630 const mbchar *basename = path_basename(path);
631 if (exclude_prefix) {
632 wimlib_assert(strlen(path) >= config->prefix_len);
633 if (memcmp(config->prefix, path, config->prefix_len) == 0
634 && path[config->prefix_len] == '/')
635 path += config->prefix_len;
637 return match_pattern(path, basename, &config->exclusion_list) &&
638 !match_pattern(path, basename, &config->exclusion_exception);
642 /* Strip leading and trailing forward slashes from a string. Modifies it in
643 * place and returns the stripped string. */
645 canonicalize_target_path(char *target_path)
648 if (target_path == NULL)
651 if (*target_path == '\0')
653 else if (*target_path == '/')
659 p = target_path + strlen(target_path) - 1;
667 zap_backslashes(char *s)
677 /* Strip leading and trailing slashes from the target paths */
679 canonicalize_targets(struct wimlib_capture_source *sources, size_t num_sources)
681 while (num_sources--) {
682 DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
683 sources->fs_source_path,
684 sources->wim_target_path);
686 /* The Windows API can handle forward slashes. Just get rid of
687 * backslashes to avoid confusing other parts of the library
689 zap_backslashes(sources->fs_source_path);
690 if (sources->wim_target_path)
691 zap_backslashes(sources->wim_target_path);
693 sources->wim_target_path =
694 (char*)canonicalize_target_path(sources->wim_target_path);
695 DEBUG("Canonical target: \"%s\"", sources->wim_target_path);
701 capture_source_cmp(const void *p1, const void *p2)
703 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
704 return strcmp(s1->wim_target_path, s2->wim_target_path);
707 /* Sorts the capture sources lexicographically by target path. This occurs
708 * after leading and trailing forward slashes are stripped.
710 * One purpose of this is to make sure that target paths that are inside other
711 * target paths are added after the containing target paths. */
713 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
715 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
719 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
722 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
723 if (num_sources != 1) {
724 ERROR("Must specify exactly 1 capture source "
725 "(the NTFS volume) in NTFS mode!");
726 return WIMLIB_ERR_INVALID_PARAM;
728 if (sources[0].wim_target_path[0] != '\0') {
729 ERROR("In NTFS capture mode the target path inside "
730 "the image must be the root directory!");
731 return WIMLIB_ERR_INVALID_PARAM;
733 } else if (num_sources != 0) {
734 /* This code is disabled because the current code
735 * unconditionally attempts to do overlays. So, duplicate
736 * target paths are OK. */
738 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
739 ERROR("Cannot specify root target when using multiple "
741 return WIMLIB_ERR_INVALID_PARAM;
743 for (size_t i = 0; i < num_sources - 1; i++) {
744 size_t len = strlen(sources[i].wim_target_path);
746 const char *target1 = sources[i].wim_target_path;
748 const char *target2 = sources[j].wim_target_path;
749 DEBUG("target1=%s, target2=%s",
751 if (strncmp(target1, target2, len) ||
754 if (target2[len] == '/') {
755 ERROR("Invalid target `%s': is a prefix of `%s'",
757 return WIMLIB_ERR_INVALID_PARAM;
759 if (target2[len] == '\0') {
760 ERROR("Invalid target `%s': is a duplicate of `%s'",
762 return WIMLIB_ERR_INVALID_PARAM;
764 } while (++j != num_sources);
772 /* Creates a new directory to place in the WIM image. This is to create parent
773 * directories that are not part of any target as needed. */
775 new_filler_directory(const mbchar *name, struct wim_dentry **dentry_ret)
778 struct wim_dentry *dentry;
780 DEBUG("Creating filler directory \"%s\"", name);
781 ret = new_dentry_with_inode(name, &dentry);
783 /* Leave the inode number as 0 for now. The final inode number
784 * will be assigned later by assign_inode_numbers(). */
785 dentry->d_inode->i_resolved = 1;
786 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
787 *dentry_ret = dentry;
792 /* Transfers the children of @branch to @target. It is an error if @target is
793 * not a directory or if both @branch and @target contain a child dentry with
796 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
798 struct rb_root *rb_root;
800 DEBUG("Doing overlay \"%W\" => \"%W\"",
801 branch->file_name, target->file_name);
803 if (!dentry_is_directory(target)) {
804 ERROR("Cannot overlay directory \"%W\" over non-directory",
806 return WIMLIB_ERR_INVALID_OVERLAY;
809 rb_root = &branch->d_inode->i_children;
810 while (rb_root->rb_node) { /* While @branch has children... */
811 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
812 /* Move @child to the directory @target */
813 unlink_dentry(child);
814 if (!dentry_add_child(target, child)) {
815 /* Revert the change to avoid leaking the directory tree
816 * rooted at @child */
817 dentry_add_child(branch, child);
818 ERROR("Overlay error: file \"%W\" already exists "
819 "as a child of \"%W\"",
820 child->file_name, target->file_name);
821 return WIMLIB_ERR_INVALID_OVERLAY;
829 /* Attach or overlay a branch onto the WIM image.
832 * Pointer to the root of the WIM image, or pointer to NULL if it has not
837 * Path in the WIM image to add the branch, with leading and trailing
841 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
845 struct wim_dentry *dentry, *parent, *target;
848 DEBUG("Attaching branch \"%W\" => \"%s\"",
849 branch->file_name, target_path);
851 if (*target_path == '\0') {
852 /* Target: root directory */
854 /* Overlay on existing root */
855 return do_overlay(*root_p, branch);
863 /* Adding a non-root branch. Create root if it hasn't been created
866 ret = new_filler_directory("", root_p);
871 /* Walk the path to the branch, creating filler directories as needed.
874 while ((slash = strchr(target_path, '/'))) {
876 dentry = get_dentry_child_with_name(parent, target_path);
878 ret = new_filler_directory(target_path, &dentry);
881 dentry_add_child(parent, dentry);
885 /* Skip over slashes. Note: this cannot overrun the length of
886 * the string because the last character cannot be a slash, as
887 * trailing slashes were tripped. */
890 } while (*target_path == '/');
893 /* If the target path already existed, overlay the branch onto it.
894 * Otherwise, set the branch as the target path. */
895 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
896 branch->file_name_nbytes);
898 return do_overlay(target, branch);
900 dentry_add_child(parent, branch);
906 wimlib_add_image_multisource(WIMStruct *w,
907 struct wimlib_capture_source *sources,
909 const utf8char *name,
910 const mbchar *config_str,
913 wimlib_progress_func_t progress_func)
915 int (*capture_tree)(struct wim_dentry **,
917 struct wim_lookup_table *,
918 struct wim_security_data *,
919 const struct capture_config *,
921 wimlib_progress_func_t,
924 struct wim_dentry *root_dentry;
925 struct wim_dentry *branch;
926 struct wim_security_data *sd;
927 struct capture_config config;
928 struct wim_image_metadata *imd;
931 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
933 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
934 ERROR("Cannot dereference files when capturing directly from NTFS");
935 return WIMLIB_ERR_INVALID_PARAM;
937 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
938 ERROR("Capturing UNIX owner and mode not supported "
939 "when capturing directly from NTFS");
940 return WIMLIB_ERR_INVALID_PARAM;
942 capture_tree = build_dentry_tree_ntfs;
943 extra_arg = &w->ntfs_vol;
945 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
946 " cannot capture a WIM image directly from a NTFS volume!");
947 return WIMLIB_ERR_UNSUPPORTED;
951 capture_tree = win32_build_dentry_tree;
953 capture_tree = unix_build_dentry_tree;
959 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
960 ERROR("Capturing UNIX-specific data is not supported on Windows");
961 return WIMLIB_ERR_INVALID_PARAM;
963 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
964 ERROR("Dereferencing symbolic links is not supported on Windows");
965 return WIMLIB_ERR_INVALID_PARAM;
969 if (!name || !*name) {
970 ERROR("Must specify a non-empty string for the image name");
971 return WIMLIB_ERR_INVALID_PARAM;
974 if (w->hdr.total_parts != 1) {
975 ERROR("Cannot add an image to a split WIM");
976 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
979 if (wimlib_image_name_in_use(w, name)) {
980 ERROR("There is already an image named \"%U\" in the WIM!",
982 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
986 DEBUG("Using default capture configuration");
987 config_str = default_config;
988 config_len = strlen(default_config);
990 ret = init_capture_config(&config, config_str, config_len);
994 DEBUG("Allocating security data");
995 sd = CALLOC(1, sizeof(struct wim_security_data));
997 ret = WIMLIB_ERR_NOMEM;
998 goto out_destroy_capture_config;
1000 sd->total_length = 8;
1003 DEBUG("Using %zu capture sources", num_sources);
1004 canonicalize_targets(sources, num_sources);
1005 sort_sources(sources, num_sources);
1006 ret = check_sorted_sources(sources, num_sources, add_image_flags);
1008 ret = WIMLIB_ERR_INVALID_PARAM;
1009 goto out_free_security_data;
1012 DEBUG("Building dentry tree.");
1015 for (size_t i = 0; i < num_sources; i++) {
1017 union wimlib_progress_info progress;
1019 DEBUG("Building dentry tree for source %zu of %zu "
1020 "(\"%s\" => \"%s\")", i + 1, num_sources,
1021 sources[i].fs_source_path,
1022 sources[i].wim_target_path);
1023 if (progress_func) {
1024 memset(&progress, 0, sizeof(progress));
1025 progress.scan.source = sources[i].fs_source_path;
1026 progress.scan.wim_target_path = sources[i].wim_target_path;
1027 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1029 ret = capture_config_set_prefix(&config,
1030 sources[i].fs_source_path);
1032 goto out_free_dentry_tree;
1033 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
1034 if (!*sources[i].wim_target_path)
1035 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
1036 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
1037 w->lookup_table, sd,
1040 progress_func, extra_arg);
1042 ERROR("Failed to build dentry tree for `%s'",
1043 sources[i].fs_source_path);
1044 goto out_free_dentry_tree;
1047 /* Use the target name, not the source name, for
1048 * the root of each branch from a capture
1049 * source. (This will also set the root dentry
1050 * of the entire image to be unnamed.) */
1051 ret = set_dentry_name(branch,
1052 path_basename(sources[i].wim_target_path));
1054 goto out_free_branch;
1056 ret = attach_branch(&root_dentry, branch,
1057 sources[i].wim_target_path);
1059 goto out_free_branch;
1062 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1065 if (root_dentry == NULL) {
1066 ret = new_filler_directory("" , &root_dentry);
1068 goto out_free_dentry_tree;
1071 DEBUG("Calculating full paths of dentries.");
1072 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1074 goto out_free_dentry_tree;
1076 ret = add_new_dentry_tree(w, root_dentry, sd);
1078 goto out_free_dentry_tree;
1080 imd = &w->image_metadata[w->hdr.image_count - 1];
1082 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1084 goto out_destroy_imd;
1086 DEBUG("Assigning hard link group IDs");
1087 assign_inode_numbers(&imd->inode_list);
1089 ret = xml_add_image(w, name);
1091 goto out_destroy_imd;
1093 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1094 wimlib_set_boot_idx(w, w->hdr.image_count);
1096 goto out_destroy_capture_config;
1098 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1100 w->hdr.image_count--;
1103 free_dentry_tree(branch, w->lookup_table);
1104 out_free_dentry_tree:
1105 free_dentry_tree(root_dentry, w->lookup_table);
1106 out_free_security_data:
1107 free_security_data(sd);
1108 out_destroy_capture_config:
1109 destroy_capture_config(&config);
1115 wimlib_add_image(WIMStruct *w,
1116 const mbchar *source,
1117 const utf8char *name,
1118 const mbchar *config_str,
1120 int add_image_flags,
1121 wimlib_progress_func_t progress_func)
1123 if (!source || !*source)
1124 return WIMLIB_ERR_INVALID_PARAM;
1126 mbchar *fs_source_path = STRDUP(source);
1128 struct wimlib_capture_source capture_src = {
1129 .fs_source_path = fs_source_path,
1130 .wim_target_path = NULL,
1133 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1134 config_str, config_len,
1135 add_image_flags, progress_func);
1136 FREE(fs_source_path);