Consolidate `struct lookup_table_entry'
[wimlib] / src / modify.c
1 /*
2  * modify.c
3  *
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.
8  */
9
10 /*
11  * Copyright (C) 2012 Eric Biggers
12  *
13  * This file is part of wimlib, a library for working with WIM files.
14  *
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)
18  * any later version.
19  *
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
23  * details.
24  *
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/.
27  */
28
29 #include "wimlib_internal.h"
30 #include "util.h"
31 #include "sha1.h"
32 #include "dentry.h"
33 #include "xml.h"
34 #include "lookup_table.h"
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <fnmatch.h>
40 #include <ctype.h>
41 #include <unistd.h>
42
43 /** Private flag: Used to mark that we currently adding the root directory of
44  * the WIM image. */
45 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
46
47 void destroy_image_metadata(struct image_metadata *imd, struct lookup_table *lt)
48 {
49         free_dentry_tree(imd->root_dentry, lt);
50         free_security_data(imd->security_data);
51
52         /* Get rid of the lookup table entry for this image's metadata resource
53          * */
54         if (lt)
55                 lookup_table_remove(lt, imd->metadata_lte);
56 }
57
58 /*
59  * Recursively builds a dentry tree from a directory tree on disk, outside the
60  * WIM file.
61  *
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.
65  *
66  * @root_disk_path:  The path to the root of the directory tree on disk.
67  *
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
72  *              the file on disk.
73  *
74  * @sd:         Ignored.  (Security data only captured in NTFS mode.)
75  *
76  * @capture_config:
77  *              Configuration for files to be excluded from capture.
78  *
79  * @add_flags:  Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
80  *
81  * @extra_arg:  Ignored. (Only used in NTFS mode.)
82  *
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
88  *              wimlib_overwrite().
89  */
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,
95                              int add_flags,
96                              void *extra_arg)
97 {
98         struct stat root_stbuf;
99         int ret = 0;
100         int (*stat_fn)(const char *restrict, struct stat *restrict);
101         struct dentry *root;
102         const char *filename;
103
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;
108                 }
109                 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
110                         printf("Excluding file `%s' from capture\n",
111                                root_disk_path);
112                 *root_ret = NULL;
113                 return 0;
114         }
115
116
117         if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
118                 stat_fn = stat;
119         else
120                 stat_fn = lstat;
121
122         if (add_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
123                 printf("Scanning `%s'\n", root_disk_path);
124
125         ret = (*stat_fn)(root_disk_path, &root_stbuf);
126         if (ret != 0) {
127                 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
128                 return WIMLIB_ERR_STAT;
129         }
130
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;
135         }
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.",
139                       root_disk_path);
140                 return WIMLIB_ERR_SPECIAL_FILE;
141         }
142
143         if (add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
144                 filename = "";
145         else
146                 filename = path_basename(root_disk_path);
147
148         root = new_dentry_with_timeless_inode(filename);
149         if (!root)
150                 return WIMLIB_ERR_NOMEM;
151
152         stbuf_to_inode(&root_stbuf, root->d_inode);
153         add_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
154         root->d_inode->resolved = true;
155
156         if (dentry_is_directory(root)) { /* Archiving a directory */
157                 DIR *dir;
158                 struct dirent *p;
159                 struct dentry *child;
160
161                 dir = opendir(root_disk_path);
162                 if (!dir) {
163                         ERROR_WITH_ERRNO("Failed to open the directory `%s'",
164                                          root_disk_path);
165                         ret = WIMLIB_ERR_OPEN;
166                         goto out;
167                 }
168
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);
173                 name[len] = '/';
174
175                 /* Create a dentry for each entry in the directory on disk, and recurse
176                  * to any subdirectories. */
177                 while (1) {
178                         errno = 0;
179                         p = readdir(dir);
180                         if (p == NULL) {
181                                 if (errno) {
182                                         ret = WIMLIB_ERR_READ;
183                                         ERROR_WITH_ERRNO("Error reading the "
184                                                          "directory `%s'",
185                                                          root_disk_path);
186                                 }
187                                 break;
188                         }
189                         if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
190                               || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
191                                         continue;
192                         strcpy(name + len + 1, p->d_name);
193                         ret = build_dentry_tree(&child, name, lookup_table,
194                                                 NULL, config,
195                                                 add_flags, NULL);
196                         if (ret != 0)
197                                 break;
198                         if (child)
199                                 dentry_add_child(root, child);
200                 }
201                 closedir(dir);
202         } else if (dentry_is_symlink(root)) { /* Archiving a symbolic link */
203                 char deref_name_buf[4096];
204                 ssize_t deref_name_len;
205
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,
212                                                 lookup_table, NULL);
213                 } else {
214                         ERROR_WITH_ERRNO("Failed to read target of "
215                                          "symbolic link `%s'", root_disk_path);
216                         ret = WIMLIB_ERR_READLINK;
217                 }
218         } else { /* Archiving a regular file */
219
220                 struct lookup_table_entry *lte;
221                 u8 hash[SHA1_HASH_SIZE];
222
223                 /* Empty files do not have to have a lookup table entry. */
224                 if (root_stbuf.st_size == 0)
225                         goto out;
226
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.
230                  * */
231
232                 ret = sha1sum(root_disk_path, hash);
233                 if (ret != 0)
234                         goto out;
235
236                 lte = __lookup_resource(lookup_table, hash);
237                 if (lte) {
238                         lte->refcnt++;
239                         DEBUG("Add lte reference %u for `%s'", lte->refcnt,
240                               root_disk_path);
241                 } else {
242                         char *file_on_disk = STRDUP(root_disk_path);
243                         if (!file_on_disk) {
244                                 ERROR("Failed to allocate memory for file path");
245                                 ret = WIMLIB_ERR_NOMEM;
246                                 goto out;
247                         }
248                         lte = new_lookup_table_entry();
249                         if (!lte) {
250                                 FREE(file_on_disk);
251                                 ret = WIMLIB_ERR_NOMEM;
252                                 goto out;
253                         }
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);
260                 }
261                 root->d_inode->lte = lte;
262         }
263 out:
264         if (ret == 0)
265                 *root_ret = root;
266         else
267                 free_dentry_tree(root, lookup_table);
268         return ret;
269 }
270
271 struct wim_pair {
272         WIMStruct *src_wim;
273         WIMStruct *dest_wim;
274         struct list_head lte_list_head;
275 };
276
277 static int allocate_lte_if_needed(struct dentry *dentry, void *arg)
278 {
279         const WIMStruct *src_wim, *dest_wim;
280         struct list_head *lte_list_head;
281         struct inode *inode;
282
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;
287
288         wimlib_assert(!inode->resolved);
289
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);
294
295                 if (src_lte && ++src_lte->out_refcnt == 1) {
296                         dest_lte = inode_stream_lte_unresolved(inode, i,
297                                                                dest_wim->lookup_table);
298
299                         if (!dest_lte) {
300                                 dest_lte = clone_lookup_table_entry(src_lte);
301                                 if (!dest_lte)
302                                         return WIMLIB_ERR_NOMEM;
303                                 list_add_tail(&dest_lte->staging_list, lte_list_head);
304                         }
305                 }
306         }
307         return 0;
308 }
309
310 /*
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.)
318  */
319 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
320 {
321         WIMStruct *src_wim, *dest_wim;
322         struct inode *inode;
323
324         src_wim = ((struct wim_pair*)arg)->src_wim;
325         dest_wim = ((struct wim_pair*)arg)->dest_wim;
326         inode = dentry->d_inode;
327
328         wimlib_assert(!inode->resolved);
329
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);
334
335                 if (!src_lte) /* Empty or nonexistent stream. */
336                         continue;
337
338                 dest_lte = inode_stream_lte_unresolved(inode, i,
339                                                        dest_wim->lookup_table);
340                 if (dest_lte) {
341                         dest_lte->refcnt++;
342                 } else {
343                         struct list_head *lte_list_head;
344                         struct list_head *next;
345
346                         lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
347                         wimlib_assert(!list_empty(lte_list_head));
348
349                         next = lte_list_head->next;
350                         list_del(next);
351                         dest_lte = container_of(next, struct lookup_table_entry,
352                                                 staging_list);
353                         dest_lte->part_number = 1;
354                         dest_lte->refcnt = 1;
355                         wimlib_assert(hashes_equal(dest_lte->hash, src_lte->hash));
356
357                         lookup_table_insert(dest_wim->lookup_table, dest_lte);
358                 }
359         }
360         return 0;
361 }
362
363 /*
364  * Adds an image (given by its dentry tree) to the image metadata array of a WIM
365  * file, adds an entry to the lookup table for the image metadata, updates the
366  * image count in the header, and selects the new image.
367  *
368  * Does not update the XML data.
369  *
370  * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made.  Otherwise,
371  * 0 is returned and the image metadata array of @w is modified.
372  *
373  * @w:            The WIMStruct for the WIM file.
374  * @root_dentry:  The root of the directory tree for the image.
375  * @sd:           The security data for the image.
376  */
377 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
378                                struct wim_security_data *sd)
379 {
380         struct lookup_table_entry *metadata_lte;
381         struct image_metadata *imd;
382         struct image_metadata *new_imd;
383         int ret;
384
385         wimlib_assert(root_dentry != NULL);
386
387         DEBUG("Reallocating image metadata array for image_count = %u",
388               w->hdr.image_count + 1);
389         imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
390
391         if (!imd) {
392                 ERROR("Failed to allocate memory for new image metadata array");
393                 goto err;
394         }
395
396         memcpy(imd, w->image_metadata,
397                w->hdr.image_count * sizeof(struct image_metadata));
398
399         metadata_lte = new_lookup_table_entry();
400         if (!metadata_lte)
401                 goto err_free_imd;
402
403         metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
404         random_hash(metadata_lte->hash);
405         lookup_table_insert(w->lookup_table, metadata_lte);
406
407         new_imd = &imd[w->hdr.image_count];
408
409         new_imd->root_dentry    = root_dentry;
410         new_imd->metadata_lte   = metadata_lte;
411         new_imd->security_data  = sd;
412         new_imd->modified       = true;
413
414         FREE(w->image_metadata);
415         w->image_metadata       = imd;
416         w->hdr.image_count++;
417
418         /* Change the current image to the new one.  There should not be any
419          * ways for this to fail, since the image is valid and the dentry tree
420          * is already in memory. */
421         ret = select_wim_image(w, w->hdr.image_count);
422         wimlib_assert(ret == 0);
423         return ret;
424 err_free_imd:
425         FREE(imd);
426 err:
427         return WIMLIB_ERR_NOMEM;
428
429 }
430
431 /*
432  * Copies an image, or all the images, from a WIM file, into another WIM file.
433  */
434 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
435                                   int src_image,
436                                   WIMStruct *dest_wim,
437                                   const char *dest_name,
438                                   const char *dest_description,
439                                   int flags,
440                                   WIMStruct **additional_swms,
441                                   unsigned num_additional_swms)
442 {
443         int i;
444         int ret;
445         struct dentry *root;
446         struct wim_pair wims;
447         struct wim_security_data *sd;
448         struct lookup_table *joined_tab, *src_wim_tab_save;
449
450         if (!src_wim || !dest_wim)
451                 return WIMLIB_ERR_INVALID_PARAM;
452
453         if (dest_wim->hdr.total_parts != 1) {
454                 ERROR("Exporting an image to a split WIM is "
455                       "unsupported");
456                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
457         }
458
459         if (src_image == WIM_ALL_IMAGES) {
460                 if (src_wim->hdr.image_count > 1) {
461
462                         /* multi-image export. */
463
464                         if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
465                               (src_wim->hdr.boot_idx == 0))
466                         {
467                                 /* Specifying the boot flag on a multi-image
468                                  * source WIM makes the boot index default to
469                                  * the bootable image in the source WIM.  It is
470                                  * an error if there is no such bootable image.
471                                  * */
472                                 ERROR("Cannot specify `boot' flag when "
473                                       "exporting multiple images from a WIM "
474                                       "with no bootable images");
475                                 return WIMLIB_ERR_INVALID_PARAM;
476                         }
477                         if (dest_name || dest_description) {
478                                 ERROR("Image name or image description was "
479                                       "specified, but we are exporting "
480                                       "multiple images");
481                                 return WIMLIB_ERR_INVALID_PARAM;
482                         }
483                         for (i = 1; i <= src_wim->hdr.image_count; i++) {
484                                 int export_flags = flags;
485
486                                 if (i != src_wim->hdr.boot_idx)
487                                         export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
488
489                                 ret = wimlib_export_image(src_wim, i, dest_wim,
490                                                           NULL, NULL,
491                                                           export_flags,
492                                                           additional_swms,
493                                                           num_additional_swms);
494                                 if (ret != 0)
495                                         return ret;
496                         }
497                         return 0;
498                 } else if (src_wim->hdr.image_count == 1) {
499                         src_image = 1;
500                 } else {
501                         return 0;
502                 }
503         }
504
505         if (!dest_name) {
506                 dest_name = wimlib_get_image_name(src_wim, src_image);
507                 DEBUG("Using name `%s' for source image %d",
508                       dest_name, src_image);
509         }
510
511         if (!dest_description) {
512                 dest_description = wimlib_get_image_description(src_wim,
513                                                                 src_image);
514                 DEBUG("Using description `%s' for source image %d",
515                       dest_description, src_image);
516         }
517
518         DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
519
520         if (wimlib_image_name_in_use(dest_wim, dest_name)) {
521                 ERROR("There is already an image named `%s' in the "
522                       "destination WIM", dest_name);
523                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
524         }
525
526         ret = verify_swm_set(src_wim, additional_swms, num_additional_swms);
527         if (ret != 0)
528                 return ret;
529
530         if (num_additional_swms) {
531                 ret = new_joined_lookup_table(src_wim, additional_swms,
532                                               num_additional_swms,
533                                               &joined_tab);
534                 if (ret != 0)
535                         return ret;
536                 src_wim_tab_save = src_wim->lookup_table;
537                 src_wim->lookup_table = joined_tab;
538         }
539
540         ret = select_wim_image(src_wim, src_image);
541         if (ret != 0) {
542                 ERROR("Could not select image %d from the WIM `%s' "
543                       "to export it", src_image, src_wim->filename);
544                 goto out;
545         }
546
547         /* Pre-allocate the new lookup table entries that will be needed.  This
548          * way, it's not possible to run out of memory part-way through
549          * modifying the lookup table of the destination WIM. */
550         wims.src_wim = src_wim;
551         wims.dest_wim = dest_wim;
552         INIT_LIST_HEAD(&wims.lte_list_head);
553         for_lookup_table_entry(src_wim->lookup_table, lte_zero_out_refcnt, NULL);
554         root = wim_root_dentry(src_wim);
555         for_dentry_in_tree(root, dentry_unresolve_ltes, NULL);
556         ret = for_dentry_in_tree(root, allocate_lte_if_needed, &wims);
557         if (ret != 0)
558                 goto out_free_ltes;
559
560         ret = xml_export_image(src_wim->wim_info, src_image,
561                                &dest_wim->wim_info, dest_name, dest_description);
562         if (ret != 0)
563                 goto out_free_ltes;
564
565         sd = wim_security_data(src_wim);
566         ret = add_new_dentry_tree(dest_wim, root, sd);
567         if (ret != 0)
568                 goto out_xml_delete_image;
569
570
571         /* All memory allocations have been taken care of, so it's no longer
572          * possible for this function to fail.  Go ahead and increment the
573          * reference counts of the dentry tree and security data, then update
574          * the lookup table of the destination WIM and the boot index, if
575          * needed. */
576         for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
577         sd->refcnt++;
578         for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
579         wimlib_assert(list_empty(&wims.lte_list_head));
580
581         if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
582                 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
583                 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
584         }
585         ret = 0;
586         goto out;
587
588 out_xml_delete_image:
589         xml_delete_image(&dest_wim->wim_info, dest_wim->hdr.image_count);
590 out_free_ltes:
591         {
592                 struct lookup_table_entry *lte, *tmp;
593                 list_for_each_entry_safe(lte, tmp, &wims.lte_list_head, staging_list)
594                         free_lookup_table_entry(lte);
595         }
596
597 out:
598         if (num_additional_swms) {
599                 free_lookup_table(src_wim->lookup_table);
600                 src_wim->lookup_table = src_wim_tab_save;
601         }
602         return ret;
603 }
604
605 /*
606  * Deletes an image from the WIM.
607  */
608 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
609 {
610         int i;
611         int ret;
612
613         if (w->hdr.total_parts != 1) {
614                 ERROR("Deleting an image from a split WIM is not supported.");
615                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
616         }
617
618         if (image == WIM_ALL_IMAGES) {
619                 for (i = w->hdr.image_count; i >= 1; i--) {
620                         ret = wimlib_delete_image(w, i);
621                         if (ret != 0)
622                                 return ret;
623                 }
624                 return 0;
625         }
626
627         DEBUG("Deleting image %d", image);
628
629         /* Even if the dentry tree is not allocated, we must select it (and
630          * therefore allocate it) so that we can decrement the reference counts
631          * in the lookup table.  */
632         ret = select_wim_image(w, image);
633         if (ret != 0)
634                 return ret;
635
636         /* Free the dentry tree, any lookup table entries that have their
637          * refcnt decremented to 0, and the security data. */
638         destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table);
639
640         /* Get rid of the empty slot in the image metadata array. */
641         memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
642                 (w->hdr.image_count - image) * sizeof(struct image_metadata));
643
644         /* Decrement the image count. */
645         if (--w->hdr.image_count == 0) {
646                 FREE(w->image_metadata);
647                 w->image_metadata = NULL;
648         }
649
650         /* Fix the boot index. */
651         if (w->hdr.boot_idx == image)
652                 w->hdr.boot_idx = 0;
653         else if (w->hdr.boot_idx > image)
654                 w->hdr.boot_idx--;
655
656         w->current_image = WIM_NO_IMAGE;
657
658         /* Remove the image from the XML information. */
659         xml_delete_image(&w->wim_info, image);
660         return 0;
661 }
662
663 enum pattern_type {
664         NONE = 0,
665         EXCLUSION_LIST,
666         EXCLUSION_EXCEPTION,
667         COMPRESSION_EXCLUSION_LIST,
668         ALIGNMENT_LIST,
669 };
670
671 /* Default capture configuration file when none is specified. */
672 static const char *default_config =
673 "[ExclusionList]\n"
674 "\\$ntfs.log\n"
675 "\\hiberfil.sys\n"
676 "\\pagefile.sys\n"
677 "\\System Volume Information\n"
678 "\\RECYCLER\n"
679 "\\Windows\\CSC\n"
680 "\n"
681 "[CompressionExclusionList]\n"
682 "*.mp3\n"
683 "*.zip\n"
684 "*.cab\n"
685 "\\WINDOWS\\inf\\*.pnf\n";
686
687 static void destroy_pattern_list(struct pattern_list *list)
688 {
689         FREE(list->pats);
690 }
691
692 static void destroy_capture_config(struct capture_config *config)
693 {
694         destroy_pattern_list(&config->exclusion_list);
695         destroy_pattern_list(&config->exclusion_exception);
696         destroy_pattern_list(&config->compression_exclusion_list);
697         destroy_pattern_list(&config->alignment_list);
698         FREE(config->config_str);
699         FREE(config->prefix);
700         memset(config, 0, sizeof(*config));
701 }
702
703 static int pattern_list_add_pattern(struct pattern_list *list,
704                                     const char *pattern)
705 {
706         const char **pats;
707         if (list->num_pats >= list->num_allocated_pats) {
708                 pats = REALLOC(list->pats,
709                                sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
710                 if (!pats)
711                         return WIMLIB_ERR_NOMEM;
712                 list->num_allocated_pats += 8;
713                 list->pats = pats;
714         }
715         list->pats[list->num_pats++] = pattern;
716         return 0;
717 }
718
719 /* Parses the contents of the image capture configuration file and fills in a
720  * `struct capture_config'. */
721 static int init_capture_config(const char *_config_str, size_t config_len,
722                                const char *_prefix, struct capture_config *config)
723 {
724         char *config_str;
725         char *prefix;
726         char *p;
727         char *eol;
728         char *next_p;
729         size_t bytes_remaining;
730         enum pattern_type type = NONE;
731         int ret;
732         unsigned long line_no = 0;
733
734         DEBUG("config_len = %zu", config_len);
735         bytes_remaining = config_len;
736         memset(config, 0, sizeof(*config));
737         config_str = MALLOC(config_len);
738         if (!config_str) {
739                 ERROR("Could not duplicate capture config string");
740                 return WIMLIB_ERR_NOMEM;
741         }
742         prefix = STRDUP(_prefix);
743         if (!prefix) {
744                 FREE(config_str);
745                 return WIMLIB_ERR_NOMEM;
746         }
747
748         memcpy(config_str, _config_str, config_len);
749         next_p = config_str;
750         config->config_str = config_str;
751         config->prefix = prefix;
752         config->prefix_len = strlen(prefix);
753         while (bytes_remaining) {
754                 line_no++;
755                 p = next_p;
756                 eol = memchr(p, '\n', bytes_remaining);
757                 if (!eol) {
758                         ERROR("Expected end-of-line in capture config file on "
759                               "line %lu", line_no);
760                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
761                         goto out_destroy;
762                 }
763
764                 next_p = eol + 1;
765                 bytes_remaining -= (next_p - p);
766                 if (eol == p)
767                         continue;
768
769                 if (*(eol - 1) == '\r')
770                         eol--;
771                 *eol = '\0';
772
773                 /* Translate backslash to forward slash */
774                 for (char *pp = p; pp != eol; pp++)
775                         if (*pp == '\\')
776                                 *pp = '/';
777
778                 /* Remove drive letter */
779                 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
780                         p += 2;
781
782                 ret = 0;
783                 if (strcmp(p, "[ExclusionList]") == 0)
784                         type = EXCLUSION_LIST;
785                 else if (strcmp(p, "[ExclusionException]") == 0)
786                         type = EXCLUSION_EXCEPTION;
787                 else if (strcmp(p, "[CompressionExclusionList]") == 0)
788                         type = COMPRESSION_EXCLUSION_LIST;
789                 else if (strcmp(p, "[AlignmentList]") == 0)
790                         type = ALIGNMENT_LIST;
791                 else if (p[0] == '[' && strrchr(p, ']')) {
792                         ERROR("Unknown capture configuration section `%s'", p);
793                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
794                 } else switch (type) {
795                 case EXCLUSION_LIST:
796                         DEBUG("Adding pattern \"%s\" to exclusion list", p);
797                         ret = pattern_list_add_pattern(&config->exclusion_list, p);
798                         break;
799                 case EXCLUSION_EXCEPTION:
800                         DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
801                         ret = pattern_list_add_pattern(&config->exclusion_exception, p);
802                         break;
803                 case COMPRESSION_EXCLUSION_LIST:
804                         DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
805                         ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
806                         break;
807                 case ALIGNMENT_LIST:
808                         DEBUG("Adding pattern \"%s\" to alignment list", p);
809                         ret = pattern_list_add_pattern(&config->alignment_list, p);
810                         break;
811                 default:
812                         ERROR("Line %lu of capture configuration is not "
813                               "in a block (such as [ExclusionList])",
814                               line_no);
815                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
816                         break;
817                 }
818                 if (ret != 0)
819                         goto out_destroy;
820         }
821         return 0;
822 out_destroy:
823         destroy_capture_config(config);
824         return ret;
825 }
826
827 static bool match_pattern(const char *path, const char *path_basename,
828                           const struct pattern_list *list)
829 {
830         for (size_t i = 0; i < list->num_pats; i++) {
831                 const char *pat = list->pats[i];
832                 const char *string;
833                 if (pat[0] == '/')
834                         /* Absolute path from root of capture */
835                         string = path;
836                 else {
837                         if (strchr(pat, '/'))
838                                 /* Relative path from root of capture */
839                                 string = path + 1;
840                         else
841                                 /* A file name pattern */
842                                 string = path_basename;
843                 }
844                 if (fnmatch(pat, string, FNM_PATHNAME
845                         #ifdef FNM_CASEFOLD
846                                         | FNM_CASEFOLD
847                         #endif
848                         ) == 0)
849                 {
850                         DEBUG("`%s' matches the pattern \"%s\"",
851                               string, pat);
852                         return true;
853                 }
854         }
855         return false;
856 }
857
858 static void print_pattern_list(const struct pattern_list *list)
859 {
860         for (size_t i = 0; i < list->num_pats; i++)
861                 printf("    %s\n", list->pats[i]);
862 }
863
864 static void print_capture_config(const struct capture_config *config)
865 {
866         if (config->exclusion_list.num_pats) {
867                 puts("Files or folders excluded from image capture:");
868                 print_pattern_list(&config->exclusion_list);
869                 putchar('\n');
870         }
871 }
872
873 /* Return true if the image capture configuration file indicates we should
874  * exclude the filename @path from capture.
875  *
876  * If @exclude_prefix is %true, the part of the path up and including the name
877  * of the directory being captured is not included in the path for matching
878  * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
879  * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
880  * directory.
881  */
882 bool exclude_path(const char *path, const struct capture_config *config,
883                   bool exclude_prefix)
884 {
885         const char *basename = path_basename(path);
886         if (exclude_prefix) {
887                 wimlib_assert(strlen(path) >= config->prefix_len);
888                 if (memcmp(config->prefix, path, config->prefix_len) == 0
889                      && path[config->prefix_len] == '/')
890                         path += config->prefix_len;
891         }
892         return match_pattern(path, basename, &config->exclusion_list) &&
893                 !match_pattern(path, basename, &config->exclusion_exception);
894
895 }
896
897
898
899 /*
900  * Adds an image to the WIM, delegating the capture of the dentry tree and
901  * security data to the function @capture_tree passed as a parameter.
902  * Currently, @capture_tree may be build_dentry_tree() for capturing a "regular"
903  * directory tree on disk, or build_dentry_tree_ntfs() for capturing a WIM image
904  * directory from a NTFS volume using libntfs-3g.
905  *
906  * The @capture_tree function is also expected to create lookup table entries
907  * for all the file streams it captures and insert them into @lookup_table,
908  * being careful to look for identical entries that already exist and simply
909  * increment the reference count for them rather than duplicating the entry.
910  */
911 int do_add_image(WIMStruct *w, const char *dir, const char *name,
912                  const char *config_str, size_t config_len,
913                  int flags,
914                  int (*capture_tree)(struct dentry **, const char *,
915                                      struct lookup_table *,
916                                      struct wim_security_data *,
917                                      const struct capture_config *,
918                                      int, void *),
919                  void *extra_arg)
920 {
921         struct dentry *root_dentry = NULL;
922         struct wim_security_data *sd;
923         struct capture_config config;
924         struct inode_table inode_tab;
925         struct hlist_head inode_list;
926         int ret;
927
928         DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", dir);
929
930         if (!name || !*name) {
931                 ERROR("Must specify a non-empty string for the image name");
932                 return WIMLIB_ERR_INVALID_PARAM;
933         }
934         if (!dir) {
935                 ERROR("Must specify the name of a directory or NTFS volume");
936                 return WIMLIB_ERR_INVALID_PARAM;
937         }
938
939         if (w->hdr.total_parts != 1) {
940                 ERROR("Cannot add an image to a split WIM");
941                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
942         }
943
944         if (wimlib_image_name_in_use(w, name)) {
945                 ERROR("There is already an image named \"%s\" in `%s'",
946                       name, w->filename);
947                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
948         }
949
950         DEBUG("Initializing capture configuration");
951         if (!config_str) {
952                 DEBUG("Using default capture configuration");
953                 config_str = default_config;
954                 config_len = strlen(default_config);
955         }
956         ret = init_capture_config(config_str, config_len, dir, &config);
957         if (ret != 0)
958                 return ret;
959         print_capture_config(&config);
960
961         DEBUG("Allocating security data");
962
963         sd = CALLOC(1, sizeof(struct wim_security_data));
964         if (!sd) {
965                 ret = WIMLIB_ERR_NOMEM;
966                 goto out_destroy_config;
967         }
968         sd->total_length = 8;
969         sd->refcnt = 1;
970
971         DEBUG("Building dentry tree.");
972         if (flags & WIMLIB_ADD_IMAGE_FLAG_SHOW_PROGRESS) {
973                 printf("Scanning `%s'...\n", dir);
974         }
975         ret = (*capture_tree)(&root_dentry, dir, w->lookup_table, sd,
976                               &config, flags | WIMLIB_ADD_IMAGE_FLAG_ROOT,
977                               extra_arg);
978         destroy_capture_config(&config);
979
980         if (ret != 0) {
981                 ERROR("Failed to build dentry tree for `%s'", dir);
982                 goto out_free_security_data;
983         }
984
985         DEBUG("Calculating full paths of dentries.");
986         ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
987         if (ret != 0)
988                 goto out_free_dentry_tree;
989
990         ret = add_new_dentry_tree(w, root_dentry, sd);
991         if (ret != 0)
992                 goto out_free_dentry_tree;
993
994         DEBUG("Inserting dentries into inode table");
995         ret = init_inode_table(&inode_tab, 9001);
996         if (ret != 0)
997                 goto out_destroy_imd;
998
999         for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
1000
1001         DEBUG("Cleaning up the hard link groups");
1002         ret = fix_inodes(&inode_tab, &inode_list);
1003         destroy_inode_table(&inode_tab);
1004         if (ret != 0)
1005                 goto out_destroy_imd;
1006
1007         DEBUG("Assigning hard link group IDs");
1008         assign_inode_numbers(&inode_list);
1009
1010         ret = xml_add_image(w, name);
1011         if (ret != 0)
1012                 goto out_destroy_imd;
1013
1014         if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1015                 w->hdr.boot_idx = w->hdr.image_count;
1016
1017         return 0;
1018 out_destroy_imd:
1019         destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1020                                w->lookup_table);
1021         w->hdr.image_count--;
1022         return ret;
1023 out_free_dentry_tree:
1024         free_dentry_tree(root_dentry, w->lookup_table);
1025 out_free_security_data:
1026         free_security_data(sd);
1027 out_destroy_config:
1028         destroy_capture_config(&config);
1029         return ret;
1030 }
1031
1032 /*
1033  * Adds an image to a WIM file from a directory tree on disk.
1034  */
1035 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
1036                                const char *name, const char *config_str,
1037                                size_t config_len, int flags)
1038 {
1039         return do_add_image(w, dir, name, config_str, config_len, flags,
1040                             build_dentry_tree, NULL);
1041 }