2 /* Replacements for functions needed specifically by the 'imagex' program in
3 * Windows native builds; also, Windows-specific code to acquire and release
4 * privileges needed to backup and restore files */
7 # error "This file contains Windows code"
10 #include "imagex-win32.h"
19 /* Replacement for glob() in Windows native builds that operates on wide
22 win32_wglob(const wchar_t *pattern, int flags,
23 int (*errfunc)(const wchar_t *epath, int eerrno),
32 const wchar_t *backslash, *end_slash;
35 backslash = wcsrchr(pattern, L'\\');
36 end_slash = wcsrchr(pattern, L'/');
38 if (backslash > end_slash)
39 end_slash = backslash;
42 prefix_len = end_slash - pattern + 1;
46 /* This function does not support all functionality of the POSIX glob(),
47 * so make sure the parameters are consistent with supported
49 assert(errfunc == NULL);
50 assert((flags & GLOB_ERR) == GLOB_ERR);
51 assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
53 hFind = FindFirstFileW(pattern, &dat);
54 if (hFind == INVALID_HANDLE_VALUE) {
56 if (err == ERROR_FILE_NOT_FOUND) {
60 /* The other possible error codes for FindFirstFile()
61 * are undocumented. */
67 pglob->gl_pathv = NULL;
71 if (pglob->gl_pathc == nspaces) {
75 new_nspaces = nspaces * 2 + 1;
76 pathv = realloc(pglob->gl_pathv,
77 new_nspaces * sizeof(pglob->gl_pathv[0]));
80 pglob->gl_pathv = pathv;
81 nspaces = new_nspaces;
83 size_t filename_len = wcslen(dat.cFileName);
84 size_t len_needed = prefix_len + filename_len;
86 path = malloc((len_needed + 1) * sizeof(wchar_t));
90 wmemcpy(path, pattern, prefix_len);
91 wmemcpy(path + prefix_len, dat.cFileName, filename_len + 1);
92 pglob->gl_pathv[pglob->gl_pathc++] = path;
93 } while (FindNextFileW(hFind, &dat));
96 if (err == ERROR_NO_MORE_FILES) {
100 /* Other possible error codes for FindNextFile() are
116 globfree(glob_t *pglob)
119 for (i = 0; i < pglob->gl_pathc; i++)
120 free(pglob->gl_pathv[i]);
121 free(pglob->gl_pathv);
125 win32_modify_privilege(const wchar_t *privilege, bool enable)
129 TOKEN_PRIVILEGES newState;
132 if (!OpenProcessToken(GetCurrentProcess(),
133 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
137 if (!LookupPrivilegeValue(NULL, privilege, &luid))
138 goto out_close_handle;
140 newState.PrivilegeCount = 1;
141 newState.Privileges[0].Luid = luid;
142 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
143 SetLastError(ERROR_SUCCESS);
144 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
145 if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
150 if (!ret && enable) {
152 L"WARNING: Failed to enable %ls!\n"
153 " The program will continue, but if "
154 "permission issues are\n"
155 " encountered, you may need to run "
156 "this program as the Administrator.\n",
163 win32_modify_capture_privileges(bool enable)
165 win32_modify_privilege(SE_BACKUP_NAME, enable);
166 win32_modify_privilege(SE_SECURITY_NAME, enable);
170 win32_modify_restore_privileges(bool enable)
172 win32_modify_privilege(SE_RESTORE_NAME, enable);
173 win32_modify_privilege(SE_SECURITY_NAME, enable);
174 win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
178 win32_acquire_capture_privileges(void)
180 win32_modify_capture_privileges(true);
184 win32_release_capture_privileges(void)
186 win32_modify_capture_privileges(false);
190 win32_acquire_restore_privileges(void)
192 win32_modify_restore_privileges(true);
196 win32_release_restore_privileges(void)
198 win32_modify_restore_privileges(false);
201 /* Convert a string from the "current Windows codepage" to UTF-16LE. */
203 win32_mbs_to_wcs(const char *mbs, size_t mbs_nbytes, size_t *num_wchars_ret)
205 if (mbs_nbytes > INT_MAX) {
206 fwprintf(stderr, L"ERROR: too much data (%zu bytes)!\n",
210 if (mbs_nbytes == 0) {
212 return (wchar_t*)mbs;
214 int len = MultiByteToWideChar(CP_ACP,
215 MB_ERR_INVALID_CHARS,
222 wchar_t *wcs = malloc(len * sizeof(wchar_t));
224 fwprintf(stderr, L"ERROR: out of memory!\n");
227 int len2 = MultiByteToWideChar(CP_ACP,
228 MB_ERR_INVALID_CHARS,
237 *num_wchars_ret = len;
241 L"ERROR: Invalid multi-byte string in the text file you provided as input!\n"
242 L" Maybe try converting your text file to UTF-16LE?\n"
248 is_path_separator(wchar_t c)
250 return c == L'/' || c == L'\\';
253 /* basename() (modifying, trailing-slash stripping version) for wide-character
256 win32_wbasename(wchar_t *path)
258 wchar_t *p = wcschr(path, L'\0');
261 while (p >= path && is_path_separator(*p))
263 while (p >= path && !is_path_separator(*p))
269 void set_fd_to_binary_mode(int fd)
271 _setmode(fd, _O_BINARY);