From f1cc5973173a049e3e62a6bc59a1b90682736afd Mon Sep 17 00:00:00 2001 From: vylsaz Date: Sat, 27 Jan 2024 06:59:56 +0000 Subject: [PATCH 1/3] Util to report Windows error --- src/windows/winError.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/windows/winError.c diff --git a/src/windows/winError.c b/src/windows/winError.c new file mode 100644 index 00000000..67d2ca41 --- /dev/null +++ b/src/windows/winError.c @@ -0,0 +1,16 @@ +#include + +static char* winErrorEx(DWORD dwError) { + char* buffer = NULL; + DWORD dwFlags = FORMAT_MESSAGE_MAX_WIDTH_MASK + | FORMAT_MESSAGE_ALLOCATE_BUFFER + | FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS; + DWORD dwResult = FormatMessageA(dwFlags, NULL, dwError, 0, (LPSTR)&buffer, 0, NULL); + if (dwResult==0 || buffer==NULL) { + fatal("Failed to get error message from FormatMessageA()"); + } + return buffer; +} + +static char* winError(void) { return winErrorEx(GetLastError()); } From d570f8d30317d657db8341b0258926b911282aae Mon Sep 17 00:00:00 2001 From: vylsaz Date: Sat, 27 Jan 2024 07:12:09 +0000 Subject: [PATCH 2/3] =?UTF-8?q?Implement=20=E2=80=A2file.MapBytes=20for=20?= =?UTF-8?q?Windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/file.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/utils/file.c b/src/utils/file.c index c2182ad2..a67f1336 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -259,24 +259,40 @@ B path_list(B path) { return res; } -#if __has_include() && __has_include() && __has_include() && !WASM && !NO_MMAP +#if defined(_WIN32) || (__has_include() && __has_include() && __has_include() && !WASM && !NO_MMAP) +#if !defined(_WIN32) #include #include #include +#else +#include +#include "../windows/winError.c" +#endif typedef struct MmapHolder { struct Arr; +#if !defined(_WIN32) int fd; u64 size; +#else + HANDLE hFile; + HANDLE hMapFile; +#endif u8* a; } MmapHolder; void mmapH_visit(Value* v) { } DEF_FREE(mmapH) { MmapHolder* p = (MmapHolder*)x; +#if !defined(_WIN32) if (munmap(p->a, p->size)) thrF("Failed to unmap: %S", strerror(errno)); if (close(p->fd)) thrF("Failed to close file: %S", strerror(errno)); +#else + if (!UnmapViewOfFile(p->a)) thrF("Failed to unmap: %S", winError()); + if (!CloseHandle(p->hMapFile)) thrF("Failed to close file mapping: %S", winError()); + if (!CloseHandle(p->hFile)) thrF("Failed to close file: %S", winError()); +#endif } B info_c1(B,B); @@ -290,6 +306,7 @@ static Arr* mmapH_slice(B x, usz s, usz ia) { B mmap_file(B path) { char* p = toCStr(path); dec(path); +#if !defined(_WIN32) int fd = open(p, 0); freeCStr(p); if (fd==-1) thrF("Failed to open file: %S", strerror(errno)); @@ -300,11 +317,41 @@ B mmap_file(B path) { close(fd); thrM("failed to mmap file"); } +#else + HANDLE hFile = CreateFileA( + p, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + freeCStr(p); + if (hFile==INVALID_HANDLE_VALUE) thrF("Failed to open file: %S", winError()); + LARGE_INTEGER fileSize; + if (!GetFileSizeEx(hFile, &fileSize)) { + CloseHandle(hFile); + thrF("Failed to get file size: %S", winError()); + } + u64 len = fileSize.QuadPart; + + HANDLE hMapFile = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL); + if (hMapFile==INVALID_HANDLE_VALUE) { + CloseHandle(hFile); + thrF("Failed to create file mapping: %S", winError()); + } + u8* data = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0); + if (data==NULL) { + CloseHandle(hFile); + CloseHandle(hMapFile); + thrF("Failed to map view of file: %S", winError()); + } +#endif MmapHolder* holder = m_arr(sizeof(MmapHolder), t_mmapH, len); - holder->fd = fd; holder->a = data; +#if !defined(_WIN32) + holder->fd = fd; holder->size = len; +#else + holder->hFile = hFile; + holder->hMapFile = hMapFile; +#endif arr_shVec((Arr*)holder); return taga(arr_shVec(mmapH_slice(taga(holder), 0, len))); } From 7c5806762085ed6f850edc1b59672eace409d5f1 Mon Sep 17 00:00:00 2001 From: vylsaz Date: Sat, 27 Jan 2024 15:47:35 +0000 Subject: [PATCH 3/3] Enable delete access --- src/utils/file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/file.c b/src/utils/file.c index a67f1336..eccc25d2 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -319,7 +319,7 @@ B mmap_file(B path) { } #else HANDLE hFile = CreateFileA( - p, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, + p, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); freeCStr(p); if (hFile==INVALID_HANDLE_VALUE) thrF("Failed to open file: %S", winError()); @@ -327,7 +327,7 @@ B mmap_file(B path) { if (!GetFileSizeEx(hFile, &fileSize)) { CloseHandle(hFile); thrF("Failed to get file size: %S", winError()); - } + } u64 len = fileSize.QuadPart; HANDLE hMapFile = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);