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"
17 /* Replacement for glob() in Windows native builds that operates on wide
20 win32_wglob(const wchar_t *pattern, int flags,
21 int (*errfunc)(const wchar_t *epath, int eerrno),
30 const wchar_t *backslash, *end_slash;
33 backslash = wcsrchr(pattern, L'\\');
34 end_slash = wcsrchr(pattern, L'/');
36 if (backslash > end_slash)
37 end_slash = backslash;
40 prefix_len = end_slash - pattern + 1;
44 /* This function does not support all functionality of the POSIX glob(),
45 * so make sure the parameters are consistent with supported
47 assert(errfunc == NULL);
48 assert((flags & GLOB_ERR) == GLOB_ERR);
49 assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
51 hFind = FindFirstFileW(pattern, &dat);
52 if (hFind == INVALID_HANDLE_VALUE) {
54 if (err == ERROR_FILE_NOT_FOUND) {
58 /* The other possible error codes for FindFirstFile()
59 * are undocumented. */
65 pglob->gl_pathv = NULL;
69 if (pglob->gl_pathc == nspaces) {
73 new_nspaces = nspaces * 2 + 1;
74 pathv = realloc(pglob->gl_pathv,
75 new_nspaces * sizeof(pglob->gl_pathv[0]));
78 pglob->gl_pathv = pathv;
79 nspaces = new_nspaces;
81 size_t filename_len = wcslen(dat.cFileName);
82 size_t len_needed = prefix_len + filename_len;
84 path = malloc(len_needed + sizeof(wchar_t));
88 wmemcpy(path, pattern, prefix_len);
89 wmemcpy(path + prefix_len, dat.cFileName, filename_len + 1);
90 pglob->gl_pathv[pglob->gl_pathc++] = path;
91 } while (FindNextFileW(hFind, &dat));
94 if (err == ERROR_NO_MORE_FILES) {
98 /* Other possible error codes for FindNextFile() are
114 globfree(glob_t *pglob)
117 for (i = 0; i < pglob->gl_pathc; i++)
118 free(pglob->gl_pathv[i]);
119 free(pglob->gl_pathv);
123 win32_modify_privilege(const wchar_t *privilege, bool enable)
127 TOKEN_PRIVILEGES newState;
130 if (!OpenProcessToken(GetCurrentProcess(),
131 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
135 if (!LookupPrivilegeValueW(NULL, privilege, &luid))
138 newState.PrivilegeCount = 1;
139 newState.Privileges[0].Luid = luid;
140 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
141 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
145 fwprintf(stderr, L"WARNING: Failed to %ls privilege %s\n",
146 enable ? L"enable" : L"disable", privilege);
148 L"WARNING: The program will continue, "
149 L"but if permission issues are\n"
150 L"encountered, you may need to run "
151 L"this program as the administrator\n");
157 win32_modify_capture_privileges(bool enable)
159 win32_modify_privilege(SE_BACKUP_NAME, enable);
160 win32_modify_privilege(SE_SECURITY_NAME, enable);
164 win32_modify_restore_privileges(bool enable)
166 win32_modify_privilege(SE_RESTORE_NAME, enable);
167 win32_modify_privilege(SE_SECURITY_NAME, enable);
168 win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
172 win32_acquire_capture_privileges()
174 win32_modify_capture_privileges(true);
178 win32_release_capture_privileges()
180 win32_modify_capture_privileges(false);
184 win32_acquire_restore_privileges()
186 win32_modify_restore_privileges(true);
190 win32_release_restore_privileges()
192 win32_modify_restore_privileges(false);
195 /* Convert a string from the "current Windows codepage" to UTF-16LE. */
197 win32_mbs_to_wcs(const char *mbs, size_t mbs_nbytes, size_t *num_wchars_ret)
199 if (mbs_nbytes > INT_MAX) {
200 fwprintf(stderr, L"ERROR: too much data (%zu bytes)!\n",
204 if (mbs_nbytes == 0) {
206 return (wchar_t*)mbs;
208 int len = MultiByteToWideChar(CP_ACP,
209 MB_ERR_INVALID_CHARS,
216 wchar_t *wcs = malloc(len * sizeof(wchar_t));
218 fwprintf(stderr, L"ERROR: out of memory!\n");
221 int len2 = MultiByteToWideChar(CP_ACP,
222 MB_ERR_INVALID_CHARS,
231 *num_wchars_ret = len;
235 L"ERROR: Invalid multi-byte string in the text file you provided as input!\n"
236 L" Maybe try converting your text file to UTF-16LE?\n"
242 is_path_separator(wchar_t c)
244 return c == L'/' || c == L'\\';
247 /* basename() (modifying, trailing-slash stripping version) for wide-character
250 win32_wbasename(wchar_t *path)
252 wchar_t *p = wcschr(path, L'\0');
255 while (p >= path && is_path_separator(*p))
257 while (p >= path && !is_path_separator(*p))