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