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"
43 /** Private flag: Used to mark that we currently adding the root directory of
45 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
47 void destroy_image_metadata(struct image_metadata *imd, struct lookup_table *lt)
49 free_dentry_tree(imd->root_dentry, lt);
50 free_security_data(imd->security_data);
52 /* Get rid of the lookup table entry for this image's metadata resource
55 lookup_table_remove(lt, imd->metadata_lte);
59 * Recursively builds a dentry tree from a directory tree on disk, outside the
62 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
63 * modified if successful. NULL if the file or directory was
64 * excluded from capture.
66 * @root_disk_path: The path to the root of the directory tree on disk.
68 * @lookup_table: The lookup table for the WIM file. For each file added to the
69 * dentry tree being built, an entry is added to the lookup table,
70 * unless an identical stream is already in the lookup table.
71 * These lookup table entries that are added point to the path of
74 * @sd: Ignored. (Security data only captured in NTFS mode.)
77 * Configuration for files to be excluded from capture.
79 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
81 * @extra_arg: Ignored. (Only used in NTFS mode.)
83 * @return: 0 on success, nonzero on failure. It is a failure if any of
84 * the files cannot be `stat'ed, or if any of the needed
85 * directories cannot be opened or read. Failure to add the files
86 * to the WIM may still occur later when trying to actually read
87 * the on-disk files during a call to wimlib_write() or
90 static int build_dentry_tree(struct dentry **root_ret,
91 const char *root_disk_path,
92 struct lookup_table *lookup_table,
93 struct wim_security_data *sd,
94 const struct capture_config *config,
98 struct stat root_stbuf;
100 int (*stat_fn)(const char *restrict, struct stat *restrict);
102 const char *filename;
104 if (exclude_path(root_disk_path, config, true)) {
105 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
106 ERROR("Cannot exclude the root directory from capture");
107 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
109 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
110 printf("Excluding file `%s' from capture\n",
117 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
122 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
123 printf("Scanning `%s'\n", root_disk_path);
125 ret = (*stat_fn)(root_disk_path, &root_stbuf);
127 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
128 return WIMLIB_ERR_STAT;
131 if ((add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
132 !S_ISDIR(root_stbuf.st_mode)) {
133 ERROR("`%s' is not a directory", root_disk_path);
134 return WIMLIB_ERR_NOTDIR;
136 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
137 && !S_ISLNK(root_stbuf.st_mode)) {
138 ERROR("`%s' is not a regular file, directory, or symbolic link.",
140 return WIMLIB_ERR_SPECIAL_FILE;
143 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
146 filename = path_basename(root_disk_path);
148 root = new_dentry_with_timeless_inode(filename);
150 return WIMLIB_ERR_NOMEM;
152 stbuf_to_inode(&root_stbuf, root->d_inode);
153 add_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
154 root->d_inode->resolved = true;
156 if (dentry_is_directory(root)) { /* Archiving a directory */
159 struct dentry *child;
161 dir = opendir(root_disk_path);
163 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
165 ret = WIMLIB_ERR_OPEN;
169 /* Buffer for names of files in directory. */
170 size_t len = strlen(root_disk_path);
171 char name[len + 1 + FILENAME_MAX + 1];
172 memcpy(name, root_disk_path, len);
175 /* Create a dentry for each entry in the directory on disk, and recurse
176 * to any subdirectories. */
182 ret = WIMLIB_ERR_READ;
183 ERROR_WITH_ERRNO("Error reading the "
189 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
190 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
192 strcpy(name + len + 1, p->d_name);
193 ret = build_dentry_tree(&child, name, lookup_table,
199 dentry_add_child(root, child);
202 } else if (dentry_is_symlink(root)) { /* Archiving a symbolic link */
203 char deref_name_buf[4096];
204 ssize_t deref_name_len;
206 deref_name_len = readlink(root_disk_path, deref_name_buf,
207 sizeof(deref_name_buf) - 1);
208 if (deref_name_len >= 0) {
209 deref_name_buf[deref_name_len] = '\0';
210 DEBUG("Read symlink `%s'", deref_name_buf);
211 ret = inode_set_symlink(root->d_inode, deref_name_buf,
214 ERROR_WITH_ERRNO("Failed to read target of "
215 "symbolic link `%s'", root_disk_path);
216 ret = WIMLIB_ERR_READLINK;
218 } else { /* Archiving a regular file */
220 struct lookup_table_entry *lte;
221 u8 hash[SHA1_HASH_SIZE];
223 /* Empty files do not have to have a lookup table entry. */
224 if (root_stbuf.st_size == 0)
227 /* For each regular file, we must check to see if the file is in
228 * the lookup table already; if it is, we increment its refcnt;
229 * otherwise, we create a new lookup table entry and insert it.
232 ret = sha1sum(root_disk_path, hash);
236 lte = __lookup_resource(lookup_table, hash);
239 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
242 char *file_on_disk = STRDUP(root_disk_path);
244 ERROR("Failed to allocate memory for file path");
245 ret = WIMLIB_ERR_NOMEM;
248 lte = new_lookup_table_entry();
251 ret = WIMLIB_ERR_NOMEM;
254 lte->file_on_disk = file_on_disk;
255 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
256 lte->resource_entry.original_size = root_stbuf.st_size;
257 lte->resource_entry.size = root_stbuf.st_size;
258 copy_hash(lte->hash, hash);
259 lookup_table_insert(lookup_table, lte);
261 root->d_inode->lte = lte;
267 free_dentry_tree(root, lookup_table);
274 struct list_head lte_list_head;
277 static int allocate_lte_if_needed(struct dentry *dentry, void *arg)
279 const WIMStruct *src_wim, *dest_wim;
280 struct list_head *lte_list_head;
283 src_wim = ((struct wim_pair*)arg)->src_wim;
284 dest_wim = ((struct wim_pair*)arg)->dest_wim;
285 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
286 inode = dentry->d_inode;
288 wimlib_assert(!inode->resolved);
290 for (unsigned i = 0; i <= inode->num_ads; i++) {
291 struct lookup_table_entry *src_lte, *dest_lte;
292 src_lte = inode_stream_lte_unresolved(inode, i,
293 src_wim->lookup_table);
295 if (src_lte && ++src_lte->out_refcnt == 1) {
296 dest_lte = inode_stream_lte_unresolved(inode, i,
297 dest_wim->lookup_table);
300 dest_lte = clone_lookup_table_entry(src_lte);
302 return WIMLIB_ERR_NOMEM;
303 list_add_tail(&dest_lte->list, lte_list_head);
311 * This function takes in a dentry that was previously located only in image(s)
312 * in @src_wim, but now is being added to @dest_wim. For each stream associated
313 * with the dentry, if there is already a lookup table entry for that stream in
314 * the lookup table of the destination WIM file, its reference count is
315 * incrementej. Otherwise, a new lookup table entry is created that points back
316 * to the stream in the source WIM file (through the @hash field combined with
317 * the @wim field of the lookup table entry.)
319 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
321 WIMStruct *src_wim, *dest_wim;
324 src_wim = ((struct wim_pair*)arg)->src_wim;
325 dest_wim = ((struct wim_pair*)arg)->dest_wim;
326 inode = dentry->d_inode;
328 wimlib_assert(!inode->resolved);
330 for (unsigned i = 0; i <= inode->num_ads; i++) {
331 struct lookup_table_entry *src_lte, *dest_lte;
332 src_lte = inode_stream_lte_unresolved(inode, i,
333 src_wim->lookup_table);
335 if (!src_lte) /* Empty or nonexistent stream. */
338 dest_lte = inode_stream_lte_unresolved(inode, i,
339 dest_wim->lookup_table);
343 struct list_head *lte_list_head;
344 struct list_head *next;
346 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
347 wimlib_assert(!list_empty(lte_list_head));
349 next = lte_list_head->next;
351 dest_lte = container_of(next, struct lookup_table_entry, list);
352 dest_lte->part_number = 1;
353 dest_lte->refcnt = 1;
354 wimlib_assert(hashes_equal(dest_lte->hash, src_lte->hash));
356 lookup_table_insert(dest_wim->lookup_table, dest_lte);
363 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
364 * file, adds an entry to the lookup table for the image metadata, updates the
365 * image count in the header, and selects the new image.
367 * Does not update the XML data.
369 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
370 * 0 is returned and the image metadata array of @w is modified.
372 * @w: The WIMStruct for the WIM file.
373 * @root_dentry: The root of the directory tree for the image.
374 * @sd: The security data for the image.
376 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
377 struct wim_security_data *sd)
379 struct lookup_table_entry *metadata_lte;
380 struct image_metadata *imd;
381 struct image_metadata *new_imd;
384 wimlib_assert(root_dentry != NULL);
386 DEBUG("Reallocating image metadata array for image_count = %u",
387 w->hdr.image_count + 1);
388 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
391 ERROR("Failed to allocate memory for new image metadata array");
395 memcpy(imd, w->image_metadata,
396 w->hdr.image_count * sizeof(struct image_metadata));
398 metadata_lte = new_lookup_table_entry();
402 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
403 random_hash(metadata_lte->hash);
404 lookup_table_insert(w->lookup_table, metadata_lte);
406 new_imd = &imd[w->hdr.image_count];
408 new_imd->root_dentry = root_dentry;
409 new_imd->metadata_lte = metadata_lte;
410 new_imd->security_data = sd;
411 new_imd->modified = true;
413 FREE(w->image_metadata);
414 w->image_metadata = imd;
415 w->hdr.image_count++;
417 /* Change the current image to the new one. There should not be any
418 * ways for this to fail, since the image is valid and the dentry tree
419 * is already in memory. */
420 ret = select_wim_image(w, w->hdr.image_count);
421 wimlib_assert(ret == 0);
426 return WIMLIB_ERR_NOMEM;
431 * Copies an image, or all the images, from a WIM file, into another WIM file.
433 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
436 const char *dest_name,
437 const char *dest_description,
439 WIMStruct **additional_swms,
440 unsigned num_additional_swms)
445 struct wim_pair wims;
446 struct wim_security_data *sd;
447 struct lookup_table *joined_tab, *src_wim_tab_save;
449 if (!src_wim || !dest_wim)
450 return WIMLIB_ERR_INVALID_PARAM;
452 if (dest_wim->hdr.total_parts != 1) {
453 ERROR("Exporting an image to a split WIM is "
455 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
458 if (src_image == WIM_ALL_IMAGES) {
459 if (src_wim->hdr.image_count > 1) {
461 /* multi-image export. */
463 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
464 (src_wim->hdr.boot_idx == 0))
466 /* Specifying the boot flag on a multi-image
467 * source WIM makes the boot index default to
468 * the bootable image in the source WIM. It is
469 * an error if there is no such bootable image.
471 ERROR("Cannot specify `boot' flag when "
472 "exporting multiple images from a WIM "
473 "with no bootable images");
474 return WIMLIB_ERR_INVALID_PARAM;
476 if (dest_name || dest_description) {
477 ERROR("Image name or image description was "
478 "specified, but we are exporting "
480 return WIMLIB_ERR_INVALID_PARAM;
482 for (i = 1; i <= src_wim->hdr.image_count; i++) {
483 int export_flags = flags;
485 if (i != src_wim->hdr.boot_idx)
486 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
488 ret = wimlib_export_image(src_wim, i, dest_wim,
492 num_additional_swms);
497 } else if (src_wim->hdr.image_count == 1) {
505 dest_name = wimlib_get_image_name(src_wim, src_image);
506 DEBUG("Using name `%s' for source image %d",
507 dest_name, src_image);
510 if (!dest_description) {
511 dest_description = wimlib_get_image_description(src_wim,
513 DEBUG("Using description `%s' for source image %d",
514 dest_description, src_image);
517 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
519 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
520 ERROR("There is already an image named `%s' in the "
521 "destination WIM", dest_name);
522 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
525 ret = verify_swm_set(src_wim, additional_swms, num_additional_swms);
529 if (num_additional_swms) {
530 ret = new_joined_lookup_table(src_wim, additional_swms,
535 src_wim_tab_save = src_wim->lookup_table;
536 src_wim->lookup_table = joined_tab;
539 ret = select_wim_image(src_wim, src_image);
541 ERROR("Could not select image %d from the WIM `%s' "
542 "to export it", src_image, src_wim->filename);
546 /* Pre-allocate the new lookup table entries that will be needed. This
547 * way, it's not possible to run out of memory part-way through
548 * modifying the lookup table of the destination WIM. */
549 wims.src_wim = src_wim;
550 wims.dest_wim = dest_wim;
551 INIT_LIST_HEAD(&wims.lte_list_head);
552 for_lookup_table_entry(src_wim->lookup_table, lte_zero_out_refcnt, NULL);
553 root = wim_root_dentry(src_wim);
554 for_dentry_in_tree(root, dentry_unresolve_ltes, NULL);
555 ret = for_dentry_in_tree(root, allocate_lte_if_needed, &wims);
559 ret = xml_export_image(src_wim->wim_info, src_image,
560 &dest_wim->wim_info, dest_name, dest_description);
564 sd = wim_security_data(src_wim);
565 ret = add_new_dentry_tree(dest_wim, root, sd);
567 goto out_xml_delete_image;
570 /* All memory allocations have been taken care of, so it's no longer
571 * possible for this function to fail. Go ahead and increment the
572 * reference counts of the dentry tree and security data, then update
573 * the lookup table of the destination WIM and the boot index, if
575 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
577 for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
578 wimlib_assert(list_empty(&wims.lte_list_head));
580 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
581 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
582 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
587 out_xml_delete_image:
588 xml_delete_image(&dest_wim->wim_info, dest_wim->hdr.image_count);
591 struct lookup_table_entry *lte, *tmp;
592 list_for_each_entry_safe(lte, tmp, &wims.lte_list_head, list)
593 free_lookup_table_entry(lte);
597 if (num_additional_swms) {
598 free_lookup_table(src_wim->lookup_table);
599 src_wim->lookup_table = src_wim_tab_save;
605 * Deletes an image from the WIM.
607 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
612 if (w->hdr.total_parts != 1) {
613 ERROR("Deleting an image from a split WIM is not supported.");
614 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
617 if (image == WIM_ALL_IMAGES) {
618 for (i = w->hdr.image_count; i >= 1; i--) {
619 ret = wimlib_delete_image(w, i);
626 DEBUG("Deleting image %d", image);
628 /* Even if the dentry tree is not allocated, we must select it (and
629 * therefore allocate it) so that we can decrement the reference counts
630 * in the lookup table. */
631 ret = select_wim_image(w, image);
635 /* Free the dentry tree, any lookup table entries that have their
636 * refcnt decremented to 0, and the security data. */
637 destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table);
639 /* Get rid of the empty slot in the image metadata array. */
640 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
641 (w->hdr.image_count - image) * sizeof(struct image_metadata));
643 /* Decrement the image count. */
644 if (--w->hdr.image_count == 0) {
645 FREE(w->image_metadata);
646 w->image_metadata = NULL;
649 /* Fix the boot index. */
650 if (w->hdr.boot_idx == image)
652 else if (w->hdr.boot_idx > image)
655 w->current_image = WIM_NO_IMAGE;
657 /* Remove the image from the XML information. */
658 xml_delete_image(&w->wim_info, image);
666 COMPRESSION_EXCLUSION_LIST,
670 /* Default capture configuration file when none is specified. */
671 static const char *default_config =
676 "\\System Volume Information\n"
680 "[CompressionExclusionList]\n"
684 "\\WINDOWS\\inf\\*.pnf\n";
686 static void destroy_pattern_list(struct pattern_list *list)
691 static void destroy_capture_config(struct capture_config *config)
693 destroy_pattern_list(&config->exclusion_list);
694 destroy_pattern_list(&config->exclusion_exception);
695 destroy_pattern_list(&config->compression_exclusion_list);
696 destroy_pattern_list(&config->alignment_list);
697 FREE(config->config_str);
698 FREE(config->prefix);
699 memset(config, 0, sizeof(*config));
702 static int pattern_list_add_pattern(struct pattern_list *list,
706 if (list->num_pats >= list->num_allocated_pats) {
707 pats = REALLOC(list->pats,
708 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
710 return WIMLIB_ERR_NOMEM;
711 list->num_allocated_pats += 8;
714 list->pats[list->num_pats++] = pattern;
718 /* Parses the contents of the image capture configuration file and fills in a
719 * `struct capture_config'. */
720 static int init_capture_config(const char *_config_str, size_t config_len,
721 const char *_prefix, struct capture_config *config)
728 size_t bytes_remaining;
729 enum pattern_type type = NONE;
731 unsigned long line_no = 0;
733 DEBUG("config_len = %zu", config_len);
734 bytes_remaining = config_len;
735 memset(config, 0, sizeof(*config));
736 config_str = MALLOC(config_len);
738 ERROR("Could not duplicate capture config string");
739 return WIMLIB_ERR_NOMEM;
741 prefix = STRDUP(_prefix);
744 return WIMLIB_ERR_NOMEM;
747 memcpy(config_str, _config_str, config_len);
749 config->config_str = config_str;
750 config->prefix = prefix;
751 config->prefix_len = strlen(prefix);
752 while (bytes_remaining) {
755 eol = memchr(p, '\n', bytes_remaining);
757 ERROR("Expected end-of-line in capture config file on "
758 "line %lu", line_no);
759 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
764 bytes_remaining -= (next_p - p);
768 if (*(eol - 1) == '\r')
772 /* Translate backslash to forward slash */
773 for (char *pp = p; pp != eol; pp++)
777 /* Remove drive letter */
778 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
782 if (strcmp(p, "[ExclusionList]") == 0)
783 type = EXCLUSION_LIST;
784 else if (strcmp(p, "[ExclusionException]") == 0)
785 type = EXCLUSION_EXCEPTION;
786 else if (strcmp(p, "[CompressionExclusionList]") == 0)
787 type = COMPRESSION_EXCLUSION_LIST;
788 else if (strcmp(p, "[AlignmentList]") == 0)
789 type = ALIGNMENT_LIST;
790 else if (p[0] == '[' && strrchr(p, ']')) {
791 ERROR("Unknown capture configuration section `%s'", p);
792 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
793 } else switch (type) {
795 DEBUG("Adding pattern \"%s\" to exclusion list", p);
796 ret = pattern_list_add_pattern(&config->exclusion_list, p);
798 case EXCLUSION_EXCEPTION:
799 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
800 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
802 case COMPRESSION_EXCLUSION_LIST:
803 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
804 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
807 DEBUG("Adding pattern \"%s\" to alignment list", p);
808 ret = pattern_list_add_pattern(&config->alignment_list, p);
811 ERROR("Line %lu of capture configuration is not "
812 "in a block (such as [ExclusionList])",
814 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
822 destroy_capture_config(config);
826 static bool match_pattern(const char *path, const char *path_basename,
827 const struct pattern_list *list)
829 for (size_t i = 0; i < list->num_pats; i++) {
830 const char *pat = list->pats[i];
833 /* Absolute path from root of capture */
836 if (strchr(pat, '/'))
837 /* Relative path from root of capture */
840 /* A file name pattern */
841 string = path_basename;
843 if (fnmatch(pat, string, FNM_PATHNAME
849 DEBUG("`%s' matches the pattern \"%s\"",
857 static void print_pattern_list(const struct pattern_list *list)
859 for (size_t i = 0; i < list->num_pats; i++)
860 printf(" %s\n", list->pats[i]);
863 static void print_capture_config(const struct capture_config *config)
865 if (config->exclusion_list.num_pats) {
866 puts("Files or folders excluded from image capture:");
867 print_pattern_list(&config->exclusion_list);
872 /* Return true if the image capture configuration file indicates we should
873 * exclude the filename @path from capture.
875 * If @exclude_prefix is %true, the part of the path up and including the name
876 * of the directory being captured is not included in the path for matching
877 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
878 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
881 bool exclude_path(const char *path, const struct capture_config *config,
884 const char *basename = path_basename(path);
885 if (exclude_prefix) {
886 wimlib_assert(strlen(path) >= config->prefix_len);
887 if (memcmp(config->prefix, path, config->prefix_len) == 0
888 && path[config->prefix_len] == '/')
889 path += config->prefix_len;
891 return match_pattern(path, basename, &config->exclusion_list) &&
892 !match_pattern(path, basename, &config->exclusion_exception);
899 * Adds an image to the WIM, delegating the capture of the dentry tree and
900 * security data to the function @capture_tree passed as a parameter.
901 * Currently, @capture_tree may be build_dentry_tree() for capturing a "regular"
902 * directory tree on disk, or build_dentry_tree_ntfs() for capturing a WIM image
903 * directory from a NTFS volume using libntfs-3g.
905 * The @capture_tree function is also expected to create lookup table entries
906 * for all the file streams it captures and insert them into @lookup_table,
907 * being careful to look for identical entries that already exist and simply
908 * increment the reference count for them rather than duplicating the entry.
910 int do_add_image(WIMStruct *w, const char *dir, const char *name,
911 const char *config_str, size_t config_len,
913 int (*capture_tree)(struct dentry **, const char *,
914 struct lookup_table *,
915 struct wim_security_data *,
916 const struct capture_config *,
920 struct dentry *root_dentry = NULL;
921 struct wim_security_data *sd;
922 struct capture_config config;
923 struct inode_table inode_tab;
924 struct hlist_head inode_list;
927 DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", dir);
929 if (!name || !*name) {
930 ERROR("Must specify a non-empty string for the image name");
931 return WIMLIB_ERR_INVALID_PARAM;
934 ERROR("Must specify the name of a directory or NTFS volume");
935 return WIMLIB_ERR_INVALID_PARAM;
938 if (w->hdr.total_parts != 1) {
939 ERROR("Cannot add an image to a split WIM");
940 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
943 if (wimlib_image_name_in_use(w, name)) {
944 ERROR("There is already an image named \"%s\" in `%s'",
946 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
949 DEBUG("Initializing capture configuration");
951 DEBUG("Using default capture configuration");
952 config_str = default_config;
953 config_len = strlen(default_config);
955 ret = init_capture_config(config_str, config_len, dir, &config);
958 print_capture_config(&config);
960 DEBUG("Allocating security data");
962 sd = CALLOC(1, sizeof(struct wim_security_data));
964 ret = WIMLIB_ERR_NOMEM;
965 goto out_destroy_config;
967 sd->total_length = 8;
970 DEBUG("Building dentry tree.");
971 ret = (*capture_tree)(&root_dentry, dir, w->lookup_table, sd,
972 &config, flags | WIMLIB_ADD_IMAGE_FLAG_ROOT,
974 destroy_capture_config(&config);
977 ERROR("Failed to build dentry tree for `%s'", dir);
978 goto out_free_security_data;
981 DEBUG("Calculating full paths of dentries.");
982 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
984 goto out_free_dentry_tree;
986 ret = add_new_dentry_tree(w, root_dentry, sd);
988 goto out_free_dentry_tree;
990 DEBUG("Inserting dentries into inode table");
991 ret = init_inode_table(&inode_tab, 9001);
993 goto out_destroy_imd;
995 for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
997 DEBUG("Cleaning up the hard link groups");
998 ret = fix_inodes(&inode_tab, &inode_list);
999 destroy_inode_table(&inode_tab);
1001 goto out_destroy_imd;
1003 DEBUG("Assigning hard link group IDs");
1004 assign_inode_numbers(&inode_list);
1006 ret = xml_add_image(w, name);
1008 goto out_destroy_imd;
1010 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1011 w->hdr.boot_idx = w->hdr.image_count;
1015 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1017 w->hdr.image_count--;
1019 out_free_dentry_tree:
1020 free_dentry_tree(root_dentry, w->lookup_table);
1021 out_free_security_data:
1022 free_security_data(sd);
1024 destroy_capture_config(&config);
1029 * Adds an image to a WIM file from a directory tree on disk.
1031 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
1032 const char *name, const char *config_str,
1033 size_t config_len, int flags)
1035 return do_add_image(w, dir, name, config_str, config_len, flags,
1036 build_dentry_tree, NULL);