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"
53 * Adds the dentry tree and security data for a new image to the image metadata
54 * array of the WIMStruct.
57 add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
58 struct wim_security_data *sd)
60 struct wim_lookup_table_entry *metadata_lte;
61 struct wim_image_metadata *imd;
62 struct wim_image_metadata *new_imd;
64 wimlib_assert(root_dentry != NULL);
66 DEBUG("Reallocating image metadata array for image_count = %u",
67 w->hdr.image_count + 1);
68 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
71 ERROR("Failed to allocate memory for new image metadata array");
75 memcpy(imd, w->image_metadata,
76 w->hdr.image_count * sizeof(struct wim_image_metadata));
78 metadata_lte = new_lookup_table_entry();
82 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
83 random_hash(metadata_lte->hash);
84 lookup_table_insert(w->lookup_table, metadata_lte);
86 new_imd = &imd[w->hdr.image_count];
88 new_imd->root_dentry = root_dentry;
89 new_imd->metadata_lte = metadata_lte;
90 new_imd->security_data = sd;
91 new_imd->modified = 1;
93 FREE(w->image_metadata);
94 w->image_metadata = imd;
100 return WIMLIB_ERR_NOMEM;
106 * unix_build_dentry_tree():
107 * Recursively builds a tree of WIM dentries from an on-disk directory
108 * tree (UNIX version; no NTFS-specific data is captured).
110 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
111 * modified if successful. Set to NULL if the file or directory was
112 * excluded from capture.
114 * @root_disk_path: The path to the root of the directory tree on disk.
116 * @lookup_table: The lookup table for the WIM file. For each file added to the
117 * dentry tree being built, an entry is added to the lookup table,
118 * unless an identical stream is already in the lookup table.
119 * These lookup table entries that are added point to the path of
122 * @sd_set: Ignored. (Security data only captured in NTFS mode.)
125 * Configuration for files to be excluded from capture.
127 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
129 * @extra_arg: Ignored
131 * @return: 0 on success, nonzero on failure. It is a failure if any of
132 * the files cannot be `stat'ed, or if any of the needed
133 * directories cannot be opened or read. Failure to add the files
134 * to the WIM may still occur later when trying to actually read
135 * the on-disk files during a call to wimlib_write() or
136 * wimlib_overwrite().
139 unix_build_dentry_tree(struct wim_dentry **root_ret,
140 const char *root_disk_path,
141 struct wim_lookup_table *lookup_table,
142 struct sd_set *sd_set,
143 const struct capture_config *config,
145 wimlib_progress_func_t progress_func,
148 struct wim_dentry *root = NULL;
150 struct wim_inode *inode;
152 if (exclude_path(root_disk_path, config, true)) {
153 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
154 ERROR("Cannot exclude the root directory from capture");
155 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
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 = true;
164 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
169 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
172 union wimlib_progress_info info;
173 info.scan.cur_path = root_disk_path;
174 info.scan.excluded = false;
175 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
178 /* UNIX version of capturing a directory tree */
179 struct stat root_stbuf;
180 int (*stat_fn)(const char *restrict, struct stat *restrict);
181 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
186 ret = (*stat_fn)(root_disk_path, &root_stbuf);
188 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
192 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
193 !S_ISDIR(root_stbuf.st_mode))
195 /* Do a dereference-stat in case the root is a symbolic link.
196 * This case is allowed, provided that the symbolic link points
198 ret = stat(root_disk_path, &root_stbuf);
200 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
201 ret = WIMLIB_ERR_STAT;
204 if (!S_ISDIR(root_stbuf.st_mode)) {
205 ERROR("`%s' is not a directory", root_disk_path);
206 ret = WIMLIB_ERR_NOTDIR;
210 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
211 && !S_ISLNK(root_stbuf.st_mode)) {
212 ERROR("`%s' is not a regular file, directory, or symbolic link.",
214 ret = WIMLIB_ERR_SPECIAL_FILE;
218 ret = new_dentry_with_timeless_inode(path_basename(root_disk_path),
223 inode = root->d_inode;
225 #ifdef HAVE_STAT_NANOSECOND_PRECISION
226 inode->i_creation_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
227 inode->i_last_write_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
228 inode->i_last_access_time = timespec_to_wim_timestamp(root_stbuf.st_atim);
230 inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
231 inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
232 inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
234 /* Leave the inode number at 0 for directories. */
235 if (!S_ISDIR(root_stbuf.st_mode)) {
236 if (sizeof(ino_t) >= 8)
237 inode->i_ino = (u64)root_stbuf.st_ino;
239 inode->i_ino = (u64)root_stbuf.st_ino |
240 ((u64)root_stbuf.st_dev <<
241 ((sizeof(ino_t) * 8) & 63));
243 inode->i_resolved = 1;
244 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
245 ret = inode_set_unix_data(inode, root_stbuf.st_uid,
249 UNIX_DATA_ALL | UNIX_DATA_CREATE);
253 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
254 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
256 struct wim_lookup_table_entry *lte;
257 u8 hash[SHA1_HASH_SIZE];
259 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
261 /* Empty files do not have to have a lookup table entry. */
262 if (root_stbuf.st_size == 0)
265 /* For each regular file, we must check to see if the file is in
266 * the lookup table already; if it is, we increment its refcnt;
267 * otherwise, we create a new lookup table entry and insert it.
270 ret = sha1sum(root_disk_path, hash);
274 lte = __lookup_resource(lookup_table, hash);
277 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
280 char *file_on_disk = STRDUP(root_disk_path);
282 ERROR("Failed to allocate memory for file path");
283 ret = WIMLIB_ERR_NOMEM;
286 lte = new_lookup_table_entry();
289 ret = WIMLIB_ERR_NOMEM;
292 lte->file_on_disk = file_on_disk;
293 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
294 lte->resource_entry.original_size = root_stbuf.st_size;
295 lte->resource_entry.size = root_stbuf.st_size;
296 copy_hash(lte->hash, hash);
297 lookup_table_insert(lookup_table, lte);
299 root->d_inode->i_lte = lte;
300 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
302 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
305 struct dirent entry, *result;
306 struct wim_dentry *child;
308 dir = opendir(root_disk_path);
310 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
312 ret = WIMLIB_ERR_OPEN;
316 /* Buffer for names of files in directory. */
317 size_t len = strlen(root_disk_path);
318 char name[len + 1 + FILENAME_MAX + 1];
319 memcpy(name, root_disk_path, len);
322 /* Create a dentry for each entry in the directory on disk, and recurse
323 * to any subdirectories. */
326 ret = readdir_r(dir, &entry, &result);
328 ret = WIMLIB_ERR_READ;
329 ERROR_WITH_ERRNO("Error reading the "
336 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
337 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
339 strcpy(name + len + 1, result->d_name);
340 ret = unix_build_dentry_tree(&child, name,
344 progress_func, NULL);
348 dentry_add_child(root, child);
351 } else { /* Archiving a symbolic link */
352 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
353 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
355 /* The idea here is to call readlink() to get the UNIX target of
356 * the symbolic link, then turn the target into a reparse point
357 * data buffer that contains a relative or absolute symbolic
358 * link (NOT a junction point or *full* path symbolic link with
362 char deref_name_buf[4096];
363 ssize_t deref_name_len;
365 deref_name_len = readlink(root_disk_path, deref_name_buf,
366 sizeof(deref_name_buf) - 1);
367 if (deref_name_len >= 0) {
368 deref_name_buf[deref_name_len] = '\0';
369 DEBUG("Read symlink `%s'", deref_name_buf);
370 ret = inode_set_symlink(root->d_inode, deref_name_buf,
374 * Unfortunately, Windows seems to have the
375 * concept of "file" symbolic links as being
376 * different from "directory" symbolic links...
377 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
378 * on the symbolic link if the *target* of the
379 * symbolic link is a directory.
382 if (stat(root_disk_path, &stbuf) == 0 &&
383 S_ISDIR(stbuf.st_mode))
385 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
389 ERROR_WITH_ERRNO("Failed to read target of "
390 "symbolic link `%s'", root_disk_path);
391 ret = WIMLIB_ERR_READLINK;
398 free_dentry_tree(root, lookup_table);
401 #endif /* !__WIN32__ */
407 COMPRESSION_EXCLUSION_LIST,
411 #define COMPAT_DEFAULT_CONFIG
413 /* Default capture configuration file when none is specified. */
414 static const tchar *default_config =
415 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
416 users. The next ABI-incompatible library
417 version will default to the empty string here. */
423 "\\System Volume Information\n"
427 "[CompressionExclusionList]\n"
431 "\\WINDOWS\\inf\\*.pnf\n"
438 destroy_pattern_list(struct pattern_list *list)
444 destroy_capture_config(struct capture_config *config)
446 destroy_pattern_list(&config->exclusion_list);
447 destroy_pattern_list(&config->exclusion_exception);
448 destroy_pattern_list(&config->compression_exclusion_list);
449 destroy_pattern_list(&config->alignment_list);
450 FREE(config->config_str);
451 FREE(config->prefix);
452 memset(config, 0, sizeof(*config));
456 pattern_list_add_pattern(struct pattern_list *list, const tchar *pattern)
459 if (list->num_pats >= list->num_allocated_pats) {
460 pats = REALLOC(list->pats,
461 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
463 return WIMLIB_ERR_NOMEM;
464 list->num_allocated_pats += 8;
467 list->pats[list->num_pats++] = pattern;
471 /* Parses the contents of the image capture configuration file and fills in a
472 * `struct capture_config'. */
474 init_capture_config(struct capture_config *config,
475 const tchar *_config_str,
476 size_t config_num_tchars)
482 size_t num_tchars_remaining;
483 enum pattern_type type = NONE;
485 unsigned long line_no = 0;
487 DEBUG("config_num_tchars = %zu", config_num_tchars);
488 num_tchars_remaining = config_num_tchars;
489 memset(config, 0, sizeof(*config));
490 config_str = TMALLOC(config_num_tchars);
492 ERROR("Could not duplicate capture config string");
493 return WIMLIB_ERR_NOMEM;
496 tmemcpy(config_str, _config_str, config_num_tchars);
498 config->config_str = config_str;
499 while (num_tchars_remaining != 0) {
502 eol = tmemchr(p, T('\n'), num_tchars_remaining);
504 ERROR("Expected end-of-line in capture config file on "
505 "line %lu", line_no);
506 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
511 num_tchars_remaining -= (next_p - p);
515 if (*(eol - 1) == T('\r'))
519 /* Translate backslash to forward slash */
520 for (tchar *pp = p; pp != eol; pp++)
524 /* Remove drive letter (UNIX only) */
526 if (eol - p > 2 && istalpha(*p) && *(p + 1) == T(':'))
531 if (!tstrcmp(p, T("[ExclusionList]")))
532 type = EXCLUSION_LIST;
533 else if (!tstrcmp(p, T("[ExclusionException]")))
534 type = EXCLUSION_EXCEPTION;
535 else if (!tstrcmp(p, T("[CompressionExclusionList]")))
536 type = COMPRESSION_EXCLUSION_LIST;
537 else if (!tstrcmp(p, T("[AlignmentList]")))
538 type = ALIGNMENT_LIST;
539 else if (p[0] == T('[') && tstrrchr(p, T(']'))) {
540 ERROR("Unknown capture configuration section \"%"TS"\"", p);
541 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
542 } else switch (type) {
544 DEBUG("Adding pattern \"%"TS"\" to exclusion list", p);
545 ret = pattern_list_add_pattern(&config->exclusion_list, p);
547 case EXCLUSION_EXCEPTION:
548 DEBUG("Adding pattern \"%"TS"\" to exclusion exception list", p);
549 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
551 case COMPRESSION_EXCLUSION_LIST:
552 DEBUG("Adding pattern \"%"TS"\" to compression exclusion list", p);
553 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
556 DEBUG("Adding pattern \"%"TS"\" to alignment list", p);
557 ret = pattern_list_add_pattern(&config->alignment_list, p);
560 ERROR("Line %lu of capture configuration is not "
561 "in a block (such as [ExclusionList])",
563 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
571 destroy_capture_config(config);
575 static int capture_config_set_prefix(struct capture_config *config,
576 const tchar *_prefix)
578 tchar *prefix = TSTRDUP(_prefix);
581 return WIMLIB_ERR_NOMEM;
582 FREE(config->prefix);
583 config->prefix = prefix;
584 config->prefix_num_tchars = tstrlen(prefix);
588 static bool match_pattern(const tchar *path,
589 const tchar *path_basename,
590 const struct pattern_list *list)
592 for (size_t i = 0; i < list->num_pats; i++) {
593 const tchar *pat = list->pats[i];
596 /* Absolute path from root of capture */
599 if (tstrchr(pat, T('/')))
600 /* Relative path from root of capture */
603 /* A file name pattern */
604 string = path_basename;
607 /* Warning: on Windows native builds, fnmatch() calls the
608 * replacement function in win32.c. */
609 if (fnmatch(pat, string, FNM_PATHNAME
615 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
623 /* Return true if the image capture configuration file indicates we should
624 * exclude the filename @path from capture.
626 * If @exclude_prefix is %true, the part of the path up and including the name
627 * of the directory being captured is not included in the path for matching
628 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
629 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
633 exclude_path(const tchar *path, const struct capture_config *config,
636 const tchar *basename = path_basename(path);
637 if (exclude_prefix) {
638 wimlib_assert(tstrlen(path) >= config->prefix_num_tchars);
639 if (!tmemcmp(config->prefix, path, config->prefix_num_tchars) &&
640 path[config->prefix_num_tchars] == T('/'))
642 path += config->prefix_num_tchars;
645 return match_pattern(path, basename, &config->exclusion_list) &&
646 !match_pattern(path, basename, &config->exclusion_exception);
650 /* Strip leading and trailing forward slashes from a string. Modifies it in
651 * place and returns the stripped string. */
653 canonicalize_target_path(tchar *target_path)
656 if (target_path == NULL)
659 if (*target_path == T('\0'))
661 else if (*target_path == T('/'))
667 p = tstrchr(target_path, T('\0')) - 1;
673 /* Strip leading and trailing slashes from the target paths */
675 canonicalize_targets(struct wimlib_capture_source *sources, size_t num_sources)
677 while (num_sources--) {
678 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
679 sources->fs_source_path,
680 sources->wim_target_path);
682 /* The Windows API can handle forward slashes. Just get rid of
683 * backslashes to avoid confusing other parts of the library
685 zap_backslashes(sources->fs_source_path);
686 if (sources->wim_target_path)
687 zap_backslashes(sources->wim_target_path);
689 sources->wim_target_path =
690 (tchar*)canonicalize_target_path(sources->wim_target_path);
691 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
697 capture_source_cmp(const void *p1, const void *p2)
699 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
700 return tstrcmp(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. */
709 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
711 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
715 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
718 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
719 if (num_sources != 1) {
720 ERROR("Must specify exactly 1 capture source "
721 "(the NTFS volume) in NTFS mode!");
722 return WIMLIB_ERR_INVALID_PARAM;
724 if (sources[0].wim_target_path[0] != T('\0')) {
725 ERROR("In NTFS capture mode the target path inside "
726 "the image must be the root directory!");
727 return WIMLIB_ERR_INVALID_PARAM;
729 } else if (num_sources != 0) {
730 /* This code is disabled because the current code
731 * unconditionally attempts to do overlays. So, duplicate
732 * target paths are OK. */
734 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
735 ERROR("Cannot specify root target when using multiple "
737 return WIMLIB_ERR_INVALID_PARAM;
739 for (size_t i = 0; i < num_sources - 1; i++) {
740 size_t len = strlen(sources[i].wim_target_path);
742 const char *target1 = sources[i].wim_target_path;
744 const char *target2 = sources[j].wim_target_path;
745 DEBUG("target1=%s, target2=%s",
747 if (strncmp(target1, target2, len) ||
750 if (target2[len] == '/') {
751 ERROR("Invalid target `%s': is a prefix of `%s'",
753 return WIMLIB_ERR_INVALID_PARAM;
755 if (target2[len] == '\0') {
756 ERROR("Invalid target `%s': is a duplicate of `%s'",
758 return WIMLIB_ERR_INVALID_PARAM;
760 } while (++j != num_sources);
768 /* Creates a new directory to place in the WIM image. This is to create parent
769 * directories that are not part of any target as needed. */
771 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
774 struct wim_dentry *dentry;
776 DEBUG("Creating filler directory \"%"TS"\"", name);
777 ret = new_dentry_with_inode(name, &dentry);
779 /* Leave the inode number as 0 for now. The final inode number
780 * will be assigned later by assign_inode_numbers(). */
781 dentry->d_inode->i_resolved = 1;
782 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
783 *dentry_ret = dentry;
788 /* Transfers the children of @branch to @target. It is an error if @target is
789 * not a directory or if both @branch and @target contain a child dentry with
792 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
794 struct rb_root *rb_root;
796 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
797 branch->file_name, target->file_name);
799 if (!dentry_is_directory(target)) {
800 ERROR("Cannot overlay directory \"%"WS"\" "
801 "over non-directory", branch->file_name);
802 return WIMLIB_ERR_INVALID_OVERLAY;
805 rb_root = &branch->d_inode->i_children;
806 while (rb_root->rb_node) { /* While @branch has children... */
807 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
808 /* Move @child to the directory @target */
809 unlink_dentry(child);
810 if (!dentry_add_child(target, child)) {
811 /* Revert the change to avoid leaking the directory tree
812 * rooted at @child */
813 dentry_add_child(branch, child);
814 ERROR("Overlay error: file \"%"WS"\" already exists "
815 "as a child of \"%"WS"\"",
816 child->file_name, target->file_name);
817 return WIMLIB_ERR_INVALID_OVERLAY;
825 /* Attach or overlay a branch onto the WIM image.
828 * Pointer to the root of the WIM image, or pointer to NULL if it has not
833 * Path in the WIM image to add the branch, with leading and trailing
837 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
841 struct wim_dentry *dentry, *parent, *target;
844 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
845 branch->file_name, target_path);
847 if (*target_path == T('\0')) {
848 /* Target: root directory */
850 /* Overlay on existing root */
851 return do_overlay(*root_p, branch);
859 /* Adding a non-root branch. Create root if it hasn't been created
862 ret = new_filler_directory(T(""), root_p);
867 /* Walk the path to the branch, creating filler directories as needed.
870 while ((slash = tstrchr(target_path, T('/')))) {
872 dentry = get_dentry_child_with_name(parent, target_path);
874 ret = new_filler_directory(target_path, &dentry);
877 dentry_add_child(parent, dentry);
881 /* Skip over slashes. Note: this cannot overrun the length of
882 * the string because the last character cannot be a slash, as
883 * trailing slashes were tripped. */
886 } while (*target_path == T('/'));
889 /* If the target path already existed, overlay the branch onto it.
890 * Otherwise, set the branch as the target path. */
891 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
892 branch->file_name_nbytes);
894 return do_overlay(target, branch);
896 dentry_add_child(parent, branch);
902 wimlib_add_image_multisource(WIMStruct *w,
903 struct wimlib_capture_source *sources,
906 const tchar *config_str,
909 wimlib_progress_func_t progress_func)
911 int (*capture_tree)(struct wim_dentry **,
913 struct wim_lookup_table *,
915 const struct capture_config *,
917 wimlib_progress_func_t,
920 struct wim_dentry *root_dentry;
921 struct wim_dentry *branch;
922 struct wim_security_data *sd;
923 struct capture_config config;
924 struct wim_image_metadata *imd;
926 struct sd_set sd_set;
928 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
930 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
931 ERROR("Cannot dereference files when capturing directly from NTFS");
932 return WIMLIB_ERR_INVALID_PARAM;
934 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
935 ERROR("Capturing UNIX owner and mode not supported "
936 "when capturing directly from NTFS");
937 return WIMLIB_ERR_INVALID_PARAM;
939 capture_tree = build_dentry_tree_ntfs;
940 extra_arg = &w->ntfs_vol;
942 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
943 " cannot capture a WIM image directly from a NTFS volume!");
944 return WIMLIB_ERR_UNSUPPORTED;
948 capture_tree = win32_build_dentry_tree;
950 capture_tree = unix_build_dentry_tree;
956 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
957 ERROR("Capturing UNIX-specific data is not supported on Windows");
958 return WIMLIB_ERR_INVALID_PARAM;
960 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
961 ERROR("Dereferencing symbolic links is not supported on Windows");
962 return WIMLIB_ERR_INVALID_PARAM;
966 if (!name || !*name) {
967 ERROR("Must specify a non-empty string for the image name");
968 return WIMLIB_ERR_INVALID_PARAM;
971 if (w->hdr.total_parts != 1) {
972 ERROR("Cannot add an image to a split WIM");
973 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
976 if (wimlib_image_name_in_use(w, name)) {
977 ERROR("There is already an image named \"%"TS"\" in the WIM!",
979 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
983 DEBUG("Using default capture configuration");
984 config_str = default_config;
985 config_len = tstrlen(default_config);
987 ret = init_capture_config(&config, config_str, config_len);
991 DEBUG("Allocating security data");
992 sd = CALLOC(1, sizeof(struct wim_security_data));
994 ret = WIMLIB_ERR_NOMEM;
995 goto out_destroy_capture_config;
997 sd->total_length = 8;
1001 sd_set.rb_root.rb_node = NULL;
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 "(\"%"TS"\" => \"%"TS"\")", 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,
1037 sources[i].fs_source_path,
1042 progress_func, extra_arg);
1044 ERROR("Failed to build dentry tree for `%"TS"'",
1045 sources[i].fs_source_path);
1046 goto out_free_dentry_tree;
1049 /* Use the target name, not the source name, for
1050 * the root of each branch from a capture
1051 * source. (This will also set the root dentry
1052 * of the entire image to be unnamed.) */
1053 ret = set_dentry_name(branch,
1054 path_basename(sources[i].wim_target_path));
1056 goto out_free_branch;
1058 ret = attach_branch(&root_dentry, branch,
1059 sources[i].wim_target_path);
1061 goto out_free_branch;
1064 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1067 if (root_dentry == NULL) {
1068 ret = new_filler_directory(T(""), &root_dentry);
1070 goto out_free_dentry_tree;
1073 DEBUG("Calculating full paths of dentries.");
1074 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1076 goto out_free_dentry_tree;
1078 ret = add_new_dentry_tree(w, root_dentry, sd);
1080 goto out_free_dentry_tree;
1082 imd = &w->image_metadata[w->hdr.image_count - 1];
1084 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1086 goto out_destroy_imd;
1088 DEBUG("Assigning hard link group IDs");
1089 assign_inode_numbers(&imd->inode_list);
1091 ret = xml_add_image(w, name);
1093 goto out_destroy_imd;
1095 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1096 wimlib_set_boot_idx(w, w->hdr.image_count);
1098 goto out_destroy_sd_set;
1100 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1102 w->hdr.image_count--;
1103 goto out_destroy_sd_set;
1105 free_dentry_tree(branch, w->lookup_table);
1106 out_free_dentry_tree:
1107 free_dentry_tree(root_dentry, w->lookup_table);
1108 out_free_security_data:
1109 free_security_data(sd);
1111 destroy_sd_set(&sd_set);
1112 out_destroy_capture_config:
1113 destroy_capture_config(&config);
1119 wimlib_add_image(WIMStruct *w,
1120 const tchar *source,
1122 const tchar *config_str,
1124 int add_image_flags,
1125 wimlib_progress_func_t progress_func)
1127 if (!source || !*source)
1128 return WIMLIB_ERR_INVALID_PARAM;
1130 tchar *fs_source_path = TSTRDUP(source);
1132 struct wimlib_capture_source capture_src = {
1133 .fs_source_path = fs_source_path,
1134 .wim_target_path = NULL,
1137 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1138 config_str, config_len,
1139 add_image_flags, progress_func);
1140 FREE(fs_source_path);