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/.
25 #if defined(__CYGWIN__) || defined(__WIN32__)
29 # include <sys/cygwin.h>
34 # include "security.h"
37 #include "wimlib_internal.h"
39 #include "timestamp.h"
40 #include "lookup_table.h"
51 #if defined(__CYGWIN__) || defined(__WIN32__)
52 /*#define ERROR_WIN32_SAFE(format, ...) \*/
54 /*DWORD err = GetLastError(); \*/
55 /*ERROR(format, ##__VA_ARGS__); \*/
56 /*SetLastError(err); \*/
58 #define DEBUG_WIN32_SAFE(format, ...) \
60 DWORD err = GetLastError(); \
61 DEBUG(format, ##__VA_ARGS__); \
66 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
67 #define WIMLIB_ADD_IMAGE_FLAG_SOURCE 0x40000000
70 * Adds the dentry tree and security data for a new image to the image metadata
71 * array of the WIMStruct.
73 int add_new_dentry_tree(WIMStruct *w, struct wim_dentry *root_dentry,
74 struct wim_security_data *sd)
76 struct wim_lookup_table_entry *metadata_lte;
77 struct wim_image_metadata *imd;
78 struct wim_image_metadata *new_imd;
80 wimlib_assert(root_dentry != NULL);
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));
87 ERROR("Failed to allocate memory for new image metadata array");
91 memcpy(imd, w->image_metadata,
92 w->hdr.image_count * sizeof(struct wim_image_metadata));
94 metadata_lte = new_lookup_table_entry();
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);
102 new_imd = &imd[w->hdr.image_count];
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;
109 FREE(w->image_metadata);
110 w->image_metadata = imd;
111 w->hdr.image_count++;
116 return WIMLIB_ERR_NOMEM;
120 #if defined(__CYGWIN__) || defined(__WIN32__)
121 static u64 FILETIME_to_u64(const FILETIME *ft)
123 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
126 #ifdef ENABLE_ERROR_MESSAGES
127 static void win32_error(DWORD err_code)
131 nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
133 (char*)&buffer, 0, NULL);
135 ERROR("Error printing error message! "
136 "Computer will self-destruct in 3 seconds.");
138 ERROR("Win32 error: %s", buffer);
143 #define win32_error(err_code)
146 static HANDLE win32_open_file(const wchar_t *path)
148 return CreateFileW(path,
149 GENERIC_READ | READ_CONTROL,
151 NULL, /* lpSecurityAttributes */
153 FILE_FLAG_BACKUP_SEMANTICS |
154 FILE_FLAG_OPEN_REPARSE_POINT,
155 NULL /* hTemplateFile */);
158 int win32_read_file(const char *filename,
159 void *handle, u64 offset, size_t size, u8 *buf)
164 LARGE_INTEGER liOffset = {.QuadPart = offset};
166 wimlib_assert(size <= 0xffffffff);
168 if (SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
169 if (ReadFile(h, buf, size, &bytesRead, NULL) && bytesRead == size)
171 err = GetLastError();
172 ERROR("Error reading \"%s\"", filename);
174 return WIMLIB_ERR_READ;
177 void win32_close_handle(void *handle)
179 CloseHandle((HANDLE)handle);
182 void *win32_open_handle(const char *path_utf16)
184 return (void*)win32_open_file((const wchar_t*)path_utf16);
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,
193 wimlib_progress_func_t progress_func,
196 static int win32_get_short_name(struct wim_dentry *dentry,
197 const wchar_t *path_utf16)
199 WIN32_FIND_DATAW dat;
200 if (FindFirstFileW(path_utf16, &dat) &&
201 dat.cAlternateFileName[0] != L'\0')
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;
214 static int win32_get_security_descriptor(struct wim_dentry *dentry,
215 struct sd_set *sd_set,
216 const wchar_t *path_utf16,
219 SECURITY_INFORMATION requestedInformation;
224 #ifdef BACKUP_SECURITY_INFORMATION
225 requestedInformation = BACKUP_SECURITY_INFORMATION;
227 requestedInformation = DACL_SECURITY_INFORMATION |
228 SACL_SECURITY_INFORMATION |
229 OWNER_SECURITY_INFORMATION |
230 GROUP_SECURITY_INFORMATION;
232 /* Request length of security descriptor */
233 status = GetFileSecurityW(path_utf16, requestedInformation,
234 NULL, 0, &lenNeeded);
235 err = GetLastError();
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;
242 if (GetFileSecurityW(path_utf16, requestedInformation,
243 buf, len, &lenNeeded))
245 int security_id = sd_set_add_sd(sd_set, buf, len);
247 return WIMLIB_ERR_NOMEM;
249 dentry->d_inode->i_security_id = security_id;
253 err = GetLastError();
256 ERROR("Win32 API: Failed to read security descriptor of \"%s\"",
259 return WIMLIB_ERR_READ;
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,
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)
274 WIN32_FIND_DATAW dat;
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);
288 if (hFind == INVALID_HANDLE_VALUE) {
289 err = GetLastError();
290 if (err == ERROR_FILE_NOT_FOUND) {
293 ERROR("Win32 API: Failed to read directory \"%s\"",
296 return WIMLIB_ERR_READ;
301 if (!(dat.cFileName[0] == L'.' &&
302 (dat.cFileName[1] == L'\0' ||
303 (dat.cFileName[1] == L'.' && dat.cFileName[2] == L'\0'))))
305 struct wim_dentry *child;
308 size_t utf8_name_nbytes;
309 ret = utf16_to_utf8((const char*)dat.cFileName,
310 wcslen(dat.cFileName) * sizeof(wchar_t),
316 char name[strlen(root_disk_path) + utf8_name_nbytes + 1];
317 sprintf(name, "%s/%s", root_disk_path, utf8_name);
319 ret = build_dentry_tree(&child, name, lookup_table,
320 sd, config, add_image_flags,
321 progress_func, sd_set);
325 dentry_add_child(root, child);
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);
333 ret = WIMLIB_ERR_READ;
340 static int win32_capture_reparse_point(const char *path,
342 struct wim_inode *inode,
343 struct wim_lookup_table *lookup_table)
345 /* "Reparse point data, including the tag and optional GUID,
346 * cannot exceed 16 kilobytes." - MSDN */
347 char reparse_point_buf[16 * 1024];
349 const REPARSE_DATA_BUFFER *buf;
351 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
352 NULL, 0, reparse_point_buf,
353 sizeof(reparse_point_buf), &bytesReturned, NULL))
355 ERROR("Win32 API: Failed to get reparse data of \"%s\"", path);
356 return WIMLIB_ERR_READ;
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);
364 static int win32_sha1sum(const wchar_t *path, u8 hash[SHA1_HASH_SIZE])
372 hFile = win32_open_file(path);
373 if (hFile == INVALID_HANDLE_VALUE)
374 return WIMLIB_ERR_OPEN;
378 if (!ReadFile(hFile, buf, sizeof(buf), &bytesRead, NULL)) {
379 ret = WIMLIB_ERR_READ;
380 goto out_close_handle;
384 sha1_update(&ctx, buf, bytesRead);
387 sha1_final(hash, &ctx);
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)
400 struct wim_ads_entry *ads_entry;
401 u8 hash[SHA1_HASH_SIZE];
402 struct wim_lookup_table_entry *lte;
405 bool is_named_stream;
410 p = dat->cStreamName;
411 wimlib_assert(*p == L':');
413 colon = wcschr(p, L':');
414 wimlib_assert(colon != NULL);
416 if (wcscmp(colon + 1, L"$DATA")) {
417 /* Not a DATA stream */
422 is_named_stream = (p != colon);
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,
430 &utf8_stream_name_len);
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);
437 ret = WIMLIB_ERR_NOMEM;
443 spath_nchars = path_utf16_nchars;
445 spath_nchars += colon - p + 1;
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));
453 spath[spath_nchars] = L'\0';
455 ret = win32_sha1sum(spath, hash);
457 err = GetLastError();
458 ERROR("Win32 API: Failed to read \"%s\" to calculate SHA1sum", path);
463 lte = __lookup_resource(lookup_table, hash);
467 lte = new_lookup_table_entry();
469 ret = WIMLIB_ERR_NOMEM;
472 lte->file_on_disk = (char*)spath;
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);
481 ads_entry->lte = lte;
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)
496 WIN32_FIND_STREAM_DATA dat;
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\"",
505 return WIMLIB_ERR_READ;
508 ret = win32_capture_stream(path, path_utf16,
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);
519 ret = WIMLIB_ERR_READ;
528 * build_dentry_tree():
529 * Recursively builds a tree of WIM dentries from an on-disk directory
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.
536 * @root_disk_path: The path to the root of the directory tree on disk.
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
544 * @sd: Ignored. (Security data only captured in NTFS mode.)
547 * Configuration for files to be excluded from capture.
549 * @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
551 * @extra_arg: Ignored. (Only used in NTFS mode.)
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().
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,
566 wimlib_progress_func_t progress_func,
569 struct wim_dentry *root = NULL;
571 struct wim_inode *inode;
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;
579 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
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);
590 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
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);
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)
608 ret = (*stat_fn)(root_disk_path, &root_stbuf);
610 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
614 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
615 !S_ISDIR(root_stbuf.st_mode))
617 /* Do a dereference-stat in case the root is a symbolic link.
618 * This case is allowed, provided that the symbolic link points
620 ret = stat(root_disk_path, &root_stbuf);
622 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
623 ret = WIMLIB_ERR_STAT;
626 if (!S_ISDIR(root_stbuf.st_mode)) {
627 ERROR("`%s' is not a directory", root_disk_path);
628 ret = WIMLIB_ERR_NOTDIR;
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.",
636 ret = WIMLIB_ERR_SPECIAL_FILE;
640 root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
643 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
644 else if (errno == ENOMEM)
645 ret = WIMLIB_ERR_NOMEM;
647 ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
651 inode = root->d_inode;
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);
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);
662 if (sizeof(ino_t) >= 8)
663 inode->i_ino = (u64)root_stbuf.st_ino;
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,
673 UNIX_DATA_ALL | UNIX_DATA_CREATE);
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 */
680 struct wim_lookup_table_entry *lte;
681 u8 hash[SHA1_HASH_SIZE];
683 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
685 /* Empty files do not have to have a lookup table entry. */
686 if (root_stbuf.st_size == 0)
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.
694 ret = sha1sum(root_disk_path, hash);
698 lte = __lookup_resource(lookup_table, hash);
701 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
704 char *file_on_disk = STRDUP(root_disk_path);
706 ERROR("Failed to allocate memory for file path");
707 ret = WIMLIB_ERR_NOMEM;
710 lte = new_lookup_table_entry();
713 ret = WIMLIB_ERR_NOMEM;
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);
723 root->d_inode->i_lte = lte;
724 } else if (S_ISDIR(root_stbuf.st_mode)) { /* Archiving a directory */
726 inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
729 struct dirent entry, *result;
730 struct wim_dentry *child;
732 dir = opendir(root_disk_path);
734 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
736 ret = WIMLIB_ERR_OPEN;
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);
746 /* Create a dentry for each entry in the directory on disk, and recurse
747 * to any subdirectories. */
750 ret = readdir_r(dir, &entry, &result);
752 ret = WIMLIB_ERR_READ;
753 ERROR_WITH_ERRNO("Error reading the "
760 if (result->d_name[0] == '.' && (result->d_name[1] == '\0'
761 || (result->d_name[1] == '.' && result->d_name[2] == '\0')))
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);
770 dentry_add_child(root, child);
773 } else { /* Archiving a symbolic link */
774 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
775 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
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
784 char deref_name_buf[4096];
785 ssize_t deref_name_len;
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,
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.
804 if (stat(root_disk_path, &stbuf) == 0 &&
805 S_ISDIR(stbuf.st_mode))
807 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
811 ERROR_WITH_ERRNO("Failed to read target of "
812 "symbolic link `%s'", root_disk_path);
813 ret = WIMLIB_ERR_READLINK;
817 /* Win32 version of capturing a directory tree */
820 size_t path_utf16_nchars;
821 struct sd_set *sd_set;
824 if (extra_arg == NULL) {
825 sd_set = alloca(sizeof(struct sd_set));
826 sd_set->rb_root.rb_node = NULL,
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);
836 goto out_destroy_sd_set;
837 path_utf16_nchars /= sizeof(wchar_t);
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);
845 ret = WIMLIB_ERR_OPEN;
846 goto out_free_path_utf16;
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\"",
855 ret = WIMLIB_ERR_STAT;
856 goto out_close_handle;
859 /* Create a WIM dentry */
860 root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
863 ret = WIMLIB_ERR_INVALID_UTF8_STRING;
864 else if (errno == ENOMEM)
865 ret = WIMLIB_ERR_NOMEM;
867 ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
868 goto out_free_path_utf16;
871 /* Start preparing the associated WIM inode */
872 inode = root->d_inode;
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;
881 inode->i_resolved = 1;
882 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
884 /* Get DOS name and security descriptor (if any). */
885 ret = win32_get_short_name(root, path_utf16);
887 goto out_close_handle;
888 ret = win32_get_security_descriptor(root, sd_set, path_utf16,
891 goto out_close_handle;
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,
906 } else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
907 /* Reparse point: save the reparse tag and data */
909 DEBUG_WIN32_SAFE("Capturing reparse point `%s'", root_disk_path);
910 ret = win32_capture_reparse_point(root_disk_path,
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,
927 if (extra_arg == NULL)
928 destroy_sd_set(sd_set);
936 free_dentry_tree(root, lookup_table);
944 COMPRESSION_EXCLUSION_LIST,
948 #define COMPAT_DEFAULT_CONFIG
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. */
959 "\\System Volume Information\n"
963 "[CompressionExclusionList]\n"
967 "\\WINDOWS\\inf\\*.pnf\n";
972 static void destroy_pattern_list(struct pattern_list *list)
977 static void destroy_capture_config(struct capture_config *config)
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));
988 static int pattern_list_add_pattern(struct pattern_list *list,
992 if (list->num_pats >= list->num_allocated_pats) {
993 pats = REALLOC(list->pats,
994 sizeof(list->pats[0]) * (list->num_allocated_pats + 8));
996 return WIMLIB_ERR_NOMEM;
997 list->num_allocated_pats += 8;
1000 list->pats[list->num_pats++] = pattern;
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)
1013 size_t bytes_remaining;
1014 enum pattern_type type = NONE;
1016 unsigned long line_no = 0;
1018 DEBUG("config_len = %zu", config_len);
1019 bytes_remaining = config_len;
1020 memset(config, 0, sizeof(*config));
1021 config_str = MALLOC(config_len);
1023 ERROR("Could not duplicate capture config string");
1024 return WIMLIB_ERR_NOMEM;
1027 memcpy(config_str, _config_str, config_len);
1028 next_p = config_str;
1029 config->config_str = config_str;
1030 while (bytes_remaining) {
1033 eol = memchr(p, '\n', bytes_remaining);
1035 ERROR("Expected end-of-line in capture config file on "
1036 "line %lu", line_no);
1037 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1042 bytes_remaining -= (next_p - p);
1046 if (*(eol - 1) == '\r')
1050 /* Translate backslash to forward slash */
1051 for (char *pp = p; pp != eol; pp++)
1055 /* Remove drive letter */
1056 if (eol - p > 2 && isalpha(*p) && *(p + 1) == ':')
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);
1076 case EXCLUSION_EXCEPTION:
1077 DEBUG("Adding pattern \"%s\" to exclusion exception list", p);
1078 ret = pattern_list_add_pattern(&config->exclusion_exception, p);
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);
1084 case ALIGNMENT_LIST:
1085 DEBUG("Adding pattern \"%s\" to alignment list", p);
1086 ret = pattern_list_add_pattern(&config->alignment_list, p);
1089 ERROR("Line %lu of capture configuration is not "
1090 "in a block (such as [ExclusionList])",
1092 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1100 destroy_capture_config(config);
1104 static int capture_config_set_prefix(struct capture_config *config,
1105 const char *_prefix)
1107 char *prefix = STRDUP(_prefix);
1110 return WIMLIB_ERR_NOMEM;
1111 FREE(config->prefix);
1112 config->prefix = prefix;
1113 config->prefix_len = strlen(prefix);
1117 static bool match_pattern(const char *path, const char *path_basename,
1118 const struct pattern_list *list)
1120 for (size_t i = 0; i < list->num_pats; i++) {
1121 const char *pat = list->pats[i];
1124 /* Absolute path from root of capture */
1127 if (strchr(pat, '/'))
1128 /* Relative path from root of capture */
1131 /* A file name pattern */
1132 string = path_basename;
1134 if (fnmatch(pat, string, FNM_PATHNAME
1140 DEBUG("`%s' matches the pattern \"%s\"",
1148 /* Return true if the image capture configuration file indicates we should
1149 * exclude the filename @path from capture.
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
1157 bool exclude_path(const char *path, const struct capture_config *config,
1158 bool exclude_prefix)
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;
1167 return match_pattern(path, basename, &config->exclusion_list) &&
1168 !match_pattern(path, basename, &config->exclusion_exception);
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)
1177 if (target_path == NULL)
1180 if (*target_path == '\0')
1182 else if (*target_path == '/')
1188 p = target_path + strlen(target_path) - 1;
1194 /* Strip leading and trailing slashes from the target paths */
1195 static void canonicalize_targets(struct wimlib_capture_source *sources,
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);
1209 static int capture_source_cmp(const void *p1, const void *p2)
1211 const struct wimlib_capture_source *s1 = p1, *s2 = p2;
1212 return strcmp(s1->wim_target_path, s2->wim_target_path);
1215 /* Sorts the capture sources lexicographically by target path. This occurs
1216 * after leading and trailing forward slashes are stripped.
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,
1223 qsort(sources, num_sources, sizeof(sources[0]), capture_source_cmp);
1226 static int check_sorted_sources(struct wimlib_capture_source *sources,
1227 size_t num_sources, int add_image_flags)
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;
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;
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. */
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;
1250 for (size_t i = 0; i < num_sources - 1; i++) {
1251 size_t len = strlen(sources[i].wim_target_path);
1253 const char *target1 = sources[i].wim_target_path;
1255 const char *target2 = sources[j].wim_target_path;
1256 DEBUG("target1=%s, target2=%s",
1258 if (strncmp(target1, target2, len) ||
1261 if (target2[len] == '/') {
1262 ERROR("Invalid target `%s': is a prefix of `%s'",
1264 return WIMLIB_ERR_INVALID_PARAM;
1266 if (target2[len] == '\0') {
1267 ERROR("Invalid target `%s': is a duplicate of `%s'",
1269 return WIMLIB_ERR_INVALID_PARAM;
1271 } while (++j != num_sources);
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)
1284 struct wim_dentry *dentry;
1285 DEBUG("Creating filler directory \"%s\"", name);
1286 dentry = new_dentry_with_inode(name);
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;
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
1300 static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
1302 struct rb_root *rb_root;
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;
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;
1329 /* Attach or overlay a branch onto the WIM image.
1332 * Pointer to the root of the WIM image, or pointer to NULL if it has not
1337 * Path in the WIM image to add the branch, with leading and trailing
1340 static int attach_branch(struct wim_dentry **root_p,
1341 struct wim_dentry *branch,
1345 struct wim_dentry *dentry, *parent, *target;
1347 if (*target_path == '\0') {
1348 /* Target: root directory */
1350 /* Overlay on existing root */
1351 return do_overlay(*root_p, branch);
1359 /* Adding a non-root branch. Create root if it hasn't been created
1362 *root_p = new_filler_directory("");
1364 return WIMLIB_ERR_NOMEM;
1367 /* Walk the path to the branch, creating filler directories as needed.
1370 while ((slash = strchr(target_path, '/'))) {
1372 dentry = get_dentry_child_with_name(parent, target_path);
1374 dentry = new_filler_directory(target_path);
1376 return WIMLIB_ERR_NOMEM;
1377 dentry_add_child(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. */
1386 } while (*target_path == '/');
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);
1393 return do_overlay(target, branch);
1395 dentry_add_child(parent, branch);
1400 WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
1401 struct wimlib_capture_source *sources,
1404 const char *config_str,
1406 int add_image_flags,
1407 wimlib_progress_func_t progress_func)
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 *);
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;
1422 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NTFS) {
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;
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;
1433 capture_tree = build_dentry_tree_ntfs;
1434 extra_arg = &w->ntfs_vol;
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;
1441 capture_tree = build_dentry_tree;
1445 if (!name || !*name) {
1446 ERROR("Must specify a non-empty string for the image name");
1447 return WIMLIB_ERR_INVALID_PARAM;
1450 if (w->hdr.total_parts != 1) {
1451 ERROR("Cannot add an image to a split WIM");
1452 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1455 if (wimlib_image_name_in_use(w, name)) {
1456 ERROR("There is already an image named \"%s\" in `%s'",
1458 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
1462 DEBUG("Using default capture configuration");
1463 config_str = default_config;
1464 config_len = strlen(default_config);
1466 ret = init_capture_config(&config, config_str, config_len);
1470 DEBUG("Allocating security data");
1471 sd = CALLOC(1, sizeof(struct wim_security_data));
1473 ret = WIMLIB_ERR_NOMEM;
1474 goto out_destroy_capture_config;
1476 sd->total_length = 8;
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);
1484 ret = WIMLIB_ERR_INVALID_PARAM;
1485 goto out_free_security_data;
1488 DEBUG("Building dentry tree.");
1489 if (num_sources == 0) {
1490 root_dentry = new_filler_directory("");
1492 goto out_free_security_data;
1500 union wimlib_progress_info progress;
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);
1512 ret = capture_config_set_prefix(&config,
1513 sources[i].fs_source_path);
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,
1523 progress_func, extra_arg);
1525 ERROR("Failed to build dentry tree for `%s'",
1526 sources[i].fs_source_path);
1527 goto out_free_dentry_tree;
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));
1537 goto out_free_branch;
1539 ret = attach_branch(&root_dentry, branch,
1540 sources[i].wim_target_path);
1542 goto out_free_branch;
1545 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
1546 } while (++i != num_sources);
1549 DEBUG("Calculating full paths of dentries.");
1550 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
1552 goto out_free_dentry_tree;
1554 ret = add_new_dentry_tree(w, root_dentry, sd);
1556 goto out_free_dentry_tree;
1558 imd = &w->image_metadata[w->hdr.image_count - 1];
1560 ret = dentry_tree_fix_inodes(root_dentry, &imd->inode_list);
1562 goto out_destroy_imd;
1564 DEBUG("Assigning hard link group IDs");
1565 assign_inode_numbers(&imd->inode_list);
1567 ret = xml_add_image(w, name);
1569 goto out_destroy_imd;
1571 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
1572 wimlib_set_boot_idx(w, w->hdr.image_count);
1576 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
1578 w->hdr.image_count--;
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);
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)
1597 if (!source || !*source)
1598 return WIMLIB_ERR_INVALID_PARAM;
1600 char *fs_source_path = STRDUP(source);
1602 struct wimlib_capture_source capture_src = {
1603 .fs_source_path = fs_source_path,
1604 .wim_target_path = NULL,
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);