2 * win32_replacements.c - Replacements for various functions not available on
3 * Windows, such as fsync().
7 * Copyright (C) 2013-2016 Eric Biggers
9 * This file is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU Lesser General Public License as published by the Free
11 * Software Foundation; either version 3 of the License, or (at your option) any
14 * This file is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this file; if not, see http://www.gnu.org/licenses/.
31 #include <io.h> /* for _get_osfhandle() */
34 #include "wimlib/win32_common.h"
36 #include "wimlib/assert.h"
37 #include "wimlib/glob.h"
38 #include "wimlib/error.h"
39 #include "wimlib/timestamp.h"
40 #include "wimlib/util.h"
43 win32_error_to_errno(DWORD err_code)
45 /* This mapping is that used in Cygwin.
46 * Some of these choices are arbitrary. */
48 case ERROR_ACCESS_DENIED:
50 case ERROR_ACTIVE_CONNECTIONS:
52 case ERROR_ALREADY_EXISTS:
54 case ERROR_BAD_DEVICE:
56 case ERROR_BAD_EXE_FORMAT:
58 case ERROR_BAD_NETPATH:
60 case ERROR_BAD_NET_NAME:
62 case ERROR_BAD_NET_RESP:
64 case ERROR_BAD_PATHNAME:
70 case ERROR_BAD_USERNAME:
72 case ERROR_BEGINNING_OF_MEDIA:
74 case ERROR_BROKEN_PIPE:
80 case ERROR_CALL_NOT_IMPLEMENTED:
82 case ERROR_CANNOT_MAKE:
84 case ERROR_CHILD_NOT_COMPLETE:
86 case ERROR_COMMITMENT_LIMIT:
90 case ERROR_DEVICE_DOOR_OPEN:
92 case ERROR_DEVICE_IN_USE:
94 case ERROR_DEVICE_REQUIRES_CLEANING:
98 case ERROR_DIR_NOT_EMPTY:
100 case ERROR_DISK_CORRUPT:
102 case ERROR_DISK_FULL:
108 case ERROR_EAS_DIDNT_FIT:
111 case ERROR_EAS_NOT_SUPPORTED:
114 case ERROR_EA_LIST_INCONSISTENT:
116 case ERROR_EA_TABLE_FULL:
118 case ERROR_END_OF_MEDIA:
120 case ERROR_EOM_OVERFLOW:
122 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
124 case ERROR_EXE_MARKED_INVALID:
126 case ERROR_FILEMARK_DETECTED:
128 case ERROR_FILENAME_EXCED_RANGE:
130 case ERROR_FILE_CORRUPT:
132 case ERROR_FILE_EXISTS:
134 case ERROR_FILE_INVALID:
136 case ERROR_FILE_NOT_FOUND:
138 case ERROR_HANDLE_DISK_FULL:
141 case ERROR_HANDLE_EOF:
144 case ERROR_INVALID_ADDRESS:
146 case ERROR_INVALID_AT_INTERRUPT_TIME:
148 case ERROR_INVALID_BLOCK_LENGTH:
150 case ERROR_INVALID_DATA:
152 case ERROR_INVALID_DRIVE:
154 case ERROR_INVALID_EA_NAME:
156 case ERROR_INVALID_EXE_SIGNATURE:
159 case ERROR_INVALID_FUNCTION:
162 case ERROR_INVALID_HANDLE:
164 case ERROR_INVALID_NAME:
166 case ERROR_INVALID_PARAMETER:
168 case ERROR_INVALID_SIGNAL_NUMBER:
170 case ERROR_IOPL_NOT_ENABLED:
172 case ERROR_IO_DEVICE:
174 case ERROR_IO_INCOMPLETE:
176 case ERROR_IO_PENDING:
178 case ERROR_LOCK_VIOLATION:
180 case ERROR_MAX_THRDS_REACHED:
182 case ERROR_META_EXPANSION_TOO_LONG:
184 case ERROR_MOD_NOT_FOUND:
187 case ERROR_MORE_DATA:
190 case ERROR_NEGATIVE_SEEK:
192 case ERROR_NETNAME_DELETED:
196 case ERROR_NONE_MAPPED:
198 case ERROR_NONPAGED_SYSTEM_RESOURCES:
201 case ERROR_NOT_CONNECTED:
204 case ERROR_NOT_ENOUGH_MEMORY:
206 case ERROR_NOT_OWNER:
209 case ERROR_NOT_READY:
212 case ERROR_NOT_SAME_DEVICE:
214 case ERROR_NOT_SUPPORTED:
218 case ERROR_NO_DATA_DETECTED:
221 case ERROR_NO_MEDIA_IN_DRIVE:
225 case ERROR_NO_MORE_FILES:
229 case ERROR_NO_MORE_ITEMS:
232 case ERROR_NO_MORE_SEARCH_HANDLES:
234 case ERROR_NO_PROC_SLOTS:
236 case ERROR_NO_SIGNAL_SENT:
238 case ERROR_NO_SYSTEM_RESOURCES:
242 case ERROR_OPEN_FAILED:
244 case ERROR_OPEN_FILES:
246 case ERROR_OUTOFMEMORY:
248 case ERROR_PAGED_SYSTEM_RESOURCES:
250 case ERROR_PAGEFILE_QUOTA:
252 case ERROR_PATH_NOT_FOUND:
254 case ERROR_PIPE_BUSY:
256 case ERROR_PIPE_CONNECTED:
259 case ERROR_PIPE_LISTENING:
261 case ERROR_PIPE_NOT_CONNECTED:
264 case ERROR_POSSIBLE_DEADLOCK:
266 case ERROR_PRIVILEGE_NOT_HELD:
268 case ERROR_PROCESS_ABORTED:
270 case ERROR_PROC_NOT_FOUND:
273 case ERROR_REM_NOT_LIST:
276 case ERROR_SECTOR_NOT_FOUND:
280 case ERROR_SETMARK_DETECTED:
282 case ERROR_SHARING_BUFFER_EXCEEDED:
284 case ERROR_SHARING_VIOLATION:
286 case ERROR_SIGNAL_PENDING:
288 case ERROR_SIGNAL_REFUSED:
291 case ERROR_SXS_CANT_GEN_ACTCTX:
294 case ERROR_THREAD_1_INACTIVE:
296 case ERROR_TOO_MANY_LINKS:
298 case ERROR_TOO_MANY_OPEN_FILES:
300 case ERROR_WAIT_NO_CHILDREN:
302 case ERROR_WORKING_SET_QUOTA:
304 case ERROR_WRITE_PROTECT:
312 set_errno_from_win32_error(DWORD err)
314 errno = win32_error_to_errno(err);
318 set_errno_from_GetLastError(void)
320 set_errno_from_win32_error(GetLastError());
323 /* Replacement for POSIX fsync() */
329 h = (HANDLE)_get_osfhandle(fd);
330 if (h == INVALID_HANDLE_VALUE)
332 if (!FlushFileBuffers(h))
336 set_errno_from_GetLastError();
341 /* Use the Win32 API to get the number of processors. */
343 get_available_cpus(void)
346 GetSystemInfo(&sysinfo);
347 return sysinfo.dwNumberOfProcessors;
350 /* Use the Win32 API to get the amount of available memory. */
352 get_available_memory(void)
354 MEMORYSTATUSEX status = {
355 .dwLength = sizeof(status),
357 GlobalMemoryStatusEx(&status);
358 return (u64)min(status.ullTotalPhys, status.ullTotalVirtual) * 85 / 100;
361 /* Replacement for POSIX-2008 realpath(). Warning: partial functionality only
362 * (resolved_path must be NULL). Also I highly doubt that GetFullPathName
363 * really does the right thing under all circumstances. */
365 realpath(const wchar_t *path, wchar_t *resolved_path)
369 wimlib_assert(resolved_path == NULL);
371 ret = GetFullPathNameW(path, 0, NULL, NULL);
373 err = GetLastError();
377 resolved_path = MALLOC(ret * sizeof(wchar_t));
380 ret = GetFullPathNameW(path, ret, resolved_path, NULL);
382 err = GetLastError();
384 resolved_path = NULL;
389 set_errno_from_win32_error(err);
391 return resolved_path;
394 /* A quick hack to get reasonable rename() semantics on Windows, in particular
395 * deleting the destination file instead of failing with ERROR_FILE_EXISTS and
396 * working around any processes that may have the destination file open.
398 * Note: This is intended to be called when overwriting a regular file with an
399 * updated copy and is *not* a fully POSIX compliant rename(). For that you may
400 * wish to take a look at Cygwin's implementation, but be prepared...
402 * Return 0 on success, -1 on regular error, or 1 if the destination file was
403 * deleted but the source could not be renamed and therefore should not be
407 win32_rename_replacement(const wchar_t *srcpath, const wchar_t *dstpath)
411 /* Normally, MoveFileExW() with the MOVEFILE_REPLACE_EXISTING flag does
414 if (MoveFileExW(srcpath, dstpath, MOVEFILE_REPLACE_EXISTING))
417 /* MoveFileExW() failed. One way this can happen is if any process has
418 * the destination file open, in which case ERROR_ACCESS_DENIED is
419 * produced. This can commonly happen if there is a backup or antivirus
420 * program monitoring or scanning the files. This behavior is very
421 * different from the behavior of POSIX rename(), which simply unlinks
422 * the destination file and allows other processes to keep it open! */
424 if (GetLastError() != ERROR_ACCESS_DENIED)
427 /* We can work around the above-mentioned problem by renaming the
428 * destination file to yet another temporary file, then "deleting" it,
429 * which on Windows will in fact not actually delete it immediately but
430 * rather mark it for deletion when the last handle to it is closed. */
432 static const wchar_t orig_suffix[5] = L".orig";
433 const size_t num_rand_chars = 9;
436 size_t dstlen = wcslen(dstpath);
438 tmpname = alloca(sizeof(wchar_t) *
439 (dstlen + ARRAY_LEN(orig_suffix) + num_rand_chars + 1));
441 p = wmempcpy(p, dstpath, dstlen);
442 p = wmempcpy(p, orig_suffix, ARRAY_LEN(orig_suffix));
443 get_random_alnum_chars(p, num_rand_chars);
448 if (!MoveFile(dstpath, tmpname))
451 if (!DeleteFile(tmpname)) {
452 set_errno_from_GetLastError();
453 WARNING_WITH_ERRNO("Failed to delete original file "
454 "(moved to \"%ls\")", tmpname);
457 if (!MoveFile(srcpath, dstpath)) {
458 set_errno_from_GetLastError();
459 WARNING_WITH_ERRNO("Atomic semantics not respected in "
460 "failed rename() (new file is at \"%ls\")",
468 set_errno_from_GetLastError();
472 /* This really could be replaced with _wcserror_s, but this doesn't seem to
473 * actually be available in MSVCRT.DLL on Windows XP (perhaps it's statically
474 * linked in by Visual Studio...?). */
476 win32_strerror_r_replacement(int errnum, wchar_t *buf, size_t buflen)
478 static pthread_mutex_t strerror_lock = PTHREAD_MUTEX_INITIALIZER;
480 pthread_mutex_lock(&strerror_lock);
481 mbstowcs(buf, strerror(errnum), buflen);
482 buf[buflen - 1] = '\0';
483 pthread_mutex_unlock(&strerror_lock);
487 #define MAX_IO_AMOUNT 1048576
490 do_pread_or_pwrite(int fd, void *buf, size_t count, off_t offset,
494 LARGE_INTEGER orig_offset;
495 DWORD result = 0xFFFFFFFF;
496 LARGE_INTEGER relative_offset;
497 OVERLAPPED overlapped;
501 h = (HANDLE)_get_osfhandle(fd);
502 if (h == INVALID_HANDLE_VALUE)
505 if (GetFileType(h) == FILE_TYPE_PIPE) {
510 /* Get original position */
511 relative_offset.QuadPart = 0;
512 if (!SetFilePointerEx(h, relative_offset, &orig_offset, FILE_CURRENT)) {
513 err = GetLastError();
514 win32_error(err, L"Failed to get original file position");
518 memset(&overlapped, 0, sizeof(overlapped));
519 overlapped.Offset = offset;
520 overlapped.OffsetHigh = offset >> 32;
522 /* Do the read or write at the specified offset */
523 count = min(count, MAX_IO_AMOUNT);
526 bret = WriteFile(h, buf, count, &result, &overlapped);
528 bret = ReadFile(h, buf, count, &result, &overlapped);
530 err = GetLastError();
531 win32_error(err, L"Failed to %s %zu bytes at offset %"PRIu64,
532 (is_pwrite ? "write" : "read"), count, offset);
536 wimlib_assert(result <= count);
538 /* Restore the original position */
539 if (!SetFilePointerEx(h, orig_offset, NULL, FILE_BEGIN)) {
540 err = GetLastError();
541 win32_error(err, L"Failed to restore file position to %"PRIu64,
550 set_errno_from_win32_error(err);
554 /* Dumb Windows implementation of pread(). It temporarily changes the file
555 * offset, so it is not safe to use with readers/writers on the same file
558 win32_pread(int fd, void *buf, size_t count, off_t offset)
560 return do_pread_or_pwrite(fd, buf, count, offset, false);
563 /* Dumb Windows implementation of pwrite(). It temporarily changes the file
564 * offset, so it is not safe to use with readers/writers on the same file
567 win32_pwrite(int fd, const void *buf, size_t count, off_t offset)
569 return do_pread_or_pwrite(fd, (void*)buf, count, offset, true);
572 /* Replacement for read() which doesn't hide the Win32 error code */
574 win32_read(int fd, void *buf, size_t count)
576 HANDLE h = (HANDLE)_get_osfhandle(fd);
577 DWORD result = 0xFFFFFFFF;
579 if (h == INVALID_HANDLE_VALUE)
582 count = min(count, MAX_IO_AMOUNT);
584 if (!ReadFile(h, buf, count, &result, NULL)) {
585 DWORD err = GetLastError();
587 L"Error reading %zu bytes from fd %d", count, fd);
588 set_errno_from_win32_error(err);
592 wimlib_assert(result <= count);
596 /* Replacement for write() which doesn't hide the Win32 error code */
598 win32_write(int fd, const void *buf, size_t count)
600 HANDLE h = (HANDLE)_get_osfhandle(fd);
601 DWORD result = 0xFFFFFFFF;
603 if (h == INVALID_HANDLE_VALUE)
606 count = min(count, MAX_IO_AMOUNT);
608 if (!WriteFile(h, buf, count, &result, NULL)) {
609 DWORD err = GetLastError();
611 L"Error writing %zu bytes to fd %d", count, fd);
612 set_errno_from_win32_error(err);
616 wimlib_assert(result <= count);
620 /* Replacement for glob() in Windows native builds that operates on wide
623 win32_wglob(const wchar_t *pattern, int flags,
624 int (*errfunc)(const wchar_t *epath, int eerrno),
627 WIN32_FIND_DATAW dat;
634 const wchar_t *backslash, *end_slash;
637 backslash = wcsrchr(pattern, L'\\');
638 end_slash = wcsrchr(pattern, L'/');
640 if (backslash > end_slash)
641 end_slash = backslash;
644 prefix_len = end_slash - pattern + 1;
648 /* This function does not support all functionality of the POSIX glob(),
649 * so make sure the parameters are consistent with supported
651 wimlib_assert(errfunc == NULL);
652 wimlib_assert((flags & GLOB_ERR) == GLOB_ERR);
653 wimlib_assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
655 hFind = FindFirstFileW(pattern, &dat);
656 if (hFind == INVALID_HANDLE_VALUE) {
657 err = GetLastError();
658 if (err == ERROR_FILE_NOT_FOUND) {
662 set_errno_from_win32_error(err);
667 pglob->gl_pathv = NULL;
671 if (pglob->gl_pathc == nspaces) {
675 new_nspaces = nspaces * 2 + 1;
676 pathv = REALLOC(pglob->gl_pathv,
677 new_nspaces * sizeof(pglob->gl_pathv[0]));
680 pglob->gl_pathv = pathv;
681 nspaces = new_nspaces;
683 size_t filename_len = wcslen(dat.cFileName);
684 size_t len_needed = prefix_len + filename_len;
686 path = MALLOC((len_needed + 1) * sizeof(wchar_t));
690 wmemcpy(path, pattern, prefix_len);
691 wmemcpy(path + prefix_len, dat.cFileName, filename_len + 1);
692 pglob->gl_pathv[pglob->gl_pathc++] = path;
693 } while (FindNextFileW(hFind, &dat));
694 err = GetLastError();
696 if (err != ERROR_NO_MORE_FILES) {
697 set_errno_from_win32_error(err);
715 globfree(glob_t *pglob)
718 for (i = 0; i < pglob->gl_pathc; i++)
719 FREE(pglob->gl_pathv[i]);
720 FREE(pglob->gl_pathv);
723 /* Replacement for fopen(path, "a") that doesn't prevent other processes from
724 * reading the file */
726 win32_open_logfile(const wchar_t *path)
732 h = CreateFile(path, FILE_APPEND_DATA, FILE_SHARE_VALID_FLAGS,
733 NULL, OPEN_ALWAYS, 0, NULL);
734 if (h == INVALID_HANDLE_VALUE)
737 fd = _open_osfhandle((intptr_t)h, O_APPEND);
743 fp = fdopen(fd, "a");
752 #define RtlGenRandom SystemFunction036
753 BOOLEAN WINAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
756 * Generate @n cryptographically secure random bytes (thread-safe)
758 * This is the Windows version. It uses RtlGenRandom() (actually called
759 * SystemFunction036) from advapi32.dll.
762 get_random_bytes(void *p, size_t n)
765 u32 count = min(n, UINT32_MAX);
767 if (!RtlGenRandom(p, count)) {
768 win32_error(GetLastError(),
769 L"RtlGenRandom() failed (count=%u)", count);
778 /* Retrieve the current time as a WIM timestamp. */
780 now_as_wim_timestamp(void)
784 GetSystemTimeAsFileTime(&ft);
786 return ((u64)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
789 #endif /* __WIN32__ */