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