2 * win32_common.c - Windows code common to applying and capturing images.
6 * Copyright (C) 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/.
32 #include "wimlib/win32_common.h"
34 #include "wimlib/assert.h"
35 #include "wimlib/error.h"
36 #include "wimlib/util.h"
38 #ifdef ENABLE_ERROR_MESSAGES
40 win32_error(DWORD err_code)
44 nchars = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
45 FORMAT_MESSAGE_ALLOCATE_BUFFER,
47 (wchar_t*)&buffer, 0, NULL);
49 ERROR("Error printing error message! "
50 "Computer will self-destruct in 3 seconds.");
52 ERROR("Win32 error: %ls", buffer);
56 #endif /* ENABLE_ERROR_MESSAGES */
59 win32_error_to_errno(DWORD err_code)
61 /* This mapping is that used in Cygwin.
62 * Some of these choices are arbitrary. */
64 case ERROR_ACCESS_DENIED:
66 case ERROR_ACTIVE_CONNECTIONS:
68 case ERROR_ALREADY_EXISTS:
70 case ERROR_BAD_DEVICE:
72 case ERROR_BAD_EXE_FORMAT:
74 case ERROR_BAD_NETPATH:
76 case ERROR_BAD_NET_NAME:
78 case ERROR_BAD_NET_RESP:
80 case ERROR_BAD_PATHNAME:
86 case ERROR_BAD_USERNAME:
88 case ERROR_BEGINNING_OF_MEDIA:
90 case ERROR_BROKEN_PIPE:
96 case ERROR_CALL_NOT_IMPLEMENTED:
98 case ERROR_CANNOT_MAKE:
100 case ERROR_CHILD_NOT_COMPLETE:
102 case ERROR_COMMITMENT_LIMIT:
106 case ERROR_DEVICE_DOOR_OPEN:
108 case ERROR_DEVICE_IN_USE:
110 case ERROR_DEVICE_REQUIRES_CLEANING:
112 case ERROR_DIRECTORY:
114 case ERROR_DIR_NOT_EMPTY:
116 case ERROR_DISK_CORRUPT:
118 case ERROR_DISK_FULL:
124 case ERROR_EAS_DIDNT_FIT:
127 case ERROR_EAS_NOT_SUPPORTED:
130 case ERROR_EA_LIST_INCONSISTENT:
132 case ERROR_EA_TABLE_FULL:
134 case ERROR_END_OF_MEDIA:
136 case ERROR_EOM_OVERFLOW:
138 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
140 case ERROR_EXE_MARKED_INVALID:
142 case ERROR_FILEMARK_DETECTED:
144 case ERROR_FILENAME_EXCED_RANGE:
146 case ERROR_FILE_CORRUPT:
148 case ERROR_FILE_EXISTS:
150 case ERROR_FILE_INVALID:
152 case ERROR_FILE_NOT_FOUND:
154 case ERROR_HANDLE_DISK_FULL:
157 case ERROR_HANDLE_EOF:
160 case ERROR_INVALID_ADDRESS:
162 case ERROR_INVALID_AT_INTERRUPT_TIME:
164 case ERROR_INVALID_BLOCK_LENGTH:
166 case ERROR_INVALID_DATA:
168 case ERROR_INVALID_DRIVE:
170 case ERROR_INVALID_EA_NAME:
172 case ERROR_INVALID_EXE_SIGNATURE:
175 case ERROR_INVALID_FUNCTION:
178 case ERROR_INVALID_HANDLE:
180 case ERROR_INVALID_NAME:
182 case ERROR_INVALID_PARAMETER:
184 case ERROR_INVALID_SIGNAL_NUMBER:
186 case ERROR_IOPL_NOT_ENABLED:
188 case ERROR_IO_DEVICE:
190 case ERROR_IO_INCOMPLETE:
192 case ERROR_IO_PENDING:
194 case ERROR_LOCK_VIOLATION:
196 case ERROR_MAX_THRDS_REACHED:
198 case ERROR_META_EXPANSION_TOO_LONG:
200 case ERROR_MOD_NOT_FOUND:
203 case ERROR_MORE_DATA:
206 case ERROR_NEGATIVE_SEEK:
208 case ERROR_NETNAME_DELETED:
212 case ERROR_NONE_MAPPED:
214 case ERROR_NONPAGED_SYSTEM_RESOURCES:
217 case ERROR_NOT_CONNECTED:
220 case ERROR_NOT_ENOUGH_MEMORY:
222 case ERROR_NOT_OWNER:
225 case ERROR_NOT_READY:
228 case ERROR_NOT_SAME_DEVICE:
230 case ERROR_NOT_SUPPORTED:
234 case ERROR_NO_DATA_DETECTED:
237 case ERROR_NO_MEDIA_IN_DRIVE:
241 case ERROR_NO_MORE_FILES:
245 case ERROR_NO_MORE_ITEMS:
248 case ERROR_NO_MORE_SEARCH_HANDLES:
250 case ERROR_NO_PROC_SLOTS:
252 case ERROR_NO_SIGNAL_SENT:
254 case ERROR_NO_SYSTEM_RESOURCES:
258 case ERROR_OPEN_FAILED:
260 case ERROR_OPEN_FILES:
262 case ERROR_OUTOFMEMORY:
264 case ERROR_PAGED_SYSTEM_RESOURCES:
266 case ERROR_PAGEFILE_QUOTA:
268 case ERROR_PATH_NOT_FOUND:
270 case ERROR_PIPE_BUSY:
272 case ERROR_PIPE_CONNECTED:
275 case ERROR_PIPE_LISTENING:
277 case ERROR_PIPE_NOT_CONNECTED:
280 case ERROR_POSSIBLE_DEADLOCK:
282 case ERROR_PRIVILEGE_NOT_HELD:
284 case ERROR_PROCESS_ABORTED:
286 case ERROR_PROC_NOT_FOUND:
289 case ERROR_REM_NOT_LIST:
292 case ERROR_SECTOR_NOT_FOUND:
296 case ERROR_SETMARK_DETECTED:
298 case ERROR_SHARING_BUFFER_EXCEEDED:
300 case ERROR_SHARING_VIOLATION:
302 case ERROR_SIGNAL_PENDING:
304 case ERROR_SIGNAL_REFUSED:
307 case ERROR_SXS_CANT_GEN_ACTCTX:
310 case ERROR_THREAD_1_INACTIVE:
312 case ERROR_TOO_MANY_LINKS:
314 case ERROR_TOO_MANY_OPEN_FILES:
316 case ERROR_WAIT_NO_CHILDREN:
318 case ERROR_WORKING_SET_QUOTA:
320 case ERROR_WRITE_PROTECT:
328 set_errno_from_GetLastError(void)
330 errno = win32_error_to_errno(GetLastError());
333 /* Given a Windows-style path, return the number of characters of the prefix
334 * that specify the path to the root directory of a drive, or return 0 if the
335 * drive is relative (or at least on the current drive, in the case of
336 * absolute-but-not-really-absolute paths like \Windows\System32) */
338 win32_path_drive_spec_len(const wchar_t *path)
342 if (!wcsncmp(path, L"\\\\?\\", 4)) {
343 /* \\?\-prefixed path. Check for following drive letter and
345 if (path[4] != L'\0' && path[5] == L':' &&
346 is_any_path_separator(path[6]))
349 /* Not a \\?\-prefixed path. Check for an initial drive letter
350 * and path separator. */
351 if (path[0] != L'\0' && path[1] == L':' &&
352 is_any_path_separator(path[2]))
355 /* Include any additional path separators.*/
357 while (is_any_path_separator(path[n]))
363 win32_path_is_root_of_drive(const wchar_t *path)
365 size_t drive_spec_len;
367 /* Explicit drive letter and path separator? */
368 drive_spec_len = win32_path_drive_spec_len(path);
369 if (drive_spec_len > 0 && path[drive_spec_len] == L'\0')
372 /* All path separators? */
373 for (const wchar_t *p = path; *p != L'\0'; p++)
374 if (!is_any_path_separator(*p))
378 /* XXX This function does not handle paths like "c:" where the working
379 * directory on "c:" is actually "c:\", or weird paths like "\.". But
380 * currently the capture and apply code always prefixes the paths with
381 * \\?\ anyway so this is irrelevant... */
385 /* Given a path, which may not yet exist, get a set of flags that describe the
386 * features of the volume the path is on. */
388 win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret)
393 size_t drive_spec_len;
395 drive_spec_len = win32_path_drive_spec_len(path);
397 if (drive_spec_len == 0)
398 if (path[0] != L'\0' && path[1] == L':') /* Drive-relative path? */
401 if (drive_spec_len == 0) {
402 /* Path does not start with a drive letter; use the volume of
403 * the current working directory. */
406 /* Path starts with a drive letter (or \\?\ followed by a drive
407 * letter); use it. */
408 volume = alloca((drive_spec_len + 2) * sizeof(wchar_t));
409 wmemcpy(volume, path, drive_spec_len);
410 /* Add trailing backslash in case this was a drive-relative
412 volume[drive_spec_len] = L'\\';
413 volume[drive_spec_len + 1] = L'\0';
415 bret = GetVolumeInformationW(volume, /* lpRootPathName */
416 NULL, /* lpVolumeNameBuffer */
417 0, /* nVolumeNameSize */
418 NULL, /* lpVolumeSerialNumber */
419 NULL, /* lpMaximumComponentLength */
420 &vol_flags, /* lpFileSystemFlags */
421 NULL, /* lpFileSystemNameBuffer */
422 0); /* nFileSystemNameSize */
424 DWORD err = GetLastError();
425 WARNING("Failed to get volume information for path \"%ls\"", path);
427 vol_flags = 0xffffffff;
430 DEBUG("using vol_flags = %x", vol_flags);
431 *vol_flags_ret = vol_flags;
436 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
438 return CreateFileW(path,
441 NULL, /* lpSecurityAttributes */
443 FILE_FLAG_BACKUP_SEMANTICS |
444 FILE_FLAG_OPEN_REPARSE_POINT,
445 NULL /* hTemplateFile */);
449 win32_open_file_data_only(const wchar_t *path)
451 return win32_open_existing_file(path, FILE_READ_DATA);
454 /* Pointers to functions that are not available on all targetted versions of
455 * Windows (XP and later). NOTE: The WINAPI annotations seem to be important; I
456 * assume it specifies a certain calling convention. */
458 /* Vista and later */
459 HANDLE (WINAPI *win32func_FindFirstStreamW)(LPCWSTR lpFileName,
460 STREAM_INFO_LEVELS InfoLevel,
461 LPVOID lpFindStreamData,
462 DWORD dwFlags) = NULL;
464 /* Vista and later */
465 BOOL (WINAPI *win32func_FindNextStreamW)(HANDLE hFindStream,
466 LPVOID lpFindStreamData) = NULL;
468 static OSVERSIONINFO windows_version_info = {
469 .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
472 static HMODULE hKernel32 = NULL;
475 windows_version_is_at_least(unsigned major, unsigned minor)
477 return windows_version_info.dwMajorVersion > major ||
478 (windows_version_info.dwMajorVersion == major &&
479 windows_version_info.dwMinorVersion >= minor);
482 /* Try to dynamically load some functions */
484 win32_global_init(void)
488 if (hKernel32 == NULL) {
489 DEBUG("Loading Kernel32.dll");
490 hKernel32 = LoadLibraryW(L"Kernel32.dll");
491 if (hKernel32 == NULL) {
492 err = GetLastError();
493 WARNING("Can't load Kernel32.dll");
499 win32func_FindFirstStreamW = (void*)GetProcAddress(hKernel32,
501 if (win32func_FindFirstStreamW) {
502 win32func_FindNextStreamW = (void*)GetProcAddress(hKernel32,
504 if (!win32func_FindNextStreamW)
505 win32func_FindFirstStreamW = NULL;
509 GetVersionEx(&windows_version_info);
513 win32_global_cleanup(void)
515 if (hKernel32 != NULL) {
516 DEBUG("Closing Kernel32.dll");
517 FreeLibrary(hKernel32);
522 #endif /* __WIN32__ */