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 struct wim_inode_table *inode_table,
163 const struct wimlib_capture_config *config,
165 wimlib_progress_func_t progress_func);
168 unix_capture_directory(struct wim_dentry *dir_dentry,
171 struct wim_lookup_table *lookup_table,
172 struct wim_inode_table *inode_table,
173 const struct wimlib_capture_config *config,
175 wimlib_progress_func_t progress_func)
179 struct dirent entry, *result;
180 struct wim_dentry *child;
183 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
186 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
188 return WIMLIB_ERR_OPEN;
191 /* Recurse on directory contents */
194 ret = readdir_r(dir, &entry, &result);
196 ret = WIMLIB_ERR_READ;
197 ERROR_WITH_ERRNO("Error reading the "
204 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
205 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
208 size_t name_len = strlen(result->d_name);
210 path[path_len] = '/';
211 memcpy(&path[path_len + 1], result->d_name, name_len + 1);
212 ret = unix_build_dentry_tree_recursive(&child,
214 path_len + 1 + name_len,
223 dentry_add_child(dir_dentry, child);
230 unix_capture_symlink(const char *path,
231 struct wim_inode *inode,
232 struct wim_lookup_table *lookup_table)
234 char deref_name_buf[4096];
235 ssize_t deref_name_len;
238 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
239 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
241 /* The idea here is to call readlink() to get the UNIX target of
242 * the symbolic link, then turn the target into a reparse point
243 * data buffer that contains a relative or absolute symbolic
244 * link (NOT a junction point or *full* path symbolic link with
247 deref_name_len = readlink(path, deref_name_buf,
248 sizeof(deref_name_buf) - 1);
249 if (deref_name_len >= 0) {
250 deref_name_buf[deref_name_len] = '\0';
251 DEBUG("Read symlink `%s'", deref_name_buf);
252 ret = inode_set_symlink(inode, deref_name_buf,
255 /* Unfortunately, Windows seems to have the concept of
256 * "file" symbolic links as being different from
257 * "directory" symbolic links... so
258 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
259 * symbolic link if the *target* of the symbolic link is
262 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
263 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
266 ERROR_WITH_ERRNO("Failed to read target of "
267 "symbolic link `%s'", path);
268 ret = WIMLIB_ERR_READLINK;
274 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
277 struct wim_lookup_table *lookup_table,
278 struct wim_inode_table *inode_table,
279 const struct wimlib_capture_config *config,
281 wimlib_progress_func_t progress_func)
283 struct wim_dentry *root = NULL;
285 struct wim_inode *inode;
287 if (exclude_path(path, path_len, config, true)) {
288 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
289 ERROR("Cannot exclude the root directory from capture");
290 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
293 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
296 union wimlib_progress_info info;
297 info.scan.cur_path = path;
298 info.scan.excluded = true;
299 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
304 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
307 union wimlib_progress_info info;
308 info.scan.cur_path = path;
309 info.scan.excluded = false;
310 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
313 /* UNIX version of capturing a directory tree */
315 int (*stat_fn)(const char *restrict, struct stat *restrict);
316 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
321 ret = (*stat_fn)(path, &stbuf);
323 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
327 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
328 !S_ISDIR(stbuf.st_mode))
330 /* Do a dereference-stat in case the root is a symbolic link.
331 * This case is allowed, provided that the symbolic link points
333 ret = stat(path, &stbuf);
335 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
336 ret = WIMLIB_ERR_STAT;
339 if (!S_ISDIR(stbuf.st_mode)) {
340 ERROR("`%s' is not a directory", path);
341 ret = WIMLIB_ERR_NOTDIR;
345 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
346 && !S_ISLNK(stbuf.st_mode)) {
347 ERROR("`%s' is not a regular file, directory, or symbolic link.",
349 ret = WIMLIB_ERR_SPECIAL_FILE;
353 ret = inode_table_new_dentry(inode_table,
354 path_basename_with_len(path, path_len),
361 inode = root->d_inode;
363 if (inode->i_nlink > 1) /* Already captured this inode? */
366 #ifdef HAVE_STAT_NANOSECOND_PRECISION
367 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
368 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
369 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
371 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
372 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
373 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
375 inode->i_resolved = 1;
376 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
377 ret = inode_set_unix_data(inode, stbuf.st_uid,
381 UNIX_DATA_ALL | UNIX_DATA_CREATE);
385 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
386 if (S_ISREG(stbuf.st_mode))
387 ret = unix_capture_regular_file(path, stbuf.st_size,
388 inode, lookup_table);
389 else if (S_ISDIR(stbuf.st_mode))
390 ret = unix_capture_directory(root, path, path_len,
391 lookup_table, inode_table, config,
392 add_image_flags, progress_func);
394 ret = unix_capture_symlink(path, inode, lookup_table);
399 free_dentry_tree(root, lookup_table);
404 * unix_build_dentry_tree():
405 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
406 * version; no NTFS-specific data is captured).
408 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
409 * modified if successful. Set to NULL if the file or directory was
410 * excluded from capture.
412 * @root_disk_path: The path to the root of the directory tree on disk.
414 * @lookup_table: The lookup table for the WIM file. For each file added to the
415 * dentry tree being built, an entry is added to the lookup table,
416 * unless an identical stream is already in the lookup table.
417 * These lookup table entries that are added point to the path of
420 * @sd_set: Ignored. (Security data only captured in NTFS mode.)
423 * Configuration for files to be excluded from capture.
425 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
427 * @extra_arg: Ignored
429 * @return: 0 on success, nonzero on failure. It is a failure if any of
430 * the files cannot be `stat'ed, or if any of the needed
431 * directories cannot be opened or read. Failure to add the files
432 * to the WIM may still occur later when trying to actually read
433 * the on-disk files during a call to wimlib_write() or
434 * wimlib_overwrite().
437 unix_build_dentry_tree(struct wim_dentry **root_ret,
438 const char *root_disk_path,
439 struct wim_lookup_table *lookup_table,
440 struct wim_inode_table *inode_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,
473 #endif /* !__WIN32__ */
476 match_pattern(const tchar *path,
477 const tchar *path_basename,
478 const struct wimlib_pattern_list *list)
480 for (size_t i = 0; i < list->num_pats; i++) {
482 const tchar *pat = list->pats[i];
485 if (*pat == T('/')) {
486 /* Absolute path from root of capture */
489 if (tstrchr(pat, T('/')))
490 /* Relative path from root of capture */
493 /* A file name pattern */
494 string = path_basename;
497 /* Warning: on Windows native builds, fnmatch() calls the
498 * replacement function in win32.c. */
499 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
505 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
509 DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
516 /* Return true if the image capture configuration file indicates we should
517 * exclude the filename @path from capture.
519 * If @exclude_prefix is %true, the part of the path up and including the name
520 * of the directory being captured is not included in the path for matching
521 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
522 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
526 exclude_path(const tchar *path, size_t path_len,
527 const struct wimlib_capture_config *config, bool exclude_prefix)
529 const tchar *basename = path_basename_with_len(path, path_len);
530 if (exclude_prefix) {
531 wimlib_assert(path_len >= config->_prefix_num_tchars);
532 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
533 path[config->_prefix_num_tchars] == T('/'))
535 path += config->_prefix_num_tchars;
538 return match_pattern(path, basename, &config->exclusion_pats) &&
539 !match_pattern(path, basename, &config->exclusion_exception_pats);
543 /* Strip leading and trailing forward slashes from a string. Modifies it in
544 * place and returns the stripped string. */
546 canonicalize_target_path(tchar *target_path)
549 if (target_path == NULL)
552 if (*target_path == T('\0'))
554 else if (*target_path == T('/'))
560 p = tstrchr(target_path, T('\0')) - 1;
566 /* Strip leading and trailing slashes from the target paths, and translate all
567 * backslashes in the source and target paths into forward slashes. */
569 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
572 while (num_sources--) {
573 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
574 sources->fs_source_path,
575 sources->wim_target_path);
577 /* The Windows API can handle forward slashes. Just get rid of
578 * backslashes to avoid confusing other parts of the library
580 zap_backslashes(sources->fs_source_path);
581 if (sources->wim_target_path)
582 zap_backslashes(sources->wim_target_path);
584 sources->wim_target_path =
585 (tchar*)canonicalize_target_path(sources->wim_target_path);
586 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
592 capture_source_cmp(const void *p1, const void *p2)
594 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
595 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
598 /* Sorts the capture sources lexicographically by target path. This occurs
599 * after leading and trailing forward slashes are stripped.
601 * One purpose of this is to make sure that target paths that are inside other
602 * target paths are added after the containing target paths. */
604 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
606 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
610 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
613 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
614 if (num_sources != 1) {
615 ERROR("Must specify exactly 1 capture source "
616 "(the NTFS volume) in NTFS mode!");
617 return WIMLIB_ERR_INVALID_PARAM;
619 if (sources[0].wim_target_path[0] != T('\0')) {
620 ERROR("In NTFS capture mode the target path inside "
621 "the image must be the root directory!");
622 return WIMLIB_ERR_INVALID_PARAM;
624 } else if (num_sources != 0) {
625 /* This code is disabled because the current code
626 * unconditionally attempts to do overlays. So, duplicate
627 * target paths are OK. */
629 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
630 ERROR("Cannot specify root target when using multiple "
632 return WIMLIB_ERR_INVALID_PARAM;
634 for (size_t i = 0; i < num_sources - 1; i++) {
635 size_t len = strlen(sources[i].wim_target_path);
637 const char *target1 = sources[i].wim_target_path;
639 const char *target2 = sources[j].wim_target_path;
640 DEBUG("target1=%s, target2=%s",
642 if (strncmp(target1, target2, len) ||
645 if (target2[len] == '/') {
646 ERROR("Invalid target `%s': is a prefix of `%s'",
648 return WIMLIB_ERR_INVALID_PARAM;
650 if (target2[len] == '\0') {
651 ERROR("Invalid target `%s': is a duplicate of `%s'",
653 return WIMLIB_ERR_INVALID_PARAM;
655 } while (++j != num_sources);
663 /* Creates a new directory to place in the WIM image. This is to create parent
664 * directories that are not part of any target as needed. */
666 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
669 struct wim_dentry *dentry;
671 DEBUG("Creating filler directory \"%"TS"\"", name);
672 ret = new_dentry_with_inode(name, &dentry);
674 /* Leave the inode number as 0; this is allowed for non
675 * hard-linked files. */
676 dentry->d_inode->i_resolved = 1;
677 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
678 *dentry_ret = dentry;
683 /* Transfers the children of @branch to @target. It is an error if @target is
684 * not a directory or if both @branch and @target contain a child dentry with
687 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
689 struct rb_root *rb_root;
691 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
692 branch->file_name, target->file_name);
694 if (!dentry_is_directory(target)) {
695 ERROR("Cannot overlay directory \"%"WS"\" "
696 "over non-directory", branch->file_name);
697 return WIMLIB_ERR_INVALID_OVERLAY;
700 rb_root = &branch->d_inode->i_children;
701 while (rb_root->rb_node) { /* While @branch has children... */
702 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
703 /* Move @child to the directory @target */
704 unlink_dentry(child);
705 if (!dentry_add_child(target, child)) {
706 /* Revert the change to avoid leaking the directory tree
707 * rooted at @child */
708 dentry_add_child(branch, child);
709 ERROR("Overlay error: file \"%"WS"\" already exists "
710 "as a child of \"%"WS"\"",
711 child->file_name, target->file_name);
712 return WIMLIB_ERR_INVALID_OVERLAY;
720 /* Attach or overlay a branch onto the WIM image.
723 * Pointer to the root of the WIM image, or pointer to NULL if it has not
728 * Path in the WIM image to add the branch, with leading and trailing
732 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
736 struct wim_dentry *dentry, *parent, *target;
739 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
740 branch->file_name, target_path);
742 if (*target_path == T('\0')) {
743 /* Target: root directory */
745 /* Overlay on existing root */
746 return do_overlay(*root_p, branch);
754 /* Adding a non-root branch. Create root if it hasn't been created
757 ret = new_filler_directory(T(""), root_p);
762 /* Walk the path to the branch, creating filler directories as needed.
765 while ((slash = tstrchr(target_path, T('/')))) {
767 dentry = get_dentry_child_with_name(parent, target_path);
769 ret = new_filler_directory(target_path, &dentry);
772 dentry_add_child(parent, dentry);
776 /* Skip over slashes. Note: this cannot overrun the length of
777 * the string because the last character cannot be a slash, as
778 * trailing slashes were tripped. */
781 } while (*target_path == T('/'));
784 /* If the target path already existed, overlay the branch onto it.
785 * Otherwise, set the branch as the target path. */
786 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
787 branch->file_name_nbytes);
789 return do_overlay(target, branch);
791 dentry_add_child(parent, branch);
797 canonicalize_pat(tchar **pat_p)
801 /* Turn all backslashes in the pattern into forward slashes. */
802 zap_backslashes(pat);
804 if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
805 /* Pattern begins with drive letter */
806 if (*(pat + 2) != T('/')) {
807 /* Something like c:file, which is actually a path
808 * relative to the current working directory on the c:
809 * drive. We require paths with drive letters to be
811 ERROR("Invalid path \"%"TS"\"; paths including drive letters "
812 "must be absolute!", pat);
813 ERROR("Maybe try \"%"TC":/%"TS"\"?",
815 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
818 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
819 "being removed.", pat);
820 /* Strip the drive letter */
828 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
831 for (size_t i = 0; i < pat_list->num_pats; i++) {
832 ret = canonicalize_pat(&pat_list->pats[i]);
840 canonicalize_capture_config(struct wimlib_capture_config *config)
842 int ret = canonicalize_pat_list(&config->exclusion_pats);
845 return canonicalize_pat_list(&config->exclusion_exception_pats);
849 wimlib_add_image_multisource(WIMStruct *w,
850 struct wimlib_capture_source *sources,
853 struct wimlib_capture_config *config,
855 wimlib_progress_func_t progress_func)
857 int (*capture_tree)(struct wim_dentry **,
859 struct wim_lookup_table *,
860 struct wim_inode_table *,
862 const struct wimlib_capture_config *,
864 wimlib_progress_func_t,
867 struct wim_dentry *root_dentry;
868 struct wim_dentry *branch;
869 struct wim_security_data *sd;
870 struct wim_image_metadata *imd;
871 struct wim_inode_table inode_table;
873 struct sd_set sd_set;
875 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
877 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
878 ERROR("Cannot dereference files when capturing directly from NTFS");
879 return WIMLIB_ERR_INVALID_PARAM;
881 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
882 ERROR("Capturing UNIX owner and mode not supported "
883 "when capturing directly from NTFS");
884 return WIMLIB_ERR_INVALID_PARAM;
886 capture_tree = build_dentry_tree_ntfs;
887 extra_arg = &w->ntfs_vol;
889 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
890 " cannot capture a WIM image directly from a NTFS volume!");
891 return WIMLIB_ERR_UNSUPPORTED;
895 capture_tree = win32_build_dentry_tree;
897 capture_tree = unix_build_dentry_tree;
903 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
904 ERROR("Capturing UNIX-specific data is not supported on Windows");
905 return WIMLIB_ERR_INVALID_PARAM;
907 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
908 ERROR("Dereferencing symbolic links is not supported on Windows");
909 return WIMLIB_ERR_INVALID_PARAM;
913 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
914 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
916 if (!name || !*name) {
917 ERROR("Must specify a non-empty string for the image name");
918 return WIMLIB_ERR_INVALID_PARAM;
921 if (w->hdr.total_parts != 1) {
922 ERROR("Cannot add an image to a split WIM");
923 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
926 if (wimlib_image_name_in_use(w, name)) {
927 ERROR("There is already an image named \"%"TS"\" in the WIM!",
929 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
933 DEBUG("Capture config not provided; using empty config");
934 config = alloca(sizeof(*config));
935 memset(config, 0, sizeof(*config));
938 ret = canonicalize_capture_config(config);
942 ret = init_inode_table(&inode_table, 9001);
946 DEBUG("Allocating security data");
947 sd = CALLOC(1, sizeof(struct wim_security_data));
949 ret = WIMLIB_ERR_NOMEM;
950 goto out_destroy_inode_table;
952 sd->total_length = 8;
956 sd_set.rb_root.rb_node = NULL;
959 DEBUG("Using %zu capture sources", num_sources);
960 canonicalize_sources_and_targets(sources, num_sources);
961 sort_sources(sources, num_sources);
962 ret = check_sorted_sources(sources, num_sources, add_image_flags);
964 ret = WIMLIB_ERR_INVALID_PARAM;
965 goto out_free_security_data;
969 DEBUG("Building dentry tree.");
972 for (size_t i = 0; i < num_sources; i++) {
974 union wimlib_progress_info progress;
976 DEBUG("Building dentry tree for source %zu of %zu "
977 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
978 sources[i].fs_source_path,
979 sources[i].wim_target_path);
981 memset(&progress, 0, sizeof(progress));
982 progress.scan.source = sources[i].fs_source_path;
983 progress.scan.wim_target_path = sources[i].wim_target_path;
984 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
986 config->_prefix = sources[i].fs_source_path;
987 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
988 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
989 if (!*sources[i].wim_target_path)
990 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
991 ret = (*capture_tree)(&branch,
992 sources[i].fs_source_path,
998 progress_func, extra_arg);
1000 ERROR("Failed to build dentry tree for `%"TS"'",
1001 sources[i].fs_source_path);
1002 goto out_free_dentry_tree;
1005 /* Use the target name, not the source name, for
1006 * the root of each branch from a capture
1007 * source. (This will also set the root dentry
1008 * of the entire image to be unnamed.) */
1009 ret = set_dentry_name(branch,
1010 path_basename(sources[i].wim_target_path));
1012 goto out_free_branch;
1014 ret = attach_branch(&root_dentry, branch,
1015 sources[i].wim_target_path);
1017 goto out_free_branch;
1020 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1023 if (root_dentry == NULL) {
1024 ret = new_filler_directory(T(""), &root_dentry);
1026 goto out_free_dentry_tree;
1029 DEBUG("Calculating full paths of dentries.");
1030 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1032 goto out_free_dentry_tree;
1034 ret = add_new_dentry_tree(w, root_dentry, sd);
1036 goto out_free_dentry_tree;
1038 imd = &w->image_metadata[w->hdr.image_count - 1];
1040 DEBUG("Assigning hard link group IDs");
1041 inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
1043 ret = xml_add_image(w, name);
1045 goto out_destroy_imd;
1047 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1048 wimlib_set_boot_idx(w, w->hdr.image_count);
1050 goto out_destroy_inode_table;
1052 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1054 w->hdr.image_count--;
1055 goto out_destroy_inode_table;
1057 free_dentry_tree(branch, w->lookup_table);
1058 out_free_dentry_tree:
1059 free_dentry_tree(root_dentry, w->lookup_table);
1060 out_free_security_data:
1061 free_security_data(sd);
1062 out_destroy_inode_table:
1063 destroy_inode_table(&inode_table);
1064 destroy_sd_set(&sd_set);
1070 wimlib_add_image(WIMStruct *w,
1071 const tchar *source,
1073 struct wimlib_capture_config *config,
1074 int add_image_flags,
1075 wimlib_progress_func_t progress_func)
1077 if (!source || !*source)
1078 return WIMLIB_ERR_INVALID_PARAM;
1080 tchar *fs_source_path = TSTRDUP(source);
1082 struct wimlib_capture_source capture_src = {
1083 .fs_source_path = fs_source_path,
1084 .wim_target_path = NULL,
1087 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1088 config, add_image_flags,
1090 FREE(fs_source_path);