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