4 * All the library code specific to native Windows builds is in here.
8 * Copyright (C) 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
27 # error "This file contains Windows code"
34 #include <shlwapi.h> /* shlwapi.h for PathMatchSpecA() */
35 #ifdef ERROR /* windows.h defines this */
41 #include "lookup_table.h"
43 #include "endianness.h"
49 /* Pointers to functions that are not available on all targetted versions of
50 * Windows (XP and later). NOTE: The WINAPI annotations seem to be important; I
51 * assume it specifies a certain calling convention. */
54 static HANDLE (WINAPI *win32func_FindFirstStreamW)(LPCWSTR lpFileName,
55 STREAM_INFO_LEVELS InfoLevel,
56 LPVOID lpFindStreamData,
57 DWORD dwFlags) = NULL;
60 static BOOL (WINAPI *win32func_FindNextStreamW)(HANDLE hFindStream,
61 LPVOID lpFindStreamData) = NULL;
63 /* Try to dynamically load some functions */
70 DEBUG("Loading Kernel32.dll");
72 HMODULE lib = LoadLibraryA("Kernel32.dll");
75 WARNING("Can't load Kernel32.dll");
80 DEBUG("Looking for FindFirstStreamW");
81 win32func_FindFirstStreamW = (void*)GetProcAddress(lib, "FindFirstStreamW");
82 if (!win32func_FindFirstStreamW) {
83 WARNING("Could not find function FindFirstStreamW() in Kernel32.dll!");
84 WARNING("Capturing alternate data streams will not be supported.");
88 DEBUG("Looking for FindNextStreamW");
89 win32func_FindNextStreamW = (void*)GetProcAddress(lib, "FindNextStreamW");
90 if (!win32func_FindNextStreamW) {
91 WARNING("Could not find function FindNextStreamW() in Kernel32.dll!");
92 WARNING("Capturing alternate data streams will not be supported.");
93 win32func_FindFirstStreamW = NULL;
96 DEBUG("Closing Kernel32.dll");
100 static const char *access_denied_msg =
101 " If you are not running this program as the administrator, you may\n"
102 " need to do so, so that all data and metadata can be backed up.\n"
103 " Otherwise, there may be no way to access the desired data or\n"
104 " metadata without taking ownership of the file or directory.\n";
106 #ifdef ENABLE_ERROR_MESSAGES
108 win32_error(u32 err_code)
112 nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
114 (char*)&buffer, 0, NULL);
116 ERROR("Error printing error message! "
117 "Computer will self-destruct in 3 seconds.");
119 ERROR("Win32 error: %s", buffer);
127 win32_error(GetLastError());
132 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
134 return CreateFileW(path,
137 NULL, /* lpSecurityAttributes */
139 FILE_FLAG_BACKUP_SEMANTICS |
140 FILE_FLAG_OPEN_REPARSE_POINT,
141 NULL /* hTemplateFile */);
145 win32_open_file_data_only(const wchar_t *path)
147 return win32_open_existing_file(path, FILE_READ_DATA);
151 win32_read_file(const mbchar *filename,
152 void *handle, u64 offset, size_t size, void *buf)
157 LARGE_INTEGER liOffset = {.QuadPart = offset};
159 wimlib_assert(size <= 0xffffffff);
161 if (SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
162 if (ReadFile(h, buf, size, &bytesRead, NULL) && bytesRead == size)
164 err = GetLastError();
165 ERROR("Error reading \"%s\"", filename);
167 return WIMLIB_ERR_READ;
171 win32_close_file(void *handle)
173 CloseHandle((HANDLE)handle);
177 FILETIME_to_u64(const FILETIME *ft)
179 return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
183 win32_get_short_name(struct wim_dentry *dentry,
184 const wchar_t *path_utf16)
186 WIN32_FIND_DATAW dat;
187 if (FindFirstFileW(path_utf16, &dat) &&
188 dat.cAlternateFileName[0] != L'\0')
190 size_t short_name_nbytes = wcslen(dat.cAlternateFileName) * 2;
191 size_t n = short_name_nbytes + sizeof(wchar_t);
192 dentry->short_name = MALLOC(n);
193 if (!dentry->short_name)
194 return WIMLIB_ERR_NOMEM;
195 memcpy(dentry->short_name, dat.cAlternateFileName, n);
196 dentry->short_name_nbytes = short_name_nbytes;
202 win32_get_security_descriptor(struct wim_dentry *dentry,
203 struct sd_set *sd_set,
204 const wchar_t *path_utf16)
206 SECURITY_INFORMATION requestedInformation;
211 requestedInformation = DACL_SECURITY_INFORMATION |
212 SACL_SECURITY_INFORMATION |
213 OWNER_SECURITY_INFORMATION |
214 GROUP_SECURITY_INFORMATION;
215 /* Request length of security descriptor */
216 status = GetFileSecurityW(path_utf16, requestedInformation,
217 NULL, 0, &lenNeeded);
218 err = GetLastError();
219 if (!status && err == ERROR_INSUFFICIENT_BUFFER) {
220 DWORD len = lenNeeded;
222 if (GetFileSecurityW(path_utf16, requestedInformation,
223 (PSECURITY_DESCRIPTOR)buf, len, &lenNeeded))
225 int security_id = sd_set_add_sd(sd_set, buf, len);
227 return WIMLIB_ERR_NOMEM;
229 dentry->d_inode->i_security_id = security_id;
233 err = GetLastError();
237 if (err == ERROR_ACCESS_DENIED) {
238 WARNING("Failed to read security descriptor of \"%ls\": "
239 "Access denied!\n%s", path_utf16, access_denied_msg);
242 ERROR("Win32 API: Failed to read security descriptor of \"%ls\"",
245 return WIMLIB_ERR_READ;
249 /* Reads the directory entries of directory using a Win32 API and recursively
250 * calls win32_build_dentry_tree() on them. */
252 win32_recurse_directory(struct wim_dentry *root,
253 const mbchar *root_disk_path,
254 struct wim_lookup_table *lookup_table,
255 struct wim_security_data *sd,
256 const struct capture_config *config,
258 wimlib_progress_func_t progress_func,
259 struct sd_set *sd_set,
260 const wchar_t *path_utf16,
261 size_t path_utf16_nchars)
263 WIN32_FIND_DATAW dat;
269 /* Begin reading the directory by calling FindFirstFileW.
270 * Unlike UNIX opendir(), FindFirstFileW has file globbing built
271 * into it. But this isn't what we actually want, so just add a
272 * dummy glob to get all entries. */
273 wchar_t pattern_buf[path_utf16_nchars + 3];
274 memcpy(pattern_buf, path_utf16,
275 path_utf16_nchars * sizeof(wchar_t));
276 pattern_buf[path_utf16_nchars] = L'/';
277 pattern_buf[path_utf16_nchars + 1] = L'*';
278 pattern_buf[path_utf16_nchars + 2] = L'\0';
279 hFind = FindFirstFileW(pattern_buf, &dat);
281 if (hFind == INVALID_HANDLE_VALUE) {
282 err = GetLastError();
283 if (err == ERROR_FILE_NOT_FOUND) {
286 ERROR("Win32 API: Failed to read directory \"%s\"",
289 return WIMLIB_ERR_READ;
294 /* Skip . and .. entries */
295 if (!(dat.cFileName[0] == cpu_to_le16(L'.') &&
296 (dat.cFileName[1] == cpu_to_le16(L'\0') ||
297 (dat.cFileName[1] == cpu_to_le16(L'.') &&
298 dat.cFileName[2] == cpu_to_le16(L'\0')))))
300 struct wim_dentry *child;
303 size_t mbs_name_nbytes;
304 ret = utf16le_to_mbs(dat.cFileName,
305 wcslen(dat.cFileName) * sizeof(wchar_t),
311 mbchar name[strlen(root_disk_path) + 1 + mbs_name_nbytes + 1];
312 sprintf(name, "%s/%s", root_disk_path, mbs_name);
314 ret = win32_build_dentry_tree(&child, name, lookup_table,
315 sd, config, add_image_flags,
316 progress_func, sd_set);
320 dentry_add_child(root, child);
322 } while (FindNextFileW(hFind, &dat));
323 err = GetLastError();
324 if (err != ERROR_NO_MORE_FILES) {
325 ERROR("Win32 API: Failed to read directory \"%s\"", root_disk_path);
328 ret = WIMLIB_ERR_READ;
335 /* Load a reparse point into a WIM inode. It is just stored in memory.
337 * @hFile: Open handle to a reparse point, with permission to read the reparse
340 * @inode: WIM inode for the reparse point.
342 * @lookup_table: Stream lookup table for the WIM; an entry will be added to it
343 * for the reparse point unless an entry already exists for
344 * the exact same data stream.
346 * @path: External path to the reparse point. Used for error messages only.
348 * Returns 0 on success; nonzero on failure. */
350 win32_capture_reparse_point(HANDLE hFile,
351 struct wim_inode *inode,
352 struct wim_lookup_table *lookup_table,
355 /* "Reparse point data, including the tag and optional GUID,
356 * cannot exceed 16 kilobytes." - MSDN */
357 char reparse_point_buf[16 * 1024];
360 if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
361 NULL, /* "Not used with this operation; set to NULL" */
362 0, /* "Not used with this operation; set to 0" */
363 reparse_point_buf, /* "A pointer to a buffer that
364 receives the reparse point data */
365 sizeof(reparse_point_buf), /* "The size of the output
370 DWORD err = GetLastError();
371 ERROR("Win32 API: Failed to get reparse data of \"%s\"", path);
373 return WIMLIB_ERR_READ;
375 if (bytesReturned < 8) {
376 ERROR("Reparse data on \"%s\" is invalid", path);
377 return WIMLIB_ERR_READ;
379 inode->i_reparse_tag = le32_to_cpu(*(u32*)reparse_point_buf);
380 return inode_add_ads_with_data(inode, "",
381 reparse_point_buf + 8,
382 bytesReturned - 8, lookup_table);
385 /* Calculate the SHA1 message digest of a Win32 data stream, which may be either
386 * an unnamed or named data stream.
388 * @path: Path to the file, with the stream noted at the end for named
389 * streams. UTF-16LE encoding.
391 * @hash: On success, the SHA1 message digest of the stream is written to
394 * Returns 0 on success; nonzero on failure.
397 win32_sha1sum(const wchar_t *path, u8 hash[SHA1_HASH_SIZE])
405 hFile = win32_open_file_data_only(path);
406 if (hFile == INVALID_HANDLE_VALUE)
407 return WIMLIB_ERR_OPEN;
411 if (!ReadFile(hFile, buf, sizeof(buf), &bytesRead, NULL)) {
412 ret = WIMLIB_ERR_READ;
413 goto out_close_handle;
417 sha1_update(&ctx, buf, bytesRead);
420 sha1_final(hash, &ctx);
426 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
427 * stream); calculates its SHA1 message digest and either creates a `struct
428 * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
429 * wim_lookup_table_entry' for an identical stream.
431 * @path_utf16: Path to the file (UTF-16LE).
433 * @path_utf16_nchars: Number of 2-byte characters in @path_utf16.
435 * @inode: WIM inode to save the stream into.
437 * @lookup_table: Stream lookup table for the WIM.
439 * @dat: A `WIN32_FIND_STREAM_DATA' structure that specifies the
442 * Returns 0 on success; nonzero on failure.
445 win32_capture_stream(const wchar_t *path_utf16,
446 size_t path_utf16_nchars,
447 struct wim_inode *inode,
448 struct wim_lookup_table *lookup_table,
449 WIN32_FIND_STREAM_DATA *dat)
451 struct wim_ads_entry *ads_entry;
452 u8 hash[SHA1_HASH_SIZE];
453 struct wim_lookup_table_entry *lte;
455 wchar_t *stream_name, *colon;
456 size_t stream_name_nchars;
457 bool is_named_stream;
461 size_t spath_buf_nbytes;
462 const wchar_t *relpath_prefix;
463 const wchar_t *colonchar;
465 /* The stream name should be returned as :NAME:TYPE */
466 stream_name = dat->cStreamName;
467 if (*stream_name != L':')
468 goto out_invalid_stream_name;
470 colon = wcschr(stream_name, L':');
472 goto out_invalid_stream_name;
474 if (wcscmp(colon + 1, L"$DATA")) {
475 /* Not a DATA stream */
482 stream_name_nchars = colon - stream_name;
483 is_named_stream = (stream_name_nchars != 0);
485 if (is_named_stream) {
486 /* Allocate an ADS entry for the named stream. */
487 ads_entry = inode_add_ads_utf16le(inode, stream_name,
488 stream_name_nchars * 2);
490 ret = WIMLIB_ERR_NOMEM;
495 /* Create a UTF-16LE string @spath that gives the filename, then a
496 * colon, then the stream name. Or, if it's an unnamed stream, just the
497 * filename. It is MALLOC()'ed so that it can be saved in the
498 * wim_lookup_table_entry if needed.
500 * As yet another special case, relative paths need to be changed to
501 * begin with an explicit "./" so that, for example, a file t:ads, where
502 * :ads is the part we added, is not interpreted as a file on the t:
504 spath_nchars = path_utf16_nchars;
505 relpath_prefix = L"";
507 if (is_named_stream) {
508 spath_nchars += 1 + stream_name_nchars;
510 if (path_utf16_nchars == 1 &&
511 path_utf16[0] != cpu_to_le16('/') &&
512 path_utf16[0] != cpu_to_le16('\\'))
515 relpath_prefix = L"./";
519 spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
520 spath = MALLOC(spath_buf_nbytes);
522 swprintf(spath, spath_buf_nbytes, L"%ls%ls%ls%ls",
523 relpath_prefix, path_utf16, colonchar, stream_name);
525 ret = win32_sha1sum(spath, hash);
527 err = GetLastError();
528 ERROR("Win32 API: Failed to read \"%ls\" to calculate SHA1sum",
534 lte = __lookup_resource(lookup_table, hash);
536 /* Use existing wim_lookup_table_entry that has the same SHA1
540 /* Make a new wim_lookup_table_entry */
541 lte = new_lookup_table_entry();
543 ret = WIMLIB_ERR_NOMEM;
546 lte->win32_file_on_disk = spath;
547 lte->file_on_disk_fp = INVALID_HANDLE_VALUE;
549 lte->resource_location = RESOURCE_WIN32;
550 lte->resource_entry.original_size = (uint64_t)dat->StreamSize.QuadPart;
551 lte->resource_entry.size = (uint64_t)dat->StreamSize.QuadPart;
552 copy_hash(lte->hash, hash);
553 lookup_table_insert(lookup_table, lte);
556 ads_entry->lte = lte;
563 out_invalid_stream_name:
564 ERROR("Invalid stream name: \"%ls:%ls\"", path_utf16, dat->cStreamName);
565 ret = WIMLIB_ERR_READ;
569 /* Scans a Win32 file for unnamed and named data streams (not reparse point
572 * @path_utf16: Path to the file (UTF-16LE).
574 * @path_utf16_nchars: Number of 2-byte characters in @path_utf16.
576 * @inode: WIM inode to save the stream into.
578 * @lookup_table: Stream lookup table for the WIM.
580 * @file_size: Size of unnamed data stream. (Used only if alternate
581 * data streams API appears to be unavailable.)
583 * Returns 0 on success; nonzero on failure.
586 win32_capture_streams(const wchar_t *path_utf16,
587 size_t path_utf16_nchars,
588 struct wim_inode *inode,
589 struct wim_lookup_table *lookup_table,
592 WIN32_FIND_STREAM_DATA dat;
597 if (win32func_FindFirstStreamW == NULL)
600 hFind = win32func_FindFirstStreamW(path_utf16, FindStreamInfoStandard, &dat, 0);
601 if (hFind == INVALID_HANDLE_VALUE) {
602 err = GetLastError();
604 if (err == ERROR_CALL_NOT_IMPLEMENTED)
607 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
608 * points and directories */
609 if ((inode->i_attributes &
610 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
611 && err == ERROR_HANDLE_EOF)
615 if (err == ERROR_ACCESS_DENIED) {
616 WARNING("Failed to look up data streams of \"%ls\": "
617 "Access denied!\n%s", path_utf16,
621 ERROR("Win32 API: Failed to look up data streams of \"%ls\"",
624 return WIMLIB_ERR_READ;
629 ret = win32_capture_stream(path_utf16,
635 } while (win32func_FindNextStreamW(hFind, &dat));
636 err = GetLastError();
637 if (err != ERROR_HANDLE_EOF) {
638 ERROR("Win32 API: Error reading data streams from \"%ls\"", path_utf16);
640 ret = WIMLIB_ERR_READ;
646 if (inode->i_attributes &
647 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
651 wcscpy(dat.cStreamName, L"::$DATA");
652 dat.StreamSize.QuadPart = file_size;
653 ret = win32_capture_stream(path_utf16,
661 /* Win32 version of capturing a directory tree */
663 win32_build_dentry_tree(struct wim_dentry **root_ret,
664 const mbchar *root_disk_path,
665 struct wim_lookup_table *lookup_table,
666 struct wim_security_data *sd,
667 const struct capture_config *config,
669 wimlib_progress_func_t progress_func,
672 struct wim_dentry *root = NULL;
674 struct wim_inode *inode;
677 size_t path_utf16_nbytes;
678 size_t path_utf16_nchars;
679 struct sd_set *sd_set;
683 if (exclude_path(root_disk_path, config, true)) {
684 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
685 ERROR("Cannot exclude the root directory from capture");
686 ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
689 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
692 union wimlib_progress_info info;
693 info.scan.cur_path = root_disk_path;
694 info.scan.excluded = true;
695 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
700 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
703 union wimlib_progress_info info;
704 info.scan.cur_path = root_disk_path;
705 info.scan.excluded = false;
706 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
709 if (extra_arg == NULL) {
710 sd_set = alloca(sizeof(struct sd_set));
711 sd_set->rb_root.rb_node = NULL,
717 ret = mbs_to_utf16le(root_disk_path, strlen(root_disk_path),
718 &path_utf16, &path_utf16_nbytes);
720 goto out_destroy_sd_set;
721 path_utf16_nchars = path_utf16_nbytes / sizeof(wchar_t);
723 HANDLE hFile = win32_open_existing_file(path_utf16,
724 FILE_READ_DATA | FILE_READ_ATTRIBUTES);
725 if (hFile == INVALID_HANDLE_VALUE) {
726 err = GetLastError();
727 ERROR("Win32 API: Failed to open \"%s\"", root_disk_path);
729 ret = WIMLIB_ERR_OPEN;
730 goto out_free_path_utf16;
733 BY_HANDLE_FILE_INFORMATION file_info;
734 if (!GetFileInformationByHandle(hFile, &file_info)) {
735 err = GetLastError();
736 ERROR("Win32 API: Failed to get file information for \"%s\"",
739 ret = WIMLIB_ERR_STAT;
740 goto out_close_handle;
743 /* Create a WIM dentry */
744 ret = new_dentry_with_timeless_inode(path_basename(root_disk_path), &root);
746 goto out_close_handle;
748 /* Start preparing the associated WIM inode */
749 inode = root->d_inode;
751 inode->i_attributes = file_info.dwFileAttributes;
752 inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
753 inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
754 inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
755 inode->i_ino = ((u64)file_info.nFileIndexHigh << 32) |
756 (u64)file_info.nFileIndexLow;
758 inode->i_resolved = 1;
759 add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
761 /* Get DOS name and security descriptor (if any). */
762 ret = win32_get_short_name(root, path_utf16);
764 goto out_close_handle;
766 if (!(add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)) {
767 ret = win32_get_security_descriptor(root, sd_set, path_utf16);
769 goto out_close_handle;
772 file_size = ((u64)file_info.nFileSizeHigh << 32) |
773 (u64)file_info.nFileSizeLow;
775 if (inode_is_directory(inode)) {
776 /* Directory (not a reparse point) --- recurse to children */
778 /* But first... directories may have alternate data streams that
779 * need to be captured. */
780 ret = win32_capture_streams(path_utf16,
786 goto out_close_handle;
787 ret = win32_recurse_directory(root,
797 } else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
798 /* Reparse point: save the reparse tag and data */
799 ret = win32_capture_reparse_point(hFile,
804 /* Not a directory, not a reparse point; capture the default
805 * file contents and any alternate data streams. */
806 ret = win32_capture_streams(path_utf16,
817 if (extra_arg == NULL)
818 destroy_sd_set(sd_set);
823 free_dentry_tree(root, lookup_table);
827 /* Replacement for POSIX fnmatch() (partial functionality only) */
829 fnmatch(const char *pattern, const char *string, int flags)
831 if (PathMatchSpecA(string, pattern))
838 win32_set_reparse_data(HANDLE h,
840 const struct wim_lookup_table_entry *lte,
848 WARNING("\"%ls\" is marked as a reparse point but had no reparse data",
852 len = wim_resource_size(lte);
853 if (len > 16 * 1024 - 8) {
854 WARNING("\"%ls\": reparse data too long!", path);
858 /* The WIM stream omits the ReparseTag and ReparseDataLength fields, so
859 * leave 8 bytes of space for them at the beginning of the buffer, then
860 * set them manually. */
861 buf = alloca(len + 8);
862 ret = read_full_wim_resource(lte, buf + 8, 0);
865 *(u32*)(buf + 0) = cpu_to_le32(reparse_tag);
866 *(u16*)(buf + 4) = cpu_to_le16(len);
867 *(u16*)(buf + 6) = 0;
869 /* Set the reparse data on the open file using the
870 * FSCTL_SET_REPARSE_POINT ioctl.
872 * There are contradictions in Microsoft's documentation for this:
874 * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
875 * lpOverlapped is ignored."
877 * --- So setting lpOverlapped to NULL is okay since it's ignored.
879 * "If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an
880 * operation returns no output data and lpOutBuffer is NULL,
881 * DeviceIoControl makes use of lpBytesReturned. After such an
882 * operation, the value of lpBytesReturned is meaningless."
884 * --- So lpOverlapped not really ignored, as it affects another
885 * parameter. This is the actual behavior: lpBytesReturned must be
886 * specified, even though lpBytesReturned is documented as:
888 * "Not used with this operation; set to NULL."
891 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, buf, len + 8,
893 &bytesReturned /* lpBytesReturned */,
894 NULL /* lpOverlapped */))
896 DWORD err = GetLastError();
897 ERROR("Failed to set reparse data on \"%ls\"", path);
899 return WIMLIB_ERR_WRITE;
906 win32_extract_chunk(const void *buf, size_t len, u64 offset, void *arg)
908 HANDLE hStream = arg;
910 DWORD nbytes_written;
911 wimlib_assert(len <= 0xffffffff);
913 if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
914 nbytes_written != len)
916 DWORD err = GetLastError();
917 ERROR("WriteFile(): write error");
919 return WIMLIB_ERR_WRITE;
925 do_win32_extract_stream(HANDLE hStream, struct wim_lookup_table_entry *lte)
927 return extract_wim_resource(lte, wim_resource_size(lte),
928 win32_extract_chunk, hStream);
932 win32_extract_stream(const struct wim_inode *inode,
934 const wchar_t *stream_name_utf16,
935 struct wim_lookup_table_entry *lte,
936 const struct wim_security_data *security_data)
938 wchar_t *stream_path;
942 DWORD creationDisposition = CREATE_ALWAYS;
944 SECURITY_ATTRIBUTES *secattr;
946 if (security_data && inode->i_security_id != -1) {
947 secattr = alloca(sizeof(*secattr));
948 secattr->nLength = sizeof(*secattr);
949 secattr->lpSecurityDescriptor = security_data->descriptors[inode->i_security_id];
950 secattr->bInheritHandle = FALSE;
955 if (stream_name_utf16) {
956 /* Named stream. Create a buffer that contains the UTF-16LE
957 * string [.\]@path:@stream_name_utf16. This is needed to
958 * create and open the stream using CreateFileW(). I'm not
959 * aware of any other APIs to do this. Note: the '$DATA' suffix
960 * seems to be unneeded. Additional note: a "./" prefix needs
961 * to be added when the path is not absolute to avoid ambiguity
962 * with drive letters. */
963 size_t stream_path_nchars;
965 size_t stream_name_nchars;
966 const wchar_t *prefix;
968 path_nchars = wcslen(path);
969 stream_name_nchars = wcslen(stream_name_utf16);
970 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
971 if (path[0] != cpu_to_le16(L'\0') &&
972 path[0] != cpu_to_le16(L'/') &&
973 path[0] != cpu_to_le16(L'\\') &&
974 path[1] != cpu_to_le16(L':'))
977 stream_path_nchars += 2;
981 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
982 swprintf(stream_path, stream_path_nchars + 1, L"%ls%ls:%ls",
983 prefix, path, stream_name_utf16);
985 /* Unnamed stream; its path is just the path to the file itself.
987 stream_path = (wchar_t*)path;
989 /* Directories must be created with CreateDirectoryW(). Then
990 * the call to CreateFileW() will merely open the directory that
991 * was already created rather than creating a new file. */
992 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
993 if (!CreateDirectoryW(stream_path, secattr)) {
994 err = GetLastError();
995 if (err != ERROR_ALREADY_EXISTS) {
996 ERROR("Failed to create directory \"%ls\"",
999 ret = WIMLIB_ERR_MKDIR;
1003 DEBUG("Created directory \"%ls\"", stream_path);
1004 if (!(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
1008 creationDisposition = OPEN_EXISTING;
1012 DEBUG("Opening \"%ls\"", stream_path);
1013 h = CreateFileW(stream_path,
1017 creationDisposition,
1018 FILE_FLAG_OPEN_REPARSE_POINT |
1019 FILE_FLAG_BACKUP_SEMANTICS |
1020 inode->i_attributes,
1022 if (h == INVALID_HANDLE_VALUE) {
1023 err = GetLastError();
1024 ERROR("Failed to create \"%ls\"", stream_path);
1026 ret = WIMLIB_ERR_OPEN;
1030 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
1031 stream_name_utf16 == NULL)
1033 DEBUG("Setting reparse data on \"%ls\"", path);
1034 ret = win32_set_reparse_data(h, inode->i_reparse_tag, lte, path);
1036 goto fail_close_handle;
1039 DEBUG("Extracting \"%ls\" (len = %"PRIu64")",
1040 stream_path, wim_resource_size(lte));
1041 ret = do_win32_extract_stream(h, lte);
1043 goto fail_close_handle;
1047 DEBUG("Closing \"%ls\"", stream_path);
1048 if (!CloseHandle(h)) {
1049 err = GetLastError();
1050 ERROR("Failed to close \"%ls\"", stream_path);
1052 ret = WIMLIB_ERR_WRITE;
1060 ERROR("Error extracting %ls", stream_path);
1066 * Creates a file, directory, or reparse point and extracts all streams to it
1067 * (unnamed data stream and/or reparse point stream, plus any alternate data
1068 * streams). This in Win32-specific code.
1070 * @inode: WIM inode for this file or directory.
1071 * @path: UTF-16LE external path to extract the inode to.
1073 * Returns 0 on success; nonzero on failure.
1076 win32_extract_streams(const struct wim_inode *inode,
1077 const wchar_t *path, u64 *completed_bytes_p,
1078 const struct wim_security_data *security_data)
1080 struct wim_lookup_table_entry *unnamed_lte;
1083 unnamed_lte = inode_unnamed_lte_resolved(inode);
1084 ret = win32_extract_stream(inode, path, NULL, unnamed_lte,
1089 *completed_bytes_p += wim_resource_size(unnamed_lte);
1090 for (u16 i = 0; i < inode->i_num_ads; i++) {
1091 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
1092 if (ads_entry->stream_name_nbytes != 0) {
1093 /* Skip special UNIX data entries (see documentation for
1094 * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
1095 if (ads_entry->stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
1096 && !memcmp(ads_entry->stream_name,
1097 WIMLIB_UNIX_DATA_TAG_UTF16LE,
1098 WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
1100 ret = win32_extract_stream(inode,
1102 ads_entry->stream_name,
1108 *completed_bytes_p += wim_resource_size(ads_entry->lte);
1115 /* Extract a file, directory, reparse point, or hard link to an
1116 * already-extracted file using the Win32 API */
1117 int win32_do_apply_dentry(const mbchar *output_path,
1118 size_t output_path_nbytes,
1119 struct wim_dentry *dentry,
1120 struct apply_args *args)
1122 wchar_t *utf16le_path;
1123 size_t utf16le_path_nbytes;
1126 struct wim_inode *inode = dentry->d_inode;
1128 ret = mbs_to_utf16le(output_path, output_path_nbytes,
1129 &utf16le_path, &utf16le_path_nbytes);
1133 if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
1134 /* Linked file, with another name already extracted. Create a
1136 DEBUG("Creating hard link \"%ls => %ls\"",
1137 utf16le_path, inode->i_extracted_file);
1138 if (!CreateHardLinkW(utf16le_path, inode->i_extracted_file, NULL))
1140 err = GetLastError();
1141 ERROR("Can't create hard link \"%ls => %ls\"",
1142 utf16le_path, inode->i_extracted_file);
1143 ret = WIMLIB_ERR_LINK;
1147 /* Create the file, directory, or reparse point, and extract the
1149 const struct wim_security_data *security_data;
1150 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NOACLS)
1151 security_data = NULL;
1153 security_data = wim_const_security_data(args->w);
1155 ret = win32_extract_streams(inode, utf16le_path,
1156 &args->progress.extract.completed_bytes,
1159 goto out_free_utf16_path;
1161 if (inode->i_nlink > 1) {
1162 /* Save extracted path for a later call to
1163 * CreateHardLinkW() if this inode has multiple links.
1165 inode->i_extracted_file = utf16le_path;
1169 out_free_utf16_path:
1175 /* Set timestamps on an extracted file using the Win32 API */
1177 win32_do_apply_dentry_timestamps(const mbchar *output_path,
1178 size_t output_path_nbytes,
1179 const struct wim_dentry *dentry,
1180 const struct apply_args *args)
1183 wchar_t *utf16le_path;
1184 size_t utf16le_path_nbytes;
1188 const struct wim_inode *inode = dentry->d_inode;
1190 ret = mbs_to_utf16le(output_path, output_path_nbytes,
1191 &utf16le_path, &utf16le_path_nbytes);
1195 DEBUG("Opening \"%s\" to set timestamps", output_path);
1196 h = win32_open_existing_file(utf16le_path, FILE_WRITE_ATTRIBUTES);
1198 if (h == INVALID_HANDLE_VALUE)
1199 err = GetLastError();
1201 if (h == INVALID_HANDLE_VALUE)
1204 FILETIME creationTime = {.dwLowDateTime = inode->i_creation_time & 0xffffffff,
1205 .dwHighDateTime = inode->i_creation_time >> 32};
1206 FILETIME lastAccessTime = {.dwLowDateTime = inode->i_last_access_time & 0xffffffff,
1207 .dwHighDateTime = inode->i_last_access_time >> 32};
1208 FILETIME lastWriteTime = {.dwLowDateTime = inode->i_last_write_time & 0xffffffff,
1209 .dwHighDateTime = inode->i_last_write_time >> 32};
1211 DEBUG("Calling SetFileTime() on \"%s\"", output_path);
1212 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
1213 err = GetLastError();
1217 DEBUG("Closing \"%s\"", output_path);
1218 if (!CloseHandle(h)) {
1219 err = GetLastError();
1224 /* Only warn if setting timestamps failed. */
1225 WARNING("Can't set timestamps on \"%s\"", output_path);
1231 /* Replacement for POSIX fsync() */
1235 HANDLE h = (HANDLE)_get_osfhandle(fd);
1236 if (h == INVALID_HANDLE_VALUE) {
1237 ERROR("Could not get Windows handle for file descriptor");
1238 win32_error(GetLastError());
1242 if (!FlushFileBuffers(h)) {
1243 ERROR("Could not flush file buffers to disk");
1244 win32_error(GetLastError());
1251 /* Use the Win32 API to get the number of processors */
1253 win32_get_number_of_processors()
1255 SYSTEM_INFO sysinfo;
1256 GetSystemInfo(&sysinfo);
1257 return sysinfo.dwNumberOfProcessors;
1260 /* Replacement for POSIX-2008 realpath(). Warning: partial functionality only
1261 * (resolved_path must be NULL). Also I highly doubt that GetFullPathName
1262 * really does the right thing under all circumstances. */
1264 realpath(const mbchar *path, mbchar *resolved_path)
1267 wimlib_assert(resolved_path == NULL);
1269 ret = GetFullPathNameA(path, 0, NULL, NULL);
1273 resolved_path = MALLOC(ret);
1276 ret = GetFullPathNameA(path, ret, resolved_path, NULL);
1278 free(resolved_path);
1281 return resolved_path;
1283 win32_error(GetLastError());
1289 nl_langinfo(nl_item item)
1291 wimlib_assert(item == CODESET);
1292 static char buf[64];
1293 strcpy(buf, "Unknown");
1297 /* rename() on Windows fails if the destination file exists. Fix it. */
1299 rename_replacement(const char *oldpath, const char *newpath)
1301 if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) {
1304 /* As usual, the possible error values are not documented */
1305 DWORD err = GetLastError();
1306 ERROR("MoveFileExA(): Can't rename \"%s\" to \"%s\"",