6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
26 #if defined(__CYGWIN__) || defined(__WIN32__)
30 # include <sys/cygwin.h>
35 # include "security.h"
38 #include "wimlib_internal.h"
40 #include "timestamp.h"
41 #include "lookup_table.h"
58 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
59 #define WIMLIB_ADD_IMAGE_FLAG_SOURCE 0x40000000
62 * Adds the dentry tree and security data for a new image to the image metadata
63 * array of the WIMStruct.
65 int add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
66 struct wim_security_data *sd)
68 struct wim_lookup_table_entry *metadata_lte;
69 struct wim_image_metadata *imd;
70 struct wim_image_metadata *new_imd;
72 wimlib_assert(root_dentry != NULL);
74 DEBUG("Reallocating image metadata array for image_count = %u",
75 w->hdr.image_count + 1);
76 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct wim_image_metadata));
79 ERROR("Failed to allocate memory for new image metadata array");
83 memcpy(imd, w->image_metadata,
84 w->hdr.image_count * sizeof(struct wim_image_metadata));
86 metadata_lte = new_lookup_table_entry();
90 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
91 random_hash(metadata_lte->hash);
92 lookup_table_insert(w->lookup_table, metadata_lte);
94 new_imd = &imd[w->hdr.image_count];
96 new_imd->root_dentry = root_dentry;
97 new_imd->metadata_lte = metadata_lte;
98 new_imd->security_data = sd;
99 new_imd->modified = 1;
101 FREE(w->image_metadata);
102 w->image_metadata = imd;
103 w->hdr.image_count++;
108 return WIMLIB_ERR_NOMEM;
112 #if defined(__CYGWIN__) || defined(__WIN32__)
114 static u64 FILETIME_to_u64(const FILETIME *ft)
116 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
120 static int build_dentry_tree(struct wim_dentry **root_ret,
121 const char *root_disk_path,
122 struct wim_lookup_table *lookup_table,
123 struct wim_security_data *sd,
124 const struct capture_config *config,
126 wimlib_progress_func_t progress_func,
129 static int win32_get_short_name(struct wim_dentry *dentry,
130 const wchar_t *path_utf16)
132 WIN32_FIND_DATAW dat;
133 if (FindFirstFileW(path_utf16, &dat) &&
134 dat.cAlternateFileName[0] != L'\0')
136 size_t short_name_len = wcslen(dat.cAlternateFileName) * 2;
137 size_t n = short_name_len + sizeof(wchar_t);
138 dentry->short_name = MALLOC(n);
139 if (!dentry->short_name)
140 return WIMLIB_ERR_NOMEM;
141 memcpy(dentry->short_name, dat.cAlternateFileName, n);
142 dentry->short_name_len = short_name_len;
147 static int win32_get_security_descriptor(struct wim_dentry *dentry,
148 struct sd_set *sd_set,
149 const wchar_t *path_utf16,
152 SECURITY_INFORMATION requestedInformation;
157 requestedInformation = DACL_SECURITY_INFORMATION |
158 SACL_SECURITY_INFORMATION |
159 OWNER_SECURITY_INFORMATION |
160 GROUP_SECURITY_INFORMATION;
161 /* Request length of security descriptor */
162 status = GetFileSecurityW(path_utf16, requestedInformation,
163 NULL, 0, &lenNeeded);
164 err = GetLastError();
166 /* Error code appears to be ERROR_INSUFFICIENT_BUFFER but
167 * GetFileSecurity is poorly documented... */
168 if (err == ERROR_INSUFFICIENT_BUFFER || err == NO_ERROR) {
169 DWORD len = lenNeeded;
171 if (GetFileSecurityW(path_utf16, requestedInformation,
172 buf, len, &lenNeeded))
174 int security_id = sd_set_add_sd(sd_set, buf, len);
176 return WIMLIB_ERR_NOMEM;
178 dentry->d_inode->i_security_id = security_id;
182 err = GetLastError();
185 ERROR("Win32 API: Failed to read security descriptor of \"%s\"",
188 return WIMLIB_ERR_READ;
192 static int win32_recurse_directory(struct wim_dentry *root,
193 const char *root_disk_path,
194 struct wim_lookup_table *lookup_table,
195 struct wim_security_data *sd,
196 const struct capture_config *config,
198 wimlib_progress_func_t progress_func,
199 struct sd_set *sd_set,
200 const wchar_t *path_utf16,
201 size_t path_utf16_nchars)
203 WIN32_FIND_DATAW dat;
209 wchar_t pattern_buf[path_utf16_nchars + 3];
210 memcpy(pattern_buf, path_utf16,
211 path_utf16_nchars * sizeof(wchar_t));
212 pattern_buf[path_utf16_nchars] = L'/';
213 pattern_buf[path_utf16_nchars + 1] = L'*';
214 pattern_buf[path_utf16_nchars + 2] = L'\0';
215 hFind = FindFirstFileW(pattern_buf, &dat);
217 if (hFind == INVALID_HANDLE_VALUE) {
218 err = GetLastError();
219 if (err == ERROR_FILE_NOT_FOUND) {
222 ERROR("Win32 API: Failed to read directory \"%s\"",
225 return WIMLIB_ERR_READ;
230 if (!(dat.cFileName[0] == L'.' &&
231 (dat.cFileName[1] == L'\0' ||
232 (dat.cFileName[1] == L'.' && dat.cFileName[2] == L'\0'))))
234 struct wim_dentry *child;
237 size_t utf8_name_nbytes;
238 ret = utf16_to_utf8((const char*)dat.cFileName,
239 wcslen(dat.cFileName) * sizeof(wchar_t),
245 char name[strlen(root_disk_path) + utf8_name_nbytes + 1];
246 sprintf(name, "%s/%s", root_disk_path, utf8_name);
248 ret = build_dentry_tree(&child, name, lookup_table,
249 sd, config, add_image_flags,
250 progress_func, sd_set);
254 dentry_add_child(root, child);
256 } while (FindNextFileW(hFind, &dat));
257 err = GetLastError();
258 if (err != ERROR_NO_MORE_FILES) {
259 ERROR("Win32 API: Failed to read directory \"%s\"", root_disk_path);
262 ret = WIMLIB_ERR_READ;
269 static int win32_capture_reparse_point(const char *path,
271 struct wim_inode *inode,
272 struct wim_lookup_table *lookup_table)
274 /* "Reparse point data, including the tag and optional GUID,
275 * cannot exceed 16 kilobytes." - MSDN */
276 char reparse_point_buf[16 * 1024];
278 const REPARSE_DATA_BUFFER *buf;
280 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
281 NULL, 0, reparse_point_buf,
282 sizeof(reparse_point_buf), &bytesReturned, NULL))
284 ERROR("Win32 API: Failed to get reparse data of \"%s\"", path);
285 return WIMLIB_ERR_READ;
287 buf = (const REPARSE_DATA_BUFFER*)reparse_point_buf;
288 inode->i_reparse_tag = buf->ReparseTag;
289 return inode_add_ads_with_data(inode, "", (const u8*)buf + 8,
290 bytesReturned - 8, lookup_table);
293 static int win32_sha1sum(const wchar_t *path, u8 hash[SHA1_HASH_SIZE])
301 hFile = win32_open_file(path);
302 if (hFile == INVALID_HANDLE_VALUE)
303 return WIMLIB_ERR_OPEN;
307 if (!ReadFile(hFile, buf, sizeof(buf), &bytesRead, NULL)) {
308 ret = WIMLIB_ERR_READ;
309 goto out_close_handle;
313 sha1_update(&ctx, buf, bytesRead);
316 sha1_final(hash, &ctx);
322 static int win32_capture_stream(const char *path,
323 const wchar_t *path_utf16,
324 size_t path_utf16_nchars,
325 struct wim_inode *inode,
326 struct wim_lookup_table *lookup_table,
327 WIN32_FIND_STREAM_DATA *dat)
329 struct wim_ads_entry *ads_entry;
330 u8 hash[SHA1_HASH_SIZE];
331 struct wim_lookup_table_entry *lte;
334 bool is_named_stream;
339 p = dat->cStreamName;
340 wimlib_assert(*p == L':');
342 colon = wcschr(p, L':');
343 wimlib_assert(colon != NULL);
345 if (wcscmp(colon + 1, L"$DATA")) {
346 /* Not a DATA stream */
351 is_named_stream = (p != colon);
353 if (is_named_stream) {
354 char *utf8_stream_name;
355 size_t utf8_stream_name_len;
356 ret = utf16_to_utf8((const char *)p,
357 (colon - p) * sizeof(wchar_t),
359 &utf8_stream_name_len);
362 ads_entry = inode_add_ads(inode, utf8_stream_name);
363 FREE(utf8_stream_name);
365 ret = WIMLIB_ERR_NOMEM;
371 spath_nchars = path_utf16_nchars;
373 spath_nchars += colon - p + 1;
375 spath = MALLOC((spath_nchars + 1) * sizeof(wchar_t));
376 memcpy(spath, path_utf16, path_utf16_nchars * sizeof(wchar_t));
377 if (is_named_stream) {
378 spath[path_utf16_nchars] = L':';
379 memcpy(&spath[path_utf16_nchars + 1], p, (colon - p) * sizeof(wchar_t));
381 spath[spath_nchars] = L'\0';
383 ret = win32_sha1sum(spath, hash);
385 err = GetLastError();
386 ERROR("Win32 API: Failed to read \"%s\" to calculate SHA1sum", path);
391 lte = __lookup_resource(lookup_table, hash);
395 lte = new_lookup_table_entry();
397 ret = WIMLIB_ERR_NOMEM;
400 lte->file_on_disk = (char*)spath;
402 lte->resource_location = RESOURCE_WIN32;
403 lte->resource_entry.original_size = (uint64_t)dat->StreamSize.QuadPart;
404 lte->resource_entry.size = (uint64_t)dat->StreamSize.QuadPart;
405 copy_hash(lte->hash, hash);
406 lookup_table_insert(lookup_table, lte);
409 ads_entry->lte = lte;
418 static int win32_capture_streams(const char *path,
419 const wchar_t *path_utf16,
420 size_t path_utf16_nchars,
421 struct wim_inode *inode,
422 struct wim_lookup_table *lookup_table)
424 WIN32_FIND_STREAM_DATA dat;
429 hFind = FindFirstStreamW(path_utf16, FindStreamInfoStandard, &dat, 0);
430 if (hFind == INVALID_HANDLE_VALUE) {
431 err = GetLastError();
433 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
434 * points and directories */
435 if ((inode->i_attributes &
436 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
437 && err == ERROR_HANDLE_EOF)
441 ERROR("Win32 API: Failed to look up data streams of \"%s\"",
444 return WIMLIB_ERR_READ;
448 ret = win32_capture_stream(path, path_utf16,
454 } while (FindNextStreamW(hFind, &dat));
455 err = GetLastError();
456 if (err != ERROR_HANDLE_EOF) {
457 ERROR("Win32 API: Error reading data streams from \"%s\"", path);
459 ret = WIMLIB_ERR_READ;
468 * build_dentry_tree():
469 * Recursively builds a tree of WIM dentries from an on-disk directory
472 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
473 * modified if successful. Set to NULL if the file or directory was
474 * excluded from capture.
476 * @root_disk_path: The path to the root of the directory tree on disk.
478 * @lookup_table: The lookup table for the WIM file. For each file added to the
479 * dentry tree being built, an entry is added to the lookup table,
480 * unless an identical stream is already in the lookup table.
481 * These lookup table entries that are added point to the path of
484 * @sd: Ignored. (Security data only captured in NTFS mode.)
487 * Configuration for files to be excluded from capture.
489 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
491 * @extra_arg: Ignored. (Only used in NTFS mode.)
493 * @return: 0 on success, nonzero on failure. It is a failure if any of
494 * the files cannot be `stat'ed, or if any of the needed
495 * directories cannot be opened or read. Failure to add the files
496 * to the WIM may still occur later when trying to actually read
497 * the on-disk files during a call to wimlib_write() or
498 * wimlib_overwrite().
500 static int build_dentry_tree(struct wim_dentry **root_ret,
501 const char *root_disk_path,
502 struct wim_lookup_table *lookup_table,
503 struct wim_security_data *sd,
504 const struct capture_config *config,
506 wimlib_progress_func_t progress_func,
509 struct wim_dentry *root = NULL;
511 struct wim_inode *inode;
513 if (exclude_path(root_disk_path, config, true)) {
514 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
515 ERROR("Cannot exclude the root directory from capture");
516 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
519 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
522 union wimlib_progress_info info;
523 info.scan.cur_path = root_disk_path;
524 info.scan.excluded = true;
525 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
530 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
533 union wimlib_progress_info info;
534 info.scan.cur_path = root_disk_path;
535 info.scan.excluded = false;
536 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
539 #if !defined(__CYGWIN__) && !defined(__WIN32__)
540 /* UNIX version of capturing a directory tree */
541 struct stat root_stbuf;
542 int (*stat_fn)(const char *restrict, struct stat *restrict);
543 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
548 ret = (*stat_fn)(root_disk_path, &root_stbuf);
550 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
554 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
555 !S_ISDIR(root_stbuf.st_mode))
557 /* Do a dereference-stat in case the root is a symbolic link.
558 * This case is allowed, provided that the symbolic link points
560 ret = stat(root_disk_path, &root_stbuf);
562 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
563 ret = WIMLIB_ERR_STAT;
566 if (!S_ISDIR(root_stbuf.st_mode)) {
567 ERROR("`%s' is not a directory", root_disk_path);
568 ret = WIMLIB_ERR_NOTDIR;
572 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
573 && !S_ISLNK(root_stbuf.st_mode)) {
574 ERROR("`%s' is not a regular file, directory, or symbolic link.",
576 ret = WIMLIB_ERR_SPECIAL_FILE;
580 root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
583 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
584 else if (errno == ENOMEM)
585 ret = WIMLIB_ERR_NOMEM;
587 ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
591 inode = root->d_inode;
593 #ifdef HAVE_STAT_NANOSECOND_PRECISION
594 inode->i_creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
595 inode->i_last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
596 inode->i_last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
598 inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
599 inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
600 inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
602 if (sizeof(ino_t) >= 8)
603 inode->i_ino = (u64)root_stbuf.st_ino;
605 inode->i_ino = (u64)root_stbuf.st_ino |
606 ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
607 inode->i_resolved = 1;
608 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
609 ret = inode_set_unix_data(inode, root_stbuf.st_uid,
613 UNIX_DATA_ALL | UNIX_DATA_CREATE);
617 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
618 if (S_ISREG(root_stbuf.st_mode)) { /* Archiving a regular file */
620 struct wim_lookup_table_entry *lte;
621 u8 hash[SHA1_HASH_SIZE];
623 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
625 /* Empty files do not have to have a lookup table entry. */
626 if (root_stbuf.st_size == 0)
629 /* For each regular file, we must check to see if the file is in
630 * the lookup table already; if it is, we increment its refcnt;
631 * otherwise, we create a new lookup table entry and insert it.
634 ret = sha1sum(root_disk_path, hash);
638 lte = __lookup_resource(lookup_table, hash);
641 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
644 char *file_on_disk = STRDUP(root_disk_path);
646 ERROR("Failed to allocate memory for file path");
647 ret = WIMLIB_ERR_NOMEM;
650 lte = new_lookup_table_entry();
653 ret = WIMLIB_ERR_NOMEM;
656 lte->file_on_disk = file_on_disk;
657 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
658 lte->resource_entry.original_size = root_stbuf.st_size;
659 lte->resource_entry.size = root_stbuf.st_size;
660 copy_hash(lte->hash, hash);
661 lookup_table_insert(lookup_table, lte);
663 root->d_inode->i_lte = lte;
664 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
666 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
669 struct dirent entry, *result;
670 struct wim_dentry *child;
672 dir = opendir(root_disk_path);
674 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
676 ret = WIMLIB_ERR_OPEN;
680 /* Buffer for names of files in directory. */
681 size_t len = strlen(root_disk_path);
682 char name[len + 1 + FILENAME_MAX + 1];
683 memcpy(name, root_disk_path, len);
686 /* Create a dentry for each entry in the directory on disk, and recurse
687 * to any subdirectories. */
690 ret = readdir_r(dir, &entry, &result);
692 ret = WIMLIB_ERR_READ;
693 ERROR_WITH_ERRNO("Error reading the "
700 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
701 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
703 strcpy(name + len + 1, result->d_name);
704 ret = build_dentry_tree(&child, name, lookup_table,
705 NULL, config, add_image_flags,
706 progress_func, NULL);
710 dentry_add_child(root, child);
713 } else { /* Archiving a symbolic link */
714 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
715 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
717 /* The idea here is to call readlink() to get the UNIX target of
718 * the symbolic link, then turn the target into a reparse point
719 * data buffer that contains a relative or absolute symbolic
720 * link (NOT a junction point or *full* path symbolic link with
724 char deref_name_buf[4096];
725 ssize_t deref_name_len;
727 deref_name_len = readlink(root_disk_path, deref_name_buf,
728 sizeof(deref_name_buf) - 1);
729 if (deref_name_len >= 0) {
730 deref_name_buf[deref_name_len] = '\0';
731 DEBUG("Read symlink `%s'", deref_name_buf);
732 ret = inode_set_symlink(root->d_inode, deref_name_buf,
736 * Unfortunately, Windows seems to have the
737 * concept of "file" symbolic links as being
738 * different from "directory" symbolic links...
739 * so FILE_ATTRIBUTE_DIRECTORY needs to be set
740 * on the symbolic link if the *target* of the
741 * symbolic link is a directory.
744 if (stat(root_disk_path, &stbuf) == 0 &&
745 S_ISDIR(stbuf.st_mode))
747 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
751 ERROR_WITH_ERRNO("Failed to read target of "
752 "symbolic link `%s'", root_disk_path);
753 ret = WIMLIB_ERR_READLINK;
757 /* Win32 version of capturing a directory tree */
760 size_t path_utf16_nchars;
761 struct sd_set *sd_set;
764 if (extra_arg == NULL) {
765 sd_set = alloca(sizeof(struct sd_set));
766 sd_set->rb_root.rb_node = NULL,
772 ret = utf8_to_utf16(root_disk_path, strlen(root_disk_path),
773 (char**)&path_utf16, &path_utf16_nchars);
775 goto out_destroy_sd_set;
776 path_utf16_nchars /= sizeof(wchar_t);
778 HANDLE hFile = win32_open_file(path_utf16);
779 if (hFile == INVALID_HANDLE_VALUE) {
780 err = GetLastError();
781 ERROR("Win32 API: Failed to open \"%s\"", root_disk_path);
783 ret = WIMLIB_ERR_OPEN;
784 goto out_free_path_utf16;
787 BY_HANDLE_FILE_INFORMATION file_info;
788 if (!GetFileInformationByHandle(hFile, &file_info)) {
789 err = GetLastError();
790 ERROR("Win32 API: Failed to get file information for \"%s\"",
793 ret = WIMLIB_ERR_STAT;
794 goto out_close_handle;
797 /* Create a WIM dentry */
798 root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
801 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
802 else if (errno == ENOMEM)
803 ret = WIMLIB_ERR_NOMEM;
805 ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
806 goto out_free_path_utf16;
809 /* Start preparing the associated WIM inode */
810 inode = root->d_inode;
812 inode->i_attributes = file_info.dwFileAttributes;
813 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
814 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
815 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
816 inode->i_ino = ((u64)file_info.nFileIndexHigh << 32) |
817 (u64)file_info.nFileIndexLow;
819 inode->i_resolved = 1;
820 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
822 /* Get DOS name and security descriptor (if any). */
823 ret = win32_get_short_name(root, path_utf16);
825 goto out_close_handle;
826 ret = win32_get_security_descriptor(root, sd_set, path_utf16,
829 goto out_close_handle;
831 if (inode_is_directory(inode)) {
832 /* Directory (not a reparse point) --- recurse to children */
834 /* But first... directories may have alternate data streams that
835 * need to be captured (maybe?) */
836 ret = win32_capture_streams(root_disk_path,
842 goto out_close_handle;
843 ret = win32_recurse_directory(root,
853 } else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
854 /* Reparse point: save the reparse tag and data */
856 ret = win32_capture_reparse_point(root_disk_path,
862 /* Not a directory, not a reparse point */
863 ret = win32_capture_streams(root_disk_path,
872 if (extra_arg == NULL)
873 destroy_sd_set(sd_set);
881 free_dentry_tree(root, lookup_table);
889 COMPRESSION_EXCLUSION_LIST,
893 #define COMPAT_DEFAULT_CONFIG
895 /* Default capture configuration file when none is specified. */
896 static const char *default_config =
897 #ifdef COMPAT_DEFAULT_CONFIG /* XXX: This policy is being moved to library
898 users. The next ABI-incompatible library
899 version will default to the empty string here. */
904 "\\System Volume Information\n"
908 "[CompressionExclusionList]\n"
912 "\\WINDOWS\\inf\\*.pnf\n";
917 static void destroy_pattern_list(struct pattern_list *list)
922 static void destroy_capture_config(struct capture_config *config)
924 destroy_pattern_list(&config->exclusion_list);
925 destroy_pattern_list(&config->exclusion_exception);
926 destroy_pattern_list(&config->compression_exclusion_list);
927 destroy_pattern_list(&config->alignment_list);
928 FREE(config->config_str);
929 FREE(config->prefix);
930 memset(config, 0, sizeof(*config));
933 static int pattern_list_add_pattern(struct pattern_list *list,
937 if (list->num_pats >= list->num_allocated_pats) {
938 pats = REALLOC(list->pats,
939 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
941 return WIMLIB_ERR_NOMEM;
942 list->num_allocated_pats += 8;
945 list->pats[list->num_pats++] = pattern;
949 /* Parses the contents of the image capture configuration file and fills in a
950 * `struct capture_config'. */
951 static int init_capture_config(struct capture_config *config,
952 const char *_config_str, size_t config_len)
958 size_t bytes_remaining;
959 enum pattern_type type = NONE;
961 unsigned long line_no = 0;
963 DEBUG("config_len = %zu", config_len);
964 bytes_remaining = config_len;
965 memset(config, 0, sizeof(*config));
966 config_str = MALLOC(config_len);
968 ERROR("Could not duplicate capture config string");
969 return WIMLIB_ERR_NOMEM;
972 memcpy(config_str, _config_str, config_len);
974 config->config_str = config_str;
975 while (bytes_remaining) {
978 eol = memchr(p, '\n', bytes_remaining);
980 ERROR("Expected end-of-line in capture config file on "
981 "line %lu", line_no);
982 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
987 bytes_remaining -= (next_p - p);
991 if (*(eol - 1) == '\r')
995 /* Translate backslash to forward slash */
996 for (char *pp = p; pp != eol; pp++)
1000 /* Remove drive letter */
1001 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
1005 if (strcmp(p, "[ExclusionList]") == 0)
1006 type = EXCLUSION_LIST;
1007 else if (strcmp(p, "[ExclusionException]") == 0)
1008 type = EXCLUSION_EXCEPTION;
1009 else if (strcmp(p, "[CompressionExclusionList]") == 0)
1010 type = COMPRESSION_EXCLUSION_LIST;
1011 else if (strcmp(p, "[AlignmentList]") == 0)
1012 type = ALIGNMENT_LIST;
1013 else if (p[0] == '[' && strrchr(p, ']')) {
1014 ERROR("Unknown capture configuration section `%s'", p);
1015 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1016 } else switch (type) {
1017 case EXCLUSION_LIST:
1018 DEBUG("Adding pattern \"%s\" to exclusion list", p);
1019 ret = pattern_list_add_pattern(&config->exclusion_list, p);
1021 case EXCLUSION_EXCEPTION:
1022 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
1023 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
1025 case COMPRESSION_EXCLUSION_LIST:
1026 DEBUG("Adding pattern \"%s\" to compression exclusion list", p);
1027 ret = pattern_list_add_pattern(&config->compression_exclusion_list, p);
1029 case ALIGNMENT_LIST:
1030 DEBUG("Adding pattern \"%s\" to alignment list", p);
1031 ret = pattern_list_add_pattern(&config->alignment_list, p);
1034 ERROR("Line %lu of capture configuration is not "
1035 "in a block (such as [ExclusionList])",
1037 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1045 destroy_capture_config(config);
1049 static int capture_config_set_prefix(struct capture_config *config,
1050 const char *_prefix)
1052 char *prefix = STRDUP(_prefix);
1055 return WIMLIB_ERR_NOMEM;
1056 FREE(config->prefix);
1057 config->prefix = prefix;
1058 config->prefix_len = strlen(prefix);
1062 static bool match_pattern(const char *path, const char *path_basename,
1063 const struct pattern_list *list)
1065 for (size_t i = 0; i < list->num_pats; i++) {
1066 const char *pat = list->pats[i];
1069 /* Absolute path from root of capture */
1072 if (strchr(pat, '/'))
1073 /* Relative path from root of capture */
1076 /* A file name pattern */
1077 string = path_basename;
1079 if (fnmatch(pat, string, FNM_PATHNAME
1085 DEBUG("`%s' matches the pattern \"%s\"",
1093 /* Return true if the image capture configuration file indicates we should
1094 * exclude the filename @path from capture.
1096 * If @exclude_prefix is %true, the part of the path up and including the name
1097 * of the directory being captured is not included in the path for matching
1098 * purposes. This allows, for example, a pattern like /hiberfil.sys to match a
1099 * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
1102 bool exclude_path(const char *path, const struct capture_config *config,
1103 bool exclude_prefix)
1105 const char *basename = path_basename(path);
1106 if (exclude_prefix) {
1107 wimlib_assert(strlen(path) >= config->prefix_len);
1108 if (memcmp(config->prefix, path, config->prefix_len) == 0
1109 && path[config->prefix_len] == '/')
1110 path += config->prefix_len;
1112 return match_pattern(path, basename, &config->exclusion_list) &&
1113 !match_pattern(path, basename, &config->exclusion_exception);
1117 /* Strip leading and trailing forward slashes from a string. Modifies it in
1118 * place and returns the stripped string. */
1119 static const char *canonicalize_target_path(char *target_path)
1122 if (target_path == NULL)
1125 if (*target_path == '\0')
1127 else if (*target_path == '/')
1133 p = target_path + strlen(target_path) - 1;
1139 /* Strip leading and trailing slashes from the target paths */
1140 static void canonicalize_targets(struct wimlib_capture_source *sources,
1143 while (num_sources--) {
1144 DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
1145 sources->fs_source_path,
1146 sources->wim_target_path);
1147 sources->wim_target_path =
1148 (char*)canonicalize_target_path(sources->wim_target_path);
1149 DEBUG("Canonical target: \"%s\"", sources->wim_target_path);
1154 static int capture_source_cmp(const void *p1, const void *p2)
1156 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
1157 return strcmp(s1->wim_target_path, s2->wim_target_path);
1160 /* Sorts the capture sources lexicographically by target path. This occurs
1161 * after leading and trailing forward slashes are stripped.
1163 * One purpose of this is to make sure that target paths that are inside other
1164 * target paths are extracted after the containing target paths. */
1165 static void sort_sources(struct wimlib_capture_source *sources,
1168 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
1171 static int check_sorted_sources(struct wimlib_capture_source *sources,
1172 size_t num_sources, int add_image_flags)
1174 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
1175 if (num_sources != 1) {
1176 ERROR("Must specify exactly 1 capture source "
1177 "(the NTFS volume) in NTFS mode!");
1178 return WIMLIB_ERR_INVALID_PARAM;
1180 if (sources[0].wim_target_path[0] != '\0') {
1181 ERROR("In NTFS capture mode the target path inside "
1182 "the image must be the root directory!");
1183 return WIMLIB_ERR_INVALID_PARAM;
1185 } else if (num_sources != 0) {
1186 /* This code is disabled because the current code
1187 * unconditionally attempts to do overlays. So, duplicate
1188 * target paths are OK. */
1190 if (num_sources > 1 && sources[0].wim_target_path[0] == '\0') {
1191 ERROR("Cannot specify root target when using multiple "
1192 "capture sources!");
1193 return WIMLIB_ERR_INVALID_PARAM;
1195 for (size_t i = 0; i < num_sources - 1; i++) {
1196 size_t len = strlen(sources[i].wim_target_path);
1198 const char *target1 = sources[i].wim_target_path;
1200 const char *target2 = sources[j].wim_target_path;
1201 DEBUG("target1=%s, target2=%s",
1203 if (strncmp(target1, target2, len) ||
1206 if (target2[len] == '/') {
1207 ERROR("Invalid target `%s': is a prefix of `%s'",
1209 return WIMLIB_ERR_INVALID_PARAM;
1211 if (target2[len] == '\0') {
1212 ERROR("Invalid target `%s': is a duplicate of `%s'",
1214 return WIMLIB_ERR_INVALID_PARAM;
1216 } while (++j != num_sources);
1224 /* Creates a new directory to place in the WIM image. This is to create parent
1225 * directories that are not part of any target as needed. */
1226 static struct wim_dentry *
1227 new_filler_directory(const char *name)
1229 struct wim_dentry *dentry;
1230 DEBUG("Creating filler directory \"%s\"", name);
1231 dentry = new_dentry_with_inode(name);
1233 /* Set the inode number to 0 for now. The final inode number
1234 * will be assigned later by assign_inode_numbers(). */
1235 dentry->d_inode->i_ino = 0;
1236 dentry->d_inode->i_resolved = 1;
1237 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1242 /* Transfers the children of @branch to @target. It is an error if @target is
1243 * not a directory or if both @branch and @target contain a child dentry with
1245 static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
1247 struct rb_root *rb_root;
1249 if (!dentry_is_directory(target)) {
1250 ERROR("Cannot overlay directory `%s' over non-directory",
1251 branch->file_name_utf8);
1252 return WIMLIB_ERR_INVALID_OVERLAY;
1255 rb_root = &branch->d_inode->i_children;
1256 while (rb_root->rb_node) { /* While @branch has children... */
1257 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
1258 /* Move @child to the directory @target */
1259 unlink_dentry(child);
1260 if (!dentry_add_child(target, child)) {
1261 /* Revert the change to avoid leaking the directory tree
1262 * rooted at @child */
1263 dentry_add_child(branch, child);
1264 ERROR("Overlay error: file `%s' already exists "
1265 "as a child of `%s'",
1266 child->file_name_utf8, target->file_name_utf8);
1267 return WIMLIB_ERR_INVALID_OVERLAY;
1274 /* Attach or overlay a branch onto the WIM image.
1277 * Pointer to the root of the WIM image, or pointer to NULL if it has not
1282 * Path in the WIM image to add the branch, with leading and trailing
1285 static int attach_branch(struct wim_dentry **root_p,
1286 struct wim_dentry *branch,
1290 struct wim_dentry *dentry, *parent, *target;
1292 if (*target_path == '\0') {
1293 /* Target: root directory */
1295 /* Overlay on existing root */
1296 return do_overlay(*root_p, branch);
1304 /* Adding a non-root branch. Create root if it hasn't been created
1307 *root_p = new_filler_directory("");
1309 return WIMLIB_ERR_NOMEM;
1312 /* Walk the path to the branch, creating filler directories as needed.
1315 while ((slash = strchr(target_path, '/'))) {
1317 dentry = get_dentry_child_with_name(parent, target_path);
1319 dentry = new_filler_directory(target_path);
1321 return WIMLIB_ERR_NOMEM;
1322 dentry_add_child(parent, dentry);
1325 target_path = slash;
1326 /* Skip over slashes. Note: this cannot overrun the length of
1327 * the string because the last character cannot be a slash, as
1328 * trailing slashes were tripped. */
1331 } while (*target_path == '/');
1334 /* If the target path already existed, overlay the branch onto it.
1335 * Otherwise, set the branch as the target path. */
1336 target = get_dentry_child_with_name(parent, branch->file_name_utf8);
1338 return do_overlay(target, branch);
1340 dentry_add_child(parent, branch);
1345 WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
1346 struct wimlib_capture_source *sources,
1349 const char *config_str,
1351 int add_image_flags,
1352 wimlib_progress_func_t progress_func)
1354 int (*capture_tree)(struct wim_dentry **, const char *,
1355 struct wim_lookup_table *,
1356 struct wim_security_data *,
1357 const struct capture_config *,
1358 int, wimlib_progress_func_t, void *);
1360 struct wim_dentry *root_dentry;
1361 struct wim_dentry *branch;
1362 struct wim_security_data *sd;
1363 struct capture_config config;
1364 struct wim_image_metadata *imd;
1367 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
1369 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
1370 ERROR("Cannot dereference files when capturing directly from NTFS");
1371 return WIMLIB_ERR_INVALID_PARAM;
1373 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
1374 ERROR("Capturing UNIX owner and mode not supported "
1375 "when capturing directly from NTFS");
1376 return WIMLIB_ERR_INVALID_PARAM;
1378 capture_tree = build_dentry_tree_ntfs;
1379 extra_arg = &w->ntfs_vol;
1381 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1382 " cannot capture a WIM image directly from a NTFS volume!");
1383 return WIMLIB_ERR_UNSUPPORTED;
1386 capture_tree = build_dentry_tree;
1390 #if defined(__CYGWIN__) || defined(__WIN32__)
1391 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
1392 ERROR("Capturing UNIX-specific data is not supported on Windows");
1393 return WIMLIB_ERR_INVALID_PARAM;
1395 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
1396 ERROR("Dereferencing symbolic links is not supported on Windows");
1397 return WIMLIB_ERR_INVALID_PARAM;
1401 if (!name || !*name) {
1402 ERROR("Must specify a non-empty string for the image name");
1403 return WIMLIB_ERR_INVALID_PARAM;
1406 if (w->hdr.total_parts != 1) {
1407 ERROR("Cannot add an image to a split WIM");
1408 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1411 if (wimlib_image_name_in_use(w, name)) {
1412 ERROR("There is already an image named \"%s\" in `%s'",
1414 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1418 DEBUG("Using default capture configuration");
1419 config_str = default_config;
1420 config_len = strlen(default_config);
1422 ret = init_capture_config(&config, config_str, config_len);
1426 DEBUG("Allocating security data");
1427 sd = CALLOC(1, sizeof(struct wim_security_data));
1429 ret = WIMLIB_ERR_NOMEM;
1430 goto out_destroy_capture_config;
1432 sd->total_length = 8;
1435 DEBUG("Using %zu capture sources", num_sources);
1436 canonicalize_targets(sources, num_sources);
1437 sort_sources(sources, num_sources);
1438 ret = check_sorted_sources(sources, num_sources, add_image_flags);
1440 ret = WIMLIB_ERR_INVALID_PARAM;
1441 goto out_free_security_data;
1444 DEBUG("Building dentry tree.");
1445 if (num_sources == 0) {
1446 root_dentry = new_filler_directory("");
1448 goto out_free_security_data;
1456 union wimlib_progress_info progress;
1458 DEBUG("Building dentry tree for source %zu of %zu "
1459 "(\"%s\" => \"%s\")", i + 1, num_sources,
1460 sources[i].fs_source_path,
1461 sources[i].wim_target_path);
1462 if (progress_func) {
1463 memset(&progress, 0, sizeof(progress));
1464 progress.scan.source = sources[i].fs_source_path;
1465 progress.scan.wim_target_path = sources[i].wim_target_path;
1466 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
1468 ret = capture_config_set_prefix(&config,
1469 sources[i].fs_source_path);
1471 goto out_free_dentry_tree;
1472 flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
1473 if (!*sources[i].wim_target_path)
1474 flags |= WIMLIB_ADD_IMAGE_FLAG_ROOT;
1475 ret = (*capture_tree)(&branch, sources[i].fs_source_path,
1476 w->lookup_table, sd,
1479 progress_func, extra_arg);
1481 ERROR("Failed to build dentry tree for `%s'",
1482 sources[i].fs_source_path);
1483 goto out_free_dentry_tree;
1486 /* Use the target name, not the source name, for
1487 * the root of each branch from a capture
1488 * source. (This will also set the root dentry
1489 * of the entire image to be unnamed.) */
1490 ret = set_dentry_name(branch,
1491 path_basename(sources[i].wim_target_path));
1493 goto out_free_branch;
1495 ret = attach_branch(&root_dentry, branch,
1496 sources[i].wim_target_path);
1498 goto out_free_branch;
1501 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1502 } while (++i != num_sources);
1505 DEBUG("Calculating full paths of dentries.");
1506 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1508 goto out_free_dentry_tree;
1510 ret = add_new_dentry_tree(w, root_dentry, sd);
1512 goto out_free_dentry_tree;
1514 imd = &w->image_metadata[w->hdr.image_count - 1];
1516 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1518 goto out_destroy_imd;
1520 DEBUG("Assigning hard link group IDs");
1521 assign_inode_numbers(&imd->inode_list);
1523 ret = xml_add_image(w, name);
1525 goto out_destroy_imd;
1527 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1528 wimlib_set_boot_idx(w, w->hdr.image_count);
1532 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1534 w->hdr.image_count--;
1537 free_dentry_tree(branch, w->lookup_table);
1538 out_free_dentry_tree:
1539 free_dentry_tree(root_dentry, w->lookup_table);
1540 out_free_security_data:
1541 free_security_data(sd);
1542 out_destroy_capture_config:
1543 destroy_capture_config(&config);
1548 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *source,
1549 const char *name, const char *config_str,
1550 size_t config_len, int add_image_flags,
1551 wimlib_progress_func_t progress_func)
1553 if (!source || !*source)
1554 return WIMLIB_ERR_INVALID_PARAM;
1556 char *fs_source_path = STRDUP(source);
1558 struct wimlib_capture_source capture_src = {
1559 .fs_source_path = fs_source_path,
1560 .wim_target_path = NULL,
1563 ret = wimlib_add_image_multisource(w, &capture_src, 1, name,
1564 config_str, config_len,
1565 add_image_flags, progress_func);
1566 FREE(fs_source_path);