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/.
32 #include <shlwapi.h> /* for PathMatchSpecW() */
33 #include "wimlib/win32_common.h"
35 #include "wimlib/assert.h"
36 #include "wimlib/file_io.h"
37 #include "wimlib/error.h"
38 #include "wimlib/util.h"
40 /* Replacement for POSIX fsync() */
46 h = (HANDLE)_get_osfhandle(fd);
47 if (h == INVALID_HANDLE_VALUE)
49 if (!FlushFileBuffers(h))
53 set_errno_from_GetLastError();
58 /* Use the Win32 API to get the number of processors */
60 win32_get_number_of_processors(void)
63 GetSystemInfo(&sysinfo);
64 return sysinfo.dwNumberOfProcessors;
67 /* Replacement for POSIX-2008 realpath(). Warning: partial functionality only
68 * (resolved_path must be NULL). Also I highly doubt that GetFullPathName
69 * really does the right thing under all circumstances. */
71 realpath(const wchar_t *path, wchar_t *resolved_path)
75 wimlib_assert(resolved_path == NULL);
77 ret = GetFullPathNameW(path, 0, NULL, NULL);
83 resolved_path = MALLOC(ret * sizeof(wchar_t));
86 ret = GetFullPathNameW(path, ret, resolved_path, NULL);
95 set_errno_from_win32_error(err);
100 /* rename() on Windows fails if the destination file exists. And we need to
101 * make it work on wide characters. Fix it. */
103 win32_rename_replacement(const wchar_t *oldpath, const wchar_t *newpath)
105 if (MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) {
108 set_errno_from_GetLastError();
113 /* Replacement for POSIX fnmatch() (partial functionality only) */
115 fnmatch(const wchar_t *pattern, const wchar_t *string, int flags)
117 if (PathMatchSpecW(string, pattern))
123 /* truncate() replacement */
125 win32_truncate_replacement(const wchar_t *path, off_t size)
127 DWORD err = NO_ERROR;
128 LARGE_INTEGER liOffset;
130 HANDLE h = win32_open_existing_file(path, GENERIC_WRITE);
131 if (h == INVALID_HANDLE_VALUE)
134 liOffset.QuadPart = size;
135 if (!SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
136 goto fail_close_handle;
138 if (!SetEndOfFile(h))
139 goto fail_close_handle;
144 err = GetLastError();
148 err = GetLastError();
149 set_errno_from_win32_error(err);
154 /* This really could be replaced with _wcserror_s, but this doesn't seem to
155 * actually be available in MSVCRT.DLL on Windows XP (perhaps it's statically
156 * linked in by Visual Studio...?). */
158 win32_strerror_r_replacement(int errnum, wchar_t *buf, size_t buflen)
160 static pthread_mutex_t strerror_lock = PTHREAD_MUTEX_INITIALIZER;
162 pthread_mutex_lock(&strerror_lock);
163 mbstowcs(buf, strerror(errnum), buflen);
164 buf[buflen - 1] = '\0';
165 pthread_mutex_unlock(&strerror_lock);
170 do_pread_or_pwrite(int fd, void *buf, size_t count, off_t offset,
174 LARGE_INTEGER orig_offset;
175 DWORD bytes_read_or_written;
176 LARGE_INTEGER relative_offset;
177 OVERLAPPED overlapped;
180 h = (HANDLE)_get_osfhandle(fd);
181 if (h == INVALID_HANDLE_VALUE)
184 if (GetFileType(h) == FILE_TYPE_PIPE) {
189 /* Get original position */
190 relative_offset.QuadPart = 0;
191 if (!SetFilePointerEx(h, relative_offset, &orig_offset, FILE_CURRENT))
194 memset(&overlapped, 0, sizeof(overlapped));
195 overlapped.Offset = offset;
196 overlapped.OffsetHigh = offset >> 32;
198 /* Do the read or write at the specified offset */
200 bret = WriteFile(h, buf, count, &bytes_read_or_written, &overlapped);
202 bret = ReadFile(h, buf, count, &bytes_read_or_written, &overlapped);
206 /* Restore the original position */
207 if (!SetFilePointerEx(h, orig_offset, NULL, FILE_BEGIN))
210 return bytes_read_or_written;
212 set_errno_from_GetLastError();
217 /* Dumb Windows implementation of pread(). It temporarily changes the file
218 * offset, so it is not safe to use with readers/writers on the same file
221 pread(int fd, void *buf, size_t count, off_t offset)
223 return do_pread_or_pwrite(fd, buf, count, offset, false);
226 /* Dumb Windows implementation of pwrite(). It temporarily changes the file
227 * offset, so it is not safe to use with readers/writers on the same file
230 pwrite(int fd, const void *buf, size_t count, off_t offset)
232 return do_pread_or_pwrite(fd, (void*)buf, count, offset, true);
235 /* Dumb Windows implementation of writev(). It writes the vectors one at a
237 ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
239 ssize_t total_bytes_written = 0;
245 for (int i = 0; i < iovcnt; i++) {
246 ssize_t bytes_written;
248 bytes_written = write(fd, iov[i].iov_base, iov[i].iov_len);
249 if (bytes_written >= 0)
250 total_bytes_written += bytes_written;
251 if (bytes_written != iov[i].iov_len) {
252 if (total_bytes_written == 0)
253 total_bytes_written = -1;
257 return total_bytes_written;
261 win32_get_file_and_vol_ids(const wchar_t *path, u64 *ino_ret, u64 *dev_ret)
264 BY_HANDLE_FILE_INFORMATION file_info;
268 h = win32_open_existing_file(path, FILE_READ_ATTRIBUTES);
269 if (h == INVALID_HANDLE_VALUE) {
270 ret = WIMLIB_ERR_OPEN;
274 if (!GetFileInformationByHandle(h, &file_info)) {
275 ret = WIMLIB_ERR_STAT;
277 *ino_ret = ((u64)file_info.nFileIndexHigh << 32) |
278 (u64)file_info.nFileIndexLow;
279 *dev_ret = file_info.dwVolumeSerialNumber;
282 err = GetLastError();
286 set_errno_from_GetLastError();
291 #endif /* __WIN32__ */