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