24f99108e6ec30d948468c9fd7e4c54ceae69655
[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 #include <fcntl.h>
17 #include <io.h>
18
19 /* Replacement for glob() in Windows native builds that operates on wide
20  * characters. */
21 int
22 win32_wglob(const wchar_t *pattern, int flags,
23             int (*errfunc)(const wchar_t *epath, int eerrno),
24             glob_t *pglob)
25 {
26         WIN32_FIND_DATAW dat;
27         DWORD err;
28         HANDLE hFind;
29         int ret;
30         size_t nspaces;
31
32         const wchar_t *backslash, *end_slash;
33         size_t prefix_len;
34
35         backslash = wcsrchr(pattern, L'\\');
36         end_slash = wcsrchr(pattern, L'/');
37
38         if (backslash > end_slash)
39                 end_slash = backslash;
40
41         if (end_slash)
42                 prefix_len = end_slash - pattern + 1;
43         else
44                 prefix_len = 0;
45
46         /* This function does not support all functionality of the POSIX glob(),
47          * so make sure the parameters are consistent with supported
48          * functionality. */
49         assert(errfunc == NULL);
50         assert((flags & GLOB_ERR) == GLOB_ERR);
51         assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
52
53         hFind = FindFirstFileW(pattern, &dat);
54         if (hFind == INVALID_HANDLE_VALUE) {
55                 err = GetLastError();
56                 if (err == ERROR_FILE_NOT_FOUND) {
57                         errno = 0;
58                         return GLOB_NOMATCH;
59                 } else {
60                         /* The other possible error codes for FindFirstFile()
61                          * are undocumented. */
62                         errno = EIO;
63                         return GLOB_ABORTED;
64                 }
65         }
66         pglob->gl_pathc = 0;
67         pglob->gl_pathv = NULL;
68         nspaces = 0;
69         do {
70                 wchar_t *path;
71                 if (pglob->gl_pathc == nspaces) {
72                         size_t new_nspaces;
73                         wchar_t **pathv;
74
75                         new_nspaces = nspaces * 2 + 1;  
76                         pathv = realloc(pglob->gl_pathv,
77                                         new_nspaces * sizeof(pglob->gl_pathv[0]));
78                         if (!pathv)
79                                 goto oom;
80                         pglob->gl_pathv = pathv;
81                         nspaces = new_nspaces;
82                 }
83                 size_t filename_len = wcslen(dat.cFileName);
84                 size_t len_needed = prefix_len + filename_len;
85
86                 path = malloc((len_needed + 1) * sizeof(wchar_t));
87                 if (!path)
88                         goto oom;
89
90                 wmemcpy(path, pattern, prefix_len);
91                 wmemcpy(path + prefix_len, dat.cFileName, filename_len + 1);
92                 pglob->gl_pathv[pglob->gl_pathc++] = path;
93         } while (FindNextFileW(hFind, &dat));
94         err = GetLastError();
95         CloseHandle(hFind);
96         if (err == ERROR_NO_MORE_FILES) {
97                 errno = 0;
98                 return 0;
99         } else {
100                 /* Other possible error codes for FindNextFile() are
101                  * undocumented */
102                 errno = EIO;
103                 ret = GLOB_ABORTED;
104                 goto fail_globfree;
105         }
106 oom:
107         CloseHandle(hFind);
108         errno = ENOMEM;
109         ret = GLOB_NOSPACE;
110 fail_globfree:
111         globfree(pglob);
112         return ret;
113 }
114
115 void
116 globfree(glob_t *pglob)
117 {
118         size_t i;
119         for (i = 0; i < pglob->gl_pathc; i++)
120                 free(pglob->gl_pathv[i]);
121         free(pglob->gl_pathv);
122 }
123
124 /* Convert a string from the "current Windows codepage" to UTF-16LE. */
125 wchar_t *
126 win32_mbs_to_wcs(const char *mbs, size_t mbs_nbytes, size_t *num_wchars_ret)
127 {
128         if (mbs_nbytes > INT_MAX) {
129                 fwprintf(stderr, L"ERROR: too much data (%zu bytes)!\n",
130                          mbs_nbytes);
131                 return NULL;
132         }
133         if (mbs_nbytes == 0) {
134                 *num_wchars_ret = 0;
135                 return (wchar_t*)mbs;
136         }
137         int len = MultiByteToWideChar(CP_ACP,
138                                       MB_ERR_INVALID_CHARS,
139                                       mbs,
140                                       mbs_nbytes,
141                                       NULL,
142                                       0);
143         if (len <= 0)
144                 goto out_invalid;
145         wchar_t *wcs = malloc(len * sizeof(wchar_t));
146         if (!wcs) {
147                 fwprintf(stderr, L"ERROR: out of memory!\n");
148                 return NULL;
149         }
150         int len2 = MultiByteToWideChar(CP_ACP,
151                                        MB_ERR_INVALID_CHARS,
152                                        mbs,
153                                        mbs_nbytes,
154                                        wcs,
155                                        len);
156         if (len2 != len) {
157                 free(wcs);
158                 goto out_invalid;
159         }
160         *num_wchars_ret = len;
161         return wcs;
162 out_invalid:
163         fwprintf(stderr,
164 L"ERROR: Invalid multi-byte string in the text file you provided as input!\n"
165 L"       Maybe try converting your text file to UTF-16LE?\n"
166         );
167         return NULL;
168 }
169
170 static inline bool
171 is_path_separator(wchar_t c)
172 {
173         return c == L'/' || c == L'\\';
174 }
175
176 /* basename() (modifying, trailing-slash stripping version) for wide-character
177  * strings. */
178 wchar_t *
179 win32_wbasename(wchar_t *path)
180 {
181         wchar_t *p = wcschr(path, L'\0');
182
183         p--;
184         while (p >= path && is_path_separator(*p))
185                 *p-- = '\0';
186         while (p >= path && !is_path_separator(*p))
187                 p--;
188         p++;
189         return p;
190 }
191
192 void set_fd_to_binary_mode(int fd)
193 {
194         _setmode(fd, _O_BINARY);
195 }