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