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,
49 struct lookup_table *table)
51 free_dentry_tree(imd->root_dentry, table);
52 free_security_data(imd->security_data);
54 /* Get rid of the lookup table entry for this image's metadata resource
57 lookup_table_unlink(table, imd->metadata_lte);
58 free_lookup_table_entry(imd->metadata_lte);
63 * Recursively builds a dentry tree from a directory tree on disk, outside the
66 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
67 * modified if successful. NULL if the file or directory was
68 * excluded from capture.
70 * @root_disk_path: The path to the root of the directory tree on disk.
72 * @lookup_table: The lookup table for the WIM file. For each file added to the
73 * dentry tree being built, an entry is added to the lookup table,
74 * unless an identical stream is already in the lookup table.
75 * These lookup table entries that are added point to the path of
78 * @sd: Ignored. (Security data only captured in NTFS mode.)
81 * Configuration for files to be excluded from capture.
83 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
85 * @extra_arg: Ignored. (Only used in NTFS mode.)
87 * @return: 0 on success, nonzero on failure. It is a failure if any of
88 * the files cannot be `stat'ed, or if any of the needed
89 * directories cannot be opened or read. Failure to add the files
90 * to the WIM may still occur later when trying to actually read
91 * the on-disk files during a call to wimlib_write() or
94 static int build_dentry_tree(struct dentry **root_ret,
95 const char *root_disk_path,
96 struct lookup_table *lookup_table,
97 struct wim_security_data *sd,
98 const struct capture_config *config,
102 struct stat root_stbuf;
104 int (*stat_fn)(const char *restrict, struct stat *restrict);
106 const char *filename;
109 if (exclude_path(root_disk_path, config, true)) {
110 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
111 ERROR("Cannot exclude the root directory from capture");
112 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
114 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
115 printf("Excluding file `%s' from capture\n",
122 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
127 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
128 printf("Scanning `%s'\n", root_disk_path);
130 ret = (*stat_fn)(root_disk_path, &root_stbuf);
132 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
133 return WIMLIB_ERR_STAT;
136 if ((add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
137 !S_ISDIR(root_stbuf.st_mode)) {
138 ERROR("`%s' is not a directory", root_disk_path);
139 return WIMLIB_ERR_NOTDIR;
141 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
142 && !S_ISLNK(root_stbuf.st_mode)) {
143 ERROR("`%s' is not a regular file, directory, or symbolic link.",
145 return WIMLIB_ERR_SPECIAL_FILE;
148 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
151 filename = path_basename(root_disk_path);
153 root = new_dentry_with_timeless_inode(filename);
155 return WIMLIB_ERR_NOMEM;
157 inode = root->d_inode;
159 inode->creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
160 inode->last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
161 inode->last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
162 if (sizeof(ino_t) >= 8)
163 inode->ino = (u64)root_stbuf.st_ino;
165 inode->ino = (u64)root_stbuf.st_ino |
166 ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
168 add_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
169 inode->resolved = true;
171 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
173 struct lookup_table_entry *lte;
174 u8 hash[SHA1_HASH_SIZE];
176 inode->attributes = FILE_ATTRIBUTE_NORMAL;
178 /* Empty files do not have to have a lookup table entry. */
179 if (root_stbuf.st_size == 0)
182 /* For each regular file, we must check to see if the file is in
183 * the lookup table already; if it is, we increment its refcnt;
184 * otherwise, we create a new lookup table entry and insert it.
187 ret = sha1sum(root_disk_path, hash);
191 lte = __lookup_resource(lookup_table, hash);
194 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
197 char *file_on_disk = STRDUP(root_disk_path);
199 ERROR("Failed to allocate memory for file path");
200 ret = WIMLIB_ERR_NOMEM;
203 lte = new_lookup_table_entry();
206 ret = WIMLIB_ERR_NOMEM;
209 lte->file_on_disk = file_on_disk;
210 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
211 lte->resource_entry.original_size = root_stbuf.st_size;
212 lte->resource_entry.size = root_stbuf.st_size;
213 copy_hash(lte->hash, hash);
214 lookup_table_insert(lookup_table, lte);
216 root->d_inode->lte = lte;
217 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
219 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
223 struct dentry *child;
225 dir = opendir(root_disk_path);
227 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
229 ret = WIMLIB_ERR_OPEN;
233 /* Buffer for names of files in directory. */
234 size_t len = strlen(root_disk_path);
235 char name[len + 1 + FILENAME_MAX + 1];
236 memcpy(name, root_disk_path, len);
239 /* Create a dentry for each entry in the directory on disk, and recurse
240 * to any subdirectories. */
246 ret = WIMLIB_ERR_READ;
247 ERROR_WITH_ERRNO("Error reading the "
253 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
254 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
256 strcpy(name + len + 1, p->d_name);
257 ret = build_dentry_tree(&child, name, lookup_table,
263 dentry_add_child(root, child);
266 } else { /* Archiving a symbolic link */
267 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
268 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
270 /* The idea here is to call readlink() to get the UNIX target of
271 * the symbolic link, then turn the target into a reparse point
272 * data buffer that contains a relative or absolute symbolic
273 * link (NOT a junction point or *full* path symbolic link with
277 char deref_name_buf[4096];
278 ssize_t deref_name_len;
280 deref_name_len = readlink(root_disk_path, deref_name_buf,
281 sizeof(deref_name_buf) - 1);
282 if (deref_name_len >= 0) {
283 deref_name_buf[deref_name_len] = '\0';
284 DEBUG("Read symlink `%s'", deref_name_buf);
285 ret = inode_set_symlink(root->d_inode, deref_name_buf,
289 * Unfortunately, Windows seems to have the
290 * concept of "file" symbolic links as being
291 * different from "directory" symbolic links...
292 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
293 * on the symbolic link if the *target* of the
294 * symbolic link is a directory.
297 if (stat(root_disk_path, &stbuf) == 0 &&
298 S_ISDIR(stbuf.st_mode))
300 inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
304 ERROR_WITH_ERRNO("Failed to read target of "
305 "symbolic link `%s'", root_disk_path);
306 ret = WIMLIB_ERR_READLINK;
313 free_dentry_tree(root, lookup_table);
320 struct list_head lte_list_head;
323 static int allocate_lte_if_needed(struct dentry *dentry, void *arg)
325 const WIMStruct *src_wim, *dest_wim;
326 struct list_head *lte_list_head;
329 src_wim = ((struct wim_pair*)arg)->src_wim;
330 dest_wim = ((struct wim_pair*)arg)->dest_wim;
331 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
332 inode = dentry->d_inode;
334 wimlib_assert(!inode->resolved);
336 for (unsigned i = 0; i <= inode->num_ads; i++) {
337 struct lookup_table_entry *src_lte, *dest_lte;
338 src_lte = inode_stream_lte_unresolved(inode, i,
339 src_wim->lookup_table);
341 if (src_lte && ++src_lte->out_refcnt == 1) {
342 dest_lte = inode_stream_lte_unresolved(inode, i,
343 dest_wim->lookup_table);
346 dest_lte = clone_lookup_table_entry(src_lte);
348 return WIMLIB_ERR_NOMEM;
349 list_add_tail(&dest_lte->staging_list, lte_list_head);
357 * This function takes in a dentry that was previously located only in image(s)
358 * in @src_wim, but now is being added to @dest_wim. For each stream associated
359 * with the dentry, if there is already a lookup table entry for that stream in
360 * the lookup table of the destination WIM file, its reference count is
361 * incrementej. Otherwise, a new lookup table entry is created that points back
362 * to the stream in the source WIM file (through the @hash field combined with
363 * the @wim field of the lookup table entry.)
365 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
367 WIMStruct *src_wim, *dest_wim;
370 src_wim = ((struct wim_pair*)arg)->src_wim;
371 dest_wim = ((struct wim_pair*)arg)->dest_wim;
372 inode = dentry->d_inode;
374 wimlib_assert(!inode->resolved);
376 for (unsigned i = 0; i <= inode->num_ads; i++) {
377 struct lookup_table_entry *src_lte, *dest_lte;
378 src_lte = inode_stream_lte_unresolved(inode, i,
379 src_wim->lookup_table);
381 if (!src_lte) /* Empty or nonexistent stream. */
384 dest_lte = inode_stream_lte_unresolved(inode, i,
385 dest_wim->lookup_table);
389 struct list_head *lte_list_head;
390 struct list_head *next;
392 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
393 wimlib_assert(!list_empty(lte_list_head));
395 next = lte_list_head->next;
397 dest_lte = container_of(next, struct lookup_table_entry,
399 dest_lte->part_number = 1;
400 dest_lte->refcnt = 1;
401 wimlib_assert(hashes_equal(dest_lte->hash, src_lte->hash));
403 lookup_table_insert(dest_wim->lookup_table, dest_lte);
410 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
411 * file, adds an entry to the lookup table for the image metadata, updates the
412 * image count in the header, and selects the new image.
414 * Does not update the XML data.
416 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
417 * 0 is returned and the image metadata array of @w is modified.
419 * @w: The WIMStruct for the WIM file.
420 * @root_dentry: The root of the directory tree for the image.
421 * @sd: The security data for the image.
423 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
424 struct wim_security_data *sd)
426 struct lookup_table_entry *metadata_lte;
427 struct image_metadata *imd;
428 struct image_metadata *new_imd;
431 wimlib_assert(root_dentry != NULL);
433 DEBUG("Reallocating image metadata array for image_count = %u",
434 w->hdr.image_count + 1);
435 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
438 ERROR("Failed to allocate memory for new image metadata array");
442 memcpy(imd, w->image_metadata,
443 w->hdr.image_count * sizeof(struct image_metadata));
445 metadata_lte = new_lookup_table_entry();
449 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
450 random_hash(metadata_lte->hash);
451 lookup_table_insert(w->lookup_table, metadata_lte);
453 new_imd = &imd[w->hdr.image_count];
455 new_imd->root_dentry = root_dentry;
456 new_imd->metadata_lte = metadata_lte;
457 new_imd->security_data = sd;
458 new_imd->modified = true;
460 FREE(w->image_metadata);
461 w->image_metadata = imd;
462 w->hdr.image_count++;
464 /* Change the current image to the new one. There should not be any
465 * ways for this to fail, since the image is valid and the dentry tree
466 * is already in memory. */
467 ret = select_wim_image(w, w->hdr.image_count);
468 wimlib_assert(ret == 0);
473 return WIMLIB_ERR_NOMEM;
478 * Copies an image, or all the images, from a WIM file, into another WIM file.
480 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
483 const char *dest_name,
484 const char *dest_description,
486 WIMStruct **additional_swms,
487 unsigned num_additional_swms)
492 struct wim_pair wims;
493 struct wim_security_data *sd;
494 struct lookup_table *joined_tab, *src_wim_tab_save;
496 if (!src_wim || !dest_wim)
497 return WIMLIB_ERR_INVALID_PARAM;
499 if (dest_wim->hdr.total_parts != 1) {
500 ERROR("Exporting an image to a split WIM is "
502 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
505 if (src_image == WIM_ALL_IMAGES) {
506 if (src_wim->hdr.image_count > 1) {
508 /* multi-image export. */
510 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
511 (src_wim->hdr.boot_idx == 0))
513 /* Specifying the boot flag on a multi-image
514 * source WIM makes the boot index default to
515 * the bootable image in the source WIM. It is
516 * an error if there is no such bootable image.
518 ERROR("Cannot specify `boot' flag when "
519 "exporting multiple images from a WIM "
520 "with no bootable images");
521 return WIMLIB_ERR_INVALID_PARAM;
523 if (dest_name || dest_description) {
524 ERROR("Image name or image description was "
525 "specified, but we are exporting "
527 return WIMLIB_ERR_INVALID_PARAM;
529 for (i = 1; i <= src_wim->hdr.image_count; i++) {
530 int export_flags = flags;
532 if (i != src_wim->hdr.boot_idx)
533 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
535 ret = wimlib_export_image(src_wim, i, dest_wim,
539 num_additional_swms);
544 } else if (src_wim->hdr.image_count == 1) {
552 dest_name = wimlib_get_image_name(src_wim, src_image);
553 DEBUG("Using name `%s' for source image %d",
554 dest_name, src_image);
557 if (!dest_description) {
558 dest_description = wimlib_get_image_description(src_wim,
560 DEBUG("Using description `%s' for source image %d",
561 dest_description, src_image);
564 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
566 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
567 ERROR("There is already an image named `%s' in the "
568 "destination WIM", dest_name);
569 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
572 ret = verify_swm_set(src_wim, additional_swms, num_additional_swms);
576 if (num_additional_swms) {
577 ret = new_joined_lookup_table(src_wim, additional_swms,
582 src_wim_tab_save = src_wim->lookup_table;
583 src_wim->lookup_table = joined_tab;
586 ret = select_wim_image(src_wim, src_image);
588 ERROR("Could not select image %d from the WIM `%s' "
589 "to export it", src_image, src_wim->filename);
593 /* Pre-allocate the new lookup table entries that will be needed. This
594 * way, it's not possible to run out of memory part-way through
595 * modifying the lookup table of the destination WIM. */
596 wims.src_wim = src_wim;
597 wims.dest_wim = dest_wim;
598 INIT_LIST_HEAD(&wims.lte_list_head);
599 for_lookup_table_entry(src_wim->lookup_table, lte_zero_out_refcnt, NULL);
600 root = wim_root_dentry(src_wim);
601 for_dentry_in_tree(root, dentry_unresolve_ltes, NULL);
602 ret = for_dentry_in_tree(root, allocate_lte_if_needed, &wims);
606 ret = xml_export_image(src_wim->wim_info, src_image,
607 &dest_wim->wim_info, dest_name, dest_description);
611 sd = wim_security_data(src_wim);
612 ret = add_new_dentry_tree(dest_wim, root, sd);
614 goto out_xml_delete_image;
617 /* All memory allocations have been taken care of, so it's no longer
618 * possible for this function to fail. Go ahead and increment the
619 * reference counts of the dentry tree and security data, then update
620 * the lookup table of the destination WIM and the boot index, if
622 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
624 for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
625 wimlib_assert(list_empty(&wims.lte_list_head));
627 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
628 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
629 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
634 out_xml_delete_image:
635 xml_delete_image(&dest_wim->wim_info, dest_wim->hdr.image_count);
638 struct lookup_table_entry *lte, *tmp;
639 list_for_each_entry_safe(lte, tmp, &wims.lte_list_head, staging_list)
640 free_lookup_table_entry(lte);
644 if (num_additional_swms) {
645 free_lookup_table(src_wim->lookup_table);
646 src_wim->lookup_table = src_wim_tab_save;
652 * Deletes an image from the WIM.
654 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
659 if (w->hdr.total_parts != 1) {
660 ERROR("Deleting an image from a split WIM is not supported.");
661 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
664 if (image == WIM_ALL_IMAGES) {
665 for (i = w->hdr.image_count; i >= 1; i--) {
666 ret = wimlib_delete_image(w, i);
673 DEBUG("Deleting image %d", image);
675 /* Even if the dentry tree is not allocated, we must select it (and
676 * therefore allocate it) so that we can decrement the reference counts
677 * in the lookup table. */
678 ret = select_wim_image(w, image);
682 /* Free the dentry tree, any lookup table entries that have their
683 * refcnt decremented to 0, and the security data. */
684 destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table);
686 /* Get rid of the empty slot in the image metadata array. */
687 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
688 (w->hdr.image_count - image) * sizeof(struct image_metadata));
690 /* Decrement the image count. */
691 if (--w->hdr.image_count == 0) {
692 FREE(w->image_metadata);
693 w->image_metadata = NULL;
696 /* Fix the boot index. */
697 if (w->hdr.boot_idx == image)
699 else if (w->hdr.boot_idx > image)
702 w->current_image = WIM_NO_IMAGE;
704 /* Remove the image from the XML information. */
705 xml_delete_image(&w->wim_info, image);
707 w->deletion_occurred = true;
715 COMPRESSION_EXCLUSION_LIST,
719 /* Default capture configuration file when none is specified. */
720 static const char *default_config =
725 "\\System Volume Information\n"
729 "[CompressionExclusionList]\n"
733 "\\WINDOWS\\inf\\*.pnf\n";
735 static void destroy_pattern_list(struct pattern_list *list)
740 static void destroy_capture_config(struct capture_config *config)
742 destroy_pattern_list(&config->exclusion_list);
743 destroy_pattern_list(&config->exclusion_exception);
744 destroy_pattern_list(&config->compression_exclusion_list);
745 destroy_pattern_list(&config->alignment_list);
746 FREE(config->config_str);
747 FREE(config->prefix);
748 memset(config, 0, sizeof(*config));
751 static int pattern_list_add_pattern(struct pattern_list *list,
755 if (list->num_pats >= list->num_allocated_pats) {
756 pats = REALLOC(list->pats,
757 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
759 return WIMLIB_ERR_NOMEM;
760 list->num_allocated_pats += 8;
763 list->pats[list->num_pats++] = pattern;
767 /* Parses the contents of the image capture configuration file and fills in a
768 * `struct capture_config'. */
769 static int init_capture_config(const char *_config_str, size_t config_len,
770 const char *_prefix, struct capture_config *config)
777 size_t bytes_remaining;
778 enum pattern_type type = NONE;
780 unsigned long line_no = 0;
782 DEBUG("config_len = %zu", config_len);
783 bytes_remaining = config_len;
784 memset(config, 0, sizeof(*config));
785 config_str = MALLOC(config_len);
787 ERROR("Could not duplicate capture config string");
788 return WIMLIB_ERR_NOMEM;
790 prefix = STRDUP(_prefix);
793 return WIMLIB_ERR_NOMEM;
796 memcpy(config_str, _config_str, config_len);
798 config->config_str = config_str;
799 config->prefix = prefix;
800 config->prefix_len = strlen(prefix);
801 while (bytes_remaining) {
804 eol = memchr(p, '\n', bytes_remaining);
806 ERROR("Expected end-of-line in capture config file on "
807 "line %lu", line_no);
808 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
813 bytes_remaining -= (next_p - p);
817 if (*(eol - 1) == '\r')
821 /* Translate backslash to forward slash */
822 for (char *pp = p; pp != eol; pp++)
826 /* Remove drive letter */
827 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
831 if (strcmp(p, "[ExclusionList]") == 0)
832 type = EXCLUSION_LIST;
833 else if (strcmp(p, "[ExclusionException]") == 0)
834 type = EXCLUSION_EXCEPTION;
835 else if (strcmp(p, "[CompressionExclusionList]") == 0)
836 type = COMPRESSION_EXCLUSION_LIST;
837 else if (strcmp(p, "[AlignmentList]") == 0)
838 type = ALIGNMENT_LIST;
839 else if (p[0] == '[' && strrchr(p, ']')) {
840 ERROR("Unknown capture configuration section `%s'", p);
841 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
842 } else switch (type) {
844 DEBUG("Adding pattern \"%s\" to exclusion list", p);
845 ret = pattern_list_add_pattern(&config->exclusion_list, p);
847 case EXCLUSION_EXCEPTION:
848 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
849 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
851 case COMPRESSION_EXCLUSION_LIST:
852 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
853 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
856 DEBUG("Adding pattern \"%s\" to alignment list", p);
857 ret = pattern_list_add_pattern(&config->alignment_list, p);
860 ERROR("Line %lu of capture configuration is not "
861 "in a block (such as [ExclusionList])",
863 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
871 destroy_capture_config(config);
875 static bool match_pattern(const char *path, const char *path_basename,
876 const struct pattern_list *list)
878 for (size_t i = 0; i < list->num_pats; i++) {
879 const char *pat = list->pats[i];
882 /* Absolute path from root of capture */
885 if (strchr(pat, '/'))
886 /* Relative path from root of capture */
889 /* A file name pattern */
890 string = path_basename;
892 if (fnmatch(pat, string, FNM_PATHNAME
898 DEBUG("`%s' matches the pattern \"%s\"",
906 static void print_pattern_list(const struct pattern_list *list)
908 for (size_t i = 0; i < list->num_pats; i++)
909 printf(" %s\n", list->pats[i]);
912 static void print_capture_config(const struct capture_config *config)
914 if (config->exclusion_list.num_pats) {
915 puts("Files or folders excluded from image capture:");
916 print_pattern_list(&config->exclusion_list);
921 /* Return true if the image capture configuration file indicates we should
922 * exclude the filename @path from capture.
924 * If @exclude_prefix is %true, the part of the path up and including the name
925 * of the directory being captured is not included in the path for matching
926 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
927 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
930 bool exclude_path(const char *path, const struct capture_config *config,
933 const char *basename = path_basename(path);
934 if (exclude_prefix) {
935 wimlib_assert(strlen(path) >= config->prefix_len);
936 if (memcmp(config->prefix, path, config->prefix_len) == 0
937 && path[config->prefix_len] == '/')
938 path += config->prefix_len;
940 return match_pattern(path, basename, &config->exclusion_list) &&
941 !match_pattern(path, basename, &config->exclusion_exception);
948 * Adds an image to the WIM, delegating the capture of the dentry tree and
949 * security data to the function @capture_tree passed as a parameter.
950 * Currently, @capture_tree may be build_dentry_tree() for capturing a "regular"
951 * directory tree on disk, or build_dentry_tree_ntfs() for capturing a WIM image
952 * directory from a NTFS volume using libntfs-3g.
954 * The @capture_tree function is also expected to create lookup table entries
955 * for all the file streams it captures and insert them into @lookup_table,
956 * being careful to look for identical entries that already exist and simply
957 * increment the reference count for them rather than duplicating the entry.
959 int do_add_image(WIMStruct *w, const char *dir, const char *name,
960 const char *config_str, size_t config_len,
962 int (*capture_tree)(struct dentry **, const char *,
963 struct lookup_table *,
964 struct wim_security_data *,
965 const struct capture_config *,
969 struct dentry *root_dentry = NULL;
970 struct wim_security_data *sd;
971 struct capture_config config;
972 struct inode_table inode_tab;
973 struct hlist_head inode_list;
976 DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", dir);
978 if (!name || !*name) {
979 ERROR("Must specify a non-empty string for the image name");
980 return WIMLIB_ERR_INVALID_PARAM;
983 ERROR("Must specify the name of a directory or NTFS volume");
984 return WIMLIB_ERR_INVALID_PARAM;
987 if (w->hdr.total_parts != 1) {
988 ERROR("Cannot add an image to a split WIM");
989 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
992 if (wimlib_image_name_in_use(w, name)) {
993 ERROR("There is already an image named \"%s\" in `%s'",
995 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
998 DEBUG("Initializing capture configuration");
1000 DEBUG("Using default capture configuration");
1001 config_str = default_config;
1002 config_len = strlen(default_config);
1004 ret = init_capture_config(config_str, config_len, dir, &config);
1007 print_capture_config(&config);
1009 DEBUG("Allocating security data");
1011 sd = CALLOC(1, sizeof(struct wim_security_data));
1013 ret = WIMLIB_ERR_NOMEM;
1014 goto out_destroy_config;
1016 sd->total_length = 8;
1019 DEBUG("Building dentry tree.");
1020 if (flags & WIMLIB_ADD_IMAGE_FLAG_SHOW_PROGRESS) {
1021 printf("Scanning `%s'...\n", dir);
1023 ret = (*capture_tree)(&root_dentry, dir, w->lookup_table, sd,
1024 &config, flags | WIMLIB_ADD_IMAGE_FLAG_ROOT,
1026 destroy_capture_config(&config);
1029 ERROR("Failed to build dentry tree for `%s'", dir);
1030 goto out_free_security_data;
1033 DEBUG("Calculating full paths of dentries.");
1034 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1036 goto out_free_dentry_tree;
1038 ret = add_new_dentry_tree(w, root_dentry, sd);
1040 goto out_free_dentry_tree;
1042 DEBUG("Inserting dentries into inode table");
1043 ret = init_inode_table(&inode_tab, 9001);
1045 goto out_destroy_imd;
1047 for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
1049 DEBUG("Cleaning up the hard link groups");
1050 ret = fix_inodes(&inode_tab, &inode_list);
1051 destroy_inode_table(&inode_tab);
1053 goto out_destroy_imd;
1055 DEBUG("Assigning hard link group IDs");
1056 assign_inode_numbers(&inode_list);
1058 ret = xml_add_image(w, name);
1060 goto out_destroy_imd;
1062 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1063 w->hdr.boot_idx = w->hdr.image_count;
1067 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1069 w->hdr.image_count--;
1071 out_free_dentry_tree:
1072 free_dentry_tree(root_dentry, w->lookup_table);
1073 out_free_security_data:
1074 free_security_data(sd);
1076 destroy_capture_config(&config);
1081 * Adds an image to a WIM file from a directory tree on disk.
1083 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
1084 const char *name, const char *config_str,
1085 size_t config_len, int flags)
1087 return do_add_image(w, dir, name, config_str, config_len, flags,
1088 build_dentry_tree, NULL);