Minor cleanups
[wimlib] / src / add_image.c
1 /*
2  * add_image.c
3  */
4
5 /*
6  * Copyright (C) 2012 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 "wimlib_internal.h"
25 #include "dentry.h"
26 #include "timestamp.h"
27 #include "lookup_table.h"
28 #include "xml.h"
29 #include <string.h>
30 #include <fnmatch.h>
31 #include <ctype.h>
32 #include <sys/stat.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #include <unistd.h>
36
37 /** Private flag: Used to mark that we currently adding the root directory of
38  * the WIM image. */
39 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
40
41 /*
42  * Adds an image (given by its dentry tree) to the image metadata array of a WIM
43  * file, adds an entry to the lookup table for the image metadata, updates the
44  * image count in the header, and selects the new image.
45  *
46  * Does not update the XML data.
47  *
48  * On failure, WIMLIB_ERR_NOMEM is returned and no changes are made.  Otherwise,
49  * 0 is returned and the image metadata array of @w is modified.
50  *
51  * @w:            The WIMStruct for the WIM file.
52  * @root_dentry:  The root of the directory tree for the image.
53  * @sd:           The security data for the image.
54  */
55 int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry,
56                                struct wim_security_data *sd)
57 {
58         struct lookup_table_entry *metadata_lte;
59         struct image_metadata *imd;
60         struct image_metadata *new_imd;
61         int ret;
62
63         wimlib_assert(root_dentry != NULL);
64
65         DEBUG("Reallocating image metadata array for image_count = %u",
66               w->hdr.image_count + 1);
67         imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
68
69         if (!imd) {
70                 ERROR("Failed to allocate memory for new image metadata array");
71                 goto err;
72         }
73
74         memcpy(imd, w->image_metadata,
75                w->hdr.image_count * sizeof(struct image_metadata));
76
77         metadata_lte = new_lookup_table_entry();
78         if (!metadata_lte)
79                 goto err_free_imd;
80
81         metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
82         random_hash(metadata_lte->hash);
83         lookup_table_insert(w->lookup_table, metadata_lte);
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       = true;
91
92         FREE(w->image_metadata);
93         w->image_metadata       = imd;
94         w->hdr.image_count++;
95
96         /* Change the current image to the new one.  There should not be any
97          * ways for this to fail, since the image is valid and the dentry tree
98          * is already in memory. */
99         ret = select_wim_image(w, w->hdr.image_count);
100         wimlib_assert(ret == 0);
101         return ret;
102 err_free_imd:
103         FREE(imd);
104 err:
105         return WIMLIB_ERR_NOMEM;
106
107 }
108
109
110 /*
111  * Recursively builds a dentry tree from a directory tree on disk, outside the
112  * WIM file.
113  *
114  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
115  *              modified if successful.  NULL if the file or directory was
116  *              excluded from capture.
117  *
118  * @root_disk_path:  The path to the root of the directory tree on disk.
119  *
120  * @lookup_table: The lookup table for the WIM file.  For each file added to the
121  *              dentry tree being built, an entry is added to the lookup table,
122  *              unless an identical stream is already in the lookup table.
123  *              These lookup table entries that are added point to the path of
124  *              the file on disk.
125  *
126  * @sd:         Ignored.  (Security data only captured in NTFS mode.)
127  *
128  * @capture_config:
129  *              Configuration for files to be excluded from capture.
130  *
131  * @add_flags:  Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
132  *
133  * @extra_arg:  Ignored. (Only used in NTFS mode.)
134  *
135  * @return:     0 on success, nonzero on failure.  It is a failure if any of
136  *              the files cannot be `stat'ed, or if any of the needed
137  *              directories cannot be opened or read.  Failure to add the files
138  *              to the WIM may still occur later when trying to actually read
139  *              the on-disk files during a call to wimlib_write() or
140  *              wimlib_overwrite().
141  */
142 static int build_dentry_tree(struct dentry **root_ret,
143                              const char *root_disk_path,
144                              struct lookup_table *lookup_table,
145                              struct wim_security_data *sd,
146                              const struct capture_config *config,
147                              int add_image_flags,
148                              wimlib_progress_func_t progress_func,
149                              void *extra_arg)
150 {
151         struct stat root_stbuf;
152         int ret = 0;
153         int (*stat_fn)(const char *restrict, struct stat *restrict);
154         struct dentry *root;
155         const char *filename;
156         struct inode *inode;
157
158         if (exclude_path(root_disk_path, config, true)) {
159                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
160                         ERROR("Cannot exclude the root directory from capture");
161                         return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
162                 }
163                 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
164                     && progress_func)
165                 {
166                         union wimlib_progress_info info;
167                         info.scan.cur_path = root_disk_path;
168                         info.scan.excluded = true;
169                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
170                 }
171                 *root_ret = NULL;
172                 return 0;
173         }
174
175         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
176             && progress_func)
177         {
178                 union wimlib_progress_info info;
179                 info.scan.cur_path = root_disk_path;
180                 info.scan.excluded = false;
181                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
182         }
183
184         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
185                 stat_fn = stat;
186         else
187                 stat_fn = lstat;
188
189         ret = (*stat_fn)(root_disk_path, &root_stbuf);
190         if (ret != 0) {
191                 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
192                 return WIMLIB_ERR_STAT;
193         }
194
195         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
196               !S_ISDIR(root_stbuf.st_mode))
197         {
198                 /* Do a dereference-stat in case the root is a symbolic link.
199                  * This case is allowed, provided that the symbolic link points
200                  * to a directory. */
201                 ret = stat(root_disk_path, &root_stbuf);
202                 if (ret != 0) {
203                         ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
204                         return WIMLIB_ERR_STAT;
205                 }
206                 if (!S_ISDIR(root_stbuf.st_mode)) {
207                         ERROR("`%s' is not a directory", root_disk_path);
208                         return WIMLIB_ERR_NOTDIR;
209                 }
210         }
211         if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
212             && !S_ISLNK(root_stbuf.st_mode)) {
213                 ERROR("`%s' is not a regular file, directory, or symbolic link.",
214                       root_disk_path);
215                 return WIMLIB_ERR_SPECIAL_FILE;
216         }
217
218         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT)
219                 filename = "";
220         else
221                 filename = path_basename(root_disk_path);
222
223         root = new_dentry_with_timeless_inode(filename);
224         if (!root)
225                 return WIMLIB_ERR_NOMEM;
226
227         inode = root->d_inode;
228
229 #ifdef HAVE_STAT_NANOSECOND_PRECISION
230         inode->creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
231         inode->last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
232         inode->last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
233 #else
234         inode->creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
235         inode->last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
236         inode->last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
237 #endif
238         if (sizeof(ino_t) >= 8)
239                 inode->ino = (u64)root_stbuf.st_ino;
240         else
241                 inode->ino = (u64)root_stbuf.st_ino |
242                                    ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
243
244         add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
245         inode->resolved = 1;
246
247         if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
248
249                 struct lookup_table_entry *lte;
250                 u8 hash[SHA1_HASH_SIZE];
251
252                 inode->attributes = FILE_ATTRIBUTE_NORMAL;
253
254                 /* Empty files do not have to have a lookup table entry. */
255                 if (root_stbuf.st_size == 0)
256                         goto out;
257
258                 /* For each regular file, we must check to see if the file is in
259                  * the lookup table already; if it is, we increment its refcnt;
260                  * otherwise, we create a new lookup table entry and insert it.
261                  * */
262
263                 ret = sha1sum(root_disk_path, hash);
264                 if (ret != 0)
265                         goto out;
266
267                 lte = __lookup_resource(lookup_table, hash);
268                 if (lte) {
269                         lte->refcnt++;
270                         DEBUG("Add lte reference %u for `%s'", lte->refcnt,
271                               root_disk_path);
272                 } else {
273                         char *file_on_disk = STRDUP(root_disk_path);
274                         if (!file_on_disk) {
275                                 ERROR("Failed to allocate memory for file path");
276                                 ret = WIMLIB_ERR_NOMEM;
277                                 goto out;
278                         }
279                         lte = new_lookup_table_entry();
280                         if (!lte) {
281                                 FREE(file_on_disk);
282                                 ret = WIMLIB_ERR_NOMEM;
283                                 goto out;
284                         }
285                         lte->file_on_disk = file_on_disk;
286                         lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
287                         lte->resource_entry.original_size = root_stbuf.st_size;
288                         lte->resource_entry.size = root_stbuf.st_size;
289                         copy_hash(lte->hash, hash);
290                         lookup_table_insert(lookup_table, lte);
291                 }
292                 root->d_inode->lte = lte;
293         } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
294
295                 inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
296
297                 DIR *dir;
298                 struct dirent entry, *result;
299                 struct dentry *child;
300
301                 dir = opendir(root_disk_path);
302                 if (!dir) {
303                         ERROR_WITH_ERRNO("Failed to open the directory `%s'",
304                                          root_disk_path);
305                         ret = WIMLIB_ERR_OPEN;
306                         goto out;
307                 }
308
309                 /* Buffer for names of files in directory. */
310                 size_t len = strlen(root_disk_path);
311                 char name[len + 1 + FILENAME_MAX + 1];
312                 memcpy(name, root_disk_path, len);
313                 name[len] = '/';
314
315                 /* Create a dentry for each entry in the directory on disk, and recurse
316                  * to any subdirectories. */
317                 while (1) {
318                         errno = 0;
319                         ret = readdir_r(dir, &entry, &result);
320                         if (ret != 0) {
321                                 ret = WIMLIB_ERR_READ;
322                                 ERROR_WITH_ERRNO("Error reading the "
323                                                  "directory `%s'",
324                                                  root_disk_path);
325                                 break;
326                         }
327                         if (result == NULL)
328                                 break;
329                         if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
330                               || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
331                                         continue;
332                         strcpy(name + len + 1, result->d_name);
333                         ret = build_dentry_tree(&child, name, lookup_table,
334                                                 NULL, config, add_image_flags,
335                                                 progress_func, NULL);
336                         if (ret != 0)
337                                 break;
338                         if (child)
339                                 dentry_add_child(root, child);
340                 }
341                 closedir(dir);
342         } else { /* Archiving a symbolic link */
343                 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
344                 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
345
346                 /* The idea here is to call readlink() to get the UNIX target of
347                  * the symbolic link, then turn the target into a reparse point
348                  * data buffer that contains a relative or absolute symbolic
349                  * link (NOT a junction point or *full* path symbolic link with
350                  * drive letter).
351                  */
352
353                 char deref_name_buf[4096];
354                 ssize_t deref_name_len;
355
356                 deref_name_len = readlink(root_disk_path, deref_name_buf,
357                                           sizeof(deref_name_buf) - 1);
358                 if (deref_name_len >= 0) {
359                         deref_name_buf[deref_name_len] = '\0';
360                         DEBUG("Read symlink `%s'", deref_name_buf);
361                         ret = inode_set_symlink(root->d_inode, deref_name_buf,
362                                                 lookup_table, NULL);
363                         if (ret == 0) {
364                                 /*
365                                  * Unfortunately, Windows seems to have the
366                                  * concept of "file" symbolic links as being
367                                  * different from "directory" symbolic links...
368                                  * so FILE_ATTRIBUTE_DIRECTORY needs to be set
369                                  * on the symbolic link if the *target* of the
370                                  * symbolic link is a directory.
371                                  */
372                                 struct stat stbuf;
373                                 if (stat(root_disk_path, &stbuf) == 0 &&
374                                     S_ISDIR(stbuf.st_mode))
375                                 {
376                                         inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
377                                 }
378                         }
379                 } else {
380                         ERROR_WITH_ERRNO("Failed to read target of "
381                                          "symbolic link `%s'", root_disk_path);
382                         ret = WIMLIB_ERR_READLINK;
383                 }
384         }
385 out:
386         if (ret == 0)
387                 *root_ret = root;
388         else
389                 free_dentry_tree(root, lookup_table);
390         return ret;
391 }
392
393
394 enum pattern_type {
395         NONE = 0,
396         EXCLUSION_LIST,
397         EXCLUSION_EXCEPTION,
398         COMPRESSION_EXCLUSION_LIST,
399         ALIGNMENT_LIST,
400 };
401
402 /* Default capture configuration file when none is specified. */
403 static const char *default_config =
404 "[ExclusionList]\n"
405 "\\$ntfs.log\n"
406 "\\hiberfil.sys\n"
407 "\\pagefile.sys\n"
408 "\\System Volume Information\n"
409 "\\RECYCLER\n"
410 "\\Windows\\CSC\n"
411 "\n"
412 "[CompressionExclusionList]\n"
413 "*.mp3\n"
414 "*.zip\n"
415 "*.cab\n"
416 "\\WINDOWS\\inf\\*.pnf\n";
417
418 static void destroy_pattern_list(struct pattern_list *list)
419 {
420         FREE(list->pats);
421 }
422
423 static void destroy_capture_config(struct capture_config *config)
424 {
425         destroy_pattern_list(&config->exclusion_list);
426         destroy_pattern_list(&config->exclusion_exception);
427         destroy_pattern_list(&config->compression_exclusion_list);
428         destroy_pattern_list(&config->alignment_list);
429         FREE(config->config_str);
430         FREE(config->prefix);
431         memset(config, 0, sizeof(*config));
432 }
433
434 static int pattern_list_add_pattern(struct pattern_list *list,
435                                     const char *pattern)
436 {
437         const char **pats;
438         if (list->num_pats >= list->num_allocated_pats) {
439                 pats = REALLOC(list->pats,
440                                sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
441                 if (!pats)
442                         return WIMLIB_ERR_NOMEM;
443                 list->num_allocated_pats += 8;
444                 list->pats = pats;
445         }
446         list->pats[list->num_pats++] = pattern;
447         return 0;
448 }
449
450 /* Parses the contents of the image capture configuration file and fills in a
451  * `struct capture_config'. */
452 static int init_capture_config(const char *_config_str, size_t config_len,
453                                const char *_prefix, struct capture_config *config)
454 {
455         char *config_str;
456         char *prefix;
457         char *p;
458         char *eol;
459         char *next_p;
460         size_t bytes_remaining;
461         enum pattern_type type = NONE;
462         int ret;
463         unsigned long line_no = 0;
464
465         DEBUG("config_len = %zu", config_len);
466         bytes_remaining = config_len;
467         memset(config, 0, sizeof(*config));
468         config_str = MALLOC(config_len);
469         if (!config_str) {
470                 ERROR("Could not duplicate capture config string");
471                 return WIMLIB_ERR_NOMEM;
472         }
473         prefix = STRDUP(_prefix);
474         if (!prefix) {
475                 FREE(config_str);
476                 return WIMLIB_ERR_NOMEM;
477         }
478
479         memcpy(config_str, _config_str, config_len);
480         next_p = config_str;
481         config->config_str = config_str;
482         config->prefix = prefix;
483         config->prefix_len = strlen(prefix);
484         while (bytes_remaining) {
485                 line_no++;
486                 p = next_p;
487                 eol = memchr(p, '\n', bytes_remaining);
488                 if (!eol) {
489                         ERROR("Expected end-of-line in capture config file on "
490                               "line %lu", line_no);
491                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
492                         goto out_destroy;
493                 }
494
495                 next_p = eol + 1;
496                 bytes_remaining -= (next_p - p);
497                 if (eol == p)
498                         continue;
499
500                 if (*(eol - 1) == '\r')
501                         eol--;
502                 *eol = '\0';
503
504                 /* Translate backslash to forward slash */
505                 for (char *pp = p; pp != eol; pp++)
506                         if (*pp == '\\')
507                                 *pp = '/';
508
509                 /* Remove drive letter */
510                 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
511                         p += 2;
512
513                 ret = 0;
514                 if (strcmp(p, "[ExclusionList]") == 0)
515                         type = EXCLUSION_LIST;
516                 else if (strcmp(p, "[ExclusionException]") == 0)
517                         type = EXCLUSION_EXCEPTION;
518                 else if (strcmp(p, "[CompressionExclusionList]") == 0)
519                         type = COMPRESSION_EXCLUSION_LIST;
520                 else if (strcmp(p, "[AlignmentList]") == 0)
521                         type = ALIGNMENT_LIST;
522                 else if (p[0] == '[' && strrchr(p, ']')) {
523                         ERROR("Unknown capture configuration section `%s'", p);
524                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
525                 } else switch (type) {
526                 case EXCLUSION_LIST:
527                         DEBUG("Adding pattern \"%s\" to exclusion list", p);
528                         ret = pattern_list_add_pattern(&config->exclusion_list, p);
529                         break;
530                 case EXCLUSION_EXCEPTION:
531                         DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
532                         ret = pattern_list_add_pattern(&config->exclusion_exception, p);
533                         break;
534                 case COMPRESSION_EXCLUSION_LIST:
535                         DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
536                         ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
537                         break;
538                 case ALIGNMENT_LIST:
539                         DEBUG("Adding pattern \"%s\" to alignment list", p);
540                         ret = pattern_list_add_pattern(&config->alignment_list, p);
541                         break;
542                 default:
543                         ERROR("Line %lu of capture configuration is not "
544                               "in a block (such as [ExclusionList])",
545                               line_no);
546                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
547                         break;
548                 }
549                 if (ret != 0)
550                         goto out_destroy;
551         }
552         return 0;
553 out_destroy:
554         destroy_capture_config(config);
555         return ret;
556 }
557
558 static bool match_pattern(const char *path, const char *path_basename,
559                           const struct pattern_list *list)
560 {
561         for (size_t i = 0; i < list->num_pats; i++) {
562                 const char *pat = list->pats[i];
563                 const char *string;
564                 if (pat[0] == '/')
565                         /* Absolute path from root of capture */
566                         string = path;
567                 else {
568                         if (strchr(pat, '/'))
569                                 /* Relative path from root of capture */
570                                 string = path + 1;
571                         else
572                                 /* A file name pattern */
573                                 string = path_basename;
574                 }
575                 if (fnmatch(pat, string, FNM_PATHNAME
576                         #ifdef FNM_CASEFOLD
577                                         | FNM_CASEFOLD
578                         #endif
579                         ) == 0)
580                 {
581                         DEBUG("`%s' matches the pattern \"%s\"",
582                               string, pat);
583                         return true;
584                 }
585         }
586         return false;
587 }
588
589 static void print_pattern_list(const struct pattern_list *list)
590 {
591         for (size_t i = 0; i < list->num_pats; i++)
592                 printf("    %s\n", list->pats[i]);
593 }
594
595 static void print_capture_config(const struct capture_config *config)
596 {
597         if (config->exclusion_list.num_pats) {
598                 puts("Files or folders excluded from image capture:");
599                 print_pattern_list(&config->exclusion_list);
600                 putchar('\n');
601         }
602 }
603
604 /* Return true if the image capture configuration file indicates we should
605  * exclude the filename @path from capture.
606  *
607  * If @exclude_prefix is %true, the part of the path up and including the name
608  * of the directory being captured is not included in the path for matching
609  * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
610  * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
611  * directory.
612  */
613 bool exclude_path(const char *path, const struct capture_config *config,
614                   bool exclude_prefix)
615 {
616         const char *basename = path_basename(path);
617         if (exclude_prefix) {
618                 wimlib_assert(strlen(path) >= config->prefix_len);
619                 if (memcmp(config->prefix, path, config->prefix_len) == 0
620                      && path[config->prefix_len] == '/')
621                         path += config->prefix_len;
622         }
623         return match_pattern(path, basename, &config->exclusion_list) &&
624                 !match_pattern(path, basename, &config->exclusion_exception);
625
626 }
627
628 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
629                                const char *name, const char *config_str,
630                                size_t config_len, int add_image_flags,
631                                wimlib_progress_func_t progress_func)
632 {
633         int (*capture_tree)(struct dentry **, const char *,
634                             struct lookup_table *,
635                             struct wim_security_data *,
636                             const struct capture_config *,
637                             int, wimlib_progress_func_t, void *);
638         void *extra_arg;
639
640         struct dentry *root_dentry = NULL;
641         struct wim_security_data *sd;
642         struct capture_config config;
643         struct inode_table inode_tab;
644         struct hlist_head inode_list;
645         int ret;
646
647         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
648 #ifdef WITH_NTFS_3G
649                 if (add_image_flags & (WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)) {
650                         ERROR("Cannot dereference files when capturing directly from NTFS");
651                         return WIMLIB_ERR_INVALID_PARAM;
652                 }
653                 capture_tree = build_dentry_tree_ntfs;
654                 extra_arg = &w->ntfs_vol;
655 #else
656                 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
657                       "        cannot capture a WIM image directly from a NTFS volume!");
658                 return WIMLIB_ERR_UNSUPPORTED;
659 #endif
660         } else {
661                 capture_tree = build_dentry_tree;
662                 extra_arg = NULL;
663         }
664
665         DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", source);
666
667         if (!name || !*name) {
668                 ERROR("Must specify a non-empty string for the image name");
669                 return WIMLIB_ERR_INVALID_PARAM;
670         }
671         if (!source || !*source) {
672                 ERROR("Must specify the name of a directory or NTFS volume");
673                 return WIMLIB_ERR_INVALID_PARAM;
674         }
675
676         if (w->hdr.total_parts != 1) {
677                 ERROR("Cannot add an image to a split WIM");
678                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
679         }
680
681         if (wimlib_image_name_in_use(w, name)) {
682                 ERROR("There is already an image named \"%s\" in `%s'",
683                       name, w->filename);
684                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
685         }
686
687         DEBUG("Initializing capture configuration");
688         if (!config_str) {
689                 DEBUG("Using default capture configuration");
690                 config_str = default_config;
691                 config_len = strlen(default_config);
692         }
693         ret = init_capture_config(config_str, config_len, source, &config);
694         if (ret != 0)
695                 return ret;
696         print_capture_config(&config);
697
698         DEBUG("Allocating security data");
699
700         sd = CALLOC(1, sizeof(struct wim_security_data));
701         if (!sd) {
702                 ret = WIMLIB_ERR_NOMEM;
703                 goto out_destroy_config;
704         }
705         sd->total_length = 8;
706         sd->refcnt = 1;
707
708         if (progress_func) {
709                 union wimlib_progress_info progress;
710                 progress.scan.source = source;
711                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
712         }
713
714         DEBUG("Building dentry tree.");
715         ret = (*capture_tree)(&root_dentry, source, w->lookup_table, sd,
716                               &config, add_image_flags | WIMLIB_ADD_IMAGE_FLAG_ROOT,
717                               progress_func, extra_arg);
718         destroy_capture_config(&config);
719
720         if (ret != 0) {
721                 ERROR("Failed to build dentry tree for `%s'", source);
722                 goto out_free_security_data;
723         }
724
725         if (progress_func) {
726                 union wimlib_progress_info progress;
727                 progress.scan.source = source;
728                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
729         }
730
731         DEBUG("Calculating full paths of dentries.");
732         ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
733         if (ret != 0)
734                 goto out_free_dentry_tree;
735
736         ret = add_new_dentry_tree(w, root_dentry, sd);
737         if (ret != 0)
738                 goto out_free_dentry_tree;
739
740         DEBUG("Inserting dentries into inode table");
741         ret = init_inode_table(&inode_tab, 9001);
742         if (ret != 0)
743                 goto out_destroy_imd;
744
745         for_dentry_in_tree(root_dentry, inode_table_insert, &inode_tab);
746
747         DEBUG("Cleaning up the hard link groups");
748         ret = fix_inodes(&inode_tab, &inode_list);
749         destroy_inode_table(&inode_tab);
750         if (ret != 0)
751                 goto out_destroy_imd;
752
753         DEBUG("Assigning hard link group IDs");
754         assign_inode_numbers(&inode_list);
755         w->image_metadata[w->hdr.image_count - 1].inode_list = inode_list;
756
757         ret = xml_add_image(w, name);
758         if (ret != 0)
759                 goto out_destroy_imd;
760
761         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
762                 wimlib_set_boot_idx(w, w->hdr.image_count);
763         return 0;
764 out_destroy_imd:
765         destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
766                                w->lookup_table);
767         w->hdr.image_count--;
768         return ret;
769 out_free_dentry_tree:
770         free_dentry_tree(root_dentry, w->lookup_table);
771 out_free_security_data:
772         free_security_data(sd);
773 out_destroy_config:
774         destroy_capture_config(&config);
775         return ret;
776 }