Fix glob()
[wimlib] / programs / imagex-win32.c
1
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 */
5
6 #ifndef __WIN32__
7 #  error "This file contains Windows code"
8 #endif
9
10 #include "imagex-win32.h"
11 #include <windows.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <assert.h>
15 #include <stdio.h>
16
17 /* Replacement for glob() in Windows native builds. */
18 int glob(const char *pattern, int flags,
19          int (*errfunc)(const char *epath, int eerrno),
20          glob_t *pglob)
21 {
22         WIN32_FIND_DATA dat;
23         DWORD err;
24         HANDLE hFind;
25         int ret;
26         size_t nspaces;
27
28         const char *backslash, *forward_slash, *end_slash;
29         size_t prefix_len;
30
31         backslash = strrchr(pattern, '\\');
32         end_slash = strrchr(pattern, '/');
33
34         if (backslash > end_slash)
35                 end_slash = backslash;
36
37         if (end_slash)
38                 prefix_len = end_slash - pattern + 1;
39         else
40                 prefix_len = 0;
41
42         /* This function does not support all functionality of the POSIX glob(),
43          * so make sure the parameters are consistent with supported
44          * functionality. */
45         assert(errfunc == NULL);
46         assert((flags & GLOB_ERR) == GLOB_ERR);
47         assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
48
49         hFind = FindFirstFileA(pattern, &dat);
50         if (hFind == INVALID_HANDLE_VALUE) {
51                 err = GetLastError();
52                 if (err == ERROR_FILE_NOT_FOUND) {
53                         errno = 0;
54                         return GLOB_NOMATCH;
55                 } else {
56                         /* The other possible error codes for FindFirstFile()
57                          * are undocumented. */
58                         errno = EIO;
59                         return GLOB_ABORTED;
60                 }
61         }
62         pglob->gl_pathc = 0;
63         pglob->gl_pathv = NULL;
64         nspaces = 0;
65         do {
66                 char *path;
67                 if (pglob->gl_pathc == nspaces) {
68                         size_t new_nspaces;
69                         char **pathv;
70
71                         new_nspaces = nspaces * 2 + 1;  
72                         pathv = realloc(pglob->gl_pathv,
73                                         new_nspaces * sizeof(pglob->gl_pathv[0]));
74                         if (!pathv)
75                                 goto oom;
76                         pglob->gl_pathv = pathv;
77                         nspaces = new_nspaces;
78                 }
79                 size_t filename_len = strlen(dat.cFileName);
80                 size_t len_needed = prefix_len + filename_len;
81
82                 path = malloc(len_needed + 1);
83                 if (!path)
84                         goto oom;
85
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));
90         err = GetLastError();
91         CloseHandle(hFind);
92         if (err == ERROR_NO_MORE_FILES) {
93                 errno = 0;
94                 return 0;
95         } else {
96                 /* Other possible error codes for FindNextFile() are
97                  * undocumented */
98                 errno = EIO;
99                 ret = GLOB_ABORTED;
100                 goto fail_globfree;
101         }
102 oom:
103         CloseHandle(hFind);
104         errno = ENOMEM;
105         ret = GLOB_NOSPACE;
106 fail_globfree:
107         globfree(pglob);
108         return ret;
109 }
110
111 void globfree(glob_t *pglob)
112 {
113         size_t i;
114         for (i = 0; i < pglob->gl_pathc; i++)
115                 free(pglob->gl_pathv[i]);
116         free(pglob->gl_pathv);
117 }
118
119 static bool
120 win32_modify_privilege(const char *privilege, bool enable)
121 {
122         HANDLE hToken;
123         LUID luid;
124         TOKEN_PRIVILEGES newState;
125         bool ret = false;
126
127         if (!OpenProcessToken(GetCurrentProcess(),
128                               TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
129                               &hToken))
130         {
131                 goto out;
132         }
133
134         if (!LookupPrivilegeValue(NULL, privilege, &luid)) {
135                 goto out;
136         }
137
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);
142         CloseHandle(hToken);
143 out:
144         if (!ret) {
145                 fprintf(stderr, "WARNING: Failed to %s privilege %s\n",
146                         enable ? "enable" : "disable", privilege);
147                 fprintf(stderr,
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");
152         }
153         return ret;
154 }
155
156 static void
157 win32_modify_capture_privileges(bool enable)
158 {
159         win32_modify_privilege(SE_BACKUP_NAME, enable);
160         win32_modify_privilege(SE_SECURITY_NAME, enable);
161 }
162
163 static void
164 win32_modify_restore_privileges(bool enable)
165 {
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);
169 }
170
171 void
172 win32_acquire_capture_privileges()
173 {
174         win32_modify_capture_privileges(true);
175 }
176
177 void
178 win32_release_capture_privileges()
179 {
180         win32_modify_capture_privileges(false);
181 }
182
183 void
184 win32_acquire_restore_privileges()
185 {
186         win32_modify_restore_privileges(true);
187 }
188
189 void
190 win32_release_restore_privileges()
191 {
192         win32_modify_restore_privileges(false);
193 }