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. */
19 win32_wglob(const wchar_t *pattern, int flags,
20 int (*errfunc)(const wchar_t *epath, int eerrno),
29 const wchar_t *backslash, *end_slash;
32 backslash = wcsrchr(pattern, L'\\');
33 end_slash = wcsrchr(pattern, L'/');
35 if (backslash > end_slash)
36 end_slash = backslash;
39 prefix_len = end_slash - pattern + 1;
43 /* This function does not support all functionality of the POSIX glob(),
44 * so make sure the parameters are consistent with supported
46 assert(errfunc == NULL);
47 assert((flags & GLOB_ERR) == GLOB_ERR);
48 assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
50 hFind = FindFirstFileW(pattern, &dat);
51 if (hFind == INVALID_HANDLE_VALUE) {
53 if (err == ERROR_FILE_NOT_FOUND) {
57 /* The other possible error codes for FindFirstFile()
58 * are undocumented. */
64 pglob->gl_pathv = NULL;
68 if (pglob->gl_pathc == nspaces) {
72 new_nspaces = nspaces * 2 + 1;
73 pathv = realloc(pglob->gl_pathv,
74 new_nspaces * sizeof(pglob->gl_pathv[0]));
77 pglob->gl_pathv = pathv;
78 nspaces = new_nspaces;
80 size_t filename_len = wcslen(dat.cFileName);
81 size_t len_needed = prefix_len + filename_len;
83 path = malloc(len_needed + sizeof(wchar_t));
87 wmemcpy(path, pattern, prefix_len);
88 wmemcpy(path + prefix_len, dat.cFileName, filename_len + 1);
89 pglob->gl_pathv[pglob->gl_pathc++] = path;
90 } while (FindNextFileW(hFind, &dat));
93 if (err == ERROR_NO_MORE_FILES) {
97 /* Other possible error codes for FindNextFile() are
113 globfree(glob_t *pglob)
116 for (i = 0; i < pglob->gl_pathc; i++)
117 free(pglob->gl_pathv[i]);
118 free(pglob->gl_pathv);
122 win32_modify_privilege(const char *privilege, bool enable)
126 TOKEN_PRIVILEGES newState;
129 if (!OpenProcessToken(GetCurrentProcess(),
130 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
136 if (!LookupPrivilegeValue(NULL, privilege, &luid)) {
140 newState.PrivilegeCount = 1;
141 newState.Privileges[0].Luid = luid;
142 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
143 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
147 fwprintf(stderr, L"WARNING: Failed to %ls privilege %s\n",
148 enable ? L"enable" : L"disable", privilege);
150 L"WARNING: The program will continue, "
151 L"but if permission issues are\n"
152 L"encountered, you may need to run "
153 L"this program as the administrator\n");
159 win32_modify_capture_privileges(bool enable)
161 win32_modify_privilege(SE_BACKUP_NAME, enable);
162 win32_modify_privilege(SE_SECURITY_NAME, enable);
166 win32_modify_restore_privileges(bool enable)
168 win32_modify_privilege(SE_RESTORE_NAME, enable);
169 win32_modify_privilege(SE_SECURITY_NAME, enable);
170 win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
174 win32_acquire_capture_privileges()
176 win32_modify_capture_privileges(true);
180 win32_release_capture_privileges()
182 win32_modify_capture_privileges(false);
186 win32_acquire_restore_privileges()
188 win32_modify_restore_privileges(true);
192 win32_release_restore_privileges()
194 win32_modify_restore_privileges(false);
198 win32_mbs_to_wcs(const char *mbs, size_t mbs_nbytes, size_t *num_wchars_ret)
200 if (mbs_nbytes > INT_MAX) {
201 fwprintf(stderr, L"ERROR: too much data (%zu bytes)!\n",
205 if (mbs_nbytes == 0) {
207 return (wchar_t*)mbs;
209 int len = MultiByteToWideChar(CP_ACP,
210 MB_ERR_INVALID_CHARS,
217 wchar_t *wcs = malloc(len * sizeof(wchar_t));
219 fwprintf(stderr, L"ERROR: out of memory!\n");
222 int len2 = MultiByteToWideChar(CP_ACP,
223 MB_ERR_INVALID_CHARS,
232 *num_wchars_ret = len;
236 L"ERROR: Invalid multi-byte string in the text file you provided as input!\n"
237 L" Maybe try converting your text file to UTF-16LE?\n"
243 is_path_separator(wchar_t c)
245 return c == L'/' || c == L'\\';
249 win32_wbasename(wchar_t *path)
251 wchar_t *p = wcschr(path, L'\0');
254 while (p >= path && is_path_separator(*p))
256 while (p >= path && !is_path_separator(*p))