4 * Support for modifying WIM files with image-level operations (delete an image,
5 * add an image, export an imagex 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: A dentry that has already been created for the root of the dentry
64 * @root_disk_path: The path to the root of the tree on disk.
65 * @lookup_table: The lookup table for the WIM file. For each file added to the
66 * dentry tree being built, an entry is added to the lookup table,
67 * unless an identical file is already in the lookup table. These
68 * lookup table entries that are added point to the file on disk.
70 * @return: 0 on success, nonzero on failure. It is a failure if any of
71 * the files cannot be `stat'ed, or if any of the needed
72 * directories cannot be opened or read. Failure to add the files
73 * to the WIM may still occur later when trying to actually read
74 * the regular files in the tree into the WIM as file resources.
76 static int build_dentry_tree(struct dentry **root_ret, const char *root_disk_path,
77 struct lookup_table *lookup_table,
78 struct wim_security_data *sd,
79 const struct capture_config *config,
83 struct stat root_stbuf;
85 int (*stat_fn)(const char *restrict, struct stat *restrict);
89 if (exclude_path(root_disk_path, config, true)) {
90 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
91 printf("Excluding file `%s' from capture\n",
98 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
103 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
104 printf("Scanning `%s'\n", root_disk_path);
107 ret = (*stat_fn)(root_disk_path, &root_stbuf);
109 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
110 return WIMLIB_ERR_STAT;
113 if ((add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
114 !S_ISDIR(root_stbuf.st_mode)) {
115 ERROR("`%s' is not a directory", root_disk_path);
116 return WIMLIB_ERR_NOTDIR;
118 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
119 && !S_ISLNK(root_stbuf.st_mode)) {
120 ERROR("`%s' is not a regular file, directory, or symbolic link.",
122 return WIMLIB_ERR_SPECIAL_FILE;
125 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
128 filename = path_basename(root_disk_path);
130 root = new_dentry_with_inode(filename);
132 return WIMLIB_ERR_NOMEM;
134 stbuf_to_inode(&root_stbuf, root->d_inode);
135 add_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
136 root->d_inode->resolved = true;
138 if (dentry_is_directory(root)) {
139 /* Open the directory on disk */
142 struct dentry *child;
144 dir = opendir(root_disk_path);
146 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
148 return WIMLIB_ERR_OPEN;
151 /* Buffer for names of files in directory. */
152 size_t len = strlen(root_disk_path);
153 char name[len + 1 + FILENAME_MAX + 1];
154 memcpy(name, root_disk_path, len);
157 /* Create a dentry for each entry in the directory on disk, and recurse
158 * to any subdirectories. */
159 while ((p = readdir(dir)) != NULL) {
160 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
161 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
163 strcpy(name + len + 1, p->d_name);
164 ret = build_dentry_tree(&child, name, lookup_table,
166 add_flags, extra_arg);
170 link_dentry(child, root);
173 } else if (dentry_is_symlink(root)) {
174 /* Archiving a symbolic link */
175 char deref_name_buf[4096];
176 ssize_t deref_name_len;
178 deref_name_len = readlink(root_disk_path, deref_name_buf,
179 sizeof(deref_name_buf) - 1);
180 if (deref_name_len == -1) {
181 ERROR_WITH_ERRNO("Failed to read target of "
182 "symbolic link `%s'", root_disk_path);
183 return WIMLIB_ERR_READLINK;
185 deref_name_buf[deref_name_len] = '\0';
186 DEBUG("Read symlink `%s'", deref_name_buf);
187 ret = inode_set_symlink(root->d_inode, deref_name_buf,
191 struct lookup_table_entry *lte;
192 u8 hash[SHA1_HASH_SIZE];
194 /* Empty files do not have to have a lookup table entry. */
195 if (root_stbuf.st_size == 0)
198 /* For each regular file, we must check to see if the file is in
199 * the lookup table already; if it is, we increment its refcnt;
200 * otherwise, we create a new lookup table entry and insert it.
202 ret = sha1sum(root_disk_path, hash);
206 lte = __lookup_resource(lookup_table, hash);
209 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
212 char *file_on_disk = STRDUP(root_disk_path);
214 ERROR("Failed to allocate memory for file path");
215 return WIMLIB_ERR_NOMEM;
217 lte = new_lookup_table_entry();
220 return WIMLIB_ERR_NOMEM;
222 lte->file_on_disk = file_on_disk;
223 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
224 lte->resource_entry.original_size = root_stbuf.st_size;
225 lte->resource_entry.size = root_stbuf.st_size;
226 copy_hash(lte->hash, hash);
227 lookup_table_insert(lookup_table, lte);
229 root->d_inode->lte = lte;
239 struct list_head lte_list_head;
242 static int allocate_lte_if_needed(struct dentry *dentry, void *arg)
244 const WIMStruct *src_wim, *dest_wim;
245 struct list_head *lte_list_head;
248 src_wim = ((struct wim_pair*)arg)->src_wim;
249 dest_wim = ((struct wim_pair*)arg)->dest_wim;
250 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
251 inode = dentry->d_inode;
253 wimlib_assert(!inode->resolved);
255 for (unsigned i = 0; i <= inode->num_ads; i++) {
256 struct lookup_table_entry *src_lte, *dest_lte;
257 src_lte = inode_stream_lte_unresolved(inode, i,
258 src_wim->lookup_table);
260 if (src_lte && ++src_lte->out_refcnt == 1) {
261 dest_lte = inode_stream_lte_unresolved(inode, i,
262 dest_wim->lookup_table);
265 dest_lte = clone_lookup_table_entry(src_lte);
267 return WIMLIB_ERR_NOMEM;
268 list_add_tail(&dest_lte->list, lte_list_head);
276 * This function takes in a dentry that was previously located only in image(s)
277 * in @src_wim, but now is being added to @dest_wim. For each stream associated
278 * with the dentry, if there is already a lookup table entry for that stream in
279 * the lookup table of the destination WIM file, its reference count is
280 * incrementej. Otherwise, a new lookup table entry is created that points back
281 * to the stream in the source WIM file (through the @hash field combined with
282 * the @wim field of the lookup table entry.)
284 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
286 WIMStruct *src_wim, *dest_wim;
289 src_wim = ((struct wim_pair*)arg)->src_wim;
290 dest_wim = ((struct wim_pair*)arg)->dest_wim;
291 inode = dentry->d_inode;
293 wimlib_assert(!inode->resolved);
295 for (unsigned i = 0; i <= inode->num_ads; i++) {
296 struct lookup_table_entry *src_lte, *dest_lte;
297 src_lte = inode_stream_lte_unresolved(inode, i,
298 src_wim->lookup_table);
300 if (!src_lte) /* Empty or nonexistent stream. */
303 dest_lte = inode_stream_lte_unresolved(inode, i,
304 dest_wim->lookup_table);
308 struct list_head *lte_list_head;
309 struct list_head *next;
311 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
312 wimlib_assert(!list_empty(lte_list_head));
314 next = lte_list_head->next;
316 dest_lte = container_of(next, struct lookup_table_entry, list);
317 dest_lte->part_number = 1;
318 dest_lte->refcnt = 1;
319 wimlib_assert(hashes_equal(dest_lte->hash, src_lte->hash));
321 lookup_table_insert(dest_wim->lookup_table, dest_lte);
328 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
329 * file, adds an entry to the lookup table for the image metadata, updates the
330 * image count in the header, and selects the new image.
332 * Does not update the XML data.
334 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
335 * 0 is returned and the image metadata array of @w is modified.
337 * @w: The WIMStruct for the WIM file.
338 * @root_dentry: The root of the directory tree for the image.
339 * @sd: The security data for the image.
341 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
342 struct wim_security_data *sd)
344 struct lookup_table_entry *metadata_lte;
345 struct image_metadata *imd;
346 struct image_metadata *new_imd;
349 wimlib_assert(root_dentry != NULL);
351 DEBUG("Reallocating image metadata array for image_count = %u",
352 w->hdr.image_count + 1);
353 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
356 ERROR("Failed to allocate memory for new image metadata array");
357 return WIMLIB_ERR_NOMEM;
360 memcpy(imd, w->image_metadata,
361 w->hdr.image_count * sizeof(struct image_metadata));
363 metadata_lte = new_lookup_table_entry();
367 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
368 random_hash(metadata_lte->hash);
369 lookup_table_insert(w->lookup_table, metadata_lte);
371 new_imd = &imd[w->hdr.image_count];
373 new_imd->root_dentry = root_dentry;
374 new_imd->metadata_lte = metadata_lte;
375 new_imd->security_data = sd;
376 new_imd->modified = true;
378 FREE(w->image_metadata);
379 w->image_metadata = imd;
380 w->hdr.image_count++;
382 /* Change the current image to the new one. There should not be any
383 * ways for this to fail, since the image is valid and the dentry tree
384 * is already in memory. */
385 ret = wimlib_select_image(w, w->hdr.image_count);
386 wimlib_assert(ret == 0);
388 out_free_metadata_lte:
392 return WIMLIB_ERR_NOMEM;
397 * Copies an image, or all the images, from a WIM file, into another WIM file.
399 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
402 const char *dest_name,
403 const char *dest_description,
405 WIMStruct **additional_swms,
406 unsigned num_additional_swms)
411 struct wim_pair wims;
412 struct wim_security_data *sd;
413 struct lookup_table *joined_tab, *src_wim_tab_save;
415 if (!src_wim || !dest_wim)
416 return WIMLIB_ERR_INVALID_PARAM;
418 if (dest_wim->hdr.total_parts != 1) {
419 ERROR("Exporting an image to a split WIM is "
421 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
424 if (src_image == WIM_ALL_IMAGES) {
425 if (src_wim->hdr.image_count > 1) {
427 /* multi-image export. */
429 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
430 (src_wim->hdr.boot_idx == 0))
432 /* Specifying the boot flag on a multi-image
433 * source WIM makes the boot index default to
434 * the bootable image in the source WIM. It is
435 * an error if there is no such bootable image.
437 ERROR("Cannot specify `boot' flag when "
438 "exporting multiple images from a WIM "
439 "with no bootable images");
440 return WIMLIB_ERR_INVALID_PARAM;
442 if (dest_name || dest_description) {
443 ERROR("Image name or image description was "
444 "specified, but we are exporting "
446 return WIMLIB_ERR_INVALID_PARAM;
448 for (i = 1; i <= src_wim->hdr.image_count; i++) {
449 int export_flags = flags;
451 if (i != src_wim->hdr.boot_idx)
452 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
454 ret = wimlib_export_image(src_wim, i, dest_wim,
458 num_additional_swms);
469 dest_name = wimlib_get_image_name(src_wim, src_image);
470 DEBUG("Using name `%s' for source image %d",
471 dest_name, src_image);
474 if (!dest_description) {
475 dest_description = wimlib_get_image_description(src_wim,
477 DEBUG("Using description `%s' for source image %d",
478 dest_description, src_image);
481 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
483 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
484 ERROR("There is already an image named `%s' in the "
485 "destination WIM", dest_name);
486 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
489 ret = verify_swm_set(src_wim, additional_swms, num_additional_swms);
493 if (num_additional_swms) {
494 ret = new_joined_lookup_table(src_wim, additional_swms,
499 src_wim_tab_save = src_wim->lookup_table;
500 src_wim->lookup_table = joined_tab;
503 ret = wimlib_select_image(src_wim, src_image);
505 ERROR("Could not select image %d from the WIM `%s' "
506 "to export it", src_image, src_wim->filename);
510 /* Pre-allocate the new lookup table entries that will be needed. This
511 * way, it's not possible to run out of memory part-way through
512 * modifying the lookup table of the destination WIM. */
513 wims.src_wim = src_wim;
514 wims.dest_wim = dest_wim;
515 INIT_LIST_HEAD(&wims.lte_list_head);
516 for_lookup_table_entry(src_wim->lookup_table, lte_zero_out_refcnt, NULL);
517 root = wim_root_dentry(src_wim);
518 for_dentry_in_tree(root, dentry_unresolve_ltes, NULL);
519 ret = for_dentry_in_tree(root, allocate_lte_if_needed, &wims);
523 ret = xml_export_image(src_wim->wim_info, src_image,
524 &dest_wim->wim_info, dest_name, dest_description);
528 sd = wim_security_data(src_wim);
529 ret = add_new_dentry_tree(dest_wim, root, sd);
531 goto out_xml_delete_image;
534 /* All memory allocations have been taken care of, so it's no longer
535 * possible for this function to fail. Go ahead and increment the
536 * reference counts of the dentry tree and security data, then update
537 * the lookup table of the destination WIM and the boot index, if
539 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
541 for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
542 wimlib_assert(list_empty(&wims.lte_list_head));
544 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
545 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
546 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
551 out_xml_delete_image:
552 xml_delete_image(&dest_wim->wim_info, dest_wim->hdr.image_count);
555 struct lookup_table_entry *lte, *tmp;
556 list_for_each_entry_safe(lte, tmp, &wims.lte_list_head, list)
557 free_lookup_table_entry(lte);
561 if (num_additional_swms) {
562 free_lookup_table(src_wim->lookup_table);
563 src_wim->lookup_table = src_wim_tab_save;
569 * Deletes an image from the WIM.
571 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
577 if (w->hdr.total_parts != 1) {
578 ERROR("Deleting an image from a split WIM is not supported.");
579 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
582 if (image == WIM_ALL_IMAGES) {
583 num_images = w->hdr.image_count;
584 for (i = 1; i <= num_images; i++) {
585 /* Always delete the first image, since by the end
586 * there won't be any more than that! */
587 ret = wimlib_delete_image(w, 1);
594 DEBUG("Deleting image %d", image);
596 /* Even if the dentry tree is not allocated, we must select it (and
597 * therefore allocate it) so that we can decrement the reference counts
598 * in the lookup table. */
599 ret = wimlib_select_image(w, image);
603 /* Free the dentry tree, any lookup table entries that have their
604 * refcnt decremented to 0, and the security data. */
605 destroy_image_metadata(wim_get_current_image_metadata(w),
608 /* Get rid of the empty slot in the image metadata array. */
609 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
610 (w->hdr.image_count - image) * sizeof(struct image_metadata));
612 /* Decrement the image count. */
613 if (--w->hdr.image_count == 0) {
614 FREE(w->image_metadata);
615 w->image_metadata = NULL;
618 /* Fix the boot index. */
619 if (w->hdr.boot_idx == image)
621 else if (w->hdr.boot_idx > image)
624 w->current_image = WIM_NO_IMAGE;
626 /* Remove the image from the XML information. */
627 xml_delete_image(&w->wim_info, image);
635 COMPRESSION_EXCLUSION_LIST,
639 /* Default capture configuration file when none is specified. */
640 static const char *default_config =
645 "\\System Volume Information\n"
649 "[CompressionExclusionList]\n"
653 "\\WINDOWS\\inf\\*.pnf\n";
655 static void destroy_pattern_list(struct pattern_list *list)
660 static void destroy_capture_config(struct capture_config *config)
662 destroy_pattern_list(&config->exclusion_list);
663 destroy_pattern_list(&config->exclusion_exception);
664 destroy_pattern_list(&config->compression_exclusion_list);
665 destroy_pattern_list(&config->alignment_list);
666 FREE(config->config_str);
667 FREE(config->prefix);
668 memset(config, 0, sizeof(*config));
671 static int pattern_list_add_pattern(struct pattern_list *list,
675 if (list->num_pats >= list->num_allocated_pats) {
676 pats = REALLOC(list->pats,
677 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
679 return WIMLIB_ERR_NOMEM;
680 list->num_allocated_pats += 8;
683 list->pats[list->num_pats++] = pattern;
687 /* Parses the contents of the image capture configuration file and fills in a
688 * `struct capture_config'. */
689 static int init_capture_config(const char *_config_str, size_t config_len,
690 const char *_prefix, struct capture_config *config)
697 size_t bytes_remaining;
698 enum pattern_type type = NONE;
700 unsigned long line_no = 0;
702 DEBUG("config_len = %zu", config_len);
703 bytes_remaining = config_len;
704 memset(config, 0, sizeof(*config));
705 config_str = MALLOC(config_len);
707 ERROR("Could not duplicate capture config string");
708 return WIMLIB_ERR_NOMEM;
710 prefix = STRDUP(_prefix);
713 return WIMLIB_ERR_NOMEM;
716 memcpy(config_str, _config_str, config_len);
718 config->config_str = config_str;
719 config->prefix = prefix;
720 config->prefix_len = strlen(prefix);
721 while (bytes_remaining) {
724 eol = memchr(p, '\n', bytes_remaining);
726 ERROR("Expected end-of-line in capture config file on "
727 "line %lu", line_no);
728 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
733 bytes_remaining -= (eol - p) + 1;
737 if (*(eol - 1) == '\r')
741 /* Translate backslash to forward slash */
742 for (char *pp = p; pp != eol; pp++)
746 /* Remove drive letter */
747 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
751 if (strcmp(p, "[ExclusionList]") == 0)
752 type = EXCLUSION_LIST;
753 else if (strcmp(p, "[ExclusionException]") == 0)
754 type = EXCLUSION_EXCEPTION;
755 else if (strcmp(p, "[CompressionExclusionList]") == 0)
756 type = COMPRESSION_EXCLUSION_LIST;
757 else if (strcmp(p, "[AlignmentList]") == 0)
758 type = ALIGNMENT_LIST;
759 else if (p[0] == '[' && strrchr(p, ']')) {
760 ERROR("Unknown capture configuration section `%s'", p);
761 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
763 } else switch (type) {
765 DEBUG("Adding pattern \"%s\" to exclusion list", p);
766 ret = pattern_list_add_pattern(&config->exclusion_list, p);
768 case EXCLUSION_EXCEPTION:
769 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
770 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
772 case COMPRESSION_EXCLUSION_LIST:
773 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
774 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
777 DEBUG("Adding pattern \"%s\" to alignment list", p);
778 ret = pattern_list_add_pattern(&config->alignment_list, p);
781 ERROR("Line %lu of capture configuration is not "
782 "in a block (such as [ExclusionList])",
784 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
792 destroy_capture_config(config);
796 static bool match_pattern(const char *path, const char *path_basename,
797 const struct pattern_list *list)
799 for (size_t i = 0; i < list->num_pats; i++) {
800 const char *pat = list->pats[i];
803 /* Absolute path from root of capture */
806 if (strchr(pat, '/'))
807 /* Relative path from root of capture */
810 /* A file name pattern */
811 string = path_basename;
813 if (fnmatch(pat, string, FNM_PATHNAME
819 DEBUG("`%s' matches the pattern \"%s\"",
827 static void print_pattern_list(const struct pattern_list *list)
829 for (size_t i = 0; i < list->num_pats; i++)
830 printf(" %s\n", list->pats[i]);
833 static void print_capture_config(const struct capture_config *config)
835 if (config->exclusion_list.num_pats) {
836 puts("Files or folders excluded from image capture:");
837 print_pattern_list(&config->exclusion_list);
842 /* Return true if the image capture configuration file indicates we should
843 * exclude the filename @path from capture.
845 * If @exclude_prefix is %true, the part of the path up and including the name
846 * of the directory being captured is not included in the path for matching
847 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
848 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
851 bool exclude_path(const char *path, const struct capture_config *config,
854 const char *basename = path_basename(path);
855 if (exclude_prefix) {
856 wimlib_assert(strlen(path) >= config->prefix_len);
857 if (memcmp(config->prefix, path, config->prefix_len) == 0
858 && path[config->prefix_len] == '/')
859 path += config->prefix_len;
861 return match_pattern(path, basename, &config->exclusion_list) &&
862 !match_pattern(path, basename, &config->exclusion_exception);
869 * Adds an image to the WIM, delegating the capture of the dentry tree and
870 * security data to the function @capture_tree passed as a parameter.
871 * Currently, @capture_tree may be build_dentry_tree() for capturing a "regular"
872 * directory tree on disk, or build_dentry_tree_ntfs() for capturing a WIM image
873 * directory from a NTFS volume using libntfs-3g.
875 * The @capture_tree function is also expected to create lookup table entries
876 * for all the file streams it captures and insert them into @lookup_table,
877 * being careful to look for identical entries that already exist and simply
878 * increment the reference count for them rather than duplicating the entry.
880 int do_add_image(WIMStruct *w, const char *dir, const char *name,
881 const char *config_str, size_t config_len,
883 int (*capture_tree)(struct dentry **, const char *,
884 struct lookup_table *,
885 struct wim_security_data *,
886 const struct capture_config *,
890 struct dentry *root_dentry = NULL;
891 struct wim_security_data *sd;
892 struct capture_config config;
893 struct inode_table inode_tab;
894 struct hlist_head inode_list;
897 DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", dir);
899 if (!name || !*name) {
900 ERROR("Must specify a non-empty string for the image name");
901 return WIMLIB_ERR_INVALID_PARAM;
904 ERROR("Must specify the name of a directory or NTFS volume");
905 return WIMLIB_ERR_INVALID_PARAM;
908 if (w->hdr.total_parts != 1) {
909 ERROR("Cannot add an image to a split WIM");
910 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
913 if (wimlib_image_name_in_use(w, name)) {
914 ERROR("There is already an image named \"%s\" in `%s'",
916 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
919 DEBUG("Initializing capture configuration");
921 DEBUG("Using default capture configuration");
922 config_str = default_config;
923 config_len = strlen(default_config);
925 ret = init_capture_config(config_str, config_len, dir, &config);
928 print_capture_config(&config);
930 DEBUG("Allocating security data");
932 sd = CALLOC(1, sizeof(struct wim_security_data));
934 goto out_destroy_config;
935 sd->total_length = 8;
938 DEBUG("Building dentry tree.");
939 ret = (*capture_tree)(&root_dentry, dir, w->lookup_table, sd,
940 &config, flags | WIMLIB_ADD_IMAGE_FLAG_ROOT,
942 destroy_capture_config(&config);
945 ERROR("Failed to build dentry tree for `%s'", dir);
946 goto out_free_dentry_tree;
949 DEBUG("Calculating full paths of dentries.");
950 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
952 goto out_free_dentry_tree;
954 ret = add_new_dentry_tree(w, root_dentry, sd);
956 goto out_free_dentry_tree;
958 DEBUG("Inserting dentries into inode table");
959 ret = init_inode_table(&inode_tab, 9001);
961 goto out_destroy_imd;
963 for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
965 DEBUG("Cleaning up the hard link groups");
966 ret = fix_inodes(&inode_tab, &inode_list);
967 destroy_inode_table(&inode_tab);
969 goto out_destroy_imd;
971 DEBUG("Assigning hard link group IDs");
972 assign_inode_numbers(&inode_list);
974 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
975 wimlib_set_boot_idx(w, w->hdr.image_count);
977 ret = xml_add_image(w, name);
979 goto out_destroy_imd;
983 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
985 w->hdr.image_count--;
987 out_free_dentry_tree:
988 free_dentry_tree(root_dentry, w->lookup_table);
989 free_security_data(sd);
991 destroy_capture_config(&config);
996 * Adds an image to a WIM file from a directory tree on disk.
998 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
999 const char *name, const char *config_str,
1000 size_t config_len, int flags)
1002 return do_add_image(w, dir, name, config_str, config_len, flags,
1003 build_dentry_tree, NULL);