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