From e29843dc7ed2297e2a0a08eca9d8c30338150cc4 Mon Sep 17 00:00:00 2001 From: Reece Date: Mon, 6 Jun 2022 22:34:49 +0100 Subject: [PATCH] [*] Beyond dumb fio offset issue --- Source/IO/FS/Async.NT.cpp | 11 ++++++++++- Source/IO/FS/FileStream.NT.cpp | 25 +++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Source/IO/FS/Async.NT.cpp b/Source/IO/FS/Async.NT.cpp index 755710ce..7001c463 100644 --- a/Source/IO/FS/Async.NT.cpp +++ b/Source/IO/FS/Async.NT.cpp @@ -103,10 +103,19 @@ namespace Aurora::IO::FS break; } case EFileOpenMode::eReadWrite: + { + CreateDirectories(pathex, true); + fileHandle = CreateFileW(win32Path.c_str(), GENERIC_WRITE | GENERIC_READ, NtLockAdvisoryToShare(lock), NULL, OPEN_ALWAYS, flags, NULL); + if (fileHandle == INVALID_HANDLE_VALUE) + { + fileHandle = CreateFileW(win32Path.c_str(), GENERIC_WRITE | GENERIC_READ, NtLockAdvisoryToShare(lock), NULL, CREATE_ALWAYS, flags, NULL); + } + break; + } case EFileOpenMode::eWrite: { CreateDirectories(pathex, true); - fileHandle = CreateFileW(win32Path.c_str(), (openMode == EFileOpenMode::eReadWrite) ? (GENERIC_WRITE | GENERIC_READ) : GENERIC_WRITE, NtLockAdvisoryToShare(lock), NULL, CREATE_ALWAYS, flags, NULL); + fileHandle = CreateFileW(win32Path.c_str(), GENERIC_WRITE, NtLockAdvisoryToShare(lock), NULL, CREATE_ALWAYS, flags, NULL); break; } } diff --git a/Source/IO/FS/FileStream.NT.cpp b/Source/IO/FS/FileStream.NT.cpp index 052e5120..a838e11d 100644 --- a/Source/IO/FS/FileStream.NT.cpp +++ b/Source/IO/FS/FileStream.NT.cpp @@ -23,8 +23,8 @@ namespace Aurora::IO::FS void WinFileStream::Init(HANDLE handle, const AuString &path) { - handle_ = handle; - path_ = path; + this->handle_ = handle; + this->path_ = path; } AuUInt64 WinFileStream::GetOffset() @@ -32,13 +32,13 @@ namespace Aurora::IO::FS LARGE_INTEGER distance {}; LARGE_INTEGER pos {}; - if (handle_ == INVALID_HANDLE_VALUE) + if (this->handle_ == INVALID_HANDLE_VALUE) { SysPushErrorUninitialized(); return 0; } - if (SetFilePointerEx(handle_, distance, &pos, FILE_CURRENT) == INVALID_SET_FILE_POINTER) + if (SetFilePointerEx(this->handle_, distance, &pos, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { AuLogWarn("SetFilePointerEx IO Error: 0x{:x}, {}", GetLastError(), path_); SysPushErrorIO(); @@ -53,7 +53,7 @@ namespace Aurora::IO::FS LARGE_INTEGER distance {}; LARGE_INTEGER pos {}; - if (handle_ == INVALID_HANDLE_VALUE) + if (this->handle_ == INVALID_HANDLE_VALUE) { SysPushErrorUninitialized(); return false; @@ -61,7 +61,7 @@ namespace Aurora::IO::FS distance.QuadPart = offset; - if (SetFilePointerEx(handle_, distance, &pos, FILE_CURRENT) == INVALID_SET_FILE_POINTER) + if (SetFilePointerEx(this->handle_, distance, &pos, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { AuLogWarn("SetFilePointerEx IO Error: 0x{:x}, {}", GetLastError(), path_); SysPushErrorIO(); @@ -81,7 +81,7 @@ namespace Aurora::IO::FS return 0; } - if (!GetFileSizeEx(handle_, &length)) + if (!GetFileSizeEx(this->handle_, &length)) { SysPushErrorIO(); return 0; @@ -238,10 +238,19 @@ namespace Aurora::IO::FS break; } case EFileOpenMode::eReadWrite: + { + CreateDirectories(pathex, true); + fileHandle = CreateFileW(win32Path.c_str(), GENERIC_WRITE | GENERIC_READ, NtLockAdvisoryToShare(lock), NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (fileHandle == INVALID_HANDLE_VALUE) + { + fileHandle = CreateFileW(win32Path.c_str(), GENERIC_WRITE, NtLockAdvisoryToShare(lock), NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + } + break; + } case EFileOpenMode::eWrite: { CreateDirectories(pathex, true); - fileHandle = CreateFileW(win32Path.c_str(), (openMode == EFileOpenMode::eReadWrite) ? (GENERIC_WRITE | GENERIC_READ) : GENERIC_WRITE, NtLockAdvisoryToShare(lock), NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + fileHandle = CreateFileW(win32Path.c_str(), GENERIC_WRITE, NtLockAdvisoryToShare(lock), NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); break; } }