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"
57 * Adds the dentry tree and security data for a new image to the image metadata
58 * array of the WIMStruct.
61 add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
62 struct wim_security_data *sd)
64 struct wim_lookup_table_entry *metadata_lte;
65 struct wim_image_metadata *imd;
66 struct wim_image_metadata *new_imd;
68 wimlib_assert(root_dentry != NULL);
70 DEBUG("Reallocating image metadata array for image_count = %u",
71 w->hdr.image_count + 1);
72 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
75 ERROR("Failed to allocate memory for new image metadata array");
79 memcpy(imd, w->image_metadata,
80 w->hdr.image_count * sizeof(struct wim_image_metadata));
82 metadata_lte = new_lookup_table_entry();
86 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
87 random_hash(metadata_lte->hash);
88 lookup_table_insert(w->lookup_table, metadata_lte);
90 new_imd = &imd[w->hdr.image_count];
92 new_imd->root_dentry = root_dentry;
93 new_imd->metadata_lte = metadata_lte;
94 new_imd->security_data = sd;
95 new_imd->modified = 1;
97 FREE(w->image_metadata);
98 w->image_metadata = imd;
104 return WIMLIB_ERR_NOMEM;
110 * unix_build_dentry_tree():
111 * Recursively builds a tree of WIM dentries from an on-disk directory
112 * tree (UNIX version; no NTFS-specific data is captured).
114 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
115 * modified if successful. Set to NULL if the file or directory was
116 * excluded from capture.
118 * @root_disk_path: The path to the root of the directory tree on disk.
120 * @lookup_table: The lookup table for the WIM file. For each file added to the
121 * dentry tree being built, an entry is added to the lookup table,
122 * unless an identical stream is already in the lookup table.
123 * These lookup table entries that are added point to the path of
126 * @sd: Ignored. (Security data only captured in NTFS mode.)
129 * Configuration for files to be excluded from capture.
131 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
133 * @extra_arg: Ignored in UNIX builds; used to pass sd_set pointer in Windows
136 * @return: 0 on success, nonzero on failure. It is a failure if any of
137 * the files cannot be `stat'ed, or if any of the needed
138 * directories cannot be opened or read. Failure to add the files
139 * to the WIM may still occur later when trying to actually read
140 * the on-disk files during a call to wimlib_write() or
141 * wimlib_overwrite().
144 unix_build_dentry_tree(struct wim_dentry **root_ret,
145 const char *root_disk_path,
146 struct wim_lookup_table *lookup_table,
147 struct wim_security_data *sd,
148 const struct capture_config *config,
150 wimlib_progress_func_t progress_func,
153 struct wim_dentry *root = NULL;
155 struct wim_inode *inode;
157 if (exclude_path(root_disk_path, config, true)) {
158 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
159 ERROR("Cannot exclude the root directory from capture");
160 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
163 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
166 union wimlib_progress_info info;
167 info.scan.cur_path = root_disk_path;
168 info.scan.excluded = true;
169 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
174 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
177 union wimlib_progress_info info;
178 info.scan.cur_path = root_disk_path;
179 info.scan.excluded = false;
180 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
183 /* UNIX version of capturing a directory tree */
184 struct stat root_stbuf;
185 int (*stat_fn)(const char *restrict, struct stat *restrict);
186 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
191 ret = (*stat_fn)(root_disk_path, &root_stbuf);
193 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
197 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
198 !S_ISDIR(root_stbuf.st_mode))
200 /* Do a dereference-stat in case the root is a symbolic link.
201 * This case is allowed, provided that the symbolic link points
203 ret = stat(root_disk_path, &root_stbuf);
205 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
206 ret = WIMLIB_ERR_STAT;
209 if (!S_ISDIR(root_stbuf.st_mode)) {
210 ERROR("`%s' is not a directory", root_disk_path);
211 ret = WIMLIB_ERR_NOTDIR;
215 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
216 && !S_ISLNK(root_stbuf.st_mode)) {
217 ERROR("`%s' is not a regular file, directory, or symbolic link.",
219 ret = WIMLIB_ERR_SPECIAL_FILE;
223 ret = new_dentry_with_timeless_inode(path_basename(root_disk_path),
228 inode = root->d_inode;
230 #ifdef HAVE_STAT_NANOSECOND_PRECISION
231 inode->i_creation_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
232 inode->i_last_write_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
233 inode->i_last_access_time = timespec_to_wim_timestamp(root_stbuf.st_atim);
235 inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
236 inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
237 inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
239 /* Leave the inode number at 0 for directories. */
240 if (!S_ISDIR(root_stbuf.st_mode)) {
241 if (sizeof(ino_t) >= 8)
242 inode->i_ino = (u64)root_stbuf.st_ino;
244 inode->i_ino = (u64)root_stbuf.st_ino |
245 ((u64)root_stbuf.st_dev <<
246 ((sizeof(ino_t) * 8) & 63));
248 inode->i_resolved = 1;
249 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
250 ret = inode_set_unix_data(inode, root_stbuf.st_uid,
254 UNIX_DATA_ALL | UNIX_DATA_CREATE);
258 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
259 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
261 struct wim_lookup_table_entry *lte;
262 u8 hash[SHA1_HASH_SIZE];
264 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
266 /* Empty files do not have to have a lookup table entry. */
267 if (root_stbuf.st_size == 0)
270 /* For each regular file, we must check to see if the file is in
271 * the lookup table already; if it is, we increment its refcnt;
272 * otherwise, we create a new lookup table entry and insert it.
275 ret = sha1sum(root_disk_path, hash);
279 lte = __lookup_resource(lookup_table, hash);
282 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
285 char *file_on_disk = STRDUP(root_disk_path);
287 ERROR("Failed to allocate memory for file path");
288 ret = WIMLIB_ERR_NOMEM;
291 lte = new_lookup_table_entry();
294 ret = WIMLIB_ERR_NOMEM;
297 lte->file_on_disk = file_on_disk;
298 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
299 lte->resource_entry.original_size = root_stbuf.st_size;
300 lte->resource_entry.size = root_stbuf.st_size;
301 copy_hash(lte->hash, hash);
302 lookup_table_insert(lookup_table, lte);
304 root->d_inode->i_lte = lte;
305 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
307 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
310 struct dirent entry, *result;
311 struct wim_dentry *child;
313 dir = opendir(root_disk_path);
315 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
317 ret = WIMLIB_ERR_OPEN;
321 /* Buffer for names of files in directory. */
322 size_t len = strlen(root_disk_path);
323 char name[len + 1 + FILENAME_MAX + 1];
324 memcpy(name, root_disk_path, len);
327 /* Create a dentry for each entry in the directory on disk, and recurse
328 * to any subdirectories. */
331 ret = readdir_r(dir, &entry, &result);
333 ret = WIMLIB_ERR_READ;
334 ERROR_WITH_ERRNO("Error reading the "
341 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
342 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
344 strcpy(name + len + 1, result->d_name);
345 ret = unix_build_dentry_tree(&child, name,
349 progress_func, NULL);
353 dentry_add_child(root, child);
356 } else { /* Archiving a symbolic link */
357 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
358 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
360 /* The idea here is to call readlink() to get the UNIX target of
361 * the symbolic link, then turn the target into a reparse point
362 * data buffer that contains a relative or absolute symbolic
363 * link (NOT a junction point or *full* path symbolic link with
367 char deref_name_buf[4096];
368 ssize_t deref_name_len;
370 deref_name_len = readlink(root_disk_path, deref_name_buf,
371 sizeof(deref_name_buf) - 1);
372 if (deref_name_len >= 0) {
373 deref_name_buf[deref_name_len] = '\0';
374 DEBUG("Read symlink `%s'", deref_name_buf);
375 ret = inode_set_symlink(root->d_inode, deref_name_buf,
379 * Unfortunately, Windows seems to have the
380 * concept of "file" symbolic links as being
381 * different from "directory" symbolic links...
382 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
383 * on the symbolic link if the *target* of the
384 * symbolic link is a directory.
387 if (stat(root_disk_path, &stbuf) == 0 &&
388 S_ISDIR(stbuf.st_mode))
390 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
394 ERROR_WITH_ERRNO("Failed to read target of "
395 "symbolic link `%s'", root_disk_path);
396 ret = WIMLIB_ERR_READLINK;
403 free_dentry_tree(root, lookup_table);
406 #endif /* !__WIN32__ */
412 COMPRESSION_EXCLUSION_LIST,
416 #define COMPAT_DEFAULT_CONFIG
418 /* Default capture configuration file when none is specified. */
419 static const tchar *default_config =
420 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
421 users. The next ABI-incompatible library
422 version will default to the empty string here. */
428 "\\System Volume Information\n"
432 "[CompressionExclusionList]\n"
436 "\\WINDOWS\\inf\\*.pnf\n"
443 destroy_pattern_list(struct pattern_list *list)
449 destroy_capture_config(struct capture_config *config)
451 destroy_pattern_list(&config->exclusion_list);
452 destroy_pattern_list(&config->exclusion_exception);
453 destroy_pattern_list(&config->compression_exclusion_list);
454 destroy_pattern_list(&config->alignment_list);
455 FREE(config->config_str);
456 FREE(config->prefix);
457 memset(config, 0, sizeof(*config));
461 pattern_list_add_pattern(struct pattern_list *list, const tchar *pattern)
464 if (list->num_pats >= list->num_allocated_pats) {
465 pats = REALLOC(list->pats,
466 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
468 return WIMLIB_ERR_NOMEM;
469 list->num_allocated_pats += 8;
472 list->pats[list->num_pats++] = pattern;
476 /* Parses the contents of the image capture configuration file and fills in a
477 * `struct capture_config'. */
479 init_capture_config(struct capture_config *config,
480 const tchar *_config_str,
481 size_t config_num_tchars)
487 size_t num_tchars_remaining;
488 enum pattern_type type = NONE;
490 unsigned long line_no = 0;
492 DEBUG("config_num_tchars = %zu", config_num_tchars);
493 num_tchars_remaining = config_num_tchars;
494 memset(config, 0, sizeof(*config));
495 config_str = TMALLOC(config_num_tchars);
497 ERROR("Could not duplicate capture config string");
498 return WIMLIB_ERR_NOMEM;
501 tmemcpy(config_str, _config_str, config_num_tchars);
503 config->config_str = config_str;
504 while (num_tchars_remaining != 0) {
507 eol = tmemchr(p, T('\n'), num_tchars_remaining);
509 ERROR("Expected end-of-line in capture config file on "
510 "line %lu", line_no);
511 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
516 num_tchars_remaining -= (next_p - p);
520 if (*(eol - 1) == T('\r'))
524 /* Translate backslash to forward slash */
525 for (tchar *pp = p; pp != eol; pp++)
529 /* Remove drive letter
530 * XXX maybe keep drive letter on Windows */
531 if (eol - p > 2 && istalpha(*p) && *(p + 1) == T(':'))
535 if (tstrcmp(p, T("[ExclusionList]")) == 0)
536 type = EXCLUSION_LIST;
537 else if (tstrcmp(p, T("[ExclusionException]")) == 0)
538 type = EXCLUSION_EXCEPTION;
539 else if (tstrcmp(p, T("[CompressionExclusionList]")) == 0)
540 type = COMPRESSION_EXCLUSION_LIST;
541 else if (tstrcmp(p, T("[AlignmentList]")) == 0)
542 type = ALIGNMENT_LIST;
543 else if (p[0] == T('[') && tstrrchr(p, T(']'))) {
544 ERROR("Unknown capture configuration section \"%"TS"\"", p);
545 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
546 } else switch (type) {
548 DEBUG("Adding pattern \"%"TS"\" to exclusion list", p);
549 ret = pattern_list_add_pattern(&config->exclusion_list, p);
551 case EXCLUSION_EXCEPTION:
552 DEBUG("Adding pattern \"%"TS"\" to exclusion exception list", p);
553 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
555 case COMPRESSION_EXCLUSION_LIST:
556 DEBUG("Adding pattern \"%"TS"\" to compression exclusion list", p);
557 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
560 DEBUG("Adding pattern \"%"TS"\" to alignment list", p);
561 ret = pattern_list_add_pattern(&config->alignment_list, p);
564 ERROR("Line %lu of capture configuration is not "
565 "in a block (such as [ExclusionList])",
567 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
575 destroy_capture_config(config);
579 static int capture_config_set_prefix(struct capture_config *config,
580 const tchar *_prefix)
582 tchar *prefix = TSTRDUP(_prefix);
585 return WIMLIB_ERR_NOMEM;
586 FREE(config->prefix);
587 config->prefix = prefix;
588 config->prefix_num_tchars = tstrlen(prefix);
592 static bool match_pattern(const tchar *path,
593 const tchar *path_basename,
594 const struct pattern_list *list)
596 for (size_t i = 0; i < list->num_pats; i++) {
597 const tchar *pat = list->pats[i];
600 /* Absolute path from root of capture */
603 if (tstrchr(pat, T('/')))
604 /* Relative path from root of capture */
607 /* A file name pattern */
608 string = path_basename;
611 /* Warning: on Windows native builds, fnmatch() calls the
612 * replacement function in win32.c. */
613 if (fnmatch(pat, string, FNM_PATHNAME
619 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
627 /* Return true if the image capture configuration file indicates we should
628 * exclude the filename @path from capture.
630 * If @exclude_prefix is %true, the part of the path up and including the name
631 * of the directory being captured is not included in the path for matching
632 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
633 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
637 exclude_path(const tchar *path, const struct capture_config *config,
640 const tchar *basename = path_basename(path);
641 if (exclude_prefix) {
642 wimlib_assert(tstrlen(path) >= config->prefix_num_tchars);
643 if (tmemcmp(config->prefix, path, config->prefix_num_tchars) == 0
644 && path[config->prefix_num_tchars] == T('/'))
645 path += config->prefix_num_tchars;
647 return match_pattern(path, basename, &config->exclusion_list) &&
648 !match_pattern(path, basename, &config->exclusion_exception);
652 /* Strip leading and trailing forward slashes from a string. Modifies it in
653 * place and returns the stripped string. */
655 canonicalize_target_path(tchar *target_path)
658 if (target_path == NULL)
661 if (*target_path == T('\0'))
663 else if (*target_path == T('/'))
669 p = target_path + tstrlen(target_path) - 1;
676 zap_backslashes(tchar *s)
685 /* Strip leading and trailing slashes from the target paths */
687 canonicalize_targets(struct wimlib_capture_source *sources, size_t num_sources)
689 while (num_sources--) {
690 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
691 sources->fs_source_path,
692 sources->wim_target_path);
694 /* The Windows API can handle forward slashes. Just get rid of
695 * backslashes to avoid confusing other parts of the library
697 zap_backslashes(sources->fs_source_path);
698 if (sources->wim_target_path)
699 zap_backslashes(sources->wim_target_path);
701 sources->wim_target_path =
702 (tchar*)canonicalize_target_path(sources->wim_target_path);
703 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
709 capture_source_cmp(const void *p1, const void *p2)
711 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
712 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
715 /* Sorts the capture sources lexicographically by target path. This occurs
716 * after leading and trailing forward slashes are stripped.
718 * One purpose of this is to make sure that target paths that are inside other
719 * target paths are added after the containing target paths. */
721 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
723 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
727 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
730 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
731 if (num_sources != 1) {
732 ERROR("Must specify exactly 1 capture source "
733 "(the NTFS volume) in NTFS mode!");
734 return WIMLIB_ERR_INVALID_PARAM;
736 if (sources[0].wim_target_path[0] != '\0') {
737 ERROR("In NTFS capture mode the target path inside "
738 "the image must be the root directory!");
739 return WIMLIB_ERR_INVALID_PARAM;
741 } else if (num_sources != 0) {
742 /* This code is disabled because the current code
743 * unconditionally attempts to do overlays. So, duplicate
744 * target paths are OK. */
746 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
747 ERROR("Cannot specify root target when using multiple "
749 return WIMLIB_ERR_INVALID_PARAM;
751 for (size_t i = 0; i < num_sources - 1; i++) {
752 size_t len = strlen(sources[i].wim_target_path);
754 const char *target1 = sources[i].wim_target_path;
756 const char *target2 = sources[j].wim_target_path;
757 DEBUG("target1=%s, target2=%s",
759 if (strncmp(target1, target2, len) ||
762 if (target2[len] == '/') {
763 ERROR("Invalid target `%s': is a prefix of `%s'",
765 return WIMLIB_ERR_INVALID_PARAM;
767 if (target2[len] == '\0') {
768 ERROR("Invalid target `%s': is a duplicate of `%s'",
770 return WIMLIB_ERR_INVALID_PARAM;
772 } while (++j != num_sources);
780 /* Creates a new directory to place in the WIM image. This is to create parent
781 * directories that are not part of any target as needed. */
783 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
786 struct wim_dentry *dentry;
788 DEBUG("Creating filler directory \"%"TS"\"", name);
789 ret = new_dentry_with_inode(name, &dentry);
791 /* Leave the inode number as 0 for now. The final inode number
792 * will be assigned later by assign_inode_numbers(). */
793 dentry->d_inode->i_resolved = 1;
794 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
795 *dentry_ret = dentry;
800 /* Transfers the children of @branch to @target. It is an error if @target is
801 * not a directory or if both @branch and @target contain a child dentry with
804 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
806 struct rb_root *rb_root;
808 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
809 branch->file_name, target->file_name);
811 if (!dentry_is_directory(target)) {
812 ERROR("Cannot overlay directory \"%"WS"\" "
813 "over non-directory", branch->file_name);
814 return WIMLIB_ERR_INVALID_OVERLAY;
817 rb_root = &branch->d_inode->i_children;
818 while (rb_root->rb_node) { /* While @branch has children... */
819 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
820 /* Move @child to the directory @target */
821 unlink_dentry(child);
822 if (!dentry_add_child(target, child)) {
823 /* Revert the change to avoid leaking the directory tree
824 * rooted at @child */
825 dentry_add_child(branch, child);
826 ERROR("Overlay error: file \"%"WS"\" already exists "
827 "as a child of \"%"WS"\"",
828 child->file_name, target->file_name);
829 return WIMLIB_ERR_INVALID_OVERLAY;
837 /* Attach or overlay a branch onto the WIM image.
840 * Pointer to the root of the WIM image, or pointer to NULL if it has not
845 * Path in the WIM image to add the branch, with leading and trailing
849 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
853 struct wim_dentry *dentry, *parent, *target;
856 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
857 branch->file_name, target_path);
859 if (*target_path == T('\0')) {
860 /* Target: root directory */
862 /* Overlay on existing root */
863 return do_overlay(*root_p, branch);
871 /* Adding a non-root branch. Create root if it hasn't been created
874 ret = new_filler_directory(T(""), root_p);
879 /* Walk the path to the branch, creating filler directories as needed.
882 while ((slash = tstrchr(target_path, T('/')))) {
884 dentry = get_dentry_child_with_name(parent, target_path);
886 ret = new_filler_directory(target_path, &dentry);
889 dentry_add_child(parent, dentry);
893 /* Skip over slashes. Note: this cannot overrun the length of
894 * the string because the last character cannot be a slash, as
895 * trailing slashes were tripped. */
898 } while (*target_path == T('/'));
901 /* If the target path already existed, overlay the branch onto it.
902 * Otherwise, set the branch as the target path. */
903 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
904 branch->file_name_nbytes);
906 return do_overlay(target, branch);
908 dentry_add_child(parent, branch);
914 wimlib_add_image_multisource(WIMStruct *w,
915 struct wimlib_capture_source *sources,
918 const tchar *config_str,
921 wimlib_progress_func_t progress_func)
923 int (*capture_tree)(struct wim_dentry **,
925 struct wim_lookup_table *,
926 struct wim_security_data *,
927 const struct capture_config *,
929 wimlib_progress_func_t,
932 struct wim_dentry *root_dentry;
933 struct wim_dentry *branch;
934 struct wim_security_data *sd;
935 struct capture_config config;
936 struct wim_image_metadata *imd;
939 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
941 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
942 ERROR("Cannot dereference files when capturing directly from NTFS");
943 return WIMLIB_ERR_INVALID_PARAM;
945 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
946 ERROR("Capturing UNIX owner and mode not supported "
947 "when capturing directly from NTFS");
948 return WIMLIB_ERR_INVALID_PARAM;
950 capture_tree = build_dentry_tree_ntfs;
951 extra_arg = &w->ntfs_vol;
953 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
954 " cannot capture a WIM image directly from a NTFS volume!");
955 return WIMLIB_ERR_UNSUPPORTED;
959 capture_tree = win32_build_dentry_tree;
961 capture_tree = unix_build_dentry_tree;
967 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
968 ERROR("Capturing UNIX-specific data is not supported on Windows");
969 return WIMLIB_ERR_INVALID_PARAM;
971 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
972 ERROR("Dereferencing symbolic links is not supported on Windows");
973 return WIMLIB_ERR_INVALID_PARAM;
977 if (!name || !*name) {
978 ERROR("Must specify a non-empty string for the image name");
979 return WIMLIB_ERR_INVALID_PARAM;
982 if (w->hdr.total_parts != 1) {
983 ERROR("Cannot add an image to a split WIM");
984 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
987 if (wimlib_image_name_in_use(w, name)) {
988 ERROR("There is already an image named \"%"TS"\" in the WIM!",
990 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
994 DEBUG("Using default capture configuration");
995 config_str = default_config;
996 config_len = tstrlen(default_config);
998 ret = init_capture_config(&config, config_str, config_len);
1002 DEBUG("Allocating security data");
1003 sd = CALLOC(1, sizeof(struct wim_security_data));
1005 ret = WIMLIB_ERR_NOMEM;
1006 goto out_destroy_capture_config;
1008 sd->total_length = 8;
1011 DEBUG("Using %zu capture sources", num_sources);
1012 canonicalize_targets(sources, num_sources);
1013 sort_sources(sources, num_sources);
1014 ret = check_sorted_sources(sources, num_sources, add_image_flags);
1016 ret = WIMLIB_ERR_INVALID_PARAM;
1017 goto out_free_security_data;
1020 DEBUG("Building dentry tree.");
1023 for (size_t i = 0; i < num_sources; i++) {
1025 union wimlib_progress_info progress;
1027 DEBUG("Building dentry tree for source %zu of %zu "
1028 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
1029 sources[i].fs_source_path,
1030 sources[i].wim_target_path);
1031 if (progress_func) {
1032 memset(&progress, 0, sizeof(progress));
1033 progress.scan.source = sources[i].fs_source_path;
1034 progress.scan.wim_target_path = sources[i].wim_target_path;
1035 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1037 ret = capture_config_set_prefix(&config,
1038 sources[i].fs_source_path);
1040 goto out_free_dentry_tree;
1041 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
1042 if (!*sources[i].wim_target_path)
1043 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
1044 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
1045 w->lookup_table, sd,
1048 progress_func, extra_arg);
1050 ERROR("Failed to build dentry tree for `%"TS"'",
1051 sources[i].fs_source_path);
1052 goto out_free_dentry_tree;
1055 /* Use the target name, not the source name, for
1056 * the root of each branch from a capture
1057 * source. (This will also set the root dentry
1058 * of the entire image to be unnamed.) */
1059 ret = set_dentry_name(branch,
1060 path_basename(sources[i].wim_target_path));
1062 goto out_free_branch;
1064 ret = attach_branch(&root_dentry, branch,
1065 sources[i].wim_target_path);
1067 goto out_free_branch;
1070 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1073 if (root_dentry == NULL) {
1074 ret = new_filler_directory(T(""), &root_dentry);
1076 goto out_free_dentry_tree;
1079 DEBUG("Calculating full paths of dentries.");
1080 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1082 goto out_free_dentry_tree;
1084 ret = add_new_dentry_tree(w, root_dentry, sd);
1086 goto out_free_dentry_tree;
1088 imd = &w->image_metadata[w->hdr.image_count - 1];
1090 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1092 goto out_destroy_imd;
1094 DEBUG("Assigning hard link group IDs");
1095 assign_inode_numbers(&imd->inode_list);
1097 ret = xml_add_image(w, name);
1099 goto out_destroy_imd;
1101 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1102 wimlib_set_boot_idx(w, w->hdr.image_count);
1104 goto out_destroy_capture_config;
1106 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1108 w->hdr.image_count--;
1111 free_dentry_tree(branch, w->lookup_table);
1112 out_free_dentry_tree:
1113 free_dentry_tree(root_dentry, w->lookup_table);
1114 out_free_security_data:
1115 free_security_data(sd);
1116 out_destroy_capture_config:
1117 destroy_capture_config(&config);
1123 wimlib_add_image(WIMStruct *w,
1124 const tchar *source,
1126 const tchar *config_str,
1128 int add_image_flags,
1129 wimlib_progress_func_t progress_func)
1131 if (!source || !*source)
1132 return WIMLIB_ERR_INVALID_PARAM;
1134 tchar *fs_source_path = TSTRDUP(source);
1136 struct wimlib_capture_source capture_src = {
1137 .fs_source_path = fs_source_path,
1138 .wim_target_path = NULL,
1141 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1142 config_str, config_len,
1143 add_image_flags, progress_func);
1144 FREE(fs_source_path);