]> wimlib.net Git - wimlib/blob - src/modify.c
Increment real_refcnt for metadata lte's
[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_image_flags,
100                              wimlib_progress_func_t progress_func,
101                              void *extra_arg)
102 {
103         struct stat root_stbuf;
104         int ret = 0;
105         int (*stat_fn)(const char *restrict, struct stat *restrict);
106         struct dentry *root;
107         const char *filename;
108         struct inode *inode;
109
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;
114                 }
115                 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
116                     && progress_func)
117                 {
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);
122                 }
123                 *root_ret = NULL;
124                 return 0;
125         }
126
127         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
128             && progress_func)
129         {
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);
134         }
135
136         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
137                 stat_fn = stat;
138         else
139                 stat_fn = lstat;
140
141         ret = (*stat_fn)(root_disk_path, &root_stbuf);
142         if (ret != 0) {
143                 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
144                 return WIMLIB_ERR_STAT;
145         }
146
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;
151         }
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.",
155                       root_disk_path);
156                 return WIMLIB_ERR_SPECIAL_FILE;
157         }
158
159         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
160                 filename = "";
161         else
162                 filename = path_basename(root_disk_path);
163
164         root = new_dentry_with_timeless_inode(filename);
165         if (!root)
166                 return WIMLIB_ERR_NOMEM;
167
168         inode = root->d_inode;
169
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);
174 #else
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);
178 #endif
179         if (sizeof(ino_t) >= 8)
180                 inode->ino = (u64)root_stbuf.st_ino;
181         else
182                 inode->ino = (u64)root_stbuf.st_ino |
183                                    ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
184
185         add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
186         inode->resolved = true;
187
188         if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
189
190                 struct lookup_table_entry *lte;
191                 u8 hash[SHA1_HASH_SIZE];
192
193                 inode->attributes = FILE_ATTRIBUTE_NORMAL;
194
195                 /* Empty files do not have to have a lookup table entry. */
196                 if (root_stbuf.st_size == 0)
197                         goto out;
198
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.
202                  * */
203
204                 ret = sha1sum(root_disk_path, hash);
205                 if (ret != 0)
206                         goto out;
207
208                 lte = __lookup_resource(lookup_table, hash);
209                 if (lte) {
210                         lte->refcnt++;
211                         DEBUG("Add lte reference %u for `%s'", lte->refcnt,
212                               root_disk_path);
213                 } else {
214                         char *file_on_disk = STRDUP(root_disk_path);
215                         if (!file_on_disk) {
216                                 ERROR("Failed to allocate memory for file path");
217                                 ret = WIMLIB_ERR_NOMEM;
218                                 goto out;
219                         }
220                         lte = new_lookup_table_entry();
221                         if (!lte) {
222                                 FREE(file_on_disk);
223                                 ret = WIMLIB_ERR_NOMEM;
224                                 goto out;
225                         }
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);
232                 }
233                 root->d_inode->lte = lte;
234         } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
235
236                 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
237
238                 DIR *dir;
239                 struct dirent entry, *result;
240                 struct dentry *child;
241
242                 dir = opendir(root_disk_path);
243                 if (!dir) {
244                         ERROR_WITH_ERRNO("Failed to open the directory `%s'",
245                                          root_disk_path);
246                         ret = WIMLIB_ERR_OPEN;
247                         goto out;
248                 }
249
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);
254                 name[len] = '/';
255
256                 /* Create a dentry for each entry in the directory on disk, and recurse
257                  * to any subdirectories. */
258                 while (1) {
259                         errno = 0;
260                         ret = readdir_r(dir, &entry, &result);
261                         if (ret != 0) {
262                                 ret = WIMLIB_ERR_READ;
263                                 ERROR_WITH_ERRNO("Error reading the "
264                                                  "directory `%s'",
265                                                  root_disk_path);
266                                 break;
267                         }
268                         if (result == NULL)
269                                 break;
270                         if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
271                               || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
272                                         continue;
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);
277                         if (ret != 0)
278                                 break;
279                         if (child)
280                                 dentry_add_child(root, child);
281                 }
282                 closedir(dir);
283         } else { /* Archiving a symbolic link */
284                 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
285                 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
286
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
291                  * drive letter).
292                  */
293
294                 char deref_name_buf[4096];
295                 ssize_t deref_name_len;
296
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,
303                                                 lookup_table, NULL);
304                         if (ret == 0) {
305                                 /*
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.
312                                  */
313                                 struct stat stbuf;
314                                 if (stat(root_disk_path, &stbuf) == 0 &&
315                                     S_ISDIR(stbuf.st_mode))
316                                 {
317                                         inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
318                                 }
319                         }
320                 } else {
321                         ERROR_WITH_ERRNO("Failed to read target of "
322                                          "symbolic link `%s'", root_disk_path);
323                         ret = WIMLIB_ERR_READLINK;
324                 }
325         }
326 out:
327         if (ret == 0)
328                 *root_ret = root;
329         else
330                 free_dentry_tree(root, lookup_table);
331         return ret;
332 }
333
334 struct wim_pair {
335         WIMStruct *src_wim;
336         WIMStruct *dest_wim;
337         struct list_head lte_list_head;
338 };
339
340 static int allocate_lte_if_needed(struct dentry *dentry, void *arg)
341 {
342         const WIMStruct *src_wim, *dest_wim;
343         struct list_head *lte_list_head;
344         struct inode *inode;
345
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;
350
351         wimlib_assert(!inode->resolved);
352
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);
357
358                 if (src_lte && ++src_lte->out_refcnt == 1) {
359                         dest_lte = inode_stream_lte_unresolved(inode, i,
360                                                                dest_wim->lookup_table);
361
362                         if (!dest_lte) {
363                                 dest_lte = clone_lookup_table_entry(src_lte);
364                                 if (!dest_lte)
365                                         return WIMLIB_ERR_NOMEM;
366                                 list_add_tail(&dest_lte->staging_list, lte_list_head);
367                         }
368                 }
369         }
370         return 0;
371 }
372
373 /*
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.)
381  */
382 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
383 {
384         WIMStruct *src_wim, *dest_wim;
385         struct inode *inode;
386
387         src_wim = ((struct wim_pair*)arg)->src_wim;
388         dest_wim = ((struct wim_pair*)arg)->dest_wim;
389         inode = dentry->d_inode;
390
391         wimlib_assert(!inode->resolved);
392
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);
397
398                 if (!src_lte) /* Empty or nonexistent stream. */
399                         continue;
400
401                 dest_lte = inode_stream_lte_unresolved(inode, i,
402                                                        dest_wim->lookup_table);
403                 if (dest_lte) {
404                         dest_lte->refcnt++;
405                 } else {
406                         struct list_head *lte_list_head;
407                         struct list_head *next;
408
409                         lte_list_head = &((struct wim_pair*)arg)->lte_list_head;
410                         wimlib_assert(!list_empty(lte_list_head));
411
412                         next = lte_list_head->next;
413                         list_del(next);
414                         dest_lte = container_of(next, struct lookup_table_entry,
415                                                 staging_list);
416                         dest_lte->part_number = 1;
417                         dest_lte->refcnt = 1;
418                         wimlib_assert(hashes_equal(dest_lte->hash, src_lte->hash));
419
420                         lookup_table_insert(dest_wim->lookup_table, dest_lte);
421                 }
422         }
423         return 0;
424 }
425
426 /*
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.
430  *
431  * Does not update the XML data.
432  *
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.
435  *
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.
439  */
440 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
441                                struct wim_security_data *sd)
442 {
443         struct lookup_table_entry *metadata_lte;
444         struct image_metadata *imd;
445         struct image_metadata *new_imd;
446         int ret;
447
448         wimlib_assert(root_dentry != NULL);
449
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));
453
454         if (!imd) {
455                 ERROR("Failed to allocate memory for new image metadata array");
456                 goto err;
457         }
458
459         memcpy(imd, w->image_metadata,
460                w->hdr.image_count * sizeof(struct image_metadata));
461
462         metadata_lte = new_lookup_table_entry();
463         if (!metadata_lte)
464                 goto err_free_imd;
465
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);
469
470         new_imd = &imd[w->hdr.image_count];
471
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;
476
477         FREE(w->image_metadata);
478         w->image_metadata       = imd;
479         w->hdr.image_count++;
480
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);
486         return ret;
487 err_free_imd:
488         FREE(imd);
489 err:
490         return WIMLIB_ERR_NOMEM;
491
492 }
493
494 /*
495  * Copies an image, or all the images, from a WIM file, into another WIM file.
496  */
497 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
498                                   int src_image,
499                                   WIMStruct *dest_wim,
500                                   const char *dest_name,
501                                   const char *dest_description,
502                                   int export_flags,
503                                   WIMStruct **additional_swms,
504                                   unsigned num_additional_swms,
505                                   wimlib_progress_func_t progress_func)
506 {
507         int i;
508         int ret;
509         struct dentry *root;
510         struct wim_pair wims;
511         struct wim_security_data *sd;
512         struct lookup_table *joined_tab, *src_wim_tab_save;
513
514         if (dest_wim->hdr.total_parts != 1) {
515                 ERROR("Exporting an image to a split WIM is "
516                       "unsupported");
517                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
518         }
519
520         if (src_image == WIMLIB_ALL_IMAGES) {
521                 if (src_wim->hdr.image_count > 1) {
522
523                         /* multi-image export. */
524
525                         if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
526                               (src_wim->hdr.boot_idx == 0))
527                         {
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.
532                                  * */
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;
537                         }
538                         if (dest_name || dest_description) {
539                                 ERROR("Image name or image description was "
540                                       "specified, but we are exporting "
541                                       "multiple images");
542                                 return WIMLIB_ERR_INVALID_PARAM;
543                         }
544                         for (i = 1; i <= src_wim->hdr.image_count; i++) {
545                                 int new_flags = export_flags;
546
547                                 if (i != src_wim->hdr.boot_idx)
548                                         new_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
549
550                                 ret = wimlib_export_image(src_wim, i, dest_wim,
551                                                           NULL, NULL,
552                                                           new_flags,
553                                                           additional_swms,
554                                                           num_additional_swms,
555                                                           progress_func);
556                                 if (ret != 0)
557                                         return ret;
558                         }
559                         return 0;
560                 } else if (src_wim->hdr.image_count == 1) {
561                         src_image = 1;
562                 } else {
563                         return 0;
564                 }
565         }
566
567         if (!dest_name) {
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);
571         }
572
573         if (!dest_description) {
574                 dest_description = wimlib_get_image_description(src_wim,
575                                                                 src_image);
576                 DEBUG("Using description `%s' for source image %d",
577                       dest_description, src_image);
578         }
579
580         DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
581
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;
586         }
587
588         ret = verify_swm_set(src_wim, additional_swms, num_additional_swms);
589         if (ret != 0)
590                 return ret;
591
592         if (num_additional_swms) {
593                 ret = new_joined_lookup_table(src_wim, additional_swms,
594                                               num_additional_swms,
595                                               &joined_tab);
596                 if (ret != 0)
597                         return ret;
598                 src_wim_tab_save = src_wim->lookup_table;
599                 src_wim->lookup_table = joined_tab;
600         }
601
602         ret = select_wim_image(src_wim, src_image);
603         if (ret != 0) {
604                 ERROR("Could not select image %d from the WIM `%s' "
605                       "to export it", src_image, src_wim->filename);
606                 goto out;
607         }
608
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);
619         if (ret != 0)
620                 goto out_free_ltes;
621
622         ret = xml_export_image(src_wim->wim_info, src_image,
623                                &dest_wim->wim_info, dest_name, dest_description);
624         if (ret != 0)
625                 goto out_free_ltes;
626
627         sd = wim_security_data(src_wim);
628         ret = add_new_dentry_tree(dest_wim, root, sd);
629         if (ret != 0)
630                 goto out_xml_delete_image;
631
632
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
637          * needed. */
638         for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
639         sd->refcnt++;
640         for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
641         wimlib_assert(list_empty(&wims.lte_list_head));
642
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);
646         }
647         ret = 0;
648         goto out;
649
650 out_xml_delete_image:
651         xml_delete_image(&dest_wim->wim_info, dest_wim->hdr.image_count);
652 out_free_ltes:
653         {
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);
657         }
658
659 out:
660         if (num_additional_swms) {
661                 free_lookup_table(src_wim->lookup_table);
662                 src_wim->lookup_table = src_wim_tab_save;
663         }
664         return ret;
665 }
666
667 static int image_run_full_verifications(WIMStruct *w)
668 {
669         return for_dentry_in_tree(wim_root_dentry(w), verify_dentry, w);
670 }
671
672 static int lte_fix_refcnt(struct lookup_table_entry *lte, void *ctr)
673 {
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",
678                         lte->real_refcnt);
679                 print_lookup_table_entry(lte);
680                 lte->refcnt = lte->real_refcnt;
681                 ++*(unsigned long *)ctr;
682         }
683         return 0;
684 }
685
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)
693 {
694         int ret;
695
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;
701         if (ret == 0) {
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);
712                 }
713         } else {
714                 w->all_images_verified = false;
715         }
716         return ret;
717 }
718
719 /*
720  * Deletes an image from the WIM.
721  */
722 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
723 {
724         int i;
725         int ret;
726
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;
730         }
731
732         if (image == WIMLIB_ALL_IMAGES) {
733                 for (i = w->hdr.image_count; i >= 1; i--) {
734                         ret = wimlib_delete_image(w, i);
735                         if (ret != 0)
736                                 return ret;
737                 }
738                 return 0;
739         }
740
741         if (!w->all_images_verified) {
742                 ret = wim_run_full_verifications(w);
743                 if (ret != 0)
744                         return ret;
745         }
746
747         DEBUG("Deleting image %d", image);
748
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);
753         if (ret != 0)
754                 return ret;
755
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);
759
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));
763
764         /* Decrement the image count. */
765         if (--w->hdr.image_count == 0) {
766                 FREE(w->image_metadata);
767                 w->image_metadata = NULL;
768         }
769
770         /* Fix the boot index. */
771         if (w->hdr.boot_idx == image)
772                 w->hdr.boot_idx = 0;
773         else if (w->hdr.boot_idx > image)
774                 w->hdr.boot_idx--;
775
776         w->current_image = WIMLIB_NO_IMAGE;
777
778         /* Remove the image from the XML information. */
779         xml_delete_image(&w->wim_info, image);
780
781         w->deletion_occurred = true;
782         return 0;
783 }
784
785 enum pattern_type {
786         NONE = 0,
787         EXCLUSION_LIST,
788         EXCLUSION_EXCEPTION,
789         COMPRESSION_EXCLUSION_LIST,
790         ALIGNMENT_LIST,
791 };
792
793 /* Default capture configuration file when none is specified. */
794 static const char *default_config =
795 "[ExclusionList]\n"
796 "\\$ntfs.log\n"
797 "\\hiberfil.sys\n"
798 "\\pagefile.sys\n"
799 "\\System Volume Information\n"
800 "\\RECYCLER\n"
801 "\\Windows\\CSC\n"
802 "\n"
803 "[CompressionExclusionList]\n"
804 "*.mp3\n"
805 "*.zip\n"
806 "*.cab\n"
807 "\\WINDOWS\\inf\\*.pnf\n";
808
809 static void destroy_pattern_list(struct pattern_list *list)
810 {
811         FREE(list->pats);
812 }
813
814 static void destroy_capture_config(struct capture_config *config)
815 {
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));
823 }
824
825 static int pattern_list_add_pattern(struct pattern_list *list,
826                                     const char *pattern)
827 {
828         const char **pats;
829         if (list->num_pats >= list->num_allocated_pats) {
830                 pats = REALLOC(list->pats,
831                                sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
832                 if (!pats)
833                         return WIMLIB_ERR_NOMEM;
834                 list->num_allocated_pats += 8;
835                 list->pats = pats;
836         }
837         list->pats[list->num_pats++] = pattern;
838         return 0;
839 }
840
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)
845 {
846         char *config_str;
847         char *prefix;
848         char *p;
849         char *eol;
850         char *next_p;
851         size_t bytes_remaining;
852         enum pattern_type type = NONE;
853         int ret;
854         unsigned long line_no = 0;
855
856         DEBUG("config_len = %zu", config_len);
857         bytes_remaining = config_len;
858         memset(config, 0, sizeof(*config));
859         config_str = MALLOC(config_len);
860         if (!config_str) {
861                 ERROR("Could not duplicate capture config string");
862                 return WIMLIB_ERR_NOMEM;
863         }
864         prefix = STRDUP(_prefix);
865         if (!prefix) {
866                 FREE(config_str);
867                 return WIMLIB_ERR_NOMEM;
868         }
869
870         memcpy(config_str, _config_str, config_len);
871         next_p = config_str;
872         config->config_str = config_str;
873         config->prefix = prefix;
874         config->prefix_len = strlen(prefix);
875         while (bytes_remaining) {
876                 line_no++;
877                 p = next_p;
878                 eol = memchr(p, '\n', bytes_remaining);
879                 if (!eol) {
880                         ERROR("Expected end-of-line in capture config file on "
881                               "line %lu", line_no);
882                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
883                         goto out_destroy;
884                 }
885
886                 next_p = eol + 1;
887                 bytes_remaining -= (next_p - p);
888                 if (eol == p)
889                         continue;
890
891                 if (*(eol - 1) == '\r')
892                         eol--;
893                 *eol = '\0';
894
895                 /* Translate backslash to forward slash */
896                 for (char *pp = p; pp != eol; pp++)
897                         if (*pp == '\\')
898                                 *pp = '/';
899
900                 /* Remove drive letter */
901                 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
902                         p += 2;
903
904                 ret = 0;
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) {
917                 case EXCLUSION_LIST:
918                         DEBUG("Adding pattern \"%s\" to exclusion list", p);
919                         ret = pattern_list_add_pattern(&config->exclusion_list, p);
920                         break;
921                 case EXCLUSION_EXCEPTION:
922                         DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
923                         ret = pattern_list_add_pattern(&config->exclusion_exception, p);
924                         break;
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);
928                         break;
929                 case ALIGNMENT_LIST:
930                         DEBUG("Adding pattern \"%s\" to alignment list", p);
931                         ret = pattern_list_add_pattern(&config->alignment_list, p);
932                         break;
933                 default:
934                         ERROR("Line %lu of capture configuration is not "
935                               "in a block (such as [ExclusionList])",
936                               line_no);
937                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
938                         break;
939                 }
940                 if (ret != 0)
941                         goto out_destroy;
942         }
943         return 0;
944 out_destroy:
945         destroy_capture_config(config);
946         return ret;
947 }
948
949 static bool match_pattern(const char *path, const char *path_basename,
950                           const struct pattern_list *list)
951 {
952         for (size_t i = 0; i < list->num_pats; i++) {
953                 const char *pat = list->pats[i];
954                 const char *string;
955                 if (pat[0] == '/')
956                         /* Absolute path from root of capture */
957                         string = path;
958                 else {
959                         if (strchr(pat, '/'))
960                                 /* Relative path from root of capture */
961                                 string = path + 1;
962                         else
963                                 /* A file name pattern */
964                                 string = path_basename;
965                 }
966                 if (fnmatch(pat, string, FNM_PATHNAME
967                         #ifdef FNM_CASEFOLD
968                                         | FNM_CASEFOLD
969                         #endif
970                         ) == 0)
971                 {
972                         DEBUG("`%s' matches the pattern \"%s\"",
973                               string, pat);
974                         return true;
975                 }
976         }
977         return false;
978 }
979
980 static void print_pattern_list(const struct pattern_list *list)
981 {
982         for (size_t i = 0; i < list->num_pats; i++)
983                 printf("    %s\n", list->pats[i]);
984 }
985
986 static void print_capture_config(const struct capture_config *config)
987 {
988         if (config->exclusion_list.num_pats) {
989                 puts("Files or folders excluded from image capture:");
990                 print_pattern_list(&config->exclusion_list);
991                 putchar('\n');
992         }
993 }
994
995 /* Return true if the image capture configuration file indicates we should
996  * exclude the filename @path from capture.
997  *
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
1002  * directory.
1003  */
1004 bool exclude_path(const char *path, const struct capture_config *config,
1005                   bool exclude_prefix)
1006 {
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;
1013         }
1014         return match_pattern(path, basename, &config->exclusion_list) &&
1015                 !match_pattern(path, basename, &config->exclusion_exception);
1016
1017 }
1018
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)
1023 {
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 *);
1029         void *extra_arg;
1030
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;
1036         int ret;
1037
1038         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
1039 #ifdef WITH_NTFS_3G
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;
1043                 }
1044                 capture_tree = build_dentry_tree_ntfs;
1045                 extra_arg = &w->ntfs_vol;
1046 #else
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;
1050 #endif
1051         } else {
1052                 capture_tree = build_dentry_tree;
1053                 extra_arg = NULL;
1054         }
1055
1056         DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", source);
1057
1058         if (!name || !*name) {
1059                 ERROR("Must specify a non-empty string for the image name");
1060                 return WIMLIB_ERR_INVALID_PARAM;
1061         }
1062         if (!source) {
1063                 ERROR("Must specify the name of a directory or NTFS volume");
1064                 return WIMLIB_ERR_INVALID_PARAM;
1065         }
1066
1067         if (w->hdr.total_parts != 1) {
1068                 ERROR("Cannot add an image to a split WIM");
1069                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1070         }
1071
1072         if (wimlib_image_name_in_use(w, name)) {
1073                 ERROR("There is already an image named \"%s\" in `%s'",
1074                       name, w->filename);
1075                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1076         }
1077
1078         DEBUG("Initializing capture configuration");
1079         if (!config_str) {
1080                 DEBUG("Using default capture configuration");
1081                 config_str = default_config;
1082                 config_len = strlen(default_config);
1083         }
1084         ret = init_capture_config(config_str, config_len, source, &config);
1085         if (ret != 0)
1086                 return ret;
1087         print_capture_config(&config);
1088
1089         DEBUG("Allocating security data");
1090
1091         sd = CALLOC(1, sizeof(struct wim_security_data));
1092         if (!sd) {
1093                 ret = WIMLIB_ERR_NOMEM;
1094                 goto out_destroy_config;
1095         }
1096         sd->total_length = 8;
1097         sd->refcnt = 1;
1098
1099         if (progress_func) {
1100                 union wimlib_progress_info progress;
1101                 progress.scan.source = source;
1102                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1103         }
1104
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);
1110
1111         if (ret != 0) {
1112                 ERROR("Failed to build dentry tree for `%s'", source);
1113                 goto out_free_security_data;
1114         }
1115
1116         if (progress_func) {
1117                 union wimlib_progress_info progress;
1118                 progress.scan.source = source;
1119                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1120         }
1121
1122         DEBUG("Calculating full paths of dentries.");
1123         ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1124         if (ret != 0)
1125                 goto out_free_dentry_tree;
1126
1127         ret = add_new_dentry_tree(w, root_dentry, sd);
1128         if (ret != 0)
1129                 goto out_free_dentry_tree;
1130
1131         DEBUG("Inserting dentries into inode table");
1132         ret = init_inode_table(&inode_tab, 9001);
1133         if (ret != 0)
1134                 goto out_destroy_imd;
1135
1136         for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
1137
1138         DEBUG("Cleaning up the hard link groups");
1139         ret = fix_inodes(&inode_tab, &inode_list);
1140         destroy_inode_table(&inode_tab);
1141         if (ret != 0)
1142                 goto out_destroy_imd;
1143
1144         DEBUG("Assigning hard link group IDs");
1145         assign_inode_numbers(&inode_list);
1146
1147         ret = xml_add_image(w, name);
1148         if (ret != 0)
1149                 goto out_destroy_imd;
1150
1151         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1152                 wimlib_set_boot_idx(w, w->hdr.image_count);
1153         return 0;
1154 out_destroy_imd:
1155         destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1156                                w->lookup_table);
1157         w->hdr.image_count--;
1158         return ret;
1159 out_free_dentry_tree:
1160         free_dentry_tree(root_dentry, w->lookup_table);
1161 out_free_security_data:
1162         free_security_data(sd);
1163 out_destroy_config:
1164         destroy_capture_config(&config);
1165         return ret;
1166 }