]> wimlib.net Git - wimlib/blob - src/add_image.c
WIM capture: Share inodes immediately
[wimlib] / src / add_image.c
1 /*
2  * add_image.c
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #include "config.h"
25
26 #ifdef __WIN32__
27 #  include "win32.h"
28 #else
29 #  include <dirent.h>
30 #  include <sys/stat.h>
31 #  include <fnmatch.h>
32 #  include "timestamp.h"
33 #endif
34
35 #include "wimlib_internal.h"
36 #include "dentry.h"
37 #include "lookup_table.h"
38 #include "xml.h"
39 #include "security.h"
40
41 #include <ctype.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <limits.h>
45 #include <string.h>
46
47 #include <unistd.h>
48
49 #ifdef HAVE_ALLOCA_H
50 #  include <alloca.h>
51 #endif
52
53 /*
54  * Adds the dentry tree and security data for a new image to the image metadata
55  * array of the WIMStruct.
56  */
57 int
58 add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
59                     struct wim_security_data *sd)
60 {
61         struct wim_lookup_table_entry *metadata_lte;
62         struct wim_image_metadata *imd;
63         struct wim_image_metadata *new_imd;
64
65         wimlib_assert(root_dentry != NULL);
66
67         DEBUG("Reallocating image metadata array for image_count = %u",
68               w->hdr.image_count + 1);
69         imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
70
71         if (!imd) {
72                 ERROR("Failed to allocate memory for new image metadata array");
73                 goto err;
74         }
75
76         memcpy(imd, w->image_metadata,
77                w->hdr.image_count * sizeof(struct wim_image_metadata));
78
79         metadata_lte = new_lookup_table_entry();
80         if (!metadata_lte)
81                 goto err_free_imd;
82
83         metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
84
85         new_imd = &imd[w->hdr.image_count];
86
87         new_imd->root_dentry    = root_dentry;
88         new_imd->metadata_lte   = metadata_lte;
89         new_imd->security_data  = sd;
90         new_imd->modified       = 1;
91
92         FREE(w->image_metadata);
93         w->image_metadata = imd;
94         w->hdr.image_count++;
95         return 0;
96 err_free_imd:
97         FREE(imd);
98 err:
99         return WIMLIB_ERR_NOMEM;
100
101 }
102
103 #ifndef __WIN32__
104
105 static int
106 unix_capture_regular_file(const char *path,
107                           uint64_t size,
108                           struct wim_inode *inode,
109                           struct wim_lookup_table *lookup_table)
110 {
111         struct wim_lookup_table_entry *lte;
112         u8 hash[SHA1_HASH_SIZE];
113         int ret;
114
115         inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
116
117         /* Empty files do not have to have a lookup table entry. */
118         if (size == 0)
119                 return 0;
120
121         /* For each regular file, we must check to see if the file is in
122          * the lookup table already; if it is, we increment its refcnt;
123          * otherwise, we create a new lookup table entry and insert it.
124          * */
125
126         ret = sha1sum(path, hash);
127         if (ret)
128                 return ret;
129
130         lte = __lookup_resource(lookup_table, hash);
131         if (lte) {
132                 lte->refcnt++;
133                 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
134                       path);
135         } else {
136                 char *file_on_disk = STRDUP(path);
137                 if (!file_on_disk) {
138                         ERROR("Failed to allocate memory for file path");
139                         return WIMLIB_ERR_NOMEM;
140                 }
141                 lte = new_lookup_table_entry();
142                 if (!lte) {
143                         FREE(file_on_disk);
144                         return WIMLIB_ERR_NOMEM;
145                 }
146                 lte->file_on_disk = file_on_disk;
147                 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
148                 lte->resource_entry.original_size = size;
149                 lte->resource_entry.size = size;
150                 copy_hash(lte->hash, hash);
151                 lookup_table_insert(lookup_table, lte);
152         }
153         inode->i_lte = lte;
154         return 0;
155 }
156
157 static int
158 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
159                                  char *path,
160                                  size_t path_len,
161                                  struct wim_lookup_table *lookup_table,
162                                  struct wim_inode_table *inode_table,
163                                  const struct wimlib_capture_config *config,
164                                  int add_image_flags,
165                                  wimlib_progress_func_t progress_func);
166
167 static int
168 unix_capture_directory(struct wim_dentry *dir_dentry,
169                        char *path,
170                        size_t path_len,
171                        struct wim_lookup_table *lookup_table,
172                        struct wim_inode_table *inode_table,
173                        const struct wimlib_capture_config *config,
174                        int add_image_flags,
175                        wimlib_progress_func_t progress_func)
176 {
177
178         DIR *dir;
179         struct dirent entry, *result;
180         struct wim_dentry *child;
181         int ret;
182
183         dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
184         dir = opendir(path);
185         if (!dir) {
186                 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
187                                  path);
188                 return WIMLIB_ERR_OPEN;
189         }
190
191         /* Recurse on directory contents */
192         while (1) {
193                 errno = 0;
194                 ret = readdir_r(dir, &entry, &result);
195                 if (ret != 0) {
196                         ret = WIMLIB_ERR_READ;
197                         ERROR_WITH_ERRNO("Error reading the "
198                                          "directory `%s'",
199                                          path);
200                         break;
201                 }
202                 if (result == NULL)
203                         break;
204                 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
205                       || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
206                                 continue;
207
208                 size_t name_len = strlen(result->d_name);
209
210                 path[path_len] = '/';
211                 memcpy(&path[path_len + 1], result->d_name, name_len + 1);
212                 ret = unix_build_dentry_tree_recursive(&child,
213                                                        path,
214                                                        path_len + 1 + name_len,
215                                                        lookup_table,
216                                                        inode_table,
217                                                        config,
218                                                        add_image_flags,
219                                                        progress_func);
220                 if (ret)
221                         break;
222                 if (child)
223                         dentry_add_child(dir_dentry, child);
224         }
225         closedir(dir);
226         return ret;
227 }
228
229 static int
230 unix_capture_symlink(const char *path,
231                      struct wim_inode *inode,
232                      struct wim_lookup_table *lookup_table)
233 {
234         char deref_name_buf[4096];
235         ssize_t deref_name_len;
236         int ret;
237
238         inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
239         inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
240
241         /* The idea here is to call readlink() to get the UNIX target of
242          * the symbolic link, then turn the target into a reparse point
243          * data buffer that contains a relative or absolute symbolic
244          * link (NOT a junction point or *full* path symbolic link with
245          * drive letter).
246          */
247         deref_name_len = readlink(path, deref_name_buf,
248                                   sizeof(deref_name_buf) - 1);
249         if (deref_name_len >= 0) {
250                 deref_name_buf[deref_name_len] = '\0';
251                 DEBUG("Read symlink `%s'", deref_name_buf);
252                 ret = inode_set_symlink(inode, deref_name_buf,
253                                         lookup_table, NULL);
254                 if (ret == 0) {
255                         /* Unfortunately, Windows seems to have the concept of
256                          * "file" symbolic links as being different from
257                          * "directory" symbolic links...  so
258                          * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
259                          * symbolic link if the *target* of the symbolic link is
260                          * a directory.  */
261                         struct stat stbuf;
262                         if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
263                                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
264                 }
265         } else {
266                 ERROR_WITH_ERRNO("Failed to read target of "
267                                  "symbolic link `%s'", path);
268                 ret = WIMLIB_ERR_READLINK;
269         }
270         return ret;
271 }
272
273 static int
274 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
275                                  char *path,
276                                  size_t path_len,
277                                  struct wim_lookup_table *lookup_table,
278                                  struct wim_inode_table *inode_table,
279                                  const struct wimlib_capture_config *config,
280                                  int add_image_flags,
281                                  wimlib_progress_func_t progress_func)
282 {
283         struct wim_dentry *root = NULL;
284         int ret = 0;
285         struct wim_inode *inode;
286
287         if (exclude_path(path, path_len, config, true)) {
288                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
289                         ERROR("Cannot exclude the root directory from capture");
290                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
291                         goto out;
292                 }
293                 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
294                     && progress_func)
295                 {
296                         union wimlib_progress_info info;
297                         info.scan.cur_path = path;
298                         info.scan.excluded = true;
299                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
300                 }
301                 goto out;
302         }
303
304         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
305             && progress_func)
306         {
307                 union wimlib_progress_info info;
308                 info.scan.cur_path = path;
309                 info.scan.excluded = false;
310                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
311         }
312
313         /* UNIX version of capturing a directory tree */
314         struct stat stbuf;
315         int (*stat_fn)(const char *restrict, struct stat *restrict);
316         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
317                 stat_fn = stat;
318         else
319                 stat_fn = lstat;
320
321         ret = (*stat_fn)(path, &stbuf);
322         if (ret != 0) {
323                 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
324                 goto out;
325         }
326
327         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
328               !S_ISDIR(stbuf.st_mode))
329         {
330                 /* Do a dereference-stat in case the root is a symbolic link.
331                  * This case is allowed, provided that the symbolic link points
332                  * to a directory. */
333                 ret = stat(path, &stbuf);
334                 if (ret != 0) {
335                         ERROR_WITH_ERRNO("Failed to stat `%s'", path);
336                         ret = WIMLIB_ERR_STAT;
337                         goto out;
338                 }
339                 if (!S_ISDIR(stbuf.st_mode)) {
340                         ERROR("`%s' is not a directory", path);
341                         ret = WIMLIB_ERR_NOTDIR;
342                         goto out;
343                 }
344         }
345         if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
346             && !S_ISLNK(stbuf.st_mode)) {
347                 ERROR("`%s' is not a regular file, directory, or symbolic link.",
348                       path);
349                 ret = WIMLIB_ERR_SPECIAL_FILE;
350                 goto out;
351         }
352
353         ret = inode_table_new_dentry(inode_table,
354                                      path_basename_with_len(path, path_len),
355                                      stbuf.st_ino,
356                                      stbuf.st_dev,
357                                      &root);
358         if (ret)
359                 goto out;
360
361         inode = root->d_inode;
362
363         if (inode->i_nlink > 1) /* Already captured this inode? */
364                 goto out;
365
366 #ifdef HAVE_STAT_NANOSECOND_PRECISION
367         inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
368         inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
369         inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
370 #else
371         inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
372         inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
373         inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
374 #endif
375         inode->i_resolved = 1;
376         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
377                 ret = inode_set_unix_data(inode, stbuf.st_uid,
378                                           stbuf.st_gid,
379                                           stbuf.st_mode,
380                                           lookup_table,
381                                           UNIX_DATA_ALL | UNIX_DATA_CREATE);
382                 if (ret)
383                         goto out;
384         }
385         add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
386         if (S_ISREG(stbuf.st_mode))
387                 ret = unix_capture_regular_file(path, stbuf.st_size,
388                                                 inode, lookup_table);
389         else if (S_ISDIR(stbuf.st_mode))
390                 ret = unix_capture_directory(root, path, path_len,
391                                              lookup_table, inode_table, config,
392                                              add_image_flags, progress_func);
393         else
394                 ret = unix_capture_symlink(path, inode, lookup_table);
395 out:
396         if (ret == 0)
397                 *root_ret = root;
398         else
399                 free_dentry_tree(root, lookup_table);
400         return ret;
401 }
402
403 /*
404  * unix_build_dentry_tree():
405  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
406  *      version; no NTFS-specific data is captured).
407  *
408  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
409  *              modified if successful.  Set to NULL if the file or directory was
410  *              excluded from capture.
411  *
412  * @root_disk_path:  The path to the root of the directory tree on disk.
413  *
414  * @lookup_table: The lookup table for the WIM file.  For each file added to the
415  *              dentry tree being built, an entry is added to the lookup table,
416  *              unless an identical stream is already in the lookup table.
417  *              These lookup table entries that are added point to the path of
418  *              the file on disk.
419  *
420  * @sd_set:     Ignored.  (Security data only captured in NTFS mode.)
421  *
422  * @config:
423  *              Configuration for files to be excluded from capture.
424  *
425  * @add_flags:  Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
426  *
427  * @extra_arg:  Ignored
428  *
429  * @return:     0 on success, nonzero on failure.  It is a failure if any of
430  *              the files cannot be `stat'ed, or if any of the needed
431  *              directories cannot be opened or read.  Failure to add the files
432  *              to the WIM may still occur later when trying to actually read
433  *              the on-disk files during a call to wimlib_write() or
434  *              wimlib_overwrite().
435  */
436 static int
437 unix_build_dentry_tree(struct wim_dentry **root_ret,
438                        const char *root_disk_path,
439                        struct wim_lookup_table *lookup_table,
440                        struct wim_inode_table *inode_table,
441                        struct sd_set *sd_set,
442                        const struct wimlib_capture_config *config,
443                        int add_image_flags,
444                        wimlib_progress_func_t progress_func,
445                        void *extra_arg)
446 {
447         char *path_buf;
448         int ret;
449         size_t path_len;
450         size_t path_bufsz;
451
452         path_bufsz = min(32790, PATH_MAX + 1);
453         path_len = strlen(root_disk_path);
454
455         if (path_len >= path_bufsz)
456                 return WIMLIB_ERR_INVALID_PARAM;
457
458         path_buf = MALLOC(path_bufsz);
459         if (!path_buf)
460                 return WIMLIB_ERR_NOMEM;
461         memcpy(path_buf, root_disk_path, path_len + 1);
462         ret = unix_build_dentry_tree_recursive(root_ret,
463                                                path_buf,
464                                                path_len,
465                                                lookup_table,
466                                                inode_table,
467                                                config,
468                                                add_image_flags,
469                                                progress_func);
470         FREE(path_buf);
471         return ret;
472 }
473 #endif /* !__WIN32__ */
474
475 static bool
476 match_pattern(const tchar *path,
477               const tchar *path_basename,
478               const struct wimlib_pattern_list *list)
479 {
480         for (size_t i = 0; i < list->num_pats; i++) {
481
482                 const tchar *pat = list->pats[i];
483                 const tchar *string;
484
485                 if (*pat == T('/')) {
486                         /* Absolute path from root of capture */
487                         string = path;
488                 } else {
489                         if (tstrchr(pat, T('/')))
490                                 /* Relative path from root of capture */
491                                 string = path + 1;
492                         else
493                                 /* A file name pattern */
494                                 string = path_basename;
495                 }
496
497                 /* Warning: on Windows native builds, fnmatch() calls the
498                  * replacement function in win32.c. */
499                 if (fnmatch(pat, string, FNM_PATHNAME | FNM_NOESCAPE
500                                 #ifdef FNM_CASEFOLD
501                                         | FNM_CASEFOLD
502                                 #endif
503                             ) == 0)
504                 {
505                         DEBUG("\"%"TS"\" matches the pattern \"%"TS"\"",
506                               string, pat);
507                         return true;
508                 } else {
509                         DEBUG2("\"%"TS"\" does not match the pattern \"%"TS"\"",
510                                string, pat);
511                 }
512         }
513         return false;
514 }
515
516 /* Return true if the image capture configuration file indicates we should
517  * exclude the filename @path from capture.
518  *
519  * If @exclude_prefix is %true, the part of the path up and including the name
520  * of the directory being captured is not included in the path for matching
521  * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
522  * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
523  * directory.
524  */
525 bool
526 exclude_path(const tchar *path, size_t path_len,
527              const struct wimlib_capture_config *config, bool exclude_prefix)
528 {
529         const tchar *basename = path_basename_with_len(path, path_len);
530         if (exclude_prefix) {
531                 wimlib_assert(path_len >= config->_prefix_num_tchars);
532                 if (!tmemcmp(config->_prefix, path, config->_prefix_num_tchars) &&
533                     path[config->_prefix_num_tchars] == T('/'))
534                 {
535                         path += config->_prefix_num_tchars;
536                 }
537         }
538         return match_pattern(path, basename, &config->exclusion_pats) &&
539                 !match_pattern(path, basename, &config->exclusion_exception_pats);
540
541 }
542
543 /* Strip leading and trailing forward slashes from a string.  Modifies it in
544  * place and returns the stripped string. */
545 static const tchar *
546 canonicalize_target_path(tchar *target_path)
547 {
548         tchar *p;
549         if (target_path == NULL)
550                 return T("");
551         for (;;) {
552                 if (*target_path == T('\0'))
553                         return target_path;
554                 else if (*target_path == T('/'))
555                         target_path++;
556                 else
557                         break;
558         }
559
560         p = tstrchr(target_path, T('\0')) - 1;
561         while (*p == T('/'))
562                 *p-- = T('\0');
563         return target_path;
564 }
565
566 /* Strip leading and trailing slashes from the target paths, and translate all
567  * backslashes in the source and target paths into forward slashes. */
568 static void
569 canonicalize_sources_and_targets(struct wimlib_capture_source *sources,
570                                  size_t num_sources)
571 {
572         while (num_sources--) {
573                 DEBUG("Canonicalizing { source: \"%"TS"\", target=\"%"TS"\"}",
574                       sources->fs_source_path,
575                       sources->wim_target_path);
576
577                 /* The Windows API can handle forward slashes.  Just get rid of
578                  * backslashes to avoid confusing other parts of the library
579                  * code. */
580                 zap_backslashes(sources->fs_source_path);
581                 if (sources->wim_target_path)
582                         zap_backslashes(sources->wim_target_path);
583
584                 sources->wim_target_path =
585                         (tchar*)canonicalize_target_path(sources->wim_target_path);
586                 DEBUG("Canonical target: \"%"TS"\"", sources->wim_target_path);
587                 sources++;
588         }
589 }
590
591 static int
592 capture_source_cmp(const void *p1, const void *p2)
593 {
594         const struct wimlib_capture_source *s1 = p1, *s2 = p2;
595         return tstrcmp(s1->wim_target_path, s2->wim_target_path);
596 }
597
598 /* Sorts the capture sources lexicographically by target path.  This occurs
599  * after leading and trailing forward slashes are stripped.
600  *
601  * One purpose of this is to make sure that target paths that are inside other
602  * target paths are added after the containing target paths. */
603 static void
604 sort_sources(struct wimlib_capture_source *sources, size_t num_sources)
605 {
606         qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
607 }
608
609 static int
610 check_sorted_sources(struct wimlib_capture_source *sources, size_t num_sources,
611                      int add_image_flags)
612 {
613         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
614                 if (num_sources != 1) {
615                         ERROR("Must specify exactly 1 capture source "
616                               "(the NTFS volume) in NTFS mode!");
617                         return WIMLIB_ERR_INVALID_PARAM;
618                 }
619                 if (sources[0].wim_target_path[0] != T('\0')) {
620                         ERROR("In NTFS capture mode the target path inside "
621                               "the image must be the root directory!");
622                         return WIMLIB_ERR_INVALID_PARAM;
623                 }
624         } else if (num_sources != 0) {
625                 /* This code is disabled because the current code
626                  * unconditionally attempts to do overlays.  So, duplicate
627                  * target paths are OK. */
628         #if 0
629                 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
630                         ERROR("Cannot specify root target when using multiple "
631                               "capture sources!");
632                         return WIMLIB_ERR_INVALID_PARAM;
633                 }
634                 for (size_t i = 0; i < num_sources - 1; i++) {
635                         size_t len = strlen(sources[i].wim_target_path);
636                         size_t j = i + 1;
637                         const char *target1 = sources[i].wim_target_path;
638                         do {
639                                 const char *target2 = sources[j].wim_target_path;
640                                 DEBUG("target1=%s, target2=%s",
641                                       target1,target2);
642                                 if (strncmp(target1, target2, len) ||
643                                     target2[len] > '/')
644                                         break;
645                                 if (target2[len] == '/') {
646                                         ERROR("Invalid target `%s': is a prefix of `%s'",
647                                               target1, target2);
648                                         return WIMLIB_ERR_INVALID_PARAM;
649                                 }
650                                 if (target2[len] == '\0') {
651                                         ERROR("Invalid target `%s': is a duplicate of `%s'",
652                                               target1, target2);
653                                         return WIMLIB_ERR_INVALID_PARAM;
654                                 }
655                         } while (++j != num_sources);
656                 }
657         #endif
658         }
659         return 0;
660
661 }
662
663 /* Creates a new directory to place in the WIM image.  This is to create parent
664  * directories that are not part of any target as needed.  */
665 static int
666 new_filler_directory(const tchar *name, struct wim_dentry **dentry_ret)
667 {
668         int ret;
669         struct wim_dentry *dentry;
670
671         DEBUG("Creating filler directory \"%"TS"\"", name);
672         ret = new_dentry_with_inode(name, &dentry);
673         if (ret == 0) {
674                 /* Leave the inode number as 0; this is allowed for non
675                  * hard-linked files. */
676                 dentry->d_inode->i_resolved = 1;
677                 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
678                 *dentry_ret = dentry;
679         }
680         return ret;
681 }
682
683 /* Transfers the children of @branch to @target.  It is an error if @target is
684  * not a directory or if both @branch and @target contain a child dentry with
685  * the same name. */
686 static int
687 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
688 {
689         struct rb_root *rb_root;
690
691         DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
692               branch->file_name, target->file_name);
693
694         if (!dentry_is_directory(target)) {
695                 ERROR("Cannot overlay directory \"%"WS"\" "
696                       "over non-directory", branch->file_name);
697                 return WIMLIB_ERR_INVALID_OVERLAY;
698         }
699
700         rb_root = &branch->d_inode->i_children;
701         while (rb_root->rb_node) { /* While @branch has children... */
702                 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
703                 /* Move @child to the directory @target */
704                 unlink_dentry(child);
705                 if (!dentry_add_child(target, child)) {
706                         /* Revert the change to avoid leaking the directory tree
707                          * rooted at @child */
708                         dentry_add_child(branch, child);
709                         ERROR("Overlay error: file \"%"WS"\" already exists "
710                               "as a child of \"%"WS"\"",
711                               child->file_name, target->file_name);
712                         return WIMLIB_ERR_INVALID_OVERLAY;
713                 }
714         }
715         free_dentry(branch);
716         return 0;
717
718 }
719
720 /* Attach or overlay a branch onto the WIM image.
721  *
722  * @root_p:
723  *      Pointer to the root of the WIM image, or pointer to NULL if it has not
724  *      been created yet.
725  * @branch
726  *      Branch to add.
727  * @target_path:
728  *      Path in the WIM image to add the branch, with leading and trailing
729  *      slashes stripped.
730  */
731 static int
732 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
733               tchar *target_path)
734 {
735         tchar *slash;
736         struct wim_dentry *dentry, *parent, *target;
737         int ret;
738
739         DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
740               branch->file_name, target_path);
741
742         if (*target_path == T('\0')) {
743                 /* Target: root directory */
744                 if (*root_p) {
745                         /* Overlay on existing root */
746                         return do_overlay(*root_p, branch);
747                 } else  {
748                         /* Set as root */
749                         *root_p = branch;
750                         return 0;
751                 }
752         }
753
754         /* Adding a non-root branch.  Create root if it hasn't been created
755          * already. */
756         if (!*root_p) {
757                 ret  = new_filler_directory(T(""), root_p);
758                 if (ret)
759                         return ret;
760         }
761
762         /* Walk the path to the branch, creating filler directories as needed.
763          * */
764         parent = *root_p;
765         while ((slash = tstrchr(target_path, T('/')))) {
766                 *slash = T('\0');
767                 dentry = get_dentry_child_with_name(parent, target_path);
768                 if (!dentry) {
769                         ret = new_filler_directory(target_path, &dentry);
770                         if (ret)
771                                 return ret;
772                         dentry_add_child(parent, dentry);
773                 }
774                 parent = dentry;
775                 target_path = slash;
776                 /* Skip over slashes.  Note: this cannot overrun the length of
777                  * the string because the last character cannot be a slash, as
778                  * trailing slashes were tripped.  */
779                 do {
780                         ++target_path;
781                 } while (*target_path == T('/'));
782         }
783
784         /* If the target path already existed, overlay the branch onto it.
785          * Otherwise, set the branch as the target path. */
786         target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
787                                                     branch->file_name_nbytes);
788         if (target) {
789                 return do_overlay(target, branch);
790         } else {
791                 dentry_add_child(parent, branch);
792                 return 0;
793         }
794 }
795
796 static int
797 canonicalize_pat(tchar **pat_p)
798 {
799         tchar *pat = *pat_p;
800
801         /* Turn all backslashes in the pattern into forward slashes. */
802         zap_backslashes(pat);
803
804         if (*pat != T('/') && *pat != T('\0') && *(pat + 1) == T(':')) {
805                 /* Pattern begins with drive letter */
806                 if (*(pat + 2) != T('/')) {
807                         /* Something like c:file, which is actually a path
808                          * relative to the current working directory on the c:
809                          * drive.  We require paths with drive letters to be
810                          * absolute. */
811                         ERROR("Invalid path \"%"TS"\"; paths including drive letters "
812                               "must be absolute!", pat);
813                         ERROR("Maybe try \"%"TC":/%"TS"\"?",
814                               *pat, pat + 2);
815                         return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
816                 }
817
818                 WARNING("Pattern \"%"TS"\" starts with a drive letter, which is "
819                         "being removed.", pat);
820                 /* Strip the drive letter */
821                 pat += 2;
822                 *pat_p = pat;
823         }
824         return 0;
825 }
826
827 static int
828 canonicalize_pat_list(struct wimlib_pattern_list *pat_list)
829 {
830         int ret = 0;
831         for (size_t i = 0; i < pat_list->num_pats; i++) {
832                 ret = canonicalize_pat(&pat_list->pats[i]);
833                 if (ret)
834                         break;
835         }
836         return ret;
837 }
838
839 static int
840 canonicalize_capture_config(struct wimlib_capture_config *config)
841 {
842         int ret = canonicalize_pat_list(&config->exclusion_pats);
843         if (ret)
844                 return ret;
845         return canonicalize_pat_list(&config->exclusion_exception_pats);
846 }
847
848 WIMLIBAPI int
849 wimlib_add_image_multisource(WIMStruct *w,
850                              struct wimlib_capture_source *sources,
851                              size_t num_sources,
852                              const tchar *name,
853                              struct wimlib_capture_config *config,
854                              int add_image_flags,
855                              wimlib_progress_func_t progress_func)
856 {
857         int (*capture_tree)(struct wim_dentry **,
858                             const tchar *,
859                             struct wim_lookup_table *,
860                             struct wim_inode_table *,
861                             struct sd_set *,
862                             const struct wimlib_capture_config *,
863                             int,
864                             wimlib_progress_func_t,
865                             void *);
866         void *extra_arg;
867         struct wim_dentry *root_dentry;
868         struct wim_dentry *branch;
869         struct wim_security_data *sd;
870         struct wim_image_metadata *imd;
871         struct wim_inode_table inode_table;
872         int ret;
873         struct sd_set sd_set;
874
875         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
876 #ifdef WITH_NTFS_3G
877                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
878                         ERROR("Cannot dereference files when capturing directly from NTFS");
879                         return WIMLIB_ERR_INVALID_PARAM;
880                 }
881                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
882                         ERROR("Capturing UNIX owner and mode not supported "
883                               "when capturing directly from NTFS");
884                         return WIMLIB_ERR_INVALID_PARAM;
885                 }
886                 capture_tree = build_dentry_tree_ntfs;
887                 extra_arg = &w->ntfs_vol;
888 #else
889                 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
890                       "        cannot capture a WIM image directly from a NTFS volume!");
891                 return WIMLIB_ERR_UNSUPPORTED;
892 #endif
893         } else {
894         #ifdef __WIN32__
895                 capture_tree = win32_build_dentry_tree;
896         #else
897                 capture_tree = unix_build_dentry_tree;
898         #endif
899                 extra_arg = NULL;
900         }
901
902 #ifdef __WIN32__
903         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
904                 ERROR("Capturing UNIX-specific data is not supported on Windows");
905                 return WIMLIB_ERR_INVALID_PARAM;
906         }
907         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
908                 ERROR("Dereferencing symbolic links is not supported on Windows");
909                 return WIMLIB_ERR_INVALID_PARAM;
910         }
911 #endif
912
913         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
914                 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
915
916         if (!name || !*name) {
917                 ERROR("Must specify a non-empty string for the image name");
918                 return WIMLIB_ERR_INVALID_PARAM;
919         }
920
921         if (w->hdr.total_parts != 1) {
922                 ERROR("Cannot add an image to a split WIM");
923                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
924         }
925
926         if (wimlib_image_name_in_use(w, name)) {
927                 ERROR("There is already an image named \"%"TS"\" in the WIM!",
928                       name);
929                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
930         }
931
932         if (!config) {
933                 DEBUG("Capture config not provided; using empty config");
934                 config = alloca(sizeof(*config));
935                 memset(config, 0, sizeof(*config));
936         }
937
938         ret = canonicalize_capture_config(config);
939         if (ret)
940                 goto out;
941
942         ret = init_inode_table(&inode_table, 9001);
943         if (ret)
944                 goto out;
945
946         DEBUG("Allocating security data");
947         sd = CALLOC(1, sizeof(struct wim_security_data));
948         if (!sd) {
949                 ret = WIMLIB_ERR_NOMEM;
950                 goto out_destroy_inode_table;
951         }
952         sd->total_length = 8;
953         sd->refcnt = 1;
954
955         sd_set.sd = sd;
956         sd_set.rb_root.rb_node = NULL;
957
958
959         DEBUG("Using %zu capture sources", num_sources);
960         canonicalize_sources_and_targets(sources, num_sources);
961         sort_sources(sources, num_sources);
962         ret = check_sorted_sources(sources, num_sources, add_image_flags);
963         if (ret) {
964                 ret = WIMLIB_ERR_INVALID_PARAM;
965                 goto out_free_security_data;
966         }
967
968
969         DEBUG("Building dentry tree.");
970         root_dentry = NULL;
971
972         for (size_t i = 0; i < num_sources; i++) {
973                 int flags;
974                 union wimlib_progress_info progress;
975
976                 DEBUG("Building dentry tree for source %zu of %zu "
977                       "(\"%"TS"\" => \"%"TS"\")", i + 1, num_sources,
978                       sources[i].fs_source_path,
979                       sources[i].wim_target_path);
980                 if (progress_func) {
981                         memset(&progress, 0, sizeof(progress));
982                         progress.scan.source = sources[i].fs_source_path;
983                         progress.scan.wim_target_path = sources[i].wim_target_path;
984                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
985                 }
986                 config->_prefix = sources[i].fs_source_path;
987                 config->_prefix_num_tchars = tstrlen(sources[i].fs_source_path);
988                 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
989                 if (!*sources[i].wim_target_path)
990                         flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
991                 ret = (*capture_tree)(&branch,
992                                       sources[i].fs_source_path,
993                                       w->lookup_table,
994                                       &inode_table,
995                                       &sd_set,
996                                       config,
997                                       flags,
998                                       progress_func, extra_arg);
999                 if (ret) {
1000                         ERROR("Failed to build dentry tree for `%"TS"'",
1001                               sources[i].fs_source_path);
1002                         goto out_free_dentry_tree;
1003                 }
1004                 if (branch) {
1005                         /* Use the target name, not the source name, for
1006                          * the root of each branch from a capture
1007                          * source.  (This will also set the root dentry
1008                          * of the entire image to be unnamed.) */
1009                         ret = set_dentry_name(branch,
1010                                               path_basename(sources[i].wim_target_path));
1011                         if (ret)
1012                                 goto out_free_branch;
1013
1014                         ret = attach_branch(&root_dentry, branch,
1015                                             sources[i].wim_target_path);
1016                         if (ret)
1017                                 goto out_free_branch;
1018                 }
1019                 if (progress_func)
1020                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1021         }
1022
1023         if (root_dentry == NULL) {
1024                 ret = new_filler_directory(T(""), &root_dentry);
1025                 if (ret)
1026                         goto out_free_dentry_tree;
1027         }
1028
1029         DEBUG("Calculating full paths of dentries.");
1030         ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1031         if (ret)
1032                 goto out_free_dentry_tree;
1033
1034         ret = add_new_dentry_tree(w, root_dentry, sd);
1035         if (ret)
1036                 goto out_free_dentry_tree;
1037
1038         imd = &w->image_metadata[w->hdr.image_count - 1];
1039
1040         DEBUG("Assigning hard link group IDs");
1041         inode_table_prepare_inode_list(&inode_table, &imd->inode_list);
1042
1043         ret = xml_add_image(w, name);
1044         if (ret)
1045                 goto out_destroy_imd;
1046
1047         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1048                 wimlib_set_boot_idx(w, w->hdr.image_count);
1049         ret = 0;
1050         goto out_destroy_inode_table;
1051 out_destroy_imd:
1052         destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1053                                w->lookup_table);
1054         w->hdr.image_count--;
1055         goto out_destroy_inode_table;
1056 out_free_branch:
1057         free_dentry_tree(branch, w->lookup_table);
1058 out_free_dentry_tree:
1059         free_dentry_tree(root_dentry, w->lookup_table);
1060 out_free_security_data:
1061         free_security_data(sd);
1062 out_destroy_inode_table:
1063         destroy_inode_table(&inode_table);
1064         destroy_sd_set(&sd_set);
1065 out:
1066         return ret;
1067 }
1068
1069 WIMLIBAPI int
1070 wimlib_add_image(WIMStruct *w,
1071                  const tchar *source,
1072                  const tchar *name,
1073                  struct wimlib_capture_config *config,
1074                  int add_image_flags,
1075                  wimlib_progress_func_t progress_func)
1076 {
1077         if (!source || !*source)
1078                 return WIMLIB_ERR_INVALID_PARAM;
1079
1080         tchar *fs_source_path = TSTRDUP(source);
1081         int ret;
1082         struct wimlib_capture_source capture_src = {
1083                 .fs_source_path = fs_source_path,
1084                 .wim_target_path = NULL,
1085                 .reserved = 0,
1086         };
1087         ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1088                                            config, add_image_flags,
1089                                            progress_func);
1090         FREE(fs_source_path);
1091         return ret;
1092 }