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