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