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. */
18 int glob(const char *pattern, int flags,
19 int (*errfunc)(const char *epath, int eerrno),
28 const char *backslash, *forward_slash, *end_slash;
31 backslash = strrchr(pattern, '\\');
32 end_slash = strrchr(pattern, '/');
34 if (backslash > end_slash)
35 end_slash = backslash;
38 prefix_len = end_slash - pattern + 1;
42 /* This function does not support all functionality of the POSIX glob(),
43 * so make sure the parameters are consistent with supported
45 assert(errfunc == NULL);
46 assert((flags & GLOB_ERR) == GLOB_ERR);
47 assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
49 hFind = FindFirstFileA(pattern, &dat);
50 if (hFind == INVALID_HANDLE_VALUE) {
52 if (err == ERROR_FILE_NOT_FOUND) {
56 /* The other possible error codes for FindFirstFile()
57 * are undocumented. */
63 pglob->gl_pathv = NULL;
67 if (pglob->gl_pathc == nspaces) {
71 new_nspaces = nspaces * 2 + 1;
72 pathv = realloc(pglob->gl_pathv,
73 new_nspaces * sizeof(pglob->gl_pathv[0]));
76 pglob->gl_pathv = pathv;
77 nspaces = new_nspaces;
79 size_t filename_len = strlen(dat.cFileName);
80 size_t len_needed = prefix_len + filename_len;
82 path = malloc(len_needed + 1);
86 memcpy(path, pattern, prefix_len);
87 memcpy(path + prefix_len, dat.cFileName, filename_len + 1);
88 pglob->gl_pathv[pglob->gl_pathc++] = path;
89 } while (FindNextFileA(hFind, &dat));
92 if (err == ERROR_NO_MORE_FILES) {
96 /* Other possible error codes for FindNextFile() are
111 void globfree(glob_t *pglob)
114 for (i = 0; i < pglob->gl_pathc; i++)
115 free(pglob->gl_pathv[i]);
116 free(pglob->gl_pathv);
120 win32_modify_privilege(const char *privilege, bool enable)
124 TOKEN_PRIVILEGES newState;
127 if (!OpenProcessToken(GetCurrentProcess(),
128 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
134 if (!LookupPrivilegeValue(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 fprintf(stderr, "WARNING: Failed to %s privilege %s\n",
146 enable ? "enable" : "disable", privilege);
148 "WARNING: The program will continue, "
149 "but if permission issues are\n"
150 "encountered, you may need to run "
151 "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);