3 #if defined(__CYGWIN__) || defined(__WIN32__)
9 #include "wimlib_internal.h"
12 #ifdef ENABLE_ERROR_MESSAGES
13 void win32_error(u32 err_code)
17 nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
19 (char*)&buffer, 0, NULL);
21 ERROR("Error printing error message! "
22 "Computer will self-destruct in 3 seconds.");
24 ERROR("Win32 error: %s", buffer);
29 #define win32_error(err_code)
32 void *win32_open_file_readonly(const void *path)
34 return CreateFileW((const wchar_t*)path,
36 FILE_READ_ATTRIBUTES |
38 ACCESS_SYSTEM_SECURITY,
40 NULL, /* lpSecurityAttributes */
42 FILE_FLAG_BACKUP_SEMANTICS |
43 FILE_FLAG_OPEN_REPARSE_POINT,
44 NULL /* hTemplateFile */);
47 int win32_read_file(const char *filename,
48 void *handle, u64 offset, size_t size, u8 *buf)
53 LARGE_INTEGER liOffset = {.QuadPart = offset};
55 wimlib_assert(size <= 0xffffffff);
57 if (SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
58 if (ReadFile(h, buf, size, &bytesRead, NULL) && bytesRead == size)
61 ERROR("Error reading \"%s\"", filename);
63 return WIMLIB_ERR_READ;
66 void win32_close_file(void *handle)
68 CloseHandle((HANDLE)handle);
71 static bool win32_modify_privilege(const char *privilege, bool enable)
75 TOKEN_PRIVILEGES newState;
78 DEBUG("%s privilege %s",
79 enable ? "Enabling" : "Disabling", privilege);
81 if (!OpenProcessToken(GetCurrentProcess(),
82 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
85 DEBUG("OpenProcessToken() failed");
89 if (!LookupPrivilegeValue(NULL, privilege, &luid)) {
90 DEBUG("LookupPrivilegeValue() failed");
94 newState.PrivilegeCount = 1;
95 newState.Privileges[0].Luid = luid;
96 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
97 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
99 DEBUG("AdjustTokenPrivileges() failed");
103 DWORD err = GetLastError();
105 WARNING("Failed to %s privilege %s",
106 enable ? "enable" : "disable", privilege);
107 WARNING("The program will continue, but if permission issues are "
108 "encountered, you may need to run this program as the administrator");
113 bool win32_acquire_privilege(const char *privilege)
115 return win32_modify_privilege(privilege, true);
118 bool win32_release_privilege(const char *privilege)
120 return win32_modify_privilege(privilege, false);
124 #endif /* __CYGWIN__ || __WIN32__ */