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,
100 wimlib_progress_func_t progress_func,
103 struct stat root_stbuf;
105 int (*stat_fn)(const char *restrict, struct stat *restrict);
107 const char *filename;
110 if (exclude_path(root_disk_path, config, true)) {
111 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
112 ERROR("Cannot exclude the root directory from capture");
113 return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
115 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
118 union wimlib_progress_info info;
119 info.scan.cur_path = root_disk_path;
120 info.scan.excluded = true;
121 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
127 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
130 union wimlib_progress_info info;
131 info.scan.cur_path = root_disk_path;
132 info.scan.excluded = false;
133 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
136 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
141 ret = (*stat_fn)(root_disk_path, &root_stbuf);
143 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
144 return WIMLIB_ERR_STAT;
147 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
148 !S_ISDIR(root_stbuf.st_mode)) {
149 ERROR("`%s' is not a directory", root_disk_path);
150 return WIMLIB_ERR_NOTDIR;
152 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
153 && !S_ISLNK(root_stbuf.st_mode)) {
154 ERROR("`%s' is not a regular file, directory, or symbolic link.",
156 return WIMLIB_ERR_SPECIAL_FILE;
159 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
162 filename = path_basename(root_disk_path);
164 root = new_dentry_with_timeless_inode(filename);
166 return WIMLIB_ERR_NOMEM;
168 inode = root->d_inode;
170 #ifdef HAVE_STAT_NANOSECOND_PRECISION
171 inode->creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
172 inode->last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
173 inode->last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
175 inode->creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
176 inode->last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
177 inode->last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
179 if (sizeof(ino_t) >= 8)
180 inode->ino = (u64)root_stbuf.st_ino;
182 inode->ino = (u64)root_stbuf.st_ino |
183 ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
185 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
186 inode->resolved = true;
188 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
190 struct lookup_table_entry *lte;
191 u8 hash[SHA1_HASH_SIZE];
193 inode->attributes = FILE_ATTRIBUTE_NORMAL;
195 /* Empty files do not have to have a lookup table entry. */
196 if (root_stbuf.st_size == 0)
199 /* For each regular file, we must check to see if the file is in
200 * the lookup table already; if it is, we increment its refcnt;
201 * otherwise, we create a new lookup table entry and insert it.
204 ret = sha1sum(root_disk_path, hash);
208 lte = __lookup_resource(lookup_table, hash);
211 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
214 char *file_on_disk = STRDUP(root_disk_path);
216 ERROR("Failed to allocate memory for file path");
217 ret = WIMLIB_ERR_NOMEM;
220 lte = new_lookup_table_entry();
223 ret = WIMLIB_ERR_NOMEM;
226 lte->file_on_disk = file_on_disk;
227 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
228 lte->resource_entry.original_size = root_stbuf.st_size;
229 lte->resource_entry.size = root_stbuf.st_size;
230 copy_hash(lte->hash, hash);
231 lookup_table_insert(lookup_table, lte);
233 root->d_inode->lte = lte;
234 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
236 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
239 struct dirent entry, *result;
240 struct dentry *child;
242 dir = opendir(root_disk_path);
244 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
246 ret = WIMLIB_ERR_OPEN;
250 /* Buffer for names of files in directory. */
251 size_t len = strlen(root_disk_path);
252 char name[len + 1 + FILENAME_MAX + 1];
253 memcpy(name, root_disk_path, len);
256 /* Create a dentry for each entry in the directory on disk, and recurse
257 * to any subdirectories. */
260 ret = readdir_r(dir, &entry, &result);
262 ret = WIMLIB_ERR_READ;
263 ERROR_WITH_ERRNO("Error reading the "
270 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
271 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
273 strcpy(name + len + 1, result->d_name);
274 ret = build_dentry_tree(&child, name, lookup_table,
275 NULL, config, add_image_flags,
276 progress_func, NULL);
280 dentry_add_child(root, child);
283 } else { /* Archiving a symbolic link */
284 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
285 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
287 /* The idea here is to call readlink() to get the UNIX target of
288 * the symbolic link, then turn the target into a reparse point
289 * data buffer that contains a relative or absolute symbolic
290 * link (NOT a junction point or *full* path symbolic link with
294 char deref_name_buf[4096];
295 ssize_t deref_name_len;
297 deref_name_len = readlink(root_disk_path, deref_name_buf,
298 sizeof(deref_name_buf) - 1);
299 if (deref_name_len >= 0) {
300 deref_name_buf[deref_name_len] = '\0';
301 DEBUG("Read symlink `%s'", deref_name_buf);
302 ret = inode_set_symlink(root->d_inode, deref_name_buf,
306 * Unfortunately, Windows seems to have the
307 * concept of "file" symbolic links as being
308 * different from "directory" symbolic links...
309 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
310 * on the symbolic link if the *target* of the
311 * symbolic link is a directory.
314 if (stat(root_disk_path, &stbuf) == 0 &&
315 S_ISDIR(stbuf.st_mode))
317 inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
321 ERROR_WITH_ERRNO("Failed to read target of "
322 "symbolic link `%s'", root_disk_path);
323 ret = WIMLIB_ERR_READLINK;
330 free_dentry_tree(root, lookup_table);
337 struct list_head lte_list_head;
340 static int allocate_lte_if_needed(struct dentry *dentry, void *arg)
342 const WIMStruct *src_wim, *dest_wim;
343 struct list_head *lte_list_head;
346 src_wim = ((struct wim_pair*)arg)->src_wim;
347 dest_wim = ((struct wim_pair*)arg)->dest_wim;
348 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
349 inode = dentry->d_inode;
351 wimlib_assert(!inode->resolved);
353 for (unsigned i = 0; i <= inode->num_ads; i++) {
354 struct lookup_table_entry *src_lte, *dest_lte;
355 src_lte = inode_stream_lte_unresolved(inode, i,
356 src_wim->lookup_table);
358 if (src_lte && ++src_lte->out_refcnt == 1) {
359 dest_lte = inode_stream_lte_unresolved(inode, i,
360 dest_wim->lookup_table);
363 dest_lte = clone_lookup_table_entry(src_lte);
365 return WIMLIB_ERR_NOMEM;
366 list_add_tail(&dest_lte->staging_list, lte_list_head);
374 * This function takes in a dentry that was previously located only in image(s)
375 * in @src_wim, but now is being added to @dest_wim. For each stream associated
376 * with the dentry, if there is already a lookup table entry for that stream in
377 * the lookup table of the destination WIM file, its reference count is
378 * incrementej. Otherwise, a new lookup table entry is created that points back
379 * to the stream in the source WIM file (through the @hash field combined with
380 * the @wim field of the lookup table entry.)
382 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
384 WIMStruct *src_wim, *dest_wim;
387 src_wim = ((struct wim_pair*)arg)->src_wim;
388 dest_wim = ((struct wim_pair*)arg)->dest_wim;
389 inode = dentry->d_inode;
391 wimlib_assert(!inode->resolved);
393 for (unsigned i = 0; i <= inode->num_ads; i++) {
394 struct lookup_table_entry *src_lte, *dest_lte;
395 src_lte = inode_stream_lte_unresolved(inode, i,
396 src_wim->lookup_table);
398 if (!src_lte) /* Empty or nonexistent stream. */
401 dest_lte = inode_stream_lte_unresolved(inode, i,
402 dest_wim->lookup_table);
406 struct list_head *lte_list_head;
407 struct list_head *next;
409 lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
410 wimlib_assert(!list_empty(lte_list_head));
412 next = lte_list_head->next;
414 dest_lte = container_of(next, struct lookup_table_entry,
416 dest_lte->part_number = 1;
417 dest_lte->refcnt = 1;
418 wimlib_assert(hashes_equal(dest_lte->hash, src_lte->hash));
420 lookup_table_insert(dest_wim->lookup_table, dest_lte);
427 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
428 * file, adds an entry to the lookup table for the image metadata, updates the
429 * image count in the header, and selects the new image.
431 * Does not update the XML data.
433 * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made. Otherwise,
434 * 0 is returned and the image metadata array of @w is modified.
436 * @w: The WIMStruct for the WIM file.
437 * @root_dentry: The root of the directory tree for the image.
438 * @sd: The security data for the image.
440 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
441 struct wim_security_data *sd)
443 struct lookup_table_entry *metadata_lte;
444 struct image_metadata *imd;
445 struct image_metadata *new_imd;
448 wimlib_assert(root_dentry != NULL);
450 DEBUG("Reallocating image metadata array for image_count = %u",
451 w->hdr.image_count + 1);
452 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
455 ERROR("Failed to allocate memory for new image metadata array");
459 memcpy(imd, w->image_metadata,
460 w->hdr.image_count * sizeof(struct image_metadata));
462 metadata_lte = new_lookup_table_entry();
466 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
467 random_hash(metadata_lte->hash);
468 lookup_table_insert(w->lookup_table, metadata_lte);
470 new_imd = &imd[w->hdr.image_count];
472 new_imd->root_dentry = root_dentry;
473 new_imd->metadata_lte = metadata_lte;
474 new_imd->security_data = sd;
475 new_imd->modified = true;
477 FREE(w->image_metadata);
478 w->image_metadata = imd;
479 w->hdr.image_count++;
481 /* Change the current image to the new one. There should not be any
482 * ways for this to fail, since the image is valid and the dentry tree
483 * is already in memory. */
484 ret = select_wim_image(w, w->hdr.image_count);
485 wimlib_assert(ret == 0);
490 return WIMLIB_ERR_NOMEM;
495 * Copies an image, or all the images, from a WIM file, into another WIM file.
497 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
500 const char *dest_name,
501 const char *dest_description,
503 WIMStruct **additional_swms,
504 unsigned num_additional_swms,
505 wimlib_progress_func_t progress_func)
510 struct wim_pair wims;
511 struct wim_security_data *sd;
512 struct lookup_table *joined_tab, *src_wim_tab_save;
514 if (dest_wim->hdr.total_parts != 1) {
515 ERROR("Exporting an image to a split WIM is "
517 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
520 if (src_image == WIMLIB_ALL_IMAGES) {
521 if (src_wim->hdr.image_count > 1) {
523 /* multi-image export. */
525 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
526 (src_wim->hdr.boot_idx == 0))
528 /* Specifying the boot flag on a multi-image
529 * source WIM makes the boot index default to
530 * the bootable image in the source WIM. It is
531 * an error if there is no such bootable image.
533 ERROR("Cannot specify `boot' flag when "
534 "exporting multiple images from a WIM "
535 "with no bootable images");
536 return WIMLIB_ERR_INVALID_PARAM;
538 if (dest_name || dest_description) {
539 ERROR("Image name or image description was "
540 "specified, but we are exporting "
542 return WIMLIB_ERR_INVALID_PARAM;
544 for (i = 1; i <= src_wim->hdr.image_count; i++) {
545 int new_flags = export_flags;
547 if (i != src_wim->hdr.boot_idx)
548 new_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
550 ret = wimlib_export_image(src_wim, i, dest_wim,
560 } else if (src_wim->hdr.image_count == 1) {
568 dest_name = wimlib_get_image_name(src_wim, src_image);
569 DEBUG("Using name `%s' for source image %d",
570 dest_name, src_image);
573 if (!dest_description) {
574 dest_description = wimlib_get_image_description(src_wim,
576 DEBUG("Using description `%s' for source image %d",
577 dest_description, src_image);
580 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
582 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
583 ERROR("There is already an image named `%s' in the "
584 "destination WIM", dest_name);
585 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
588 ret = verify_swm_set(src_wim, additional_swms, num_additional_swms);
592 if (num_additional_swms) {
593 ret = new_joined_lookup_table(src_wim, additional_swms,
598 src_wim_tab_save = src_wim->lookup_table;
599 src_wim->lookup_table = joined_tab;
602 ret = select_wim_image(src_wim, src_image);
604 ERROR("Could not select image %d from the WIM `%s' "
605 "to export it", src_image, src_wim->filename);
609 /* Pre-allocate the new lookup table entries that will be needed. This
610 * way, it's not possible to run out of memory part-way through
611 * modifying the lookup table of the destination WIM. */
612 wims.src_wim = src_wim;
613 wims.dest_wim = dest_wim;
614 INIT_LIST_HEAD(&wims.lte_list_head);
615 for_lookup_table_entry(src_wim->lookup_table, lte_zero_out_refcnt, NULL);
616 root = wim_root_dentry(src_wim);
617 for_dentry_in_tree(root, dentry_unresolve_ltes, NULL);
618 ret = for_dentry_in_tree(root, allocate_lte_if_needed, &wims);
622 ret = xml_export_image(src_wim->wim_info, src_image,
623 &dest_wim->wim_info, dest_name, dest_description);
627 sd = wim_security_data(src_wim);
628 ret = add_new_dentry_tree(dest_wim, root, sd);
630 goto out_xml_delete_image;
633 /* All memory allocations have been taken care of, so it's no longer
634 * possible for this function to fail. Go ahead and increment the
635 * reference counts of the dentry tree and security data, then update
636 * the lookup table of the destination WIM and the boot index, if
638 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
640 for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
641 wimlib_assert(list_empty(&wims.lte_list_head));
643 if (export_flags & WIMLIB_EXPORT_FLAG_BOOT) {
644 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
645 wimlib_set_boot_idx(dest_wim, dest_wim->hdr.image_count);
650 out_xml_delete_image:
651 xml_delete_image(&dest_wim->wim_info, dest_wim->hdr.image_count);
654 struct lookup_table_entry *lte, *tmp;
655 list_for_each_entry_safe(lte, tmp, &wims.lte_list_head, staging_list)
656 free_lookup_table_entry(lte);
660 if (num_additional_swms) {
661 free_lookup_table(src_wim->lookup_table);
662 src_wim->lookup_table = src_wim_tab_save;
667 static int image_run_full_verifications(WIMStruct *w)
669 return for_dentry_in_tree(wim_root_dentry(w), verify_dentry, w);
672 static int lte_fix_refcnt(struct lookup_table_entry *lte, void *ctr)
674 if (lte->refcnt != lte->real_refcnt) {
675 WARNING("The following lookup table entry has a reference "
676 "count of %u, but", lte->refcnt);
677 WARNING("We found %u references to it",
679 print_lookup_table_entry(lte);
680 lte->refcnt = lte->real_refcnt;
681 ++*(unsigned long *)ctr;
686 /* Ideally this would be unnecessary... however, the WIMs for Windows 8 are
687 * screwed up because some lookup table entries are referenced more times than
688 * their stated reference counts. So theoretically, if we do the delete all the
689 * references to a stream and then remove it, it might still be referenced
690 * somewhere else... So, work around this problem by looking at ALL the images
691 * to re-calculate the reference count of EVERY lookup table entry. */
692 int wim_run_full_verifications(WIMStruct *w)
696 for_lookup_table_entry(w->lookup_table, lte_zero_real_refcnt, NULL);
697 w->all_images_verified = true;
698 w->full_verification_in_progress = true;
699 ret = for_image(w, WIMLIB_ALL_IMAGES, image_run_full_verifications);
700 w->full_verification_in_progress = false;
702 unsigned long num_ltes_with_bogus_refcnt = 0;
703 for (int i = 0; i < w->hdr.image_count; i++)
704 w->image_metadata[i].metadata_lte->real_refcnt++;
705 for_lookup_table_entry(w->lookup_table, lte_fix_refcnt,
706 &num_ltes_with_bogus_refcnt);
707 if (num_ltes_with_bogus_refcnt != 0) {
708 WARNING("A total of %lu entries in the WIM's stream "
709 "lookup table had to have\n"
710 " their reference counts fixed.",
711 num_ltes_with_bogus_refcnt);
714 w->all_images_verified = false;
720 * Deletes an image from the WIM.
722 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
727 if (w->hdr.total_parts != 1) {
728 ERROR("Deleting an image from a split WIM is not supported.");
729 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
732 if (image == WIMLIB_ALL_IMAGES) {
733 for (i = w->hdr.image_count; i >= 1; i--) {
734 ret = wimlib_delete_image(w, i);
741 if (!w->all_images_verified) {
742 ret = wim_run_full_verifications(w);
747 DEBUG("Deleting image %d", image);
749 /* Even if the dentry tree is not allocated, we must select it (and
750 * therefore allocate it) so that we can decrement the reference counts
751 * in the lookup table. */
752 ret = select_wim_image(w, image);
756 /* Free the dentry tree, any lookup table entries that have their
757 * refcnt decremented to 0, and the security data. */
758 destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table);
760 /* Get rid of the empty slot in the image metadata array. */
761 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
762 (w->hdr.image_count - image) * sizeof(struct image_metadata));
764 /* Decrement the image count. */
765 if (--w->hdr.image_count == 0) {
766 FREE(w->image_metadata);
767 w->image_metadata = NULL;
770 /* Fix the boot index. */
771 if (w->hdr.boot_idx == image)
773 else if (w->hdr.boot_idx > image)
776 w->current_image = WIMLIB_NO_IMAGE;
778 /* Remove the image from the XML information. */
779 xml_delete_image(&w->wim_info, image);
781 w->deletion_occurred = true;
789 COMPRESSION_EXCLUSION_LIST,
793 /* Default capture configuration file when none is specified. */
794 static const char *default_config =
799 "\\System Volume Information\n"
803 "[CompressionExclusionList]\n"
807 "\\WINDOWS\\inf\\*.pnf\n";
809 static void destroy_pattern_list(struct pattern_list *list)
814 static void destroy_capture_config(struct capture_config *config)
816 destroy_pattern_list(&config->exclusion_list);
817 destroy_pattern_list(&config->exclusion_exception);
818 destroy_pattern_list(&config->compression_exclusion_list);
819 destroy_pattern_list(&config->alignment_list);
820 FREE(config->config_str);
821 FREE(config->prefix);
822 memset(config, 0, sizeof(*config));
825 static int pattern_list_add_pattern(struct pattern_list *list,
829 if (list->num_pats >= list->num_allocated_pats) {
830 pats = REALLOC(list->pats,
831 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
833 return WIMLIB_ERR_NOMEM;
834 list->num_allocated_pats += 8;
837 list->pats[list->num_pats++] = pattern;
841 /* Parses the contents of the image capture configuration file and fills in a
842 * `struct capture_config'. */
843 static int init_capture_config(const char *_config_str, size_t config_len,
844 const char *_prefix, struct capture_config *config)
851 size_t bytes_remaining;
852 enum pattern_type type = NONE;
854 unsigned long line_no = 0;
856 DEBUG("config_len = %zu", config_len);
857 bytes_remaining = config_len;
858 memset(config, 0, sizeof(*config));
859 config_str = MALLOC(config_len);
861 ERROR("Could not duplicate capture config string");
862 return WIMLIB_ERR_NOMEM;
864 prefix = STRDUP(_prefix);
867 return WIMLIB_ERR_NOMEM;
870 memcpy(config_str, _config_str, config_len);
872 config->config_str = config_str;
873 config->prefix = prefix;
874 config->prefix_len = strlen(prefix);
875 while (bytes_remaining) {
878 eol = memchr(p, '\n', bytes_remaining);
880 ERROR("Expected end-of-line in capture config file on "
881 "line %lu", line_no);
882 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
887 bytes_remaining -= (next_p - p);
891 if (*(eol - 1) == '\r')
895 /* Translate backslash to forward slash */
896 for (char *pp = p; pp != eol; pp++)
900 /* Remove drive letter */
901 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
905 if (strcmp(p, "[ExclusionList]") == 0)
906 type = EXCLUSION_LIST;
907 else if (strcmp(p, "[ExclusionException]") == 0)
908 type = EXCLUSION_EXCEPTION;
909 else if (strcmp(p, "[CompressionExclusionList]") == 0)
910 type = COMPRESSION_EXCLUSION_LIST;
911 else if (strcmp(p, "[AlignmentList]") == 0)
912 type = ALIGNMENT_LIST;
913 else if (p[0] == '[' && strrchr(p, ']')) {
914 ERROR("Unknown capture configuration section `%s'", p);
915 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
916 } else switch (type) {
918 DEBUG("Adding pattern \"%s\" to exclusion list", p);
919 ret = pattern_list_add_pattern(&config->exclusion_list, p);
921 case EXCLUSION_EXCEPTION:
922 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
923 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
925 case COMPRESSION_EXCLUSION_LIST:
926 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
927 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
930 DEBUG("Adding pattern \"%s\" to alignment list", p);
931 ret = pattern_list_add_pattern(&config->alignment_list, p);
934 ERROR("Line %lu of capture configuration is not "
935 "in a block (such as [ExclusionList])",
937 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
945 destroy_capture_config(config);
949 static bool match_pattern(const char *path, const char *path_basename,
950 const struct pattern_list *list)
952 for (size_t i = 0; i < list->num_pats; i++) {
953 const char *pat = list->pats[i];
956 /* Absolute path from root of capture */
959 if (strchr(pat, '/'))
960 /* Relative path from root of capture */
963 /* A file name pattern */
964 string = path_basename;
966 if (fnmatch(pat, string, FNM_PATHNAME
972 DEBUG("`%s' matches the pattern \"%s\"",
980 static void print_pattern_list(const struct pattern_list *list)
982 for (size_t i = 0; i < list->num_pats; i++)
983 printf(" %s\n", list->pats[i]);
986 static void print_capture_config(const struct capture_config *config)
988 if (config->exclusion_list.num_pats) {
989 puts("Files or folders excluded from image capture:");
990 print_pattern_list(&config->exclusion_list);
995 /* Return true if the image capture configuration file indicates we should
996 * exclude the filename @path from capture.
998 * If @exclude_prefix is %true, the part of the path up and including the name
999 * of the directory being captured is not included in the path for matching
1000 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
1001 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
1004 bool exclude_path(const char *path, const struct capture_config *config,
1005 bool exclude_prefix)
1007 const char *basename = path_basename(path);
1008 if (exclude_prefix) {
1009 wimlib_assert(strlen(path) >= config->prefix_len);
1010 if (memcmp(config->prefix, path, config->prefix_len) == 0
1011 && path[config->prefix_len] == '/')
1012 path += config->prefix_len;
1014 return match_pattern(path, basename, &config->exclusion_list) &&
1015 !match_pattern(path, basename, &config->exclusion_exception);
1019 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
1020 const char *name, const char *config_str,
1021 size_t config_len, int add_image_flags,
1022 wimlib_progress_func_t progress_func)
1024 int (*capture_tree)(struct dentry **, const char *,
1025 struct lookup_table *,
1026 struct wim_security_data *,
1027 const struct capture_config *,
1028 int, wimlib_progress_func_t, void *);
1031 struct dentry *root_dentry = NULL;
1032 struct wim_security_data *sd;
1033 struct capture_config config;
1034 struct inode_table inode_tab;
1035 struct hlist_head inode_list;
1038 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
1040 if (add_image_flags & (WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)) {
1041 ERROR("Cannot dereference files when capturing directly from NTFS");
1042 return WIMLIB_ERR_INVALID_PARAM;
1044 capture_tree = build_dentry_tree_ntfs;
1045 extra_arg = &w->ntfs_vol;
1047 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1048 " cannot capture a WIM image directly from a NTFS volume!");
1049 return WIMLIB_ERR_UNSUPPORTED;
1052 capture_tree = build_dentry_tree;
1056 DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", source);
1058 if (!name || !*name) {
1059 ERROR("Must specify a non-empty string for the image name");
1060 return WIMLIB_ERR_INVALID_PARAM;
1063 ERROR("Must specify the name of a directory or NTFS volume");
1064 return WIMLIB_ERR_INVALID_PARAM;
1067 if (w->hdr.total_parts != 1) {
1068 ERROR("Cannot add an image to a split WIM");
1069 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1072 if (wimlib_image_name_in_use(w, name)) {
1073 ERROR("There is already an image named \"%s\" in `%s'",
1075 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1078 DEBUG("Initializing capture configuration");
1080 DEBUG("Using default capture configuration");
1081 config_str = default_config;
1082 config_len = strlen(default_config);
1084 ret = init_capture_config(config_str, config_len, source, &config);
1087 print_capture_config(&config);
1089 DEBUG("Allocating security data");
1091 sd = CALLOC(1, sizeof(struct wim_security_data));
1093 ret = WIMLIB_ERR_NOMEM;
1094 goto out_destroy_config;
1096 sd->total_length = 8;
1099 if (progress_func) {
1100 union wimlib_progress_info progress;
1101 progress.scan.source = source;
1102 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1105 DEBUG("Building dentry tree.");
1106 ret = (*capture_tree)(&root_dentry, source, w->lookup_table, sd,
1107 &config, add_image_flags | WIMLIB_ADD_IMAGE_FLAG_ROOT,
1108 progress_func, extra_arg);
1109 destroy_capture_config(&config);
1112 ERROR("Failed to build dentry tree for `%s'", source);
1113 goto out_free_security_data;
1116 if (progress_func) {
1117 union wimlib_progress_info progress;
1118 progress.scan.source = source;
1119 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1122 DEBUG("Calculating full paths of dentries.");
1123 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1125 goto out_free_dentry_tree;
1127 ret = add_new_dentry_tree(w, root_dentry, sd);
1129 goto out_free_dentry_tree;
1131 DEBUG("Inserting dentries into inode table");
1132 ret = init_inode_table(&inode_tab, 9001);
1134 goto out_destroy_imd;
1136 for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
1138 DEBUG("Cleaning up the hard link groups");
1139 ret = fix_inodes(&inode_tab, &inode_list);
1140 destroy_inode_table(&inode_tab);
1142 goto out_destroy_imd;
1144 DEBUG("Assigning hard link group IDs");
1145 assign_inode_numbers(&inode_list);
1147 ret = xml_add_image(w, name);
1149 goto out_destroy_imd;
1151 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1152 wimlib_set_boot_idx(w, w->hdr.image_count);
1155 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1157 w->hdr.image_count--;
1159 out_free_dentry_tree:
1160 free_dentry_tree(root_dentry, w->lookup_table);
1161 out_free_security_data:
1162 free_security_data(sd);
1164 destroy_capture_config(&config);