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