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 /* This function does not support all functionality of the POSIX glob(),
29 * so make sure the parameters are consistent with supported
31 assert(errfunc == NULL);
32 assert((flags & GLOB_ERR) == GLOB_ERR);
33 assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
35 hFind = FindFirstFileA(pattern, &dat);
36 if (hFind == INVALID_HANDLE_VALUE) {
38 if (err == ERROR_FILE_NOT_FOUND) {
42 /* The other possible error codes for FindFirstFile()
43 * are undocumented. */
49 pglob->gl_pathv = NULL;
53 if (pglob->gl_pathc == nspaces) {
57 new_nspaces = nspaces * 2 + 1;
58 pathv = realloc(pglob->gl_pathv,
59 new_nspaces * sizeof(pglob->gl_pathv[0]));
62 pglob->gl_pathv = pathv;
63 nspaces = new_nspaces;
65 filename = strdup(dat.cFileName);
68 pglob->gl_pathv[pglob->gl_pathc++] = filename;
69 } while (FindNextFileA(hFind, &dat));
72 if (err == ERROR_NO_MORE_FILES) {
76 /* Other possible error codes for FindNextFile() are
91 void globfree(glob_t *pglob)
94 for (i = 0; i < pglob->gl_pathc; i++)
95 free(pglob->gl_pathv[i]);
96 free(pglob->gl_pathv[i]);
100 win32_modify_privilege(const char *privilege, bool enable)
104 TOKEN_PRIVILEGES newState;
107 if (!OpenProcessToken(GetCurrentProcess(),
108 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
114 if (!LookupPrivilegeValue(NULL, privilege, &luid)) {
118 newState.PrivilegeCount = 1;
119 newState.Privileges[0].Luid = luid;
120 newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
121 ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
125 fprintf(stderr, "WARNING: Failed to %s privilege %s\n",
126 enable ? "enable" : "disable", privilege);
128 "WARNING: The program will continue, "
129 "but if permission issues are\n"
130 "encountered, you may need to run "
131 "this program as the administrator\n");
137 win32_modify_capture_privileges(bool enable)
139 win32_modify_privilege(SE_BACKUP_NAME, enable);
140 win32_modify_privilege(SE_SECURITY_NAME, enable);
144 win32_modify_restore_privileges(bool enable)
146 win32_modify_privilege(SE_RESTORE_NAME, enable);
147 win32_modify_privilege(SE_SECURITY_NAME, enable);
148 win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
152 win32_acquire_capture_privileges()
154 win32_modify_capture_privileges(true);
158 win32_release_capture_privileges()
160 win32_modify_capture_privileges(false);
164 win32_acquire_restore_privileges()
166 win32_modify_restore_privileges(true);
170 win32_release_restore_privileges()
172 win32_modify_restore_privileges(false);