2 * win32_replacements.c - Replacements for various functions not available on
3 * Windows, such as fsync().
7 * Copyright (C) 2013 Eric Biggers
9 * This file is part of wimlib, a library for working with WIM files.
11 * wimlib is free software; you can redistribute it and/or modify it under the
12 * terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option)
16 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 * A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with wimlib; if not, see http://www.gnu.org/licenses/.
33 #include <shlwapi.h> /* for PathMatchSpecW() */
34 #include "wimlib/win32_common.h"
36 #include "wimlib/assert.h"
37 #include "wimlib/file_io.h"
38 #include "wimlib/glob.h"
39 #include "wimlib/error.h"
40 #include "wimlib/util.h"
42 /* Replacement for POSIX fsync() */
48 h = (HANDLE)_get_osfhandle(fd);
49 if (h == INVALID_HANDLE_VALUE)
51 if (!FlushFileBuffers(h))
55 set_errno_from_GetLastError();
60 /* Use the Win32 API to get the number of processors */
62 win32_get_number_of_processors(void)
65 GetSystemInfo(&sysinfo);
66 return sysinfo.dwNumberOfProcessors;
69 /* Replacement for POSIX-2008 realpath(). Warning: partial functionality only
70 * (resolved_path must be NULL). Also I highly doubt that GetFullPathName
71 * really does the right thing under all circumstances. */
73 realpath(const wchar_t *path, wchar_t *resolved_path)
77 wimlib_assert(resolved_path == NULL);
79 ret = GetFullPathNameW(path, 0, NULL, NULL);
85 resolved_path = MALLOC(ret * sizeof(wchar_t));
88 ret = GetFullPathNameW(path, ret, resolved_path, NULL);
97 set_errno_from_win32_error(err);
102 /* rename() on Windows fails if the destination file exists. And we need to
103 * make it work on wide characters. Fix it. */
105 win32_rename_replacement(const wchar_t *oldpath, const wchar_t *newpath)
107 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) {
110 set_errno_from_GetLastError();
115 /* Replacement for POSIX fnmatch() (partial functionality only) */
117 fnmatch(const wchar_t *pattern, const wchar_t *string, int flags)
119 if (PathMatchSpecW(string, pattern))
125 /* truncate() replacement */
127 win32_truncate_replacement(const wchar_t *path, off_t size)
129 DWORD err = NO_ERROR;
130 LARGE_INTEGER liOffset;
132 HANDLE h = win32_open_existing_file(path, GENERIC_WRITE);
133 if (h == INVALID_HANDLE_VALUE)
136 liOffset.QuadPart = size;
137 if (!SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
138 goto fail_close_handle;
140 if (!SetEndOfFile(h))
141 goto fail_close_handle;
146 err = GetLastError();
150 err = GetLastError();
151 set_errno_from_win32_error(err);
156 /* This really could be replaced with _wcserror_s, but this doesn't seem to
157 * actually be available in MSVCRT.DLL on Windows XP (perhaps it's statically
158 * linked in by Visual Studio...?). */
160 win32_strerror_r_replacement(int errnum, wchar_t *buf, size_t buflen)
162 static pthread_mutex_t strerror_lock = PTHREAD_MUTEX_INITIALIZER;
164 pthread_mutex_lock(&strerror_lock);
165 mbstowcs(buf, strerror(errnum), buflen);
166 buf[buflen - 1] = '\0';
167 pthread_mutex_unlock(&strerror_lock);
172 do_pread_or_pwrite(int fd, void *buf, size_t count, off_t offset,
176 LARGE_INTEGER orig_offset;
177 DWORD bytes_read_or_written;
178 LARGE_INTEGER relative_offset;
179 OVERLAPPED overlapped;
182 h = (HANDLE)_get_osfhandle(fd);
183 if (h == INVALID_HANDLE_VALUE)
186 if (GetFileType(h) == FILE_TYPE_PIPE) {
191 /* Get original position */
192 relative_offset.QuadPart = 0;
193 if (!SetFilePointerEx(h, relative_offset, &orig_offset, FILE_CURRENT))
196 memset(&overlapped, 0, sizeof(overlapped));
197 overlapped.Offset = offset;
198 overlapped.OffsetHigh = offset >> 32;
200 /* Do the read or write at the specified offset */
202 bret = WriteFile(h, buf, count, &bytes_read_or_written, &overlapped);
204 bret = ReadFile(h, buf, count, &bytes_read_or_written, &overlapped);
208 /* Restore the original position */
209 if (!SetFilePointerEx(h, orig_offset, NULL, FILE_BEGIN))
212 return bytes_read_or_written;
214 set_errno_from_GetLastError();
219 /* Dumb Windows implementation of pread(). It temporarily changes the file
220 * offset, so it is not safe to use with readers/writers on the same file
223 pread(int fd, void *buf, size_t count, off_t offset)
225 return do_pread_or_pwrite(fd, buf, count, offset, false);
228 /* Dumb Windows implementation of pwrite(). It temporarily changes the file
229 * offset, so it is not safe to use with readers/writers on the same file
232 pwrite(int fd, const void *buf, size_t count, off_t offset)
234 return do_pread_or_pwrite(fd, (void*)buf, count, offset, true);
237 /* Dumb Windows implementation of writev(). It writes the vectors one at a
239 ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
241 ssize_t total_bytes_written = 0;
247 for (int i = 0; i < iovcnt; i++) {
248 ssize_t bytes_written;
250 bytes_written = write(fd, iov[i].iov_base, iov[i].iov_len);
251 if (bytes_written >= 0)
252 total_bytes_written += bytes_written;
253 if (bytes_written != iov[i].iov_len) {
254 if (total_bytes_written == 0)
255 total_bytes_written = -1;
259 return total_bytes_written;
263 win32_get_file_and_vol_ids(const wchar_t *path, u64 *ino_ret, u64 *dev_ret)
266 BY_HANDLE_FILE_INFORMATION file_info;
270 h = win32_open_existing_file(path, FILE_READ_ATTRIBUTES);
271 if (h == INVALID_HANDLE_VALUE) {
272 ret = WIMLIB_ERR_OPEN;
276 if (!GetFileInformationByHandle(h, &file_info)) {
277 ret = WIMLIB_ERR_STAT;
279 *ino_ret = ((u64)file_info.nFileIndexHigh << 32) |
280 (u64)file_info.nFileIndexLow;
281 *dev_ret = file_info.dwVolumeSerialNumber;
284 err = GetLastError();
288 set_errno_from_GetLastError();
292 /* Replacement for glob() in Windows native builds that operates on wide
295 win32_wglob(const wchar_t *pattern, int flags,
296 int (*errfunc)(const wchar_t *epath, int eerrno),
299 WIN32_FIND_DATAW dat;
305 const wchar_t *backslash, *end_slash;
308 backslash = wcsrchr(pattern, L'\\');
309 end_slash = wcsrchr(pattern, L'/');
311 if (backslash > end_slash)
312 end_slash = backslash;
315 prefix_len = end_slash - pattern + 1;
319 /* This function does not support all functionality of the POSIX glob(),
320 * so make sure the parameters are consistent with supported
322 wimlib_assert(errfunc == NULL);
323 wimlib_assert((flags & GLOB_ERR) == GLOB_ERR);
324 wimlib_assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
326 hFind = FindFirstFileW(pattern, &dat);
327 if (hFind == INVALID_HANDLE_VALUE) {
328 err = GetLastError();
329 if (err == ERROR_FILE_NOT_FOUND) {
333 /* The other possible error codes for FindFirstFile()
334 * are undocumented. */
340 pglob->gl_pathv = NULL;
344 if (pglob->gl_pathc == nspaces) {
348 new_nspaces = nspaces * 2 + 1;
349 pathv = REALLOC(pglob->gl_pathv,
350 new_nspaces * sizeof(pglob->gl_pathv[0]));
353 pglob->gl_pathv = pathv;
354 nspaces = new_nspaces;
356 size_t filename_len = wcslen(dat.cFileName);
357 size_t len_needed = prefix_len + filename_len;
359 path = MALLOC((len_needed + 1) * sizeof(wchar_t));
363 wmemcpy(path, pattern, prefix_len);
364 wmemcpy(path + prefix_len, dat.cFileName, filename_len + 1);
365 pglob->gl_pathv[pglob->gl_pathc++] = path;
366 } while (FindNextFileW(hFind, &dat));
367 err = GetLastError();
369 if (err == ERROR_NO_MORE_FILES) {
373 /* Other possible error codes for FindNextFile() are
389 globfree(glob_t *pglob)
392 for (i = 0; i < pglob->gl_pathc; i++)
393 free(pglob->gl_pathv[i]);
394 free(pglob->gl_pathv);
397 #endif /* __WIN32__ */