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 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 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 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_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 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__ */
478 COMPRESSION_EXCLUSION_LIST,
482 #define COMPAT_DEFAULT_CONFIG
484 /* Default capture configuration file when none is specified. */
485 static const tchar *default_config =
486 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
487 users. The next ABI-incompatible library
488 version will default to the empty string here. */
494 "\\System Volume Information\n"
503 destroy_pattern_list(struct pattern_list *list)
509 destroy_capture_config(struct capture_config *config)
511 destroy_pattern_list(&config->exclusion_list);
512 destroy_pattern_list(&config->exclusion_exception);
513 destroy_pattern_list(&config->compression_exclusion_list);
514 destroy_pattern_list(&config->alignment_list);
515 FREE(config->config_str);
516 memset(config, 0, sizeof(*config));
520 pattern_list_add_pattern(struct pattern_list *list, const tchar *pattern)
523 if (list->num_pats >= list->num_allocated_pats) {
524 pats = REALLOC(list->pats,
525 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
527 return WIMLIB_ERR_NOMEM;
528 list->num_allocated_pats += 8;
531 list->pats[list->num_pats++] = pattern;
535 /* Parses the contents of the image capture configuration file and fills in a
536 * `struct capture_config'. */
538 init_capture_config(struct capture_config *config,
539 const tchar *_config_str,
540 size_t config_num_tchars)
546 size_t num_tchars_remaining;
547 enum pattern_type type = NONE;
549 unsigned long line_no = 0;
551 DEBUG("config_num_tchars = %zu", config_num_tchars);
552 num_tchars_remaining = config_num_tchars;
553 memset(config, 0, sizeof(*config));
554 config_str = TMALLOC(config_num_tchars);
556 ERROR("Could not duplicate capture config string");
557 return WIMLIB_ERR_NOMEM;
560 tmemcpy(config_str, _config_str, config_num_tchars);
562 config->config_str = config_str;
563 while (num_tchars_remaining != 0) {
566 eol = tmemchr(p, T('\n'), num_tchars_remaining);
568 ERROR("Expected end-of-line in capture config file on "
569 "line %lu", line_no);
570 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
575 num_tchars_remaining -= (next_p - p);
579 if (*(eol - 1) == T('\r'))
583 /* Translate backslash to forward slash */
584 for (tchar *pp = p; pp != eol; pp++)
588 /* Check if the path begins with a drive letter */
589 if (eol - p > 2 && *p != T('/') && *(p + 1) == T(':')) {
590 /* Don't allow relative paths on other drives */
591 if (eol - p < 3 || *(p + 2) != T('/')) {
592 ERROR("Relative paths including a drive letter "
594 " Perhaps you meant "
595 "\"%"TS":/%"TS"\"?\n",
597 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
601 /* UNIX: strip the drive letter */
607 if (!tstrcmp(p, T("[ExclusionList]")))
608 type = EXCLUSION_LIST;
609 else if (!tstrcmp(p, T("[ExclusionException]")))
610 type = EXCLUSION_EXCEPTION;
611 else if (!tstrcmp(p, T("[CompressionExclusionList]")))
612 type = COMPRESSION_EXCLUSION_LIST;
613 else if (!tstrcmp(p, T("[AlignmentList]")))
614 type = ALIGNMENT_LIST;
615 else if (p[0] == T('[') && tstrrchr(p, T(']'))) {
616 ERROR("Unknown capture configuration section \"%"TS"\"", p);
617 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
618 } else switch (type) {
620 DEBUG("Adding pattern \"%"TS"\" to exclusion list", p);
621 ret = pattern_list_add_pattern(&config->exclusion_list, p);
623 case EXCLUSION_EXCEPTION:
624 DEBUG("Adding pattern \"%"TS"\" to exclusion exception list", p);
625 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
627 case COMPRESSION_EXCLUSION_LIST:
628 DEBUG("Adding pattern \"%"TS"\" to compression exclusion list", p);
629 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
632 DEBUG("Adding pattern \"%"TS"\" to alignment list", p);
633 ret = pattern_list_add_pattern(&config->alignment_list, p);
636 ERROR("Line %lu of capture configuration is not "
637 "in a block (such as [ExclusionList])",
639 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
647 destroy_capture_config(config);
652 is_absolute_path(const tchar *path)
658 if (*path && *(path + 1) == T(':'))
665 match_pattern(const tchar *path,
666 const tchar *path_basename,
667 const struct pattern_list *list)
669 for (size_t i = 0; i < list->num_pats; i++) {
670 const tchar *pat = list->pats[i];
672 if (is_absolute_path(pat)) {
673 /* Absolute path from root of capture */
676 if (tstrchr(pat, T('/')))
677 /* Relative path from root of capture */
680 /* A file name pattern */
681 string = path_basename;
684 /* Warning: on Windows native builds, fnmatch() calls the
685 * replacement function in win32.c. */
686 if (fnmatch(pat, string, FNM_PATHNAME
692 DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
700 /* Return true if the image capture configuration file indicates we should
701 * exclude the filename @path from capture.
703 * If @exclude_prefix is %true, the part of the path up and including the name
704 * of the directory being captured is not included in the path for matching
705 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
706 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
710 exclude_path(const tchar *path, size_t path_len,
711 const struct capture_config *config, bool exclude_prefix)
713 const tchar *basename = path_basename_with_len(path, path_len);
714 if (exclude_prefix) {
715 wimlib_assert(path_len >= config->prefix_num_tchars);
716 if (!tmemcmp(config->prefix, path, config->prefix_num_tchars) &&
717 path[config->prefix_num_tchars] == T('/'))
719 path += config->prefix_num_tchars;
722 return match_pattern(path, basename, &config->exclusion_list) &&
723 !match_pattern(path, basename, &config->exclusion_exception);
727 /* Strip leading and trailing forward slashes from a string. Modifies it in
728 * place and returns the stripped string. */
730 canonicalize_target_path(tchar *target_path)
733 if (target_path == NULL)
736 if (*target_path == T('\0'))
738 else if (*target_path == T('/'))
744 p = tstrchr(target_path, T('\0')) - 1;
750 /* Strip leading and trailing slashes from the target paths, and translate all
751 * backslashes in the source and target paths into forward slashes. */
753 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
756 while (num_sources--) {
757 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
758 sources->fs_source_path,
759 sources->wim_target_path);
761 /* The Windows API can handle forward slashes. Just get rid of
762 * backslashes to avoid confusing other parts of the library
764 zap_backslashes(sources->fs_source_path);
765 if (sources->wim_target_path)
766 zap_backslashes(sources->wim_target_path);
768 sources->wim_target_path =
769 (tchar*)canonicalize_target_path(sources->wim_target_path);
770 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
776 capture_source_cmp(const void *p1, const void *p2)
778 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
779 return tstrcmp(s1->wim_target_path, s2->wim_target_path);
782 /* Sorts the capture sources lexicographically by target path. This occurs
783 * after leading and trailing forward slashes are stripped.
785 * One purpose of this is to make sure that target paths that are inside other
786 * target paths are added after the containing target paths. */
788 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
790 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
794 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
797 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
798 if (num_sources != 1) {
799 ERROR("Must specify exactly 1 capture source "
800 "(the NTFS volume) in NTFS mode!");
801 return WIMLIB_ERR_INVALID_PARAM;
803 if (sources[0].wim_target_path[0] != T('\0')) {
804 ERROR("In NTFS capture mode the target path inside "
805 "the image must be the root directory!");
806 return WIMLIB_ERR_INVALID_PARAM;
808 } else if (num_sources != 0) {
809 /* This code is disabled because the current code
810 * unconditionally attempts to do overlays. So, duplicate
811 * target paths are OK. */
813 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
814 ERROR("Cannot specify root target when using multiple "
816 return WIMLIB_ERR_INVALID_PARAM;
818 for (size_t i = 0; i < num_sources - 1; i++) {
819 size_t len = strlen(sources[i].wim_target_path);
821 const char *target1 = sources[i].wim_target_path;
823 const char *target2 = sources[j].wim_target_path;
824 DEBUG("target1=%s, target2=%s",
826 if (strncmp(target1, target2, len) ||
829 if (target2[len] == '/') {
830 ERROR("Invalid target `%s': is a prefix of `%s'",
832 return WIMLIB_ERR_INVALID_PARAM;
834 if (target2[len] == '\0') {
835 ERROR("Invalid target `%s': is a duplicate of `%s'",
837 return WIMLIB_ERR_INVALID_PARAM;
839 } while (++j != num_sources);
847 /* Creates a new directory to place in the WIM image. This is to create parent
848 * directories that are not part of any target as needed. */
850 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
853 struct wim_dentry *dentry;
855 DEBUG("Creating filler directory \"%"TS"\"", name);
856 ret = new_dentry_with_inode(name, &dentry);
858 /* Leave the inode number as 0 for now. The final inode number
859 * will be assigned later by assign_inode_numbers(). */
860 dentry->d_inode->i_resolved = 1;
861 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
862 *dentry_ret = dentry;
867 /* Transfers the children of @branch to @target. It is an error if @target is
868 * not a directory or if both @branch and @target contain a child dentry with
871 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
873 struct rb_root *rb_root;
875 DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
876 branch->file_name, target->file_name);
878 if (!dentry_is_directory(target)) {
879 ERROR("Cannot overlay directory \"%"WS"\" "
880 "over non-directory", branch->file_name);
881 return WIMLIB_ERR_INVALID_OVERLAY;
884 rb_root = &branch->d_inode->i_children;
885 while (rb_root->rb_node) { /* While @branch has children... */
886 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
887 /* Move @child to the directory @target */
888 unlink_dentry(child);
889 if (!dentry_add_child(target, child)) {
890 /* Revert the change to avoid leaking the directory tree
891 * rooted at @child */
892 dentry_add_child(branch, child);
893 ERROR("Overlay error: file \"%"WS"\" already exists "
894 "as a child of \"%"WS"\"",
895 child->file_name, target->file_name);
896 return WIMLIB_ERR_INVALID_OVERLAY;
904 /* Attach or overlay a branch onto the WIM image.
907 * Pointer to the root of the WIM image, or pointer to NULL if it has not
912 * Path in the WIM image to add the branch, with leading and trailing
916 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
920 struct wim_dentry *dentry, *parent, *target;
923 DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
924 branch->file_name, target_path);
926 if (*target_path == T('\0')) {
927 /* Target: root directory */
929 /* Overlay on existing root */
930 return do_overlay(*root_p, branch);
938 /* Adding a non-root branch. Create root if it hasn't been created
941 ret = new_filler_directory(T(""), root_p);
946 /* Walk the path to the branch, creating filler directories as needed.
949 while ((slash = tstrchr(target_path, T('/')))) {
951 dentry = get_dentry_child_with_name(parent, target_path);
953 ret = new_filler_directory(target_path, &dentry);
956 dentry_add_child(parent, dentry);
960 /* Skip over slashes. Note: this cannot overrun the length of
961 * the string because the last character cannot be a slash, as
962 * trailing slashes were tripped. */
965 } while (*target_path == T('/'));
968 /* If the target path already existed, overlay the branch onto it.
969 * Otherwise, set the branch as the target path. */
970 target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
971 branch->file_name_nbytes);
973 return do_overlay(target, branch);
975 dentry_add_child(parent, branch);
981 wimlib_add_image_multisource(WIMStruct *w,
982 struct wimlib_capture_source *sources,
985 const tchar *config_str,
988 wimlib_progress_func_t progress_func)
990 int (*capture_tree)(struct wim_dentry **,
992 struct wim_lookup_table *,
994 const struct capture_config *,
996 wimlib_progress_func_t,
999 struct wim_dentry *root_dentry;
1000 struct wim_dentry *branch;
1001 struct wim_security_data *sd;
1002 struct capture_config config;
1003 struct wim_image_metadata *imd;
1005 struct sd_set sd_set;
1007 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
1009 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
1010 ERROR("Cannot dereference files when capturing directly from NTFS");
1011 return WIMLIB_ERR_INVALID_PARAM;
1013 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
1014 ERROR("Capturing UNIX owner and mode not supported "
1015 "when capturing directly from NTFS");
1016 return WIMLIB_ERR_INVALID_PARAM;
1018 capture_tree = build_dentry_tree_ntfs;
1019 extra_arg = &w->ntfs_vol;
1021 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1022 " cannot capture a WIM image directly from a NTFS volume!");
1023 return WIMLIB_ERR_UNSUPPORTED;
1027 capture_tree = win32_build_dentry_tree;
1029 capture_tree = unix_build_dentry_tree;
1035 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
1036 ERROR("Capturing UNIX-specific data is not supported on Windows");
1037 return WIMLIB_ERR_INVALID_PARAM;
1039 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
1040 ERROR("Dereferencing symbolic links is not supported on Windows");
1041 return WIMLIB_ERR_INVALID_PARAM;
1045 if (!name || !*name) {
1046 ERROR("Must specify a non-empty string for the image name");
1047 return WIMLIB_ERR_INVALID_PARAM;
1050 if (w->hdr.total_parts != 1) {
1051 ERROR("Cannot add an image to a split WIM");
1052 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1055 if (wimlib_image_name_in_use(w, name)) {
1056 ERROR("There is already an image named \"%"TS"\" in the WIM!",
1058 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1062 DEBUG("Using default capture configuration");
1063 config_str = default_config;
1064 config_len = tstrlen(default_config);
1066 ret = init_capture_config(&config, config_str, config_len);
1070 DEBUG("Allocating security data");
1071 sd = CALLOC(1, sizeof(struct wim_security_data));
1073 ret = WIMLIB_ERR_NOMEM;
1074 goto out_destroy_capture_config;
1076 sd->total_length = 8;
1080 sd_set.rb_root.rb_node = NULL;
1082 DEBUG("Using %zu capture sources", num_sources);
1083 canonicalize_sources_and_targets(sources, num_sources);
1084 sort_sources(sources, num_sources);
1085 ret = check_sorted_sources(sources, num_sources, add_image_flags);
1087 ret = WIMLIB_ERR_INVALID_PARAM;
1088 goto out_free_security_data;
1091 DEBUG("Building dentry tree.");
1094 for (size_t i = 0; i < num_sources; i++) {
1096 union wimlib_progress_info progress;
1098 DEBUG("Building dentry tree for source %zu of %zu "
1099 "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
1100 sources[i].fs_source_path,
1101 sources[i].wim_target_path);
1102 if (progress_func) {
1103 memset(&progress, 0, sizeof(progress));
1104 progress.scan.source = sources[i].fs_source_path;
1105 progress.scan.wim_target_path = sources[i].wim_target_path;
1106 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1108 config.prefix = sources[i].fs_source_path;
1109 config.prefix_num_tchars = tstrlen(sources[i].fs_source_path);
1110 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
1111 if (!*sources[i].wim_target_path)
1112 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
1113 ret = (*capture_tree)(&branch,
1114 sources[i].fs_source_path,
1119 progress_func, extra_arg);
1121 ERROR("Failed to build dentry tree for `%"TS"'",
1122 sources[i].fs_source_path);
1123 goto out_free_dentry_tree;
1126 /* Use the target name, not the source name, for
1127 * the root of each branch from a capture
1128 * source. (This will also set the root dentry
1129 * of the entire image to be unnamed.) */
1130 ret = set_dentry_name(branch,
1131 path_basename(sources[i].wim_target_path));
1133 goto out_free_branch;
1135 ret = attach_branch(&root_dentry, branch,
1136 sources[i].wim_target_path);
1138 goto out_free_branch;
1141 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1144 if (root_dentry == NULL) {
1145 ret = new_filler_directory(T(""), &root_dentry);
1147 goto out_free_dentry_tree;
1150 DEBUG("Calculating full paths of dentries.");
1151 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1153 goto out_free_dentry_tree;
1155 ret = add_new_dentry_tree(w, root_dentry, sd);
1157 goto out_free_dentry_tree;
1159 imd = &w->image_metadata[w->hdr.image_count - 1];
1161 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1163 goto out_destroy_imd;
1165 DEBUG("Assigning hard link group IDs");
1166 assign_inode_numbers(&imd->inode_list);
1168 ret = xml_add_image(w, name);
1170 goto out_destroy_imd;
1172 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1173 wimlib_set_boot_idx(w, w->hdr.image_count);
1175 goto out_destroy_sd_set;
1177 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1179 w->hdr.image_count--;
1180 goto out_destroy_sd_set;
1182 free_dentry_tree(branch, w->lookup_table);
1183 out_free_dentry_tree:
1184 free_dentry_tree(root_dentry, w->lookup_table);
1185 out_free_security_data:
1186 free_security_data(sd);
1188 destroy_sd_set(&sd_set);
1189 out_destroy_capture_config:
1190 destroy_capture_config(&config);
1196 wimlib_add_image(WIMStruct *w,
1197 const tchar *source,
1199 const tchar *config_str,
1201 int add_image_flags,
1202 wimlib_progress_func_t progress_func)
1204 if (!source || !*source)
1205 return WIMLIB_ERR_INVALID_PARAM;
1207 tchar *fs_source_path = TSTRDUP(source);
1209 struct wimlib_capture_source capture_src = {
1210 .fs_source_path = fs_source_path,
1211 .wim_target_path = NULL,
1214 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1215 config_str, config_len,
1216 add_image_flags, progress_func);
1217 FREE(fs_source_path);