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 https://www.gnu.org/licenses/.
30 #include <io.h> /* for _get_osfhandle() */
33 #include "wimlib/win32_common.h"
35 #include "wimlib/assert.h"
36 #include "wimlib/glob.h"
37 #include "wimlib/error.h"
38 #include "wimlib/timestamp.h"
39 #include "wimlib/util.h"
42 win32_error_to_errno(DWORD err_code)
44 /* This mapping is that used in Cygwin.
45 * Some of these choices are arbitrary. */
47 case ERROR_ACCESS_DENIED:
49 case ERROR_ACTIVE_CONNECTIONS:
51 case ERROR_ALREADY_EXISTS:
53 case ERROR_BAD_DEVICE:
55 case ERROR_BAD_EXE_FORMAT:
57 case ERROR_BAD_NETPATH:
59 case ERROR_BAD_NET_NAME:
61 case ERROR_BAD_NET_RESP:
63 case ERROR_BAD_PATHNAME:
69 case ERROR_BAD_USERNAME:
71 case ERROR_BEGINNING_OF_MEDIA:
73 case ERROR_BROKEN_PIPE:
79 case ERROR_CALL_NOT_IMPLEMENTED:
81 case ERROR_CANNOT_MAKE:
83 case ERROR_CHILD_NOT_COMPLETE:
85 case ERROR_COMMITMENT_LIMIT:
89 case ERROR_DEVICE_DOOR_OPEN:
91 case ERROR_DEVICE_IN_USE:
93 case ERROR_DEVICE_REQUIRES_CLEANING:
97 case ERROR_DIR_NOT_EMPTY:
99 case ERROR_DISK_CORRUPT:
101 case ERROR_DISK_FULL:
107 case ERROR_EAS_DIDNT_FIT:
110 case ERROR_EAS_NOT_SUPPORTED:
113 case ERROR_EA_LIST_INCONSISTENT:
115 case ERROR_EA_TABLE_FULL:
117 case ERROR_END_OF_MEDIA:
119 case ERROR_EOM_OVERFLOW:
121 case ERROR_EXE_MACHINE_TYPE_MISMATCH:
123 case ERROR_EXE_MARKED_INVALID:
125 case ERROR_FILEMARK_DETECTED:
127 case ERROR_FILENAME_EXCED_RANGE:
129 case ERROR_FILE_CORRUPT:
131 case ERROR_FILE_EXISTS:
133 case ERROR_FILE_INVALID:
135 case ERROR_FILE_NOT_FOUND:
137 case ERROR_HANDLE_DISK_FULL:
140 case ERROR_HANDLE_EOF:
143 case ERROR_INVALID_ADDRESS:
145 case ERROR_INVALID_AT_INTERRUPT_TIME:
147 case ERROR_INVALID_BLOCK_LENGTH:
149 case ERROR_INVALID_DATA:
151 case ERROR_INVALID_DRIVE:
153 case ERROR_INVALID_EA_NAME:
155 case ERROR_INVALID_EXE_SIGNATURE:
158 case ERROR_INVALID_FUNCTION:
161 case ERROR_INVALID_HANDLE:
163 case ERROR_INVALID_NAME:
165 case ERROR_INVALID_PARAMETER:
167 case ERROR_INVALID_SIGNAL_NUMBER:
169 case ERROR_IOPL_NOT_ENABLED:
171 case ERROR_IO_DEVICE:
173 case ERROR_IO_INCOMPLETE:
175 case ERROR_IO_PENDING:
177 case ERROR_LOCK_VIOLATION:
179 case ERROR_MAX_THRDS_REACHED:
181 case ERROR_META_EXPANSION_TOO_LONG:
183 case ERROR_MOD_NOT_FOUND:
186 case ERROR_MORE_DATA:
189 case ERROR_NEGATIVE_SEEK:
191 case ERROR_NETNAME_DELETED:
195 case ERROR_NONE_MAPPED:
197 case ERROR_NONPAGED_SYSTEM_RESOURCES:
200 case ERROR_NOT_CONNECTED:
203 case ERROR_NOT_ENOUGH_MEMORY:
205 case ERROR_NOT_OWNER:
208 case ERROR_NOT_READY:
211 case ERROR_NOT_SAME_DEVICE:
213 case ERROR_NOT_SUPPORTED:
217 case ERROR_NO_DATA_DETECTED:
220 case ERROR_NO_MEDIA_IN_DRIVE:
224 case ERROR_NO_MORE_FILES:
228 case ERROR_NO_MORE_ITEMS:
231 case ERROR_NO_MORE_SEARCH_HANDLES:
233 case ERROR_NO_PROC_SLOTS:
235 case ERROR_NO_SIGNAL_SENT:
237 case ERROR_NO_SYSTEM_RESOURCES:
241 case ERROR_OPEN_FAILED:
243 case ERROR_OPEN_FILES:
245 case ERROR_OUTOFMEMORY:
247 case ERROR_PAGED_SYSTEM_RESOURCES:
249 case ERROR_PAGEFILE_QUOTA:
251 case ERROR_PATH_NOT_FOUND:
253 case ERROR_PIPE_BUSY:
255 case ERROR_PIPE_CONNECTED:
258 case ERROR_PIPE_LISTENING:
260 case ERROR_PIPE_NOT_CONNECTED:
263 case ERROR_POSSIBLE_DEADLOCK:
265 case ERROR_PRIVILEGE_NOT_HELD:
267 case ERROR_PROCESS_ABORTED:
269 case ERROR_PROC_NOT_FOUND:
272 case ERROR_REM_NOT_LIST:
275 case ERROR_SECTOR_NOT_FOUND:
279 case ERROR_SETMARK_DETECTED:
281 case ERROR_SHARING_BUFFER_EXCEEDED:
283 case ERROR_SHARING_VIOLATION:
285 case ERROR_SIGNAL_PENDING:
287 case ERROR_SIGNAL_REFUSED:
290 case ERROR_SXS_CANT_GEN_ACTCTX:
293 case ERROR_THREAD_1_INACTIVE:
295 case ERROR_TOO_MANY_LINKS:
297 case ERROR_TOO_MANY_OPEN_FILES:
299 case ERROR_WAIT_NO_CHILDREN:
301 case ERROR_WORKING_SET_QUOTA:
303 case ERROR_WRITE_PROTECT:
311 set_errno_from_win32_error(DWORD err)
313 errno = win32_error_to_errno(err);
317 set_errno_from_GetLastError(void)
319 set_errno_from_win32_error(GetLastError());
322 /* Replacement for POSIX fsync() */
328 h = (HANDLE)_get_osfhandle(fd);
329 if (h == INVALID_HANDLE_VALUE)
331 if (!FlushFileBuffers(h))
335 set_errno_from_GetLastError();
340 /* Use the Win32 API to get the number of processors. */
342 get_available_cpus(void)
345 GetSystemInfo(&sysinfo);
346 return sysinfo.dwNumberOfProcessors;
349 /* Use the Win32 API to get the amount of available memory. */
351 get_available_memory(void)
353 MEMORYSTATUSEX status = {
354 .dwLength = sizeof(status),
356 GlobalMemoryStatusEx(&status);
357 return (u64)min(status.ullTotalPhys, status.ullTotalVirtual) * 85 / 100;
360 /* Replacement for POSIX-2008 realpath(). Warning: partial functionality only
361 * (resolved_path must be NULL). Also I highly doubt that GetFullPathName
362 * really does the right thing under all circumstances. */
364 realpath(const wchar_t *path, wchar_t *resolved_path)
368 wimlib_assert(resolved_path == NULL);
370 ret = GetFullPathNameW(path, 0, NULL, NULL);
372 err = GetLastError();
376 resolved_path = MALLOC(ret * sizeof(wchar_t));
379 ret = GetFullPathNameW(path, ret, resolved_path, NULL);
381 err = GetLastError();
383 resolved_path = NULL;
388 set_errno_from_win32_error(err);
390 return resolved_path;
393 /* A quick hack to get reasonable rename() semantics on Windows, in particular
394 * deleting the destination file instead of failing with ERROR_FILE_EXISTS and
395 * working around any processes that may have the destination file open.
397 * Note: This is intended to be called when overwriting a regular file with an
398 * updated copy and is *not* a fully POSIX compliant rename(). For that you may
399 * wish to take a look at Cygwin's implementation, but be prepared...
401 * Return 0 on success, -1 on regular error, or 1 if the destination file was
402 * deleted but the source could not be renamed and therefore should not be
406 win32_rename_replacement(const wchar_t *srcpath, const wchar_t *dstpath)
410 /* Normally, MoveFileExW() with the MOVEFILE_REPLACE_EXISTING flag does
413 if (MoveFileExW(srcpath, dstpath, MOVEFILE_REPLACE_EXISTING))
416 /* MoveFileExW() failed. One way this can happen is if any process has
417 * the destination file open, in which case ERROR_ACCESS_DENIED is
418 * produced. This can commonly happen if there is a backup or antivirus
419 * program monitoring or scanning the files. This behavior is very
420 * different from the behavior of POSIX rename(), which simply unlinks
421 * the destination file and allows other processes to keep it open! */
423 if (GetLastError() != ERROR_ACCESS_DENIED)
426 /* We can work around the above-mentioned problem by renaming the
427 * destination file to yet another temporary file, then "deleting" it,
428 * which on Windows will in fact not actually delete it immediately but
429 * rather mark it for deletion when the last handle to it is closed. */
431 static const wchar_t orig_suffix[5] = L".orig";
432 const size_t num_rand_chars = 9;
435 size_t dstlen = wcslen(dstpath);
437 tmpname = alloca(sizeof(wchar_t) *
438 (dstlen + ARRAY_LEN(orig_suffix) + num_rand_chars + 1));
440 p = wmempcpy(p, dstpath, dstlen);
441 p = wmempcpy(p, orig_suffix, ARRAY_LEN(orig_suffix));
442 get_random_alnum_chars(p, num_rand_chars);
447 if (!MoveFile(dstpath, tmpname))
450 if (!DeleteFile(tmpname)) {
451 set_errno_from_GetLastError();
452 WARNING_WITH_ERRNO("Failed to delete original file "
453 "(moved to \"%ls\")", tmpname);
456 if (!MoveFile(srcpath, dstpath)) {
457 set_errno_from_GetLastError();
458 WARNING_WITH_ERRNO("Atomic semantics not respected in "
459 "failed rename() (new file is at \"%ls\")",
467 set_errno_from_GetLastError();
471 #define MAX_IO_AMOUNT 1048576
474 do_pread_or_pwrite(int fd, void *buf, size_t count, off_t offset,
478 LARGE_INTEGER orig_offset;
479 DWORD result = 0xFFFFFFFF;
480 LARGE_INTEGER relative_offset;
481 OVERLAPPED overlapped;
485 h = (HANDLE)_get_osfhandle(fd);
486 if (h == INVALID_HANDLE_VALUE)
489 if (GetFileType(h) == FILE_TYPE_PIPE) {
494 /* Get original position */
495 relative_offset.QuadPart = 0;
496 if (!SetFilePointerEx(h, relative_offset, &orig_offset, FILE_CURRENT)) {
497 err = GetLastError();
498 win32_error(err, L"Failed to get original file position");
502 memset(&overlapped, 0, sizeof(overlapped));
503 overlapped.Offset = offset;
504 overlapped.OffsetHigh = offset >> 32;
506 /* Do the read or write at the specified offset */
507 count = min(count, MAX_IO_AMOUNT);
510 bret = WriteFile(h, buf, count, &result, &overlapped);
512 bret = ReadFile(h, buf, count, &result, &overlapped);
514 err = GetLastError();
515 win32_error(err, L"Failed to %s %zu bytes at offset %"PRIu64,
516 (is_pwrite ? "write" : "read"), count, offset);
520 wimlib_assert(result <= count);
522 /* Restore the original position */
523 if (!SetFilePointerEx(h, orig_offset, NULL, FILE_BEGIN)) {
524 err = GetLastError();
525 win32_error(err, L"Failed to restore file position to %"PRIu64,
534 set_errno_from_win32_error(err);
538 /* Dumb Windows implementation of pread(). It temporarily changes the file
539 * offset, so it is not safe to use with readers/writers on the same file
542 win32_pread(int fd, void *buf, size_t count, off_t offset)
544 return do_pread_or_pwrite(fd, buf, count, offset, false);
547 /* Dumb Windows implementation of pwrite(). It temporarily changes the file
548 * offset, so it is not safe to use with readers/writers on the same file
551 win32_pwrite(int fd, const void *buf, size_t count, off_t offset)
553 return do_pread_or_pwrite(fd, (void*)buf, count, offset, true);
556 /* Replacement for read() which doesn't hide the Win32 error code */
558 win32_read(int fd, void *buf, size_t count)
560 HANDLE h = (HANDLE)_get_osfhandle(fd);
561 DWORD result = 0xFFFFFFFF;
563 if (h == INVALID_HANDLE_VALUE)
566 count = min(count, MAX_IO_AMOUNT);
568 if (!ReadFile(h, buf, count, &result, NULL)) {
569 DWORD err = GetLastError();
571 L"Error reading %zu bytes from fd %d", count, fd);
572 set_errno_from_win32_error(err);
576 wimlib_assert(result <= count);
580 /* Replacement for write() which doesn't hide the Win32 error code */
582 win32_write(int fd, const void *buf, size_t count)
584 HANDLE h = (HANDLE)_get_osfhandle(fd);
585 DWORD result = 0xFFFFFFFF;
587 if (h == INVALID_HANDLE_VALUE)
590 count = min(count, MAX_IO_AMOUNT);
592 if (!WriteFile(h, buf, count, &result, NULL)) {
593 DWORD err = GetLastError();
595 L"Error writing %zu bytes to fd %d", count, fd);
596 set_errno_from_win32_error(err);
600 wimlib_assert(result <= count);
604 /* Replacement for glob() in Windows native builds that operates on wide
607 win32_wglob(const wchar_t *pattern, int flags,
608 int (*errfunc)(const wchar_t *epath, int eerrno),
611 WIN32_FIND_DATAW dat;
618 const wchar_t *backslash, *end_slash;
621 backslash = wcsrchr(pattern, L'\\');
622 end_slash = wcsrchr(pattern, L'/');
624 if (backslash > end_slash)
625 end_slash = backslash;
628 prefix_len = end_slash - pattern + 1;
632 /* This function does not support all functionality of the POSIX glob(),
633 * so make sure the parameters are consistent with supported
635 wimlib_assert(errfunc == NULL);
636 wimlib_assert((flags & GLOB_ERR) == GLOB_ERR);
637 wimlib_assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
639 hFind = FindFirstFileW(pattern, &dat);
640 if (hFind == INVALID_HANDLE_VALUE) {
641 err = GetLastError();
642 if (err == ERROR_FILE_NOT_FOUND) {
646 set_errno_from_win32_error(err);
651 pglob->gl_pathv = NULL;
655 if (pglob->gl_pathc == nspaces) {
659 new_nspaces = nspaces * 2 + 1;
660 pathv = REALLOC(pglob->gl_pathv,
661 new_nspaces * sizeof(pglob->gl_pathv[0]));
664 pglob->gl_pathv = pathv;
665 nspaces = new_nspaces;
667 size_t filename_len = wcslen(dat.cFileName);
668 size_t len_needed = prefix_len + filename_len;
670 path = MALLOC((len_needed + 1) * sizeof(wchar_t));
674 wmemcpy(path, pattern, prefix_len);
675 wmemcpy(path + prefix_len, dat.cFileName, filename_len + 1);
676 pglob->gl_pathv[pglob->gl_pathc++] = path;
677 } while (FindNextFileW(hFind, &dat));
678 err = GetLastError();
680 if (err != ERROR_NO_MORE_FILES) {
681 set_errno_from_win32_error(err);
699 globfree(glob_t *pglob)
702 for (i = 0; i < pglob->gl_pathc; i++)
703 FREE(pglob->gl_pathv[i]);
704 FREE(pglob->gl_pathv);
707 /* Replacement for fopen(path, "a") that doesn't prevent other processes from
708 * reading the file */
710 win32_open_logfile(const wchar_t *path)
716 h = CreateFile(path, FILE_APPEND_DATA, FILE_SHARE_VALID_FLAGS,
717 NULL, OPEN_ALWAYS, 0, NULL);
718 if (h == INVALID_HANDLE_VALUE)
721 fd = _open_osfhandle((intptr_t)h, O_APPEND);
727 fp = fdopen(fd, "a");
736 #define RtlGenRandom SystemFunction036
737 BOOLEAN WINAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
740 * Generate @n cryptographically secure random bytes (thread-safe)
742 * This is the Windows version. It uses RtlGenRandom() (actually called
743 * SystemFunction036) from advapi32.dll.
746 get_random_bytes(void *p, size_t n)
749 u32 count = min(n, UINT32_MAX);
751 if (!RtlGenRandom(p, count)) {
752 win32_error(GetLastError(),
753 L"RtlGenRandom() failed (count=%u)", count);
762 /* Retrieve the current time as a WIM timestamp. */
764 now_as_wim_timestamp(void)
768 GetSystemTimeAsFileTime(&ft);
770 return ((u64)ft.dwHighDateTime << 32) | ft.dwLowDateTime;