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"
54 * Adds the dentry tree and security data for a new image to the image metadata
55 * array of the WIMStruct.
58 add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
59 struct wim_security_data *sd)
61 struct wim_lookup_table_entry *metadata_lte;
62 struct wim_image_metadata *imd;
63 struct wim_image_metadata *new_imd;
65 wimlib_assert(root_dentry != NULL);
67 DEBUG("Reallocating image metadata array for image_count = %u",
68 w->hdr.image_count + 1);
69 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
72 ERROR("Failed to allocate memory for new image metadata array");
76 memcpy(imd, w->image_metadata,
77 w->hdr.image_count * sizeof(struct wim_image_metadata));
79 metadata_lte = new_lookup_table_entry();
83 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
85 new_imd = &imd[w->hdr.image_count];
87 new_imd->root_dentry = root_dentry;
88 new_imd->metadata_lte = metadata_lte;
89 new_imd->security_data = sd;
90 new_imd->modified = 1;
92 FREE(w->image_metadata);
93 w->image_metadata = imd;
99 return WIMLIB_ERR_NOMEM;
106 unix_capture_regular_file(const char *path,
108 struct wim_inode *inode,
109 struct wim_lookup_table *lookup_table)
111 struct wim_lookup_table_entry *lte;
112 u8 hash[SHA1_HASH_SIZE];
115 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
117 /* Empty files do not have to have a lookup table entry. */
121 /* For each regular file, we must check to see if the file is in
122 * the lookup table already; if it is, we increment its refcnt;
123 * otherwise, we create a new lookup table entry and insert it.
126 ret = sha1sum(path, hash);
130 lte = __lookup_resource(lookup_table, hash);
133 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
136 char *file_on_disk = STRDUP(path);
138 ERROR("Failed to allocate memory for file path");
139 return WIMLIB_ERR_NOMEM;
141 lte = new_lookup_table_entry();
144 return WIMLIB_ERR_NOMEM;
146 lte->file_on_disk = file_on_disk;
147 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
148 lte->resource_entry.original_size = size;
149 lte->resource_entry.size = size;
150 copy_hash(lte->hash, hash);
151 lookup_table_insert(lookup_table, lte);
158 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
161 struct wim_lookup_table *lookup_table,
162 const struct wimlib_capture_config *config,
164 wimlib_progress_func_t progress_func);
167 unix_capture_directory(struct wim_dentry *dir_dentry,
170 struct wim_lookup_table *lookup_table,
171 const struct wimlib_capture_config *config,
173 wimlib_progress_func_t progress_func)
177 struct dirent entry, *result;
178 struct wim_dentry *child;
181 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
184 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
186 return WIMLIB_ERR_OPEN;
189 /* Recurse on directory contents */
192 ret = readdir_r(dir, &entry, &result);
194 ret = WIMLIB_ERR_READ;
195 ERROR_WITH_ERRNO("Error reading the "
202 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
203 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
206 size_t name_len = strlen(result->d_name);
208 path[path_len] = '/';
209 memcpy(&path[path_len + 1], result->d_name, name_len + 1);
210 ret = unix_build_dentry_tree_recursive(&child,
212 path_len + 1 + name_len,
220 dentry_add_child(dir_dentry, child);
227 unix_capture_symlink(const char *path,
228 struct wim_inode *inode,
229 struct wim_lookup_table *lookup_table)
231 char deref_name_buf[4096];
232 ssize_t deref_name_len;
235 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
236 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
238 /* The idea here is to call readlink() to get the UNIX target of
239 * the symbolic link, then turn the target into a reparse point
240 * data buffer that contains a relative or absolute symbolic
241 * link (NOT a junction point or *full* path symbolic link with
244 deref_name_len = readlink(path, deref_name_buf,
245 sizeof(deref_name_buf) - 1);
246 if (deref_name_len >= 0) {
247 deref_name_buf[deref_name_len] = '\0';
248 DEBUG("Read symlink `%s'", deref_name_buf);
249 ret = inode_set_symlink(inode, deref_name_buf,
252 /* Unfortunately, Windows seems to have the concept of
253 * "file" symbolic links as being different from
254 * "directory" symbolic links... so
255 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
256 * symbolic link if the *target* of the symbolic link is
259 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
260 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
263 ERROR_WITH_ERRNO("Failed to read target of "
264 "symbolic link `%s'", path);
265 ret = WIMLIB_ERR_READLINK;
271 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
274 struct wim_lookup_table *lookup_table,
275 const struct wimlib_capture_config *config,
277 wimlib_progress_func_t progress_func)
279 struct wim_dentry *root = NULL;
281 struct wim_inode *inode;
283 if (exclude_path(path, path_len, config, true)) {
284 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
285 ERROR("Cannot exclude the root directory from capture");
286 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
289 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
292 union wimlib_progress_info info;
293 info.scan.cur_path = path;
294 info.scan.excluded = true;
295 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
300 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
303 union wimlib_progress_info info;
304 info.scan.cur_path = path;
305 info.scan.excluded = false;
306 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
309 /* UNIX version of capturing a directory tree */
311 int (*stat_fn)(const char *restrict, struct stat *restrict);
312 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
317 ret = (*stat_fn)(path, &stbuf);
319 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
323 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
324 !S_ISDIR(stbuf.st_mode))
326 /* Do a dereference-stat in case the root is a symbolic link.
327 * This case is allowed, provided that the symbolic link points
329 ret = stat(path, &stbuf);
331 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
332 ret = WIMLIB_ERR_STAT;
335 if (!S_ISDIR(stbuf.st_mode)) {
336 ERROR("`%s' is not a directory", path);
337 ret = WIMLIB_ERR_NOTDIR;
341 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
342 && !S_ISLNK(stbuf.st_mode)) {
343 ERROR("`%s' is not a regular file, directory, or symbolic link.",
345 ret = WIMLIB_ERR_SPECIAL_FILE;
349 ret = new_dentry_with_timeless_inode(path_basename_with_len(path, path_len),
354 inode = root->d_inode;
356 #ifdef HAVE_STAT_NANOSECOND_PRECISION
357 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
358 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
359 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
361 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
362 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
363 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
365 /* Leave the inode number at 0 for directories. Otherwise grab the
366 * inode number from the `stat' buffer, including the device number if
368 if (!S_ISDIR(stbuf.st_mode)) {
369 if (sizeof(ino_t) >= 8)
370 inode->i_ino = (u64)stbuf.st_ino;
372 inode->i_ino = (u64)stbuf.st_ino |
373 ((u64)stbuf.st_dev <<
374 ((sizeof(ino_t) * 8) & 63));
376 inode->i_resolved = 1;
377 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
378 ret = inode_set_unix_data(inode, stbuf.st_uid,
382 UNIX_DATA_ALL | UNIX_DATA_CREATE);
386 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
387 if (S_ISREG(stbuf.st_mode))
388 ret = unix_capture_regular_file(path, stbuf.st_size,
389 inode, lookup_table);
390 else if (S_ISDIR(stbuf.st_mode))
391 ret = unix_capture_directory(root, path, path_len,
392 lookup_table, config,
393 add_image_flags, progress_func);
395 ret = unix_capture_symlink(path, inode, lookup_table);
400 free_dentry_tree(root, lookup_table);
405 * unix_build_dentry_tree():
406 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
407 * version; no NTFS-specific data is captured).
409 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
410 * modified if successful. Set to NULL if the file or directory was
411 * excluded from capture.
413 * @root_disk_path: The path to the root of the directory tree on disk.
415 * @lookup_table: The lookup table for the WIM file. For each file added to the
416 * dentry tree being built, an entry is added to the lookup table,
417 * unless an identical stream is already in the lookup table.
418 * These lookup table entries that are added point to the path of
421 * @sd_set: Ignored. (Security data only captured in NTFS mode.)
424 * Configuration for files to be excluded from capture.
426 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
428 * @extra_arg: Ignored
430 * @return: 0 on success, nonzero on failure. It is a failure if any of
431 * the files cannot be `stat'ed, or if any of the needed
432 * directories cannot be opened or read. Failure to add the files
433 * to the WIM may still occur later when trying to actually read
434 * the on-disk files during a call to wimlib_write() or
435 * wimlib_overwrite().
438 unix_build_dentry_tree(struct wim_dentry **root_ret,
439 const char *root_disk_path,
440 struct wim_lookup_table *lookup_table,
441 struct sd_set *sd_set,
442 const struct wimlib_capture_config *config,
444 wimlib_progress_func_t progress_func,
452 path_bufsz = min(32790, PATH_MAX + 1);
453 path_len = strlen(root_disk_path);
455 if (path_len >= path_bufsz)
456 return WIMLIB_ERR_INVALID_PARAM;
458 path_buf = MALLOC(path_bufsz);
460 return WIMLIB_ERR_NOMEM;
461 memcpy(path_buf, root_disk_path, path_len + 1);
462 ret = unix_build_dentry_tree_recursive(root_ret,
472 #endif /* !__WIN32__ */
475 match_pattern(const tchar *path,
476 const tchar *path_basename,
477 const struct wimlib_pattern_list *list)
479 for (size_t i = 0; i < list->num_pats; i++) {
481 const tchar *pat = list->pats[i];
484 if (*pat == T('/')) {
485 /* Absolute path from root of capture */
488 if (tstrchr(pat, T('/')))
489 /* Relative path from root of capture */
492 /* A file name pattern */
493 string = path_basename;
496 /* Warning: on Windows native builds, fnmatch() calls the
497 * replacement function in win32.c. */
498 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
504 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
508 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
515 /* Return true if the image capture configuration file indicates we should
516 * exclude the filename @path from capture.
518 * If @exclude_prefix is %true, the part of the path up and including the name
519 * of the directory being captured is not included in the path for matching
520 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
521 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
525 exclude_path(const tchar *path, size_t path_len,
526 const struct wimlib_capture_config *config, bool exclude_prefix)
528 const tchar *basename = path_basename_with_len(path, path_len);
529 if (exclude_prefix) {
530 wimlib_assert(path_len >= config->_prefix_num_tchars);
531 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
532 path[config->_prefix_num_tchars] == T('/'))
534 path += config->_prefix_num_tchars;
537 return match_pattern(path, basename, &config->exclusion_pats) &&
538 !match_pattern(path, basename, &config->exclusion_exception_pats);
542 /* Strip leading and trailing forward slashes from a string. Modifies it in
543 * place and returns the stripped string. */
545 canonicalize_target_path(tchar *target_path)
548 if (target_path == NULL)
551 if (*target_path == T('\0'))
553 else if (*target_path == T('/'))
559 p = tstrchr(target_path, T('\0')) - 1;
565 /* Strip leading and trailing slashes from the target paths, and translate all
566 * backslashes in the source and target paths into forward slashes. */
568 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
571 while (num_sources--) {
572 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
573 sources->fs_source_path,
574 sources->wim_target_path);
576 /* The Windows API can handle forward slashes. Just get rid of
577 * backslashes to avoid confusing other parts of the library
579 zap_backslashes(sources->fs_source_path);
580 if (sources->wim_target_path)
581 zap_backslashes(sources->wim_target_path);
583 sources->wim_target_path =
584 (tchar*)canonicalize_target_path(sources->wim_target_path);
585 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
591 capture_source_cmp(const void *p1, const void *p2)
593 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
594 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
597 /* Sorts the capture sources lexicographically by target path. This occurs
598 * after leading and trailing forward slashes are stripped.
600 * One purpose of this is to make sure that target paths that are inside other
601 * target paths are added after the containing target paths. */
603 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
605 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
609 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
612 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
613 if (num_sources != 1) {
614 ERROR("Must specify exactly 1 capture source "
615 "(the NTFS volume) in NTFS mode!");
616 return WIMLIB_ERR_INVALID_PARAM;
618 if (sources[0].wim_target_path[0] != T('\0')) {
619 ERROR("In NTFS capture mode the target path inside "
620 "the image must be the root directory!");
621 return WIMLIB_ERR_INVALID_PARAM;
623 } else if (num_sources != 0) {
624 /* This code is disabled because the current code
625 * unconditionally attempts to do overlays. So, duplicate
626 * target paths are OK. */
628 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
629 ERROR("Cannot specify root target when using multiple "
631 return WIMLIB_ERR_INVALID_PARAM;
633 for (size_t i = 0; i < num_sources - 1; i++) {
634 size_t len = strlen(sources[i].wim_target_path);
636 const char *target1 = sources[i].wim_target_path;
638 const char *target2 = sources[j].wim_target_path;
639 DEBUG("target1=%s, target2=%s",
641 if (strncmp(target1, target2, len) ||
644 if (target2[len] == '/') {
645 ERROR("Invalid target `%s': is a prefix of `%s'",
647 return WIMLIB_ERR_INVALID_PARAM;
649 if (target2[len] == '\0') {
650 ERROR("Invalid target `%s': is a duplicate of `%s'",
652 return WIMLIB_ERR_INVALID_PARAM;
654 } while (++j != num_sources);
662 /* Creates a new directory to place in the WIM image. This is to create parent
663 * directories that are not part of any target as needed. */
665 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
668 struct wim_dentry *dentry;
670 DEBUG("Creating filler directory \"%"TS"\"", name);
671 ret = new_dentry_with_inode(name, &dentry);
673 /* Leave the inode number as 0 for now. The final inode number
674 * will be assigned later by assign_inode_numbers(). */
675 dentry->d_inode->i_resolved = 1;
676 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
677 *dentry_ret = dentry;
682 /* Transfers the children of @branch to @target. It is an error if @target is
683 * not a directory or if both @branch and @target contain a child dentry with
686 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
688 struct rb_root *rb_root;
690 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
691 branch->file_name, target->file_name);
693 if (!dentry_is_directory(target)) {
694 ERROR("Cannot overlay directory \"%"WS"\" "
695 "over non-directory", branch->file_name);
696 return WIMLIB_ERR_INVALID_OVERLAY;
699 rb_root = &branch->d_inode->i_children;
700 while (rb_root->rb_node) { /* While @branch has children... */
701 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
702 /* Move @child to the directory @target */
703 unlink_dentry(child);
704 if (!dentry_add_child(target, child)) {
705 /* Revert the change to avoid leaking the directory tree
706 * rooted at @child */
707 dentry_add_child(branch, child);
708 ERROR("Overlay error: file \"%"WS"\" already exists "
709 "as a child of \"%"WS"\"",
710 child->file_name, target->file_name);
711 return WIMLIB_ERR_INVALID_OVERLAY;
719 /* Attach or overlay a branch onto the WIM image.
722 * Pointer to the root of the WIM image, or pointer to NULL if it has not
727 * Path in the WIM image to add the branch, with leading and trailing
731 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
735 struct wim_dentry *dentry, *parent, *target;
738 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
739 branch->file_name, target_path);
741 if (*target_path == T('\0')) {
742 /* Target: root directory */
744 /* Overlay on existing root */
745 return do_overlay(*root_p, branch);
753 /* Adding a non-root branch. Create root if it hasn't been created
756 ret = new_filler_directory(T(""), root_p);
761 /* Walk the path to the branch, creating filler directories as needed.
764 while ((slash = tstrchr(target_path, T('/')))) {
766 dentry = get_dentry_child_with_name(parent, target_path);
768 ret = new_filler_directory(target_path, &dentry);
771 dentry_add_child(parent, dentry);
775 /* Skip over slashes. Note: this cannot overrun the length of
776 * the string because the last character cannot be a slash, as
777 * trailing slashes were tripped. */
780 } while (*target_path == T('/'));
783 /* If the target path already existed, overlay the branch onto it.
784 * Otherwise, set the branch as the target path. */
785 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
786 branch->file_name_nbytes);
788 return do_overlay(target, branch);
790 dentry_add_child(parent, branch);
796 canonicalize_pat(tchar **pat_p)
800 /* Turn all backslashes in the pattern into forward slashes. */
801 zap_backslashes(pat);
803 if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
804 /* Pattern begins with drive letter */
805 if (*(pat + 2) != T('/')) {
806 /* Something like c:file, which is actually a path
807 * relative to the current working directory on the c:
808 * drive. We require paths with drive letters to be
810 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
811 "must be absolute!", pat);
812 ERROR("Maybe try \"%"TC":/%"TS"\"?",
814 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
817 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
818 "being removed.", pat);
819 /* Strip the drive letter */
827 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
830 for (size_t i = 0; i < pat_list->num_pats; i++) {
831 ret = canonicalize_pat(&pat_list->pats[i]);
839 canonicalize_capture_config(struct wimlib_capture_config *config)
841 int ret = canonicalize_pat_list(&config->exclusion_pats);
844 return canonicalize_pat_list(&config->exclusion_exception_pats);
848 wimlib_add_image_multisource(WIMStruct *w,
849 struct wimlib_capture_source *sources,
852 struct wimlib_capture_config *config,
854 wimlib_progress_func_t progress_func)
856 int (*capture_tree)(struct wim_dentry **,
858 struct wim_lookup_table *,
860 const struct wimlib_capture_config *,
862 wimlib_progress_func_t,
865 struct wim_dentry *root_dentry;
866 struct wim_dentry *branch;
867 struct wim_security_data *sd;
868 struct wim_image_metadata *imd;
870 struct sd_set sd_set;
872 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
874 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
875 ERROR("Cannot dereference files when capturing directly from NTFS");
876 return WIMLIB_ERR_INVALID_PARAM;
878 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
879 ERROR("Capturing UNIX owner and mode not supported "
880 "when capturing directly from NTFS");
881 return WIMLIB_ERR_INVALID_PARAM;
883 capture_tree = build_dentry_tree_ntfs;
884 extra_arg = &w->ntfs_vol;
886 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
887 " cannot capture a WIM image directly from a NTFS volume!");
888 return WIMLIB_ERR_UNSUPPORTED;
892 capture_tree = win32_build_dentry_tree;
894 capture_tree = unix_build_dentry_tree;
900 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
901 ERROR("Capturing UNIX-specific data is not supported on Windows");
902 return WIMLIB_ERR_INVALID_PARAM;
904 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
905 ERROR("Dereferencing symbolic links is not supported on Windows");
906 return WIMLIB_ERR_INVALID_PARAM;
910 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
911 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
913 if (!name || !*name) {
914 ERROR("Must specify a non-empty string for the image name");
915 return WIMLIB_ERR_INVALID_PARAM;
918 if (w->hdr.total_parts != 1) {
919 ERROR("Cannot add an image to a split WIM");
920 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
923 if (wimlib_image_name_in_use(w, name)) {
924 ERROR("There is already an image named \"%"TS"\" in the WIM!",
926 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
930 DEBUG("Capture config not provided; using empty config");
931 config = alloca(sizeof(*config));
932 memset(config, 0, sizeof(*config));
935 ret = canonicalize_capture_config(config);
939 DEBUG("Allocating security data");
940 sd = CALLOC(1, sizeof(struct wim_security_data));
942 ret = WIMLIB_ERR_NOMEM;
945 sd->total_length = 8;
949 sd_set.rb_root.rb_node = NULL;
951 DEBUG("Using %zu capture sources", num_sources);
952 canonicalize_sources_and_targets(sources, num_sources);
953 sort_sources(sources, num_sources);
954 ret = check_sorted_sources(sources, num_sources, add_image_flags);
956 ret = WIMLIB_ERR_INVALID_PARAM;
957 goto out_free_security_data;
960 DEBUG("Building dentry tree.");
963 for (size_t i = 0; i < num_sources; i++) {
965 union wimlib_progress_info progress;
967 DEBUG("Building dentry tree for source %zu of %zu "
968 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
969 sources[i].fs_source_path,
970 sources[i].wim_target_path);
972 memset(&progress, 0, sizeof(progress));
973 progress.scan.source = sources[i].fs_source_path;
974 progress.scan.wim_target_path = sources[i].wim_target_path;
975 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
977 config->_prefix = sources[i].fs_source_path;
978 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
979 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
980 if (!*sources[i].wim_target_path)
981 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
982 ret = (*capture_tree)(&branch,
983 sources[i].fs_source_path,
988 progress_func, extra_arg);
990 ERROR("Failed to build dentry tree for `%"TS"'",
991 sources[i].fs_source_path);
992 goto out_free_dentry_tree;
995 /* Use the target name, not the source name, for
996 * the root of each branch from a capture
997 * source. (This will also set the root dentry
998 * of the entire image to be unnamed.) */
999 ret = set_dentry_name(branch,
1000 path_basename(sources[i].wim_target_path));
1002 goto out_free_branch;
1004 ret = attach_branch(&root_dentry, branch,
1005 sources[i].wim_target_path);
1007 goto out_free_branch;
1010 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1013 if (root_dentry == NULL) {
1014 ret = new_filler_directory(T(""), &root_dentry);
1016 goto out_free_dentry_tree;
1019 DEBUG("Calculating full paths of dentries.");
1020 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1022 goto out_free_dentry_tree;
1024 ret = add_new_dentry_tree(w, root_dentry, sd);
1026 goto out_free_dentry_tree;
1028 imd = &w->image_metadata[w->hdr.image_count - 1];
1030 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1032 goto out_destroy_imd;
1034 DEBUG("Assigning hard link group IDs");
1035 assign_inode_numbers(&imd->inode_list);
1037 ret = xml_add_image(w, name);
1039 goto out_destroy_imd;
1041 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1042 wimlib_set_boot_idx(w, w->hdr.image_count);
1044 goto out_destroy_sd_set;
1046 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1048 w->hdr.image_count--;
1049 goto out_destroy_sd_set;
1051 free_dentry_tree(branch, w->lookup_table);
1052 out_free_dentry_tree:
1053 free_dentry_tree(root_dentry, w->lookup_table);
1054 out_free_security_data:
1055 free_security_data(sd);
1057 destroy_sd_set(&sd_set);
1063 wimlib_add_image(WIMStruct *w,
1064 const tchar *source,
1066 struct wimlib_capture_config *config,
1067 int add_image_flags,
1068 wimlib_progress_func_t progress_func)
1070 if (!source || !*source)
1071 return WIMLIB_ERR_INVALID_PARAM;
1073 tchar *fs_source_path = TSTRDUP(source);
1075 struct wimlib_capture_source capture_src = {
1076 .fs_source_path = fs_source_path,
1077 .wim_target_path = NULL,
1080 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1081 config, add_image_flags,
1083 FREE(fs_source_path);