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