From 30c586d37d5ebec39ac3b5357952ad2111331f8c Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 10 Oct 2023 12:15:14 -0700 Subject: [PATCH] Fix WIN32 file read. --- Arena.cc | 33 ++++++++++++++++++++------------- Arena.h | 2 +- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Arena.cc b/Arena.cc index 9261a03..257a56b 100644 --- a/Arena.cc +++ b/Arena.cc @@ -187,9 +187,8 @@ Arena::Open(const char *path) size_t fSize; size_t fRemaining; auto *cursor = this->store; - OVERLAPPED overlap; + OVERLAPPED overlap = {0}; - std::cout << "CreateFileA\n"; fHandle = CreateFileA( (LPSTR)path, GENERIC_READ, @@ -203,26 +202,36 @@ Arena::Open(const char *path) return -1; } - std::cout << "GetFileSizeEx\n"; - if (!GetFileSizeEx(fHandle, reinterpret_cast(&fSize))) { + if (SetFilePointer(fHandle, 0, 0, FILE_BEGIN) != 0) { + displayWinErr("SetFilePointer"); + CloseHandle(fHandle); + return -1; + } + + if (GetFileSizeEx(fHandle, reinterpret_cast(&fSize)) != TRUE) { displayWinErr("GetFileSizeEx"); CloseHandle(fHandle); return -1; } this->SetAlloc(fSize); + cursor = this->NewCursor(); + this->store[0] = 1; fRemaining = fSize; while (fRemaining != 0) { - std::cout << "ReadFile\n"; overlap.Offset = (fSize - fRemaining); - if (ReadFile(fHandle, cursor, fSize, + if (ReadFile(fHandle, cursor, fSize-1, &fRead, &overlap) != TRUE) { - displayWinErr("ReadFile"); - CloseHandle(fHandle); - this->Destroy(); - return -1; + auto errorCode = GetLastError(); + if (errorCode != ERROR_HANDLE_EOF) { + displayWinErr("ReadFile"); + CloseHandle(fHandle); + this->Destroy(); + return -1; + } + break; } cursor += fRead; @@ -239,7 +248,6 @@ Arena::Create(const char *path, size_t fileSize, DWORD mode) { HANDLE fHandle; - std::cout << "Create::CreateFileA\n"; fHandle = CreateFileA( (LPSTR)path, GENERIC_READ|GENERIC_WRITE, @@ -253,7 +261,6 @@ Arena::Create(const char *path, size_t fileSize, DWORD mode) return -1; } - std::cout << "SetFileValidData\n"; if (SetFileValidData(fHandle, fileSize) != fileSize) { displayWinErr("SetFileValidData"); CloseHandle(fHandle); @@ -379,7 +386,7 @@ operator<<(std::ostream &os, Arena &arena) int Arena::Write(const char *path) { - FILE *arenaFile = NULL; + FILE *arenaFile = nullptr; int retc = -1; #if defined(__posix__) || defined(__linux__) diff --git a/Arena.h b/Arena.h index f65c1a8..1eeb520 100644 --- a/Arena.h +++ b/Arena.h @@ -116,7 +116,7 @@ public: #else int MemoryMap(int memFileDes, size_t memSize) - { throw NotImplemented("WIN32"); } + { (void)memFileDes; (void)memSize; throw NotImplemented("WIN32"); } #endif /// Create creates a new file, truncating it if it already exists. On