4 * Support for modifying WIM files with image-level operations (delete an image,
5 * add an image, export an image from one WIM to another.) There is nothing
6 * here that lets you change individual files in the WIM; for that you will need
7 * to look at the filesystem implementation in mount.c.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
35 #include "timestamp.h"
44 /** Private flag: Used to mark that we currently adding the root directory of
46 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
48 void destroy_image_metadata(struct image_metadata *imd, struct lookup_table *lt)
50 free_dentry_tree(imd->root_dentry, lt);
51 free_security_data(imd->security_data);
53 /* Get rid of the lookup table entry for this image's metadata resource
56 lookup_table_remove(lt, imd->metadata_lte);
60 * Recursively builds a dentry tree from a directory tree on disk, outside the
63 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
64 * modified if successful. NULL if the file or directory was
65 * excluded from capture.
67 * @root_disk_path: The path to the root of the directory tree on disk.
69 * @lookup_table: The lookup table for the WIM file. For each file added to the
70 * dentry tree being built, an entry is added to the lookup table,
71 * unless an identical stream is already in the lookup table.
72 * These lookup table entries that are added point to the path of
75 * @sd: Ignored. (Security data only captured in NTFS mode.)
78 * Configuration for files to be excluded from capture.
80 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
82 * @extra_arg: Ignored. (Only used in NTFS mode.)
84 * @return: 0 on success, nonzero on failure. It is a failure if any of
85 * the files cannot be `stat'ed, or if any of the needed
86 * directories cannot be opened or read. Failure to add the files
87 * to the WIM may still occur later when trying to actually read
88 * the on-disk files during a call to wimlib_write() or
91 static int build_dentry_tree(struct dentry **root_ret,
92 const char *root_disk_path,
93 struct lookup_table *lookup_table,
94 struct wim_security_data *sd,
95 const struct capture_config *config,
99 struct stat root_stbuf;
101 int (*stat_fn)(const char *restrict, struct stat *restrict);
103 const char *filename;
106 if (exclude_path(root_disk_path, config, true)) {
107 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
108 ERROR("Cannot exclude the root directory from capture");
109 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
111 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
112 printf("Excluding file `%s' from capture\n",
119 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
124 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
125 printf("Scanning `%s'\n", root_disk_path);
127 ret = (*stat_fn)(root_disk_path, &root_stbuf);
129 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
130 return WIMLIB_ERR_STAT;
133 if ((add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
134 !S_ISDIR(root_stbuf.st_mode)) {
135 ERROR("`%s' is not a directory", root_disk_path);
136 return WIMLIB_ERR_NOTDIR;
138 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
139 && !S_ISLNK(root_stbuf.st_mode)) {
140 ERROR("`%s' is not a regular file, directory, or symbolic link.",
142 return WIMLIB_ERR_SPECIAL_FILE;
145 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
148 filename = path_basename(root_disk_path);
150 root = new_dentry_with_timeless_inode(filename);
152 return WIMLIB_ERR_NOMEM;
154 inode = root->d_inode;
156 inode->creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
157 inode->last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
158 inode->last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
159 if (sizeof(ino_t) >= 8)
160 inode->ino = (u64)root_stbuf.st_ino;
162 inode->ino = (u64)root_stbuf.st_ino |
163 ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
165 add_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
166 inode->resolved = true;
168 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
170 struct lookup_table_entry *lte;
171 u8 hash[SHA1_HASH_SIZE];
173 inode->attributes = FILE_ATTRIBUTE_NORMAL;
175 /* Empty files do not have to have a lookup table entry. */
176 if (root_stbuf.st_size == 0)
179 /* For each regular file, we must check to see if the file is in
180 * the lookup table already; if it is, we increment its refcnt;
181 * otherwise, we create a new lookup table entry and insert it.
184 ret = sha1sum(root_disk_path, hash);
188 lte = __lookup_resource(lookup_table, hash);
191 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
194 char *file_on_disk = STRDUP(root_disk_path);
196 ERROR("Failed to allocate memory for file path");
197 ret = WIMLIB_ERR_NOMEM;
200 lte = new_lookup_table_entry();
203 ret = WIMLIB_ERR_NOMEM;
206 lte->file_on_disk = file_on_disk;
207 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
208 lte->resource_entry.original_size = root_stbuf.st_size;
209 lte->resource_entry.size = root_stbuf.st_size;
210 copy_hash(lte->hash, hash);
211 lookup_table_insert(lookup_table, lte);
213 root->d_inode->lte = lte;
214 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
216 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
220 struct dentry *child;
222 dir = opendir(root_disk_path);
224 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
226 ret = WIMLIB_ERR_OPEN;
230 /* Buffer for names of files in directory. */
231 size_t len = strlen(root_disk_path);
232 char name[len + 1 + FILENAME_MAX + 1];
233 memcpy(name, root_disk_path, len);
236 /* Create a dentry for each entry in the directory on disk, and recurse
237 * to any subdirectories. */
243 ret = WIMLIB_ERR_READ;
244 ERROR_WITH_ERRNO("Error reading the "
250 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
251 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
253 strcpy(name + len + 1, p->d_name);
254 ret = build_dentry_tree(&child, name, lookup_table,
260 dentry_add_child(root, child);
263 } else { /* Archiving a symbolic link */
264 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
265 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
267 /* The idea here is to call readlink() to get the UNIX target of
268 * the symbolic link, then turn the target into a reparse point
269 * data buffer that contains a relative or absolute symbolic
270 * link (NOT a junction point or *full* path symbolic link with
274 char deref_name_buf[4096];
275 ssize_t deref_name_len;
277 deref_name_len = readlink(root_disk_path, deref_name_buf,
278 sizeof(deref_name_buf) - 1);
279 if (deref_name_len >= 0) {
280 deref_name_buf[deref_name_len] = '\0';
281 DEBUG("Read symlink `%s'", deref_name_buf);
282 ret = inode_set_symlink(root->d_inode, deref_name_buf,
286 * Unfortunately, Windows seems to have the
287 * concept of "file" symbolic links as being
288 * different from "directory" symbolic links...
289 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
290 * on the symbolic link if the *target* of the
291 * symbolic link is a directory.
294 if (stat(root_disk_path, &stbuf) == 0 &&
295 S_ISDIR(stbuf.st_mode))
297 inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
301 ERROR_WITH_ERRNO("Failed to read target of "
302 "symbolic link `%s'", root_disk_path);
303 ret = WIMLIB_ERR_READLINK;
310 free_dentry_tree(root, lookup_table);
317 struct list_head lte_list_head;
320 static int allocate_lte_if_needed(struct dentry *dentry, void *arg)
322 const WIMStruct *src_wim, *dest_wim;
323 struct list_head *lte_list_head;
326 src_wim = ((struct wim_pair*)arg)->src_wim;
327 dest_wim = ((struct wim_pair*)arg)->dest_wim;
328 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
329 inode = dentry->d_inode;
331 wimlib_assert(!inode->resolved);
333 for (unsigned i = 0; i <= inode->num_ads; i++) {
334 struct lookup_table_entry *src_lte, *dest_lte;
335 src_lte = inode_stream_lte_unresolved(inode, i,
336 src_wim->lookup_table);
338 if (src_lte && ++src_lte->out_refcnt == 1) {
339 dest_lte = inode_stream_lte_unresolved(inode, i,
340 dest_wim->lookup_table);
343 dest_lte = clone_lookup_table_entry(src_lte);
345 return WIMLIB_ERR_NOMEM;
346 list_add_tail(&dest_lte->staging_list, lte_list_head);
354 * This function takes in a dentry that was previously located only in image(s)
355 * in @src_wim, but now is being added to @dest_wim. For each stream associated
356 * with the dentry, if there is already a lookup table entry for that stream in
357 * the lookup table of the destination WIM file, its reference count is
358 * incrementej. Otherwise, a new lookup table entry is created that points back
359 * to the stream in the source WIM file (through the @hash field combined with
360 * the @wim field of the lookup table entry.)
362 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
364 WIMStruct *src_wim, *dest_wim;
367 src_wim = ((struct wim_pair*)arg)->src_wim;
368 dest_wim = ((struct wim_pair*)arg)->dest_wim;
369 inode = dentry->d_inode;
371 wimlib_assert(!inode->resolved);
373 for (unsigned i = 0; i <= inode->num_ads; i++) {
374 struct lookup_table_entry *src_lte, *dest_lte;
375 src_lte = inode_stream_lte_unresolved(inode, i,
376 src_wim->lookup_table);
378 if (!src_lte) /* Empty or nonexistent stream. */
381 dest_lte = inode_stream_lte_unresolved(inode, i,
382 dest_wim->lookup_table);
386 struct list_head *lte_list_head;
387 struct list_head *next;
389 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
390 wimlib_assert(!list_empty(lte_list_head));
392 next = lte_list_head->next;
394 dest_lte = container_of(next, struct lookup_table_entry,
396 dest_lte->part_number = 1;
397 dest_lte->refcnt = 1;
398 wimlib_assert(hashes_equal(dest_lte->hash, src_lte->hash));
400 lookup_table_insert(dest_wim->lookup_table, dest_lte);
407 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
408 * file, adds an entry to the lookup table for the image metadata, updates the
409 * image count in the header, and selects the new image.
411 * Does not update the XML data.
413 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
414 * 0 is returned and the image metadata array of @w is modified.
416 * @w: The WIMStruct for the WIM file.
417 * @root_dentry: The root of the directory tree for the image.
418 * @sd: The security data for the image.
420 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
421 struct wim_security_data *sd)
423 struct lookup_table_entry *metadata_lte;
424 struct image_metadata *imd;
425 struct image_metadata *new_imd;
428 wimlib_assert(root_dentry != NULL);
430 DEBUG("Reallocating image metadata array for image_count = %u",
431 w->hdr.image_count + 1);
432 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
435 ERROR("Failed to allocate memory for new image metadata array");
439 memcpy(imd, w->image_metadata,
440 w->hdr.image_count * sizeof(struct image_metadata));
442 metadata_lte = new_lookup_table_entry();
446 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
447 random_hash(metadata_lte->hash);
448 lookup_table_insert(w->lookup_table, metadata_lte);
450 new_imd = &imd[w->hdr.image_count];
452 new_imd->root_dentry = root_dentry;
453 new_imd->metadata_lte = metadata_lte;
454 new_imd->security_data = sd;
455 new_imd->modified = true;
457 FREE(w->image_metadata);
458 w->image_metadata = imd;
459 w->hdr.image_count++;
461 /* Change the current image to the new one. There should not be any
462 * ways for this to fail, since the image is valid and the dentry tree
463 * is already in memory. */
464 ret = select_wim_image(w, w->hdr.image_count);
465 wimlib_assert(ret == 0);
470 return WIMLIB_ERR_NOMEM;
475 * Copies an image, or all the images, from a WIM file, into another WIM file.
477 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
480 const char *dest_name,
481 const char *dest_description,
483 WIMStruct **additional_swms,
484 unsigned num_additional_swms)
489 struct wim_pair wims;
490 struct wim_security_data *sd;
491 struct lookup_table *joined_tab, *src_wim_tab_save;
493 if (!src_wim || !dest_wim)
494 return WIMLIB_ERR_INVALID_PARAM;
496 if (dest_wim->hdr.total_parts != 1) {
497 ERROR("Exporting an image to a split WIM is "
499 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
502 if (src_image == WIM_ALL_IMAGES) {
503 if (src_wim->hdr.image_count > 1) {
505 /* multi-image export. */
507 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
508 (src_wim->hdr.boot_idx == 0))
510 /* Specifying the boot flag on a multi-image
511 * source WIM makes the boot index default to
512 * the bootable image in the source WIM. It is
513 * an error if there is no such bootable image.
515 ERROR("Cannot specify `boot' flag when "
516 "exporting multiple images from a WIM "
517 "with no bootable images");
518 return WIMLIB_ERR_INVALID_PARAM;
520 if (dest_name || dest_description) {
521 ERROR("Image name or image description was "
522 "specified, but we are exporting "
524 return WIMLIB_ERR_INVALID_PARAM;
526 for (i = 1; i <= src_wim->hdr.image_count; i++) {
527 int export_flags = flags;
529 if (i != src_wim->hdr.boot_idx)
530 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
532 ret = wimlib_export_image(src_wim, i, dest_wim,
536 num_additional_swms);
541 } else if (src_wim->hdr.image_count == 1) {
549 dest_name = wimlib_get_image_name(src_wim, src_image);
550 DEBUG("Using name `%s' for source image %d",
551 dest_name, src_image);
554 if (!dest_description) {
555 dest_description = wimlib_get_image_description(src_wim,
557 DEBUG("Using description `%s' for source image %d",
558 dest_description, src_image);
561 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
563 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
564 ERROR("There is already an image named `%s' in the "
565 "destination WIM", dest_name);
566 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
569 ret = verify_swm_set(src_wim, additional_swms, num_additional_swms);
573 if (num_additional_swms) {
574 ret = new_joined_lookup_table(src_wim, additional_swms,
579 src_wim_tab_save = src_wim->lookup_table;
580 src_wim->lookup_table = joined_tab;
583 ret = select_wim_image(src_wim, src_image);
585 ERROR("Could not select image %d from the WIM `%s' "
586 "to export it", src_image, src_wim->filename);
590 /* Pre-allocate the new lookup table entries that will be needed. This
591 * way, it's not possible to run out of memory part-way through
592 * modifying the lookup table of the destination WIM. */
593 wims.src_wim = src_wim;
594 wims.dest_wim = dest_wim;
595 INIT_LIST_HEAD(&wims.lte_list_head);
596 for_lookup_table_entry(src_wim->lookup_table, lte_zero_out_refcnt, NULL);
597 root = wim_root_dentry(src_wim);
598 for_dentry_in_tree(root, dentry_unresolve_ltes, NULL);
599 ret = for_dentry_in_tree(root, allocate_lte_if_needed, &wims);
603 ret = xml_export_image(src_wim->wim_info, src_image,
604 &dest_wim->wim_info, dest_name, dest_description);
608 sd = wim_security_data(src_wim);
609 ret = add_new_dentry_tree(dest_wim, root, sd);
611 goto out_xml_delete_image;
614 /* All memory allocations have been taken care of, so it's no longer
615 * possible for this function to fail. Go ahead and increment the
616 * reference counts of the dentry tree and security data, then update
617 * the lookup table of the destination WIM and the boot index, if
619 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
621 for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
622 wimlib_assert(list_empty(&wims.lte_list_head));
624 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
625 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
626 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
631 out_xml_delete_image:
632 xml_delete_image(&dest_wim->wim_info, dest_wim->hdr.image_count);
635 struct lookup_table_entry *lte, *tmp;
636 list_for_each_entry_safe(lte, tmp, &wims.lte_list_head, staging_list)
637 free_lookup_table_entry(lte);
641 if (num_additional_swms) {
642 free_lookup_table(src_wim->lookup_table);
643 src_wim->lookup_table = src_wim_tab_save;
649 * Deletes an image from the WIM.
651 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
656 if (w->hdr.total_parts != 1) {
657 ERROR("Deleting an image from a split WIM is not supported.");
658 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
661 if (image == WIM_ALL_IMAGES) {
662 for (i = w->hdr.image_count; i >= 1; i--) {
663 ret = wimlib_delete_image(w, i);
670 DEBUG("Deleting image %d", image);
672 /* Even if the dentry tree is not allocated, we must select it (and
673 * therefore allocate it) so that we can decrement the reference counts
674 * in the lookup table. */
675 ret = select_wim_image(w, image);
679 /* Free the dentry tree, any lookup table entries that have their
680 * refcnt decremented to 0, and the security data. */
681 destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table);
683 /* Get rid of the empty slot in the image metadata array. */
684 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
685 (w->hdr.image_count - image) * sizeof(struct image_metadata));
687 /* Decrement the image count. */
688 if (--w->hdr.image_count == 0) {
689 FREE(w->image_metadata);
690 w->image_metadata = NULL;
693 /* Fix the boot index. */
694 if (w->hdr.boot_idx == image)
696 else if (w->hdr.boot_idx > image)
699 w->current_image = WIM_NO_IMAGE;
701 /* Remove the image from the XML information. */
702 xml_delete_image(&w->wim_info, image);
710 COMPRESSION_EXCLUSION_LIST,
714 /* Default capture configuration file when none is specified. */
715 static const char *default_config =
720 "\\System Volume Information\n"
724 "[CompressionExclusionList]\n"
728 "\\WINDOWS\\inf\\*.pnf\n";
730 static void destroy_pattern_list(struct pattern_list *list)
735 static void destroy_capture_config(struct capture_config *config)
737 destroy_pattern_list(&config->exclusion_list);
738 destroy_pattern_list(&config->exclusion_exception);
739 destroy_pattern_list(&config->compression_exclusion_list);
740 destroy_pattern_list(&config->alignment_list);
741 FREE(config->config_str);
742 FREE(config->prefix);
743 memset(config, 0, sizeof(*config));
746 static int pattern_list_add_pattern(struct pattern_list *list,
750 if (list->num_pats >= list->num_allocated_pats) {
751 pats = REALLOC(list->pats,
752 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
754 return WIMLIB_ERR_NOMEM;
755 list->num_allocated_pats += 8;
758 list->pats[list->num_pats++] = pattern;
762 /* Parses the contents of the image capture configuration file and fills in a
763 * `struct capture_config'. */
764 static int init_capture_config(const char *_config_str, size_t config_len,
765 const char *_prefix, struct capture_config *config)
772 size_t bytes_remaining;
773 enum pattern_type type = NONE;
775 unsigned long line_no = 0;
777 DEBUG("config_len = %zu", config_len);
778 bytes_remaining = config_len;
779 memset(config, 0, sizeof(*config));
780 config_str = MALLOC(config_len);
782 ERROR("Could not duplicate capture config string");
783 return WIMLIB_ERR_NOMEM;
785 prefix = STRDUP(_prefix);
788 return WIMLIB_ERR_NOMEM;
791 memcpy(config_str, _config_str, config_len);
793 config->config_str = config_str;
794 config->prefix = prefix;
795 config->prefix_len = strlen(prefix);
796 while (bytes_remaining) {
799 eol = memchr(p, '\n', bytes_remaining);
801 ERROR("Expected end-of-line in capture config file on "
802 "line %lu", line_no);
803 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
808 bytes_remaining -= (next_p - p);
812 if (*(eol - 1) == '\r')
816 /* Translate backslash to forward slash */
817 for (char *pp = p; pp != eol; pp++)
821 /* Remove drive letter */
822 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
826 if (strcmp(p, "[ExclusionList]") == 0)
827 type = EXCLUSION_LIST;
828 else if (strcmp(p, "[ExclusionException]") == 0)
829 type = EXCLUSION_EXCEPTION;
830 else if (strcmp(p, "[CompressionExclusionList]") == 0)
831 type = COMPRESSION_EXCLUSION_LIST;
832 else if (strcmp(p, "[AlignmentList]") == 0)
833 type = ALIGNMENT_LIST;
834 else if (p[0] == '[' && strrchr(p, ']')) {
835 ERROR("Unknown capture configuration section `%s'", p);
836 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
837 } else switch (type) {
839 DEBUG("Adding pattern \"%s\" to exclusion list", p);
840 ret = pattern_list_add_pattern(&config->exclusion_list, p);
842 case EXCLUSION_EXCEPTION:
843 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
844 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
846 case COMPRESSION_EXCLUSION_LIST:
847 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
848 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
851 DEBUG("Adding pattern \"%s\" to alignment list", p);
852 ret = pattern_list_add_pattern(&config->alignment_list, p);
855 ERROR("Line %lu of capture configuration is not "
856 "in a block (such as [ExclusionList])",
858 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
866 destroy_capture_config(config);
870 static bool match_pattern(const char *path, const char *path_basename,
871 const struct pattern_list *list)
873 for (size_t i = 0; i < list->num_pats; i++) {
874 const char *pat = list->pats[i];
877 /* Absolute path from root of capture */
880 if (strchr(pat, '/'))
881 /* Relative path from root of capture */
884 /* A file name pattern */
885 string = path_basename;
887 if (fnmatch(pat, string, FNM_PATHNAME
893 DEBUG("`%s' matches the pattern \"%s\"",
901 static void print_pattern_list(const struct pattern_list *list)
903 for (size_t i = 0; i < list->num_pats; i++)
904 printf(" %s\n", list->pats[i]);
907 static void print_capture_config(const struct capture_config *config)
909 if (config->exclusion_list.num_pats) {
910 puts("Files or folders excluded from image capture:");
911 print_pattern_list(&config->exclusion_list);
916 /* Return true if the image capture configuration file indicates we should
917 * exclude the filename @path from capture.
919 * If @exclude_prefix is %true, the part of the path up and including the name
920 * of the directory being captured is not included in the path for matching
921 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
922 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
925 bool exclude_path(const char *path, const struct capture_config *config,
928 const char *basename = path_basename(path);
929 if (exclude_prefix) {
930 wimlib_assert(strlen(path) >= config->prefix_len);
931 if (memcmp(config->prefix, path, config->prefix_len) == 0
932 && path[config->prefix_len] == '/')
933 path += config->prefix_len;
935 return match_pattern(path, basename, &config->exclusion_list) &&
936 !match_pattern(path, basename, &config->exclusion_exception);
943 * Adds an image to the WIM, delegating the capture of the dentry tree and
944 * security data to the function @capture_tree passed as a parameter.
945 * Currently, @capture_tree may be build_dentry_tree() for capturing a "regular"
946 * directory tree on disk, or build_dentry_tree_ntfs() for capturing a WIM image
947 * directory from a NTFS volume using libntfs-3g.
949 * The @capture_tree function is also expected to create lookup table entries
950 * for all the file streams it captures and insert them into @lookup_table,
951 * being careful to look for identical entries that already exist and simply
952 * increment the reference count for them rather than duplicating the entry.
954 int do_add_image(WIMStruct *w, const char *dir, const char *name,
955 const char *config_str, size_t config_len,
957 int (*capture_tree)(struct dentry **, const char *,
958 struct lookup_table *,
959 struct wim_security_data *,
960 const struct capture_config *,
964 struct dentry *root_dentry = NULL;
965 struct wim_security_data *sd;
966 struct capture_config config;
967 struct inode_table inode_tab;
968 struct hlist_head inode_list;
971 DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", dir);
973 if (!name || !*name) {
974 ERROR("Must specify a non-empty string for the image name");
975 return WIMLIB_ERR_INVALID_PARAM;
978 ERROR("Must specify the name of a directory or NTFS volume");
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 \"%s\" in `%s'",
990 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
993 DEBUG("Initializing capture configuration");
995 DEBUG("Using default capture configuration");
996 config_str = default_config;
997 config_len = strlen(default_config);
999 ret = init_capture_config(config_str, config_len, dir, &config);
1002 print_capture_config(&config);
1004 DEBUG("Allocating security data");
1006 sd = CALLOC(1, sizeof(struct wim_security_data));
1008 ret = WIMLIB_ERR_NOMEM;
1009 goto out_destroy_config;
1011 sd->total_length = 8;
1014 DEBUG("Building dentry tree.");
1015 if (flags & WIMLIB_ADD_IMAGE_FLAG_SHOW_PROGRESS) {
1016 printf("Scanning `%s'...\n", dir);
1018 ret = (*capture_tree)(&root_dentry, dir, w->lookup_table, sd,
1019 &config, flags | WIMLIB_ADD_IMAGE_FLAG_ROOT,
1021 destroy_capture_config(&config);
1024 ERROR("Failed to build dentry tree for `%s'", dir);
1025 goto out_free_security_data;
1028 DEBUG("Calculating full paths of dentries.");
1029 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1031 goto out_free_dentry_tree;
1033 ret = add_new_dentry_tree(w, root_dentry, sd);
1035 goto out_free_dentry_tree;
1037 DEBUG("Inserting dentries into inode table");
1038 ret = init_inode_table(&inode_tab, 9001);
1040 goto out_destroy_imd;
1042 for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
1044 DEBUG("Cleaning up the hard link groups");
1045 ret = fix_inodes(&inode_tab, &inode_list);
1046 destroy_inode_table(&inode_tab);
1048 goto out_destroy_imd;
1050 DEBUG("Assigning hard link group IDs");
1051 assign_inode_numbers(&inode_list);
1053 ret = xml_add_image(w, name);
1055 goto out_destroy_imd;
1057 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1058 w->hdr.boot_idx = w->hdr.image_count;
1062 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1064 w->hdr.image_count--;
1066 out_free_dentry_tree:
1067 free_dentry_tree(root_dentry, w->lookup_table);
1068 out_free_security_data:
1069 free_security_data(sd);
1071 destroy_capture_config(&config);
1076 * Adds an image to a WIM file from a directory tree on disk.
1078 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
1079 const char *name, const char *config_str,
1080 size_t config_len, int flags)
1082 return do_add_image(w, dir, name, config_str, config_len, flags,
1083 build_dentry_tree, NULL);