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/.
33 # include <winternl.h>
36 #include "wimlib/win32_common.h"
37 #include "wimlib/assert.h"
38 #include "wimlib/error.h"
39 #include "wimlib/util.h"
42 win32_error_to_errno(DWORD err_code)
44 /* This mapping is that used in Cygwin.
45 * Some of these choices are arbitrary. */
47 case ERROR_ACCESS_DENIED:
49 case ERROR_ACTIVE_CONNECTIONS:
51 case ERROR_ALREADY_EXISTS:
53 case ERROR_BAD_DEVICE:
55 case ERROR_BAD_EXE_FORMAT:
57 case ERROR_BAD_NETPATH:
59 case ERROR_BAD_NET_NAME:
61 case ERROR_BAD_NET_RESP:
63 case ERROR_BAD_PATHNAME:
69 case ERROR_BAD_USERNAME:
71 case ERROR_BEGINNING_OF_MEDIA:
73 case ERROR_BROKEN_PIPE:
79 case ERROR_CALL_NOT_IMPLEMENTED:
81 case ERROR_CANNOT_MAKE:
83 case ERROR_CHILD_NOT_COMPLETE:
85 case ERROR_COMMITMENT_LIMIT:
89 case ERROR_DEVICE_DOOR_OPEN:
91 case ERROR_DEVICE_IN_USE:
93 case ERROR_DEVICE_REQUIRES_CLEANING:
97 case ERROR_DIR_NOT_EMPTY:
99 case ERROR_DISK_CORRUPT:
101 case ERROR_DISK_FULL:
107 case ERROR_EAS_DIDNT_FIT:
110 case ERROR_EAS_NOT_SUPPORTED:
113 case ERROR_EA_LIST_INCONSISTENT:
115 case ERROR_EA_TABLE_FULL:
117 case ERROR_END_OF_MEDIA:
119 case ERROR_EOM_OVERFLOW:
121 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
123 case ERROR_EXE_MARKED_INVALID:
125 case ERROR_FILEMARK_DETECTED:
127 case ERROR_FILENAME_EXCED_RANGE:
129 case ERROR_FILE_CORRUPT:
131 case ERROR_FILE_EXISTS:
133 case ERROR_FILE_INVALID:
135 case ERROR_FILE_NOT_FOUND:
137 case ERROR_HANDLE_DISK_FULL:
140 case ERROR_HANDLE_EOF:
143 case ERROR_INVALID_ADDRESS:
145 case ERROR_INVALID_AT_INTERRUPT_TIME:
147 case ERROR_INVALID_BLOCK_LENGTH:
149 case ERROR_INVALID_DATA:
151 case ERROR_INVALID_DRIVE:
153 case ERROR_INVALID_EA_NAME:
155 case ERROR_INVALID_EXE_SIGNATURE:
158 case ERROR_INVALID_FUNCTION:
161 case ERROR_INVALID_HANDLE:
163 case ERROR_INVALID_NAME:
165 case ERROR_INVALID_PARAMETER:
167 case ERROR_INVALID_SIGNAL_NUMBER:
169 case ERROR_IOPL_NOT_ENABLED:
171 case ERROR_IO_DEVICE:
173 case ERROR_IO_INCOMPLETE:
175 case ERROR_IO_PENDING:
177 case ERROR_LOCK_VIOLATION:
179 case ERROR_MAX_THRDS_REACHED:
181 case ERROR_META_EXPANSION_TOO_LONG:
183 case ERROR_MOD_NOT_FOUND:
186 case ERROR_MORE_DATA:
189 case ERROR_NEGATIVE_SEEK:
191 case ERROR_NETNAME_DELETED:
195 case ERROR_NONE_MAPPED:
197 case ERROR_NONPAGED_SYSTEM_RESOURCES:
200 case ERROR_NOT_CONNECTED:
203 case ERROR_NOT_ENOUGH_MEMORY:
205 case ERROR_NOT_OWNER:
208 case ERROR_NOT_READY:
211 case ERROR_NOT_SAME_DEVICE:
213 case ERROR_NOT_SUPPORTED:
217 case ERROR_NO_DATA_DETECTED:
220 case ERROR_NO_MEDIA_IN_DRIVE:
224 case ERROR_NO_MORE_FILES:
228 case ERROR_NO_MORE_ITEMS:
231 case ERROR_NO_MORE_SEARCH_HANDLES:
233 case ERROR_NO_PROC_SLOTS:
235 case ERROR_NO_SIGNAL_SENT:
237 case ERROR_NO_SYSTEM_RESOURCES:
241 case ERROR_OPEN_FAILED:
243 case ERROR_OPEN_FILES:
245 case ERROR_OUTOFMEMORY:
247 case ERROR_PAGED_SYSTEM_RESOURCES:
249 case ERROR_PAGEFILE_QUOTA:
251 case ERROR_PATH_NOT_FOUND:
253 case ERROR_PIPE_BUSY:
255 case ERROR_PIPE_CONNECTED:
258 case ERROR_PIPE_LISTENING:
260 case ERROR_PIPE_NOT_CONNECTED:
263 case ERROR_POSSIBLE_DEADLOCK:
265 case ERROR_PRIVILEGE_NOT_HELD:
267 case ERROR_PROCESS_ABORTED:
269 case ERROR_PROC_NOT_FOUND:
272 case ERROR_REM_NOT_LIST:
275 case ERROR_SECTOR_NOT_FOUND:
279 case ERROR_SETMARK_DETECTED:
281 case ERROR_SHARING_BUFFER_EXCEEDED:
283 case ERROR_SHARING_VIOLATION:
285 case ERROR_SIGNAL_PENDING:
287 case ERROR_SIGNAL_REFUSED:
290 case ERROR_SXS_CANT_GEN_ACTCTX:
293 case ERROR_THREAD_1_INACTIVE:
295 case ERROR_TOO_MANY_LINKS:
297 case ERROR_TOO_MANY_OPEN_FILES:
299 case ERROR_WAIT_NO_CHILDREN:
301 case ERROR_WORKING_SET_QUOTA:
303 case ERROR_WRITE_PROTECT:
312 set_errno_from_win32_error(DWORD err)
314 errno = win32_error_to_errno(err);
318 set_errno_from_GetLastError(void)
320 set_errno_from_win32_error(GetLastError());
325 set_errno_from_nt_status(DWORD status)
327 set_errno_from_win32_error(RtlNtStatusToDosError(status));
331 /* Given a Windows-style path, return the number of characters of the prefix
332 * that specify the path to the root directory of a drive, or return 0 if the
333 * drive is relative (or at least on the current drive, in the case of
334 * absolute-but-not-really-absolute paths like \Windows\System32) */
336 win32_path_drive_spec_len(const wchar_t *path)
340 if (!wcsncmp(path, L"\\\\?\\", 4)) {
341 /* \\?\-prefixed path. Check for following drive letter and
343 if (path[4] != L'\0' && path[5] == L':' &&
344 is_any_path_separator(path[6]))
347 /* Not a \\?\-prefixed path. Check for an initial drive letter
348 * and path separator. */
349 if (path[0] != L'\0' && path[1] == L':' &&
350 is_any_path_separator(path[2]))
353 /* Include any additional path separators.*/
355 while (is_any_path_separator(path[n]))
361 win32_path_is_root_of_drive(const wchar_t *path)
363 size_t drive_spec_len;
364 wchar_t full_path[32768];
367 ret = GetFullPathName(path, ARRAY_LEN(full_path), full_path, NULL);
368 if (ret > 0 && ret < ARRAY_LEN(full_path))
371 /* Explicit drive letter and path separator? */
372 drive_spec_len = win32_path_drive_spec_len(path);
373 if (drive_spec_len > 0 && path[drive_spec_len] == L'\0')
376 /* All path separators? */
377 for (const wchar_t *p = path; *p != L'\0'; p++)
378 if (!is_any_path_separator(*p))
384 /* Given a path, which may not yet exist, get a set of flags that describe the
385 * features of the volume the path is on. */
387 win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret,
388 bool *supports_SetFileShortName_ret)
393 size_t drive_spec_len;
394 wchar_t filesystem_name[MAX_PATH + 1];
396 if (supports_SetFileShortName_ret)
397 *supports_SetFileShortName_ret = false;
399 drive_spec_len = win32_path_drive_spec_len(path);
401 if (drive_spec_len == 0)
402 if (path[0] != L'\0' && path[1] == L':') /* Drive-relative path? */
405 if (drive_spec_len == 0) {
406 /* Path does not start with a drive letter; use the volume of
407 * the current working directory. */
410 /* Path starts with a drive letter (or \\?\ followed by a drive
411 * letter); use it. */
412 volume = alloca((drive_spec_len + 2) * sizeof(wchar_t));
413 wmemcpy(volume, path, drive_spec_len);
414 /* Add trailing backslash in case this was a drive-relative
416 volume[drive_spec_len] = L'\\';
417 volume[drive_spec_len + 1] = L'\0';
419 bret = GetVolumeInformation(
420 volume, /* lpRootPathName */
421 NULL, /* lpVolumeNameBuffer */
422 0, /* nVolumeNameSize */
423 NULL, /* lpVolumeSerialNumber */
424 NULL, /* lpMaximumComponentLength */
425 &vol_flags, /* lpFileSystemFlags */
426 filesystem_name, /* lpFileSystemNameBuffer */
427 ARRAY_LEN(filesystem_name)); /* nFileSystemNameSize */
429 set_errno_from_GetLastError();
430 WARNING_WITH_ERRNO("Failed to get volume information for "
431 "path \"%ls\"", path);
432 vol_flags = 0xffffffff;
436 if (wcsstr(filesystem_name, L"NTFS")) {
437 /* FILE_SUPPORTS_HARD_LINKS is only supported on Windows 7 and later.
438 * Force it on anyway if filesystem is NTFS. */
439 vol_flags |= FILE_SUPPORTS_HARD_LINKS;
441 if (supports_SetFileShortName_ret)
442 *supports_SetFileShortName_ret = true;
446 DEBUG("using vol_flags = %x", vol_flags);
447 *vol_flags_ret = vol_flags;
452 win32_modify_privilege(const wchar_t *privilege, bool enable)
456 TOKEN_PRIVILEGES newState;
459 if (!OpenProcessToken(GetCurrentProcess(),
460 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
464 if (!LookupPrivilegeValue(NULL, privilege, &luid))
465 goto out_close_handle;
467 newState.PrivilegeCount = 1;
468 newState.Privileges[0].Luid = luid;
469 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
470 SetLastError(ERROR_SUCCESS);
471 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
472 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
481 win32_modify_capture_privileges(bool enable)
483 return win32_modify_privilege(SE_BACKUP_NAME, enable)
484 && win32_modify_privilege(SE_SECURITY_NAME, enable);
488 win32_modify_apply_privileges(bool enable)
490 return win32_modify_privilege(SE_RESTORE_NAME, enable)
491 && win32_modify_privilege(SE_SECURITY_NAME, enable)
492 && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
496 win32_release_capture_and_apply_privileges(void)
498 win32_modify_capture_privileges(false);
499 win32_modify_apply_privileges(false);
503 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
505 return CreateFileW(path,
508 NULL, /* lpSecurityAttributes */
510 FILE_FLAG_BACKUP_SEMANTICS |
511 FILE_FLAG_OPEN_REPARSE_POINT,
512 NULL /* hTemplateFile */);
516 /* Pointers to functions that are not available on all targetted versions of
517 * Windows (XP and later). NOTE: The WINAPI annotations seem to be important; I
518 * assume it specifies a certain calling convention. */
520 /* Vista and later */
521 HANDLE (WINAPI *win32func_FindFirstStreamW)(LPCWSTR lpFileName,
522 STREAM_INFO_LEVELS InfoLevel,
523 LPVOID lpFindStreamData,
524 DWORD dwFlags) = NULL;
526 /* Vista and later */
527 BOOL (WINAPI *win32func_FindNextStreamW)(HANDLE hFindStream,
528 LPVOID lpFindStreamData) = NULL;
529 #endif /* !WITH_NTDLL */
531 /* Vista and later */
532 BOOL (WINAPI *win32func_CreateSymbolicLinkW)(const wchar_t *lpSymlinkFileName,
533 const wchar_t *lpTargetFileName,
534 DWORD dwFlags) = NULL;
536 static OSVERSIONINFO windows_version_info = {
537 .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
540 static HMODULE hKernel32 = NULL;
542 static bool acquired_privileges = false;
545 windows_version_is_at_least(unsigned major, unsigned minor)
547 return windows_version_info.dwMajorVersion > major ||
548 (windows_version_info.dwMajorVersion == major &&
549 windows_version_info.dwMinorVersion >= minor);
552 /* One-time initialization for Windows capture/apply code. */
554 win32_global_init(int init_flags)
556 /* Try to acquire useful privileges. */
557 if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
558 if (!win32_modify_capture_privileges(true))
559 if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
560 goto insufficient_privileges;
561 if (!win32_modify_apply_privileges(true))
562 if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
563 goto insufficient_privileges;
564 acquired_privileges = true;
567 /* Get Windows version information. */
568 GetVersionEx(&windows_version_info);
570 /* Try to dynamically load some functions. */
571 if (hKernel32 == NULL)
572 hKernel32 = LoadLibrary(L"Kernel32.dll");
576 win32func_FindFirstStreamW = (void*)GetProcAddress(hKernel32,
578 if (win32func_FindFirstStreamW) {
579 win32func_FindNextStreamW = (void*)GetProcAddress(hKernel32,
581 if (!win32func_FindNextStreamW)
582 win32func_FindFirstStreamW = NULL;
584 #endif /* !WITH_NTDLL */
585 win32func_CreateSymbolicLinkW = (void*)GetProcAddress(hKernel32,
586 "CreateSymbolicLinkW");
590 insufficient_privileges:
591 win32_release_capture_and_apply_privileges();
592 return WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
596 win32_global_cleanup(void)
598 if (acquired_privileges)
599 win32_release_capture_and_apply_privileges();
600 if (hKernel32 != NULL) {
601 FreeLibrary(hKernel32);
606 #endif /* __WIN32__ */