Win32 capture
[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
25 #if defined(__CYGWIN__) || defined(__WIN32__)
26 #       include <windows.h>
27 #       include <ntdef.h>
28 #       include <wchar.h>
29 #       include <sys/cygwin.h>
30 #       include <fcntl.h>
31 #       ifdef ERROR
32 #               undef ERROR
33 #       endif
34 #       include "security.h"
35 #endif
36
37 #include "wimlib_internal.h"
38 #include "dentry.h"
39 #include "timestamp.h"
40 #include "lookup_table.h"
41 #include "xml.h"
42 #include <string.h>
43 #include <fnmatch.h>
44 #include <stdlib.h>
45 #include <ctype.h>
46 #include <sys/stat.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <unistd.h>
50
51 #if defined(__CYGWIN__) || defined(__WIN32__)
52 /*#define ERROR_WIN32_SAFE(format, ...)         \*/
53 /*{(                                            \*/
54           /*DWORD err = GetLastError();         \*/
55         /*ERROR(format, ##__VA_ARGS__);         \*/
56         /*SetLastError(err);                    \*/
57 /*)}*/
58 #define DEBUG_WIN32_SAFE(format, ...)           \
59 ({                                              \
60         DWORD err = GetLastError();             \
61         DEBUG(format, ##__VA_ARGS__);           \
62         SetLastError(err);                      \
63 })
64 #endif
65
66 #define WIMLIB_ADD_IMAGE_FLAG_ROOT      0x80000000
67 #define WIMLIB_ADD_IMAGE_FLAG_SOURCE    0x40000000
68
69 /*
70  * Adds the dentry tree and security data for a new image to the image metadata
71  * array of the WIMStruct.
72  */
73 int add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
74                         struct wim_security_data *sd)
75 {
76         struct wim_lookup_table_entry *metadata_lte;
77         struct wim_image_metadata *imd;
78         struct wim_image_metadata *new_imd;
79
80         wimlib_assert(root_dentry != NULL);
81
82         DEBUG("Reallocating image metadata array for image_count = %u",
83               w->hdr.image_count + 1);
84         imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
85
86         if (!imd) {
87                 ERROR("Failed to allocate memory for new image metadata array");
88                 goto err;
89         }
90
91         memcpy(imd, w->image_metadata,
92                w->hdr.image_count * sizeof(struct wim_image_metadata));
93
94         metadata_lte = new_lookup_table_entry();
95         if (!metadata_lte)
96                 goto err_free_imd;
97
98         metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
99         random_hash(metadata_lte->hash);
100         lookup_table_insert(w->lookup_table, metadata_lte);
101
102         new_imd = &imd[w->hdr.image_count];
103
104         new_imd->root_dentry    = root_dentry;
105         new_imd->metadata_lte   = metadata_lte;
106         new_imd->security_data  = sd;
107         new_imd->modified       = 1;
108
109         FREE(w->image_metadata);
110         w->image_metadata = imd;
111         w->hdr.image_count++;
112         return 0;
113 err_free_imd:
114         FREE(imd);
115 err:
116         return WIMLIB_ERR_NOMEM;
117
118 }
119
120 #if defined(__CYGWIN__) || defined(__WIN32__)
121 static u64 FILETIME_to_u64(const FILETIME *ft)
122 {
123         return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
124 }
125
126 #ifdef ENABLE_ERROR_MESSAGES
127 static void win32_error(DWORD err_code)
128 {
129         char *buffer;
130         DWORD nchars;
131         nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
132                                 NULL, err_code, 0,
133                                 (char*)&buffer, 0, NULL);
134         if (nchars == 0) {
135                 ERROR("Error printing error message! "
136                       "Computer will self-destruct in 3 seconds.");
137         } else {
138                 ERROR("Win32 error: %s", buffer);
139                 LocalFree(buffer);
140         }
141 }
142 #else
143 #define win32_error(err_code)
144 #endif
145
146 static HANDLE win32_open_file(const wchar_t *path)
147 {
148         return CreateFileW(path,
149                            GENERIC_READ | READ_CONTROL,
150                            FILE_SHARE_READ,
151                            NULL, /* lpSecurityAttributes */
152                            OPEN_EXISTING,
153                            FILE_FLAG_BACKUP_SEMANTICS |
154                                    FILE_FLAG_OPEN_REPARSE_POINT,
155                            NULL /* hTemplateFile */);
156 }
157
158 int win32_read_file(const char *filename,
159                     void *handle, u64 offset, size_t size, u8 *buf)
160 {
161         HANDLE h = handle;
162         DWORD err;
163         DWORD bytesRead;
164         LARGE_INTEGER liOffset = {.QuadPart = offset};
165
166         wimlib_assert(size <= 0xffffffff);
167
168         if (SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
169                 if (ReadFile(h, buf, size, &bytesRead, NULL) && bytesRead == size)
170                         return 0;
171         err = GetLastError();
172         ERROR("Error reading \"%s\"", filename);
173         win32_error(err);
174         return WIMLIB_ERR_READ;
175 }
176
177 void win32_close_handle(void *handle)
178 {
179         CloseHandle((HANDLE)handle);
180 }
181
182 void *win32_open_handle(const char *path_utf16)
183 {
184         return (void*)win32_open_file((const wchar_t*)path_utf16);
185 }
186
187 static int build_dentry_tree(struct wim_dentry **root_ret,
188                              const char *root_disk_path,
189                              struct wim_lookup_table *lookup_table,
190                              struct wim_security_data *sd,
191                              const struct capture_config *config,
192                              int add_image_flags,
193                              wimlib_progress_func_t progress_func,
194                              void *extra_arg);
195
196 static int win32_get_short_name(struct wim_dentry *dentry,
197                                 const wchar_t *path_utf16)
198 {
199         WIN32_FIND_DATAW dat;
200         if (FindFirstFileW(path_utf16, &dat) &&
201             dat.cAlternateFileName[0] != L'\0')
202         {
203                 size_t short_name_len = wcslen(dat.cAlternateFileName) * 2;
204                 size_t n = short_name_len + sizeof(wchar_t);
205                 dentry->short_name = MALLOC(n);
206                 if (!dentry->short_name)
207                         return WIMLIB_ERR_NOMEM;
208                 memcpy(dentry->short_name, dat.cAlternateFileName, n);
209                 dentry->short_name_len = short_name_len;
210         }
211         return 0;
212 }
213
214 static int win32_get_security_descriptor(struct wim_dentry *dentry,
215                                          struct sd_set *sd_set,
216                                          const wchar_t *path_utf16,
217                                          const char *path)
218 {
219         SECURITY_INFORMATION requestedInformation;
220         DWORD lenNeeded = 0;
221         BOOL status;
222         DWORD err;
223
224 #ifdef BACKUP_SECURITY_INFORMATION
225         requestedInformation = BACKUP_SECURITY_INFORMATION;
226 #else
227         requestedInformation = DACL_SECURITY_INFORMATION |
228                                SACL_SECURITY_INFORMATION |
229                                OWNER_SECURITY_INFORMATION |
230                                GROUP_SECURITY_INFORMATION;
231 #endif
232         /* Request length of security descriptor */
233         status = GetFileSecurityW(path_utf16, requestedInformation,
234                                   NULL, 0, &lenNeeded);
235         err = GetLastError();
236
237         /* Error code appears to be ERROR_INSUFFICIENT_BUFFER but
238          * GetFileSecurity is poorly documented... */
239         if (err == ERROR_INSUFFICIENT_BUFFER || err == NO_ERROR) {
240                 DWORD len = lenNeeded;
241                 char buf[len];
242                 if (GetFileSecurityW(path_utf16, requestedInformation,
243                                      buf, len, &lenNeeded))
244                 {
245                         int security_id = sd_set_add_sd(sd_set, buf, len);
246                         if (security_id < 0)
247                                 return WIMLIB_ERR_NOMEM;
248                         else {
249                                 dentry->d_inode->i_security_id = security_id;
250                                 return 0;
251                         }
252                 } else {
253                         err = GetLastError();
254                 }
255         }
256         ERROR("Win32 API: Failed to read security descriptor of \"%s\"",
257               path);
258         win32_error(err);
259         return WIMLIB_ERR_READ;
260 }
261
262
263 static int win32_recurse_directory(struct wim_dentry *root,
264                                    const char *root_disk_path,
265                                    struct wim_lookup_table *lookup_table,
266                                    struct wim_security_data *sd,
267                                    const struct capture_config *config,
268                                    int add_image_flags,
269                                    wimlib_progress_func_t progress_func,
270                                    struct sd_set *sd_set,
271                                    const wchar_t *path_utf16,
272                                    size_t path_utf16_nchars)
273 {
274         WIN32_FIND_DATAW dat;
275         HANDLE hFind;
276         DWORD err;
277         int ret;
278
279         {
280                 wchar_t pattern_buf[path_utf16_nchars + 3];
281                 memcpy(pattern_buf, path_utf16,
282                        path_utf16_nchars * sizeof(wchar_t));
283                 pattern_buf[path_utf16_nchars] = L'/';
284                 pattern_buf[path_utf16_nchars + 1] = L'*';
285                 pattern_buf[path_utf16_nchars + 2] = L'\0';
286                 hFind = FindFirstFileW(pattern_buf, &dat);
287         }
288         if (hFind == INVALID_HANDLE_VALUE) {
289                 err = GetLastError();
290                 if (err == ERROR_FILE_NOT_FOUND) {
291                         return 0;
292                 } else {
293                         ERROR("Win32 API: Failed to read directory \"%s\"",
294                               root_disk_path);
295                         win32_error(err);
296                         return WIMLIB_ERR_READ;
297                 }
298         }
299         ret = 0;
300         do {
301                 if (!(dat.cFileName[0] == L'.' &&
302                       (dat.cFileName[1] == L'\0' ||
303                        (dat.cFileName[1] == L'.' && dat.cFileName[2] == L'\0'))))
304                 {
305                         struct wim_dentry *child;
306
307                         char *utf8_name;
308                         size_t utf8_name_nbytes;
309                         ret = utf16_to_utf8((const char*)dat.cFileName,
310                                             wcslen(dat.cFileName) * sizeof(wchar_t),
311                                             &utf8_name,
312                                             &utf8_name_nbytes);
313                         if (ret)
314                                 goto out_find_close;
315
316                         char name[strlen(root_disk_path) + utf8_name_nbytes + 1];
317                         sprintf(name, "%s/%s", root_disk_path, utf8_name);
318                         FREE(utf8_name);
319                         ret = build_dentry_tree(&child, name, lookup_table,
320                                                 sd, config, add_image_flags,
321                                                 progress_func, sd_set);
322                         if (ret)
323                                 goto out_find_close;
324                         if (child)
325                                 dentry_add_child(root, child);
326                 }
327         } while (FindNextFileW(hFind, &dat));
328         err = GetLastError();
329         if (err != ERROR_NO_MORE_FILES) {
330                 ERROR("Win32 API: Failed to read directory \"%s\"", root_disk_path);
331                 win32_error(err);
332                 if (ret == 0)
333                         ret = WIMLIB_ERR_READ;
334         }
335 out_find_close:
336         FindClose(hFind);
337         return ret;
338 }
339
340 static int win32_capture_reparse_point(const char *path,
341                                        HANDLE hFile,
342                                        struct wim_inode *inode,
343                                        struct wim_lookup_table *lookup_table)
344 {
345         /* "Reparse point data, including the tag and optional GUID,
346          * cannot exceed 16 kilobytes." - MSDN  */
347         char reparse_point_buf[16 * 1024];
348         DWORD bytesReturned;
349         const REPARSE_DATA_BUFFER *buf;
350
351         if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
352                              NULL, 0, reparse_point_buf,
353                              sizeof(reparse_point_buf), &bytesReturned, NULL))
354         {
355                 ERROR("Win32 API: Failed to get reparse data of \"%s\"", path);
356                 return WIMLIB_ERR_READ;
357         }
358         buf = (const REPARSE_DATA_BUFFER*)reparse_point_buf;
359         inode->i_reparse_tag = buf->ReparseTag;
360         return inode_add_ads_with_data(inode, "", (const u8*)buf + 8,
361                                        bytesReturned - 8, lookup_table);
362 }
363
364 static int win32_sha1sum(const wchar_t *path, u8 hash[SHA1_HASH_SIZE])
365 {
366         HANDLE hFile;
367         SHA_CTX ctx;
368         u8 buf[32768];
369         DWORD bytesRead;
370         int ret;
371
372         hFile = win32_open_file(path);
373         if (hFile == INVALID_HANDLE_VALUE)
374                 return WIMLIB_ERR_OPEN;
375
376         sha1_init(&ctx);
377         for (;;) {
378                 if (!ReadFile(hFile, buf, sizeof(buf), &bytesRead, NULL)) {
379                         ret = WIMLIB_ERR_READ;
380                         goto out_close_handle;
381                 }
382                 if (bytesRead == 0)
383                         break;
384                 sha1_update(&ctx, buf, bytesRead);
385         }
386         ret = 0;
387         sha1_final(hash, &ctx);
388 out_close_handle:
389         CloseHandle(hFile);
390         return ret;
391 }
392
393 static int win32_capture_stream(const char *path,
394                                 const wchar_t *path_utf16,
395                                 size_t path_utf16_nchars,
396                                 struct wim_inode *inode,
397                                 struct wim_lookup_table *lookup_table,
398                                 WIN32_FIND_STREAM_DATA *dat)
399 {
400         struct wim_ads_entry *ads_entry;
401         u8 hash[SHA1_HASH_SIZE];
402         struct wim_lookup_table_entry *lte;
403         int ret;
404         wchar_t *p, *colon;
405         bool is_named_stream;
406         wchar_t *spath;
407         size_t spath_nchars;
408         DWORD err;
409
410         p = dat->cStreamName;
411         wimlib_assert(*p == L':');
412         p += 1;
413         colon = wcschr(p, L':');
414         wimlib_assert(colon != NULL);
415
416         if (wcscmp(colon + 1, L"$DATA")) {
417                 /* Not a DATA stream */
418                 ret = 0;
419                 goto out;
420         }
421
422         is_named_stream = (p != colon);
423
424         if (is_named_stream) {
425                 char *utf8_stream_name;
426                 size_t utf8_stream_name_len;
427                 ret = utf16_to_utf8((const char *)p,
428                                     colon - p,
429                                     &utf8_stream_name,
430                                     &utf8_stream_name_len);
431                 if (ret)
432                         goto out;
433                 DEBUG_WIN32_SAFE("Add alternate data stream %s:%s", path, utf8_stream_name);
434                 ads_entry = inode_add_ads(inode, utf8_stream_name);
435                 FREE(utf8_stream_name);
436                 if (!ads_entry) {
437                         ret = WIMLIB_ERR_NOMEM;
438                         goto out;
439                 }
440         }
441
442         *colon = '\0';
443         spath_nchars = path_utf16_nchars;
444         if (is_named_stream)
445                 spath_nchars += colon - p + 1;
446
447         spath = MALLOC((spath_nchars + 1) * sizeof(wchar_t));
448         memcpy(spath, path_utf16, path_utf16_nchars * sizeof(wchar_t));
449         if (is_named_stream) {
450                 spath[path_utf16_nchars] = L':';
451                 memcpy(&spath[path_utf16_nchars + 1], p, (colon - p) * sizeof(wchar_t));
452         }
453         spath[spath_nchars] = L'\0';
454
455         ret = win32_sha1sum(spath, hash);
456         if (ret) {
457                 err = GetLastError();
458                 ERROR("Win32 API: Failed to read \"%s\" to calculate SHA1sum", path);
459                 win32_error(err);
460                 goto out_free_spath;
461         }
462
463         lte = __lookup_resource(lookup_table, hash);
464         if (lte) {
465                 lte->refcnt++;
466         } else {
467                 lte = new_lookup_table_entry();
468                 if (!lte) {
469                         ret = WIMLIB_ERR_NOMEM;
470                         goto out_free_spath;
471                 }
472                 lte->file_on_disk = (char*)spath;
473                 spath = NULL;
474                 lte->resource_location = RESOURCE_WIN32;
475                 lte->resource_entry.original_size = (uint64_t)dat->StreamSize.QuadPart;
476                 lte->resource_entry.size = (uint64_t)dat->StreamSize.QuadPart;
477                 copy_hash(lte->hash, hash);
478                 lookup_table_insert(lookup_table, lte);
479         }
480         if (is_named_stream)
481                 ads_entry->lte = lte;
482         else
483                 inode->i_lte = lte;
484 out_free_spath:
485         FREE(spath);
486 out:
487         return ret;
488 }
489
490 static int win32_capture_streams(const char *path,
491                                  const wchar_t *path_utf16,
492                                  size_t path_utf16_nchars,
493                                  struct wim_inode *inode,
494                                  struct wim_lookup_table *lookup_table)
495 {
496         WIN32_FIND_STREAM_DATA dat;
497         int ret;
498         HANDLE hFind;
499         DWORD err;
500
501         hFind = FindFirstStreamW(path_utf16, FindStreamInfoStandard, &dat, 0);
502         if (hFind == INVALID_HANDLE_VALUE) {
503                 ERROR("Win32 API: Failed to look up data streams of \"%s\"",
504                       path);
505                 return WIMLIB_ERR_READ;
506         }
507         do {
508                 ret = win32_capture_stream(path, path_utf16,
509                                            path_utf16_nchars,
510                                            inode, lookup_table,
511                                            &dat);
512                 if (ret)
513                         goto out_find_close;
514         } while (FindNextStreamW(hFind, &dat));
515         err = GetLastError();
516         if (err != ERROR_HANDLE_EOF) {
517                 ERROR("Win32 API: Error reading data streams from \"%s\"", path);
518                 win32_error(err);
519                 ret = WIMLIB_ERR_READ;
520         }
521 out_find_close:
522         FindClose(hFind);
523         return ret;
524 }
525 #endif
526
527 /*
528  * build_dentry_tree():
529  *      Recursively builds a tree of WIM dentries from an on-disk directory
530  *      tree.
531  *
532  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
533  *              modified if successful.  Set to NULL if the file or directory was
534  *              excluded from capture.
535  *
536  * @root_disk_path:  The path to the root of the directory tree on disk.
537  *
538  * @lookup_table: The lookup table for the WIM file.  For each file added to the
539  *              dentry tree being built, an entry is added to the lookup table,
540  *              unless an identical stream is already in the lookup table.
541  *              These lookup table entries that are added point to the path of
542  *              the file on disk.
543  *
544  * @sd:         Ignored.  (Security data only captured in NTFS mode.)
545  *
546  * @capture_config:
547  *              Configuration for files to be excluded from capture.
548  *
549  * @add_flags:  Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
550  *
551  * @extra_arg:  Ignored. (Only used in NTFS mode.)
552  *
553  * @return:     0 on success, nonzero on failure.  It is a failure if any of
554  *              the files cannot be `stat'ed, or if any of the needed
555  *              directories cannot be opened or read.  Failure to add the files
556  *              to the WIM may still occur later when trying to actually read
557  *              the on-disk files during a call to wimlib_write() or
558  *              wimlib_overwrite().
559  */
560 static int build_dentry_tree(struct wim_dentry **root_ret,
561                              const char *root_disk_path,
562                              struct wim_lookup_table *lookup_table,
563                              struct wim_security_data *sd,
564                              const struct capture_config *config,
565                              int add_image_flags,
566                              wimlib_progress_func_t progress_func,
567                              void *extra_arg)
568 {
569         struct wim_dentry *root = NULL;
570         int ret = 0;
571         struct wim_inode *inode;
572
573         if (exclude_path(root_disk_path, config, true)) {
574                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
575                         ERROR("Cannot exclude the root directory from capture");
576                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
577                         goto out;
578                 }
579                 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
580                     && progress_func)
581                 {
582                         union wimlib_progress_info info;
583                         info.scan.cur_path = root_disk_path;
584                         info.scan.excluded = true;
585                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
586                 }
587                 goto out;
588         }
589
590         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
591             && progress_func)
592         {
593                 union wimlib_progress_info info;
594                 info.scan.cur_path = root_disk_path;
595                 info.scan.excluded = false;
596                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
597         }
598
599 #if !defined(__CYGWIN__) && !defined(__WIN32__)
600         /* UNIX version of capturing a directory tree */
601         struct stat root_stbuf;
602         int (*stat_fn)(const char *restrict, struct stat *restrict);
603         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
604                 stat_fn = stat;
605         else
606                 stat_fn = lstat;
607
608         ret = (*stat_fn)(root_disk_path, &root_stbuf);
609         if (ret != 0) {
610                 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
611                 goto out;
612         }
613
614         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
615               !S_ISDIR(root_stbuf.st_mode))
616         {
617                 /* Do a dereference-stat in case the root is a symbolic link.
618                  * This case is allowed, provided that the symbolic link points
619                  * to a directory. */
620                 ret = stat(root_disk_path, &root_stbuf);
621                 if (ret != 0) {
622                         ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
623                         ret = WIMLIB_ERR_STAT;
624                         goto out;
625                 }
626                 if (!S_ISDIR(root_stbuf.st_mode)) {
627                         ERROR("`%s' is not a directory", root_disk_path);
628                         ret = WIMLIB_ERR_NOTDIR;
629                         goto out;
630                 }
631         }
632         if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
633             && !S_ISLNK(root_stbuf.st_mode)) {
634                 ERROR("`%s' is not a regular file, directory, or symbolic link.",
635                       root_disk_path);
636                 ret = WIMLIB_ERR_SPECIAL_FILE;
637                 goto out;
638         }
639
640         root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
641         if (!root) {
642                 if (errno == EILSEQ)
643                         ret = WIMLIB_ERR_INVALID_UTF8_STRING;
644                 else if (errno == ENOMEM)
645                         ret = WIMLIB_ERR_NOMEM;
646                 else
647                         ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
648                 goto out;
649         }
650
651         inode = root->d_inode;
652
653 #ifdef HAVE_STAT_NANOSECOND_PRECISION
654         inode->i_creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
655         inode->i_last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
656         inode->i_last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
657 #else
658         inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
659         inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
660         inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
661 #endif
662         if (sizeof(ino_t) >= 8)
663                 inode->i_ino = (u64)root_stbuf.st_ino;
664         else
665                 inode->i_ino = (u64)root_stbuf.st_ino |
666                                    ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
667         inode->i_resolved = 1;
668         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
669                 ret = inode_set_unix_data(inode, root_stbuf.st_uid,
670                                           root_stbuf.st_gid,
671                                           root_stbuf.st_mode,
672                                           lookup_table,
673                                           UNIX_DATA_ALL | UNIX_DATA_CREATE);
674                 if (ret)
675                         goto out;
676         }
677         add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
678         if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
679
680                 struct wim_lookup_table_entry *lte;
681                 u8 hash[SHA1_HASH_SIZE];
682
683                 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
684
685                 /* Empty files do not have to have a lookup table entry. */
686                 if (root_stbuf.st_size == 0)
687                         goto out;
688
689                 /* For each regular file, we must check to see if the file is in
690                  * the lookup table already; if it is, we increment its refcnt;
691                  * otherwise, we create a new lookup table entry and insert it.
692                  * */
693
694                 ret = sha1sum(root_disk_path, hash);
695                 if (ret != 0)
696                         goto out;
697
698                 lte = __lookup_resource(lookup_table, hash);
699                 if (lte) {
700                         lte->refcnt++;
701                         DEBUG("Add lte reference %u for `%s'", lte->refcnt,
702                               root_disk_path);
703                 } else {
704                         char *file_on_disk = STRDUP(root_disk_path);
705                         if (!file_on_disk) {
706                                 ERROR("Failed to allocate memory for file path");
707                                 ret = WIMLIB_ERR_NOMEM;
708                                 goto out;
709                         }
710                         lte = new_lookup_table_entry();
711                         if (!lte) {
712                                 FREE(file_on_disk);
713                                 ret = WIMLIB_ERR_NOMEM;
714                                 goto out;
715                         }
716                         lte->file_on_disk = file_on_disk;
717                         lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
718                         lte->resource_entry.original_size = root_stbuf.st_size;
719                         lte->resource_entry.size = root_stbuf.st_size;
720                         copy_hash(lte->hash, hash);
721                         lookup_table_insert(lookup_table, lte);
722                 }
723                 root->d_inode->i_lte = lte;
724         } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
725
726                 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
727
728                 DIR *dir;
729                 struct dirent entry, *result;
730                 struct wim_dentry *child;
731
732                 dir = opendir(root_disk_path);
733                 if (!dir) {
734                         ERROR_WITH_ERRNO("Failed to open the directory `%s'",
735                                          root_disk_path);
736                         ret = WIMLIB_ERR_OPEN;
737                         goto out;
738                 }
739
740                 /* Buffer for names of files in directory. */
741                 size_t len = strlen(root_disk_path);
742                 char name[len + 1 + FILENAME_MAX + 1];
743                 memcpy(name, root_disk_path, len);
744                 name[len] = '/';
745
746                 /* Create a dentry for each entry in the directory on disk, and recurse
747                  * to any subdirectories. */
748                 while (1) {
749                         errno = 0;
750                         ret = readdir_r(dir, &entry, &result);
751                         if (ret != 0) {
752                                 ret = WIMLIB_ERR_READ;
753                                 ERROR_WITH_ERRNO("Error reading the "
754                                                  "directory `%s'",
755                                                  root_disk_path);
756                                 break;
757                         }
758                         if (result == NULL)
759                                 break;
760                         if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
761                               || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
762                                         continue;
763                         strcpy(name + len + 1, result->d_name);
764                         ret = build_dentry_tree(&child, name, lookup_table,
765                                                 NULL, config, add_image_flags,
766                                                 progress_func, NULL);
767                         if (ret != 0)
768                                 break;
769                         if (child)
770                                 dentry_add_child(root, child);
771                 }
772                 closedir(dir);
773         } else { /* Archiving a symbolic link */
774                 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
775                 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
776
777                 /* The idea here is to call readlink() to get the UNIX target of
778                  * the symbolic link, then turn the target into a reparse point
779                  * data buffer that contains a relative or absolute symbolic
780                  * link (NOT a junction point or *full* path symbolic link with
781                  * drive letter).
782                  */
783
784                 char deref_name_buf[4096];
785                 ssize_t deref_name_len;
786
787                 deref_name_len = readlink(root_disk_path, deref_name_buf,
788                                           sizeof(deref_name_buf) - 1);
789                 if (deref_name_len >= 0) {
790                         deref_name_buf[deref_name_len] = '\0';
791                         DEBUG("Read symlink `%s'", deref_name_buf);
792                         ret = inode_set_symlink(root->d_inode, deref_name_buf,
793                                                 lookup_table, NULL);
794                         if (ret == 0) {
795                                 /*
796                                  * Unfortunately, Windows seems to have the
797                                  * concept of "file" symbolic links as being
798                                  * different from "directory" symbolic links...
799                                  * so FILE_ATTRIBUTE_DIRECTORY needs to be set
800                                  * on the symbolic link if the *target* of the
801                                  * symbolic link is a directory.
802                                  */
803                                 struct stat stbuf;
804                                 if (stat(root_disk_path, &stbuf) == 0 &&
805                                     S_ISDIR(stbuf.st_mode))
806                                 {
807                                         inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
808                                 }
809                         }
810                 } else {
811                         ERROR_WITH_ERRNO("Failed to read target of "
812                                          "symbolic link `%s'", root_disk_path);
813                         ret = WIMLIB_ERR_READLINK;
814                 }
815         }
816 #else
817         /* Win32 version of capturing a directory tree */
818
819         wchar_t *path_utf16;
820         size_t path_utf16_nchars;
821         struct sd_set *sd_set;
822         DWORD err;
823
824         if (extra_arg == NULL) {
825                 sd_set = alloca(sizeof(struct sd_set));
826                 sd_set->rb_root.rb_node = NULL,
827                 sd_set->sd = sd;
828         } else {
829                 sd_set = extra_arg;
830         }
831
832         DEBUG_WIN32_SAFE("root_disk_path=\"%s\"", root_disk_path);
833         ret = utf8_to_utf16(root_disk_path, strlen(root_disk_path),
834                             (char**)&path_utf16, &path_utf16_nchars);
835         if (ret)
836                 goto out_destroy_sd_set;
837         path_utf16_nchars /= sizeof(wchar_t);
838
839         DEBUG_WIN32_SAFE("Win32: Opening file `%s'", root_disk_path);
840         HANDLE hFile = win32_open_file(path_utf16);
841         if (hFile == INVALID_HANDLE_VALUE) {
842                 err = GetLastError();
843                 ERROR("Win32 API: Failed to open \"%s\"", root_disk_path);
844                 win32_error(err);
845                 ret = WIMLIB_ERR_OPEN;
846                 goto out_free_path_utf16;
847         }
848
849         BY_HANDLE_FILE_INFORMATION file_info;
850         if (!GetFileInformationByHandle(hFile, &file_info)) {
851                 err = GetLastError();
852                 ERROR("Win32 API: Failed to get file information for \"%s\"",
853                       root_disk_path);
854                 win32_error(err);
855                 ret = WIMLIB_ERR_STAT;
856                 goto out_close_handle;
857         }
858
859         /* Create a WIM dentry */
860         root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
861         if (!root) {
862                 if (errno == EILSEQ)
863                         ret = WIMLIB_ERR_INVALID_UTF8_STRING;
864                 else if (errno == ENOMEM)
865                         ret = WIMLIB_ERR_NOMEM;
866                 else
867                         ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
868                 goto out_free_path_utf16;
869         }
870
871         /* Start preparing the associated WIM inode */
872         inode = root->d_inode;
873
874         inode->i_attributes = file_info.dwFileAttributes;
875         inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
876         inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
877         inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
878         inode->i_ino = ((u64)file_info.nFileIndexHigh << 32) |
879                         (u64)file_info.nFileIndexLow;
880
881         inode->i_resolved = 1;
882         add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
883
884         /* Get DOS name and security descriptor (if any). */
885         ret = win32_get_short_name(root, path_utf16);
886         if (ret)
887                 goto out_close_handle;
888         ret = win32_get_security_descriptor(root, sd_set, path_utf16,
889                                             root_disk_path);
890         if (ret)
891                 goto out_close_handle;
892
893         if (inode_is_directory(inode)) {
894                 /* Directory (not a reparse point) --- recurse to children */
895                 DEBUG_WIN32_SAFE("Recursing to directory \"%s\"", root_disk_path);
896                 ret = win32_recurse_directory(root,
897                                               root_disk_path,
898                                               lookup_table,
899                                               sd,
900                                               config,
901                                               add_image_flags,
902                                               progress_func,
903                                               sd_set,
904                                               path_utf16,
905                                               path_utf16_nchars);
906         } else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
907                 /* Reparse point: save the reparse tag and data */
908
909                 DEBUG_WIN32_SAFE("Capturing reparse point `%s'", root_disk_path);
910                 ret = win32_capture_reparse_point(root_disk_path,
911                                                   hFile,
912                                                   inode,
913                                                   lookup_table);
914
915         } else {
916                 DEBUG_WIN32_SAFE("Capturing streams of \"%s\"", root_disk_path);
917                 /* Not a directory, not a reparse point */
918                 ret = win32_capture_streams(root_disk_path,
919                                             path_utf16,
920                                             path_utf16_nchars,
921                                             inode,
922                                             lookup_table);
923         }
924 out_close_handle:
925         CloseHandle(hFile);
926 out_destroy_sd_set:
927         if (extra_arg == NULL)
928                 destroy_sd_set(sd_set);
929 out_free_path_utf16:
930         FREE(path_utf16);
931 #endif
932 out:
933         if (ret == 0)
934                 *root_ret = root;
935         else
936                 free_dentry_tree(root, lookup_table);
937         return ret;
938 }
939
940 enum pattern_type {
941         NONE = 0,
942         EXCLUSION_LIST,
943         EXCLUSION_EXCEPTION,
944         COMPRESSION_EXCLUSION_LIST,
945         ALIGNMENT_LIST,
946 };
947
948 #define COMPAT_DEFAULT_CONFIG
949
950 /* Default capture configuration file when none is specified. */
951 static const char *default_config =
952 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
953                                 users.  The next ABI-incompatible library
954                                 version will default to the empty string here. */
955 "[ExclusionList]\n"
956 "\\$ntfs.log\n"
957 "\\hiberfil.sys\n"
958 "\\pagefile.sys\n"
959 "\\System Volume Information\n"
960 "\\RECYCLER\n"
961 "\\Windows\\CSC\n"
962 "\n"
963 "[CompressionExclusionList]\n"
964 "*.mp3\n"
965 "*.zip\n"
966 "*.cab\n"
967 "\\WINDOWS\\inf\\*.pnf\n";
968 #else
969 "";
970 #endif
971
972 static void destroy_pattern_list(struct pattern_list *list)
973 {
974         FREE(list->pats);
975 }
976
977 static void destroy_capture_config(struct capture_config *config)
978 {
979         destroy_pattern_list(&config->exclusion_list);
980         destroy_pattern_list(&config->exclusion_exception);
981         destroy_pattern_list(&config->compression_exclusion_list);
982         destroy_pattern_list(&config->alignment_list);
983         FREE(config->config_str);
984         FREE(config->prefix);
985         memset(config, 0, sizeof(*config));
986 }
987
988 static int pattern_list_add_pattern(struct pattern_list *list,
989                                     const char *pattern)
990 {
991         const char **pats;
992         if (list->num_pats >= list->num_allocated_pats) {
993                 pats = REALLOC(list->pats,
994                                sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
995                 if (!pats)
996                         return WIMLIB_ERR_NOMEM;
997                 list->num_allocated_pats += 8;
998                 list->pats = pats;
999         }
1000         list->pats[list->num_pats++] = pattern;
1001         return 0;
1002 }
1003
1004 /* Parses the contents of the image capture configuration file and fills in a
1005  * `struct capture_config'. */
1006 static int init_capture_config(struct capture_config *config,
1007                                const char *_config_str, size_t config_len)
1008 {
1009         char *config_str;
1010         char *p;
1011         char *eol;
1012         char *next_p;
1013         size_t bytes_remaining;
1014         enum pattern_type type = NONE;
1015         int ret;
1016         unsigned long line_no = 0;
1017
1018         DEBUG("config_len = %zu", config_len);
1019         bytes_remaining = config_len;
1020         memset(config, 0, sizeof(*config));
1021         config_str = MALLOC(config_len);
1022         if (!config_str) {
1023                 ERROR("Could not duplicate capture config string");
1024                 return WIMLIB_ERR_NOMEM;
1025         }
1026
1027         memcpy(config_str, _config_str, config_len);
1028         next_p = config_str;
1029         config->config_str = config_str;
1030         while (bytes_remaining) {
1031                 line_no++;
1032                 p = next_p;
1033                 eol = memchr(p, '\n', bytes_remaining);
1034                 if (!eol) {
1035                         ERROR("Expected end-of-line in capture config file on "
1036                               "line %lu", line_no);
1037                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1038                         goto out_destroy;
1039                 }
1040
1041                 next_p = eol + 1;
1042                 bytes_remaining -= (next_p - p);
1043                 if (eol == p)
1044                         continue;
1045
1046                 if (*(eol - 1) == '\r')
1047                         eol--;
1048                 *eol = '\0';
1049
1050                 /* Translate backslash to forward slash */
1051                 for (char *pp = p; pp != eol; pp++)
1052                         if (*pp == '\\')
1053                                 *pp = '/';
1054
1055                 /* Remove drive letter */
1056                 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
1057                         p += 2;
1058
1059                 ret = 0;
1060                 if (strcmp(p, "[ExclusionList]") == 0)
1061                         type = EXCLUSION_LIST;
1062                 else if (strcmp(p, "[ExclusionException]") == 0)
1063                         type = EXCLUSION_EXCEPTION;
1064                 else if (strcmp(p, "[CompressionExclusionList]") == 0)
1065                         type = COMPRESSION_EXCLUSION_LIST;
1066                 else if (strcmp(p, "[AlignmentList]") == 0)
1067                         type = ALIGNMENT_LIST;
1068                 else if (p[0] == '[' && strrchr(p, ']')) {
1069                         ERROR("Unknown capture configuration section `%s'", p);
1070                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1071                 } else switch (type) {
1072                 case EXCLUSION_LIST:
1073                         DEBUG("Adding pattern \"%s\" to exclusion list", p);
1074                         ret = pattern_list_add_pattern(&config->exclusion_list, p);
1075                         break;
1076                 case EXCLUSION_EXCEPTION:
1077                         DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
1078                         ret = pattern_list_add_pattern(&config->exclusion_exception, p);
1079                         break;
1080                 case COMPRESSION_EXCLUSION_LIST:
1081                         DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
1082                         ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
1083                         break;
1084                 case ALIGNMENT_LIST:
1085                         DEBUG("Adding pattern \"%s\" to alignment list", p);
1086                         ret = pattern_list_add_pattern(&config->alignment_list, p);
1087                         break;
1088                 default:
1089                         ERROR("Line %lu of capture configuration is not "
1090                               "in a block (such as [ExclusionList])",
1091                               line_no);
1092                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1093                         break;
1094                 }
1095                 if (ret != 0)
1096                         goto out_destroy;
1097         }
1098         return 0;
1099 out_destroy:
1100         destroy_capture_config(config);
1101         return ret;
1102 }
1103
1104 static int capture_config_set_prefix(struct capture_config *config,
1105                                      const char *_prefix)
1106 {
1107         char *prefix = STRDUP(_prefix);
1108
1109         if (!prefix)
1110                 return WIMLIB_ERR_NOMEM;
1111         FREE(config->prefix);
1112         config->prefix = prefix;
1113         config->prefix_len = strlen(prefix);
1114         return 0;
1115 }
1116
1117 static bool match_pattern(const char *path, const char *path_basename,
1118                           const struct pattern_list *list)
1119 {
1120         for (size_t i = 0; i < list->num_pats; i++) {
1121                 const char *pat = list->pats[i];
1122                 const char *string;
1123                 if (pat[0] == '/')
1124                         /* Absolute path from root of capture */
1125                         string = path;
1126                 else {
1127                         if (strchr(pat, '/'))
1128                                 /* Relative path from root of capture */
1129                                 string = path + 1;
1130                         else
1131                                 /* A file name pattern */
1132                                 string = path_basename;
1133                 }
1134                 if (fnmatch(pat, string, FNM_PATHNAME
1135                         #ifdef FNM_CASEFOLD
1136                                         | FNM_CASEFOLD
1137                         #endif
1138                         ) == 0)
1139                 {
1140                         DEBUG("`%s' matches the pattern \"%s\"",
1141                               string, pat);
1142                         return true;
1143                 }
1144         }
1145         return false;
1146 }
1147
1148 /* Return true if the image capture configuration file indicates we should
1149  * exclude the filename @path from capture.
1150  *
1151  * If @exclude_prefix is %true, the part of the path up and including the name
1152  * of the directory being captured is not included in the path for matching
1153  * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
1154  * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
1155  * directory.
1156  */
1157 bool exclude_path(const char *path, const struct capture_config *config,
1158                   bool exclude_prefix)
1159 {
1160         const char *basename = path_basename(path);
1161         if (exclude_prefix) {
1162                 wimlib_assert(strlen(path) >= config->prefix_len);
1163                 if (memcmp(config->prefix, path, config->prefix_len) == 0
1164                      && path[config->prefix_len] == '/')
1165                         path += config->prefix_len;
1166         }
1167         return match_pattern(path, basename, &config->exclusion_list) &&
1168                 !match_pattern(path, basename, &config->exclusion_exception);
1169
1170 }
1171
1172 /* Strip leading and trailing forward slashes from a string.  Modifies it in
1173  * place and returns the stripped string. */
1174 static const char *canonicalize_target_path(char *target_path)
1175 {
1176         char *p;
1177         if (target_path == NULL)
1178                 target_path = "";
1179         for (;;) {
1180                 if (*target_path == '\0')
1181                         return target_path;
1182                 else if (*target_path == '/')
1183                         target_path++;
1184                 else
1185                         break;
1186         }
1187
1188         p = target_path + strlen(target_path) - 1;
1189         while (*p == '/')
1190                 *p-- = '\0';
1191         return target_path;
1192 }
1193
1194 /* Strip leading and trailing slashes from the target paths */
1195 static void canonicalize_targets(struct wimlib_capture_source *sources,
1196                                  size_t num_sources)
1197 {
1198         while (num_sources--) {
1199                 DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
1200                       sources->fs_source_path,
1201                       sources->wim_target_path);
1202                 sources->wim_target_path =
1203                         (char*)canonicalize_target_path(sources->wim_target_path);
1204                 DEBUG("Canonical target: \"%s\"", sources->wim_target_path);
1205                 sources++;
1206         }
1207 }
1208
1209 static int capture_source_cmp(const void *p1, const void *p2)
1210 {
1211         const struct wimlib_capture_source *s1 = p1, *s2 = p2;
1212         return strcmp(s1->wim_target_path, s2->wim_target_path);
1213 }
1214
1215 /* Sorts the capture sources lexicographically by target path.  This occurs
1216  * after leading and trailing forward slashes are stripped.
1217  *
1218  * One purpose of this is to make sure that target paths that are inside other
1219  * target paths are extracted after the containing target paths. */
1220 static void sort_sources(struct wimlib_capture_source *sources,
1221                          size_t num_sources)
1222 {
1223         qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
1224 }
1225
1226 static int check_sorted_sources(struct wimlib_capture_source *sources,
1227                                 size_t num_sources, int add_image_flags)
1228 {
1229         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
1230                 if (num_sources != 1) {
1231                         ERROR("Must specify exactly 1 capture source "
1232                               "(the NTFS volume) in NTFS mode!");
1233                         return WIMLIB_ERR_INVALID_PARAM;
1234                 }
1235                 if (sources[0].wim_target_path[0] != '\0') {
1236                         ERROR("In NTFS capture mode the target path inside "
1237                               "the image must be the root directory!");
1238                         return WIMLIB_ERR_INVALID_PARAM;
1239                 }
1240         } else if (num_sources != 0) {
1241                 /* This code is disabled because the current code
1242                  * unconditionally attempts to do overlays.  So, duplicate
1243                  * target paths are OK. */
1244         #if 0
1245                 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
1246                         ERROR("Cannot specify root target when using multiple "
1247                               "capture sources!");
1248                         return WIMLIB_ERR_INVALID_PARAM;
1249                 }
1250                 for (size_t i = 0; i < num_sources - 1; i++) {
1251                         size_t len = strlen(sources[i].wim_target_path);
1252                         size_t j = i + 1;
1253                         const char *target1 = sources[i].wim_target_path;
1254                         do {
1255                                 const char *target2 = sources[j].wim_target_path;
1256                                 DEBUG("target1=%s, target2=%s",
1257                                       target1,target2);
1258                                 if (strncmp(target1, target2, len) ||
1259                                     target2[len] > '/')
1260                                         break;
1261                                 if (target2[len] == '/') {
1262                                         ERROR("Invalid target `%s': is a prefix of `%s'",
1263                                               target1, target2);
1264                                         return WIMLIB_ERR_INVALID_PARAM;
1265                                 }
1266                                 if (target2[len] == '\0') {
1267                                         ERROR("Invalid target `%s': is a duplicate of `%s'",
1268                                               target1, target2);
1269                                         return WIMLIB_ERR_INVALID_PARAM;
1270                                 }
1271                         } while (++j != num_sources);
1272                 }
1273         #endif
1274         }
1275         return 0;
1276
1277 }
1278
1279 /* Creates a new directory to place in the WIM image.  This is to create parent
1280  * directories that are not part of any target as needed.  */
1281 static struct wim_dentry *
1282 new_filler_directory(const char *name)
1283 {
1284         struct wim_dentry *dentry;
1285         DEBUG("Creating filler directory \"%s\"", name);
1286         dentry = new_dentry_with_inode(name);
1287         if (dentry) {
1288                 /* Set the inode number to 0 for now.  The final inode number
1289                  * will be assigned later by assign_inode_numbers(). */
1290                 dentry->d_inode->i_ino = 0;
1291                 dentry->d_inode->i_resolved = 1;
1292                 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1293         }
1294         return dentry;
1295 }
1296
1297 /* Transfers the children of @branch to @target.  It is an error if @target is
1298  * not a directory or if both @branch and @target contain a child dentry with
1299  * the same name. */
1300 static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
1301 {
1302         struct rb_root *rb_root;
1303
1304         if (!dentry_is_directory(target)) {
1305                 ERROR("Cannot overlay directory `%s' over non-directory",
1306                       branch->file_name_utf8);
1307                 return WIMLIB_ERR_INVALID_OVERLAY;
1308         }
1309
1310         rb_root = &branch->d_inode->i_children;
1311         while (rb_root->rb_node) { /* While @branch has children... */
1312                 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
1313                 /* Move @child to the directory @target */
1314                 unlink_dentry(child);
1315                 if (!dentry_add_child(target, child)) {
1316                         /* Revert the change to avoid leaking the directory tree
1317                          * rooted at @child */
1318                         dentry_add_child(branch, child);
1319                         ERROR("Overlay error: file `%s' already exists "
1320                               "as a child of `%s'",
1321                               child->file_name_utf8, target->file_name_utf8);
1322                         return WIMLIB_ERR_INVALID_OVERLAY;
1323                 }
1324         }
1325         return 0;
1326
1327 }
1328
1329 /* Attach or overlay a branch onto the WIM image.
1330  *
1331  * @root_p:
1332  *      Pointer to the root of the WIM image, or pointer to NULL if it has not
1333  *      been created yet.
1334  * @branch
1335  *      Branch to add.
1336  * @target_path:
1337  *      Path in the WIM image to add the branch, with leading and trailing
1338  *      slashes stripped.
1339  */
1340 static int attach_branch(struct wim_dentry **root_p,
1341                          struct wim_dentry *branch,
1342                          char *target_path)
1343 {
1344         char *slash;
1345         struct wim_dentry *dentry, *parent, *target;
1346
1347         if (*target_path == '\0') {
1348                 /* Target: root directory */
1349                 if (*root_p) {
1350                         /* Overlay on existing root */
1351                         return do_overlay(*root_p, branch);
1352                 } else  {
1353                         /* Set as root */
1354                         *root_p = branch;
1355                         return 0;
1356                 }
1357         }
1358
1359         /* Adding a non-root branch.  Create root if it hasn't been created
1360          * already. */
1361         if (!*root_p) {
1362                 *root_p = new_filler_directory("");
1363                 if (!*root_p)
1364                         return WIMLIB_ERR_NOMEM;
1365         }
1366
1367         /* Walk the path to the branch, creating filler directories as needed.
1368          * */
1369         parent = *root_p;
1370         while ((slash = strchr(target_path, '/'))) {
1371                 *slash = '\0';
1372                 dentry = get_dentry_child_with_name(parent, target_path);
1373                 if (!dentry) {
1374                         dentry = new_filler_directory(target_path);
1375                         if (!dentry)
1376                                 return WIMLIB_ERR_NOMEM;
1377                         dentry_add_child(parent, dentry);
1378                 }
1379                 parent = dentry;
1380                 target_path = slash;
1381                 /* Skip over slashes.  Note: this cannot overrun the length of
1382                  * the string because the last character cannot be a slash, as
1383                  * trailing slashes were tripped.  */
1384                 do {
1385                         ++target_path;
1386                 } while (*target_path == '/');
1387         }
1388
1389         /* If the target path already existed, overlay the branch onto it.
1390          * Otherwise, set the branch as the target path. */
1391         target = get_dentry_child_with_name(parent, branch->file_name_utf8);
1392         if (target) {
1393                 return do_overlay(target, branch);
1394         } else {
1395                 dentry_add_child(parent, branch);
1396                 return 0;
1397         }
1398 }
1399
1400 WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
1401                                            struct wimlib_capture_source *sources,
1402                                            size_t num_sources,
1403                                            const char *name,
1404                                            const char *config_str,
1405                                            size_t config_len,
1406                                            int add_image_flags,
1407                                            wimlib_progress_func_t progress_func)
1408 {
1409         int (*capture_tree)(struct wim_dentry **, const char *,
1410                             struct wim_lookup_table *,
1411                             struct wim_security_data *,
1412                             const struct capture_config *,
1413                             int, wimlib_progress_func_t, void *);
1414         void *extra_arg;
1415         struct wim_dentry *root_dentry;
1416         struct wim_dentry *branch;
1417         struct wim_security_data *sd;
1418         struct capture_config config;
1419         struct wim_image_metadata *imd;
1420         int ret;
1421
1422         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
1423 #ifdef WITH_NTFS_3G
1424                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
1425                         ERROR("Cannot dereference files when capturing directly from NTFS");
1426                         return WIMLIB_ERR_INVALID_PARAM;
1427                 }
1428                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
1429                         ERROR("Capturing UNIX owner and mode not supported "
1430                               "when capturing directly from NTFS");
1431                         return WIMLIB_ERR_INVALID_PARAM;
1432                 }
1433                 capture_tree = build_dentry_tree_ntfs;
1434                 extra_arg = &w->ntfs_vol;
1435 #else
1436                 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1437                       "        cannot capture a WIM image directly from a NTFS volume!");
1438                 return WIMLIB_ERR_UNSUPPORTED;
1439 #endif
1440         } else {
1441                 capture_tree = build_dentry_tree;
1442                 extra_arg = NULL;
1443         }
1444
1445         if (!name || !*name) {
1446                 ERROR("Must specify a non-empty string for the image name");
1447                 return WIMLIB_ERR_INVALID_PARAM;
1448         }
1449
1450         if (w->hdr.total_parts != 1) {
1451                 ERROR("Cannot add an image to a split WIM");
1452                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1453         }
1454
1455         if (wimlib_image_name_in_use(w, name)) {
1456                 ERROR("There is already an image named \"%s\" in `%s'",
1457                       name, w->filename);
1458                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1459         }
1460
1461         if (!config_str) {
1462                 DEBUG("Using default capture configuration");
1463                 config_str = default_config;
1464                 config_len = strlen(default_config);
1465         }
1466         ret = init_capture_config(&config, config_str, config_len);
1467         if (ret)
1468                 goto out;
1469
1470         DEBUG("Allocating security data");
1471         sd = CALLOC(1, sizeof(struct wim_security_data));
1472         if (!sd) {
1473                 ret = WIMLIB_ERR_NOMEM;
1474                 goto out_destroy_capture_config;
1475         }
1476         sd->total_length = 8;
1477         sd->refcnt = 1;
1478
1479         DEBUG("Using %zu capture sources", num_sources);
1480         canonicalize_targets(sources, num_sources);
1481         sort_sources(sources, num_sources);
1482         ret = check_sorted_sources(sources, num_sources, add_image_flags);
1483         if (ret) {
1484                 ret = WIMLIB_ERR_INVALID_PARAM;
1485                 goto out_free_security_data;
1486         }
1487
1488         DEBUG("Building dentry tree.");
1489         if (num_sources == 0) {
1490                 root_dentry = new_filler_directory("");
1491                 if (!root_dentry)
1492                         goto out_free_security_data;
1493         } else {
1494                 size_t i;
1495
1496                 root_dentry = NULL;
1497                 i = 0;
1498                 do {
1499                         int flags;
1500                         union wimlib_progress_info progress;
1501
1502                         DEBUG("Building dentry tree for source %zu of %zu "
1503                               "(\"%s\" => \"%s\")", i + 1, num_sources,
1504                               sources[i].fs_source_path,
1505                               sources[i].wim_target_path);
1506                         if (progress_func) {
1507                                 memset(&progress, 0, sizeof(progress));
1508                                 progress.scan.source = sources[i].fs_source_path;
1509                                 progress.scan.wim_target_path = sources[i].wim_target_path;
1510                                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1511                         }
1512                         ret = capture_config_set_prefix(&config,
1513                                                         sources[i].fs_source_path);
1514                         if (ret)
1515                                 goto out_free_dentry_tree;
1516                         flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
1517                         if (!*sources[i].wim_target_path)
1518                                 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
1519                         ret = (*capture_tree)(&branch, sources[i].fs_source_path,
1520                                               w->lookup_table, sd,
1521                                               &config,
1522                                               flags,
1523                                               progress_func, extra_arg);
1524                         if (ret) {
1525                                 ERROR("Failed to build dentry tree for `%s'",
1526                                       sources[i].fs_source_path);
1527                                 goto out_free_dentry_tree;
1528                         }
1529                         if (branch) {
1530                                 /* Use the target name, not the source name, for
1531                                  * the root of each branch from a capture
1532                                  * source.  (This will also set the root dentry
1533                                  * of the entire image to be unnamed.) */
1534                                 ret = set_dentry_name(branch,
1535                                                       path_basename(sources[i].wim_target_path));
1536                                 if (ret)
1537                                         goto out_free_branch;
1538
1539                                 ret = attach_branch(&root_dentry, branch,
1540                                                     sources[i].wim_target_path);
1541                                 if (ret)
1542                                         goto out_free_branch;
1543                         }
1544                         if (progress_func)
1545                                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1546                 } while (++i != num_sources);
1547         }
1548
1549         DEBUG("Calculating full paths of dentries.");
1550         ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1551         if (ret != 0)
1552                 goto out_free_dentry_tree;
1553
1554         ret = add_new_dentry_tree(w, root_dentry, sd);
1555         if (ret != 0)
1556                 goto out_free_dentry_tree;
1557
1558         imd = &w->image_metadata[w->hdr.image_count - 1];
1559
1560         ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1561         if (ret != 0)
1562                 goto out_destroy_imd;
1563
1564         DEBUG("Assigning hard link group IDs");
1565         assign_inode_numbers(&imd->inode_list);
1566
1567         ret = xml_add_image(w, name);
1568         if (ret != 0)
1569                 goto out_destroy_imd;
1570
1571         if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1572                 wimlib_set_boot_idx(w, w->hdr.image_count);
1573         ret = 0;
1574         goto out;
1575 out_destroy_imd:
1576         destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1577                                w->lookup_table);
1578         w->hdr.image_count--;
1579         goto out;
1580 out_free_branch:
1581         free_dentry_tree(branch, w->lookup_table);
1582 out_free_dentry_tree:
1583         free_dentry_tree(root_dentry, w->lookup_table);
1584 out_free_security_data:
1585         free_security_data(sd);
1586 out_destroy_capture_config:
1587         destroy_capture_config(&config);
1588 out:
1589         return ret;
1590 }
1591
1592 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
1593                                const char *name, const char *config_str,
1594                                size_t config_len, int add_image_flags,
1595                                wimlib_progress_func_t progress_func)
1596 {
1597         if (!source || !*source)
1598                 return WIMLIB_ERR_INVALID_PARAM;
1599
1600         char *fs_source_path = STRDUP(source);
1601         int ret;
1602         struct wimlib_capture_source capture_src = {
1603                 .fs_source_path = fs_source_path,
1604                 .wim_target_path = NULL,
1605                 .reserved = 0,
1606         };
1607         ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1608                                            config_str, config_len,
1609                                            add_image_flags, progress_func);
1610         FREE(fs_source_path);
1611         return ret;
1612 }