From ce3ea816c2609ed4e5afc53aa47b3de7c9ab2bad Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 15 Mar 2022 17:41:19 +0100 Subject: Manage lifetime of FileHandle --- zenstore/basicfile.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 895db6cee..1f6ead2d3 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -279,6 +279,20 @@ BasicFile::FileSize() #endif } +void +BasicFile::SetFileSize(uint64_t FileSize) +{ +#if ZEN_PLATFORM_WINDOWS + LARGE_INTEGER liFileSize; + liFileSize.QuadPart = FileSize; + ::SetFilePointerEx(m_FileHandle, liFileSize, 0, FILE_BEGIN); + ::SetEndOfFile(m_FileHandle); +#else + int Fd = int(intptr_t(m_FileHandle)); + int ftruncate64(Fd, FileSize); +#endif +} + ////////////////////////////////////////////////////////////////////////// TemporaryFile::~TemporaryFile() -- cgit v1.2.3 From 3f9aa99659b623f51907237a3c2ae1c083a0d609 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Wed, 16 Mar 2022 12:29:30 +0100 Subject: fix posix SetFileSize --- zenstore/basicfile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 1f6ead2d3..29ca0e388 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -289,7 +289,7 @@ BasicFile::SetFileSize(uint64_t FileSize) ::SetEndOfFile(m_FileHandle); #else int Fd = int(intptr_t(m_FileHandle)); - int ftruncate64(Fd, FileSize); + ftruncate64(Fd, FileSize); #endif } -- cgit v1.2.3 From 5a34b0017d5c4fd263db759233c2d2b574cd619a Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Wed, 16 Mar 2022 13:22:47 +0100 Subject: Fix BasicFile::SetFileSize for Mac/Linux --- zenstore/basicfile.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 29ca0e388..17a57309d 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -287,9 +287,12 @@ BasicFile::SetFileSize(uint64_t FileSize) liFileSize.QuadPart = FileSize; ::SetFilePointerEx(m_FileHandle, liFileSize, 0, FILE_BEGIN); ::SetEndOfFile(m_FileHandle); +#elif ZEN_PLATFORM_MAC + int Fd = int(intptr_t(m_FileHandle)); + ftruncate(fileno(Fd), (off_t)FileSize); #else int Fd = int(intptr_t(m_FileHandle)); - ftruncate64(Fd, FileSize); + ftruncate64(fileno(Fd), (off64_t)FileSize); #endif } -- cgit v1.2.3 From f98dfd8f2f853aeb33eff572a9658f2993cc17bb Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Mon, 21 Mar 2022 10:06:10 +0100 Subject: Move MarkAsDeleteOnClose to BasicFile --- zenstore/basicfile.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 17a57309d..efa91f107 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -296,6 +296,31 @@ BasicFile::SetFileSize(uint64_t FileSize) #endif } +void +BasicFile::MarkAsDeleteOnClose(std::error_code& Ec) +{ + Ec.clear(); +#if ZEN_PLATFORM_WINDOWS + FILE_DISPOSITION_INFO Fdi{}; + Fdi.DeleteFile = TRUE; + BOOL Success = SetFileInformationByHandle(m_FileHandle, FileDispositionInfo, &Fdi, sizeof Fdi); + if (!Success) + { + Ec = MakeErrorCodeFromLastError(); + } +#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC + std::filesystem::path SourcePath = PathFromHandle(FileRef.FileHandle); + if (unlink(SourcePath.c_str()) < 0) + { + int UnlinkError = zen::GetLastError(); + if (UnlinkError != ENOENT) + { + Ec = MakeErrorCode(UnlinkError); + } + } +#endif +} + ////////////////////////////////////////////////////////////////////////// TemporaryFile::~TemporaryFile() -- cgit v1.2.3 From 77a6d84f02617b0a19bb0c5d838f91f7a20eb264 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Mon, 21 Mar 2022 14:44:48 +0100 Subject: linux fixes --- zenstore/basicfile.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index efa91f107..7d5b659c9 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -289,10 +289,10 @@ BasicFile::SetFileSize(uint64_t FileSize) ::SetEndOfFile(m_FileHandle); #elif ZEN_PLATFORM_MAC int Fd = int(intptr_t(m_FileHandle)); - ftruncate(fileno(Fd), (off_t)FileSize); + ftruncate(Fd, (off_t)FileSize); #else int Fd = int(intptr_t(m_FileHandle)); - ftruncate64(fileno(Fd), (off64_t)FileSize); + ftruncate64(Fd, (off64_t)FileSize); #endif } @@ -309,7 +309,7 @@ BasicFile::MarkAsDeleteOnClose(std::error_code& Ec) Ec = MakeErrorCodeFromLastError(); } #elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC - std::filesystem::path SourcePath = PathFromHandle(FileRef.FileHandle); + std::filesystem::path SourcePath = PathFromHandle(m_FileHandle); if (unlink(SourcePath.c_str()) < 0) { int UnlinkError = zen::GetLastError(); -- cgit v1.2.3 From 90ee5317a15cb51960b19b76e421725cf0e68dbe Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 22 Mar 2022 12:17:41 +0100 Subject: void copy of std::filesystem::path --- zenstore/basicfile.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 7d5b659c9..fd0c8a8f6 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -29,7 +29,7 @@ BasicFile::~BasicFile() } void -BasicFile::Open(std::filesystem::path FileName, bool IsCreate) +BasicFile::Open(const std::filesystem::path& FileName, bool IsCreate) { std::error_code Ec; Open(FileName, IsCreate, Ec); @@ -41,7 +41,7 @@ BasicFile::Open(std::filesystem::path FileName, bool IsCreate) } void -BasicFile::Open(std::filesystem::path FileName, bool IsCreate, std::error_code& Ec) +BasicFile::Open(const std::filesystem::path& FileName, bool IsCreate, std::error_code& Ec) { Ec.clear(); -- cgit v1.2.3 From 53222208fa59b8e1903c5ffd23c00a8703c65edd Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Wed, 23 Mar 2022 11:13:36 +0100 Subject: We need DELETE access to be able to mark a file for delete on close. --- zenstore/basicfile.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index fd0c8a8f6..77bada95f 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -47,16 +47,11 @@ BasicFile::Open(const std::filesystem::path& FileName, bool IsCreate, std::error #if ZEN_PLATFORM_WINDOWS const DWORD dwCreationDisposition = IsCreate ? CREATE_ALWAYS : OPEN_EXISTING; - DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; + DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE | DELETE; const DWORD dwShareMode = FILE_SHARE_READ; const DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; HANDLE hTemplateFile = nullptr; - if (IsCreate) - { - dwDesiredAccess |= DELETE; - } - HANDLE FileHandle = CreateFile(FileName.c_str(), dwDesiredAccess, dwShareMode, -- cgit v1.2.3 From 52bf08afc4b9da9ccdd73089c8ebfc7bda859bd3 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Thu, 24 Mar 2022 22:41:46 +0100 Subject: Migration now works in larger disk IO chunks BasicFile and CasLogFile now has new explicit modes instead of create true/false --- zenstore/basicfile.cpp | 64 +++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 24 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 77bada95f..fc7282941 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -29,10 +29,10 @@ BasicFile::~BasicFile() } void -BasicFile::Open(const std::filesystem::path& FileName, bool IsCreate) +BasicFile::Open(const std::filesystem::path& FileName, EMode Mode) { std::error_code Ec; - Open(FileName, IsCreate, Ec); + Open(FileName, Mode, Ec); if (Ec) { @@ -41,16 +41,20 @@ BasicFile::Open(const std::filesystem::path& FileName, bool IsCreate) } void -BasicFile::Open(const std::filesystem::path& FileName, bool IsCreate, std::error_code& Ec) +BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_code& Ec) { Ec.clear(); + uint32_t ModeFlags = static_cast(Mode); #if ZEN_PLATFORM_WINDOWS - const DWORD dwCreationDisposition = IsCreate ? CREATE_ALWAYS : OPEN_EXISTING; - DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE | DELETE; - const DWORD dwShareMode = FILE_SHARE_READ; - const DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; - HANDLE hTemplateFile = nullptr; + const DWORD dwCreationDisposition = (ModeFlags & static_cast(kAccessTruncate)) ? CREATE_ALWAYS : OPEN_EXISTING; + DWORD dwDesiredAccess = GENERIC_READ; + dwDesiredAccess |= (ModeFlags & static_cast(kAccessWrite)) ? GENERIC_WRITE : 0; + dwDesiredAccess |= (ModeFlags & static_cast(kAccessDelete)) ? DELETE : 0; + + const DWORD dwShareMode = FILE_SHARE_READ; + const DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; + HANDLE hTemplateFile = nullptr; HANDLE FileHandle = CreateFile(FileName.c_str(), dwDesiredAccess, @@ -67,8 +71,9 @@ BasicFile::Open(const std::filesystem::path& FileName, bool IsCreate, std::error return; } #else - int OpenFlags = O_RDWR | O_CLOEXEC; - OpenFlags |= IsCreate ? O_CREAT | O_TRUNC : 0; + int OpenFlags = O_CLOEXEC; + OpenFlags |= (ModeFlags & static_cast(kAccessWrite)) ? O_RDWR : 0; + OpenFlags |= (ModeFlags & static_cast(kAccessTruncate)) ? (O_CREAT | O_TRUNC) : 0; int Fd = open(FileName.c_str(), OpenFlags, 0666); if (Fd < 0) @@ -105,27 +110,33 @@ BasicFile::Close() void BasicFile::Read(void* Data, uint64_t BytesToRead, uint64_t FileOffset) { - const uint64_t MaxChunkSize = 2u * 1024 * 1024 * 1024; + const uint64_t MaxChunkSize = 2u * 1024 * 1024 * 1024; + uint64_t TotalFileSize = FileSize(); + ZEN_ASSERT((FileOffset + BytesToRead) <= TotalFileSize); while (BytesToRead) { const uint64_t NumberOfBytesToRead = Min(BytesToRead, MaxChunkSize); + uint64_t NumberOfBytesRead; #if ZEN_PLATFORM_WINDOWS OVERLAPPED Ovl{}; Ovl.Offset = DWORD(FileOffset & 0xffff'ffffu); Ovl.OffsetHigh = DWORD(FileOffset >> 32); + ZEN_ASSERT((FileOffset + NumberOfBytesToRead) <= TotalFileSize); DWORD dwNumberOfBytesRead = 0; BOOL Success = ::ReadFile(m_FileHandle, Data, DWORD(NumberOfBytesToRead), &dwNumberOfBytesRead, &Ovl); - ZEN_ASSERT(dwNumberOfBytesRead == NumberOfBytesToRead); + ZEN_ASSERT((dwNumberOfBytesRead <= NumberOfBytesToRead) && (dwNumberOfBytesRead > 0)); + NumberOfBytesRead = dwNumberOfBytesRead; #else static_assert(sizeof(off_t) >= sizeof(uint64_t), "sizeof(off_t) does not support large files"); - int Fd = int(uintptr_t(m_FileHandle)); - int BytesRead = pread(Fd, Data, NumberOfBytesToRead, FileOffset); - bool Success = (BytesRead > 0); + int Fd = int(uintptr_t(m_FileHandle)); + int BytesRead = pread(Fd, Data, NumberOfBytesToRead, FileOffset); + bool Success = (BytesRead > 0); + NumberOfBytesRead = static_cast(BytesRead); #endif if (!Success) @@ -133,9 +144,9 @@ BasicFile::Read(void* Data, uint64_t BytesToRead, uint64_t FileOffset) ThrowLastError(fmt::format("Failed to read from file '{}'", zen::PathFromHandle(m_FileHandle))); } - BytesToRead -= NumberOfBytesToRead; - FileOffset += NumberOfBytesToRead; - Data = reinterpret_cast(Data) + NumberOfBytesToRead; + BytesToRead -= NumberOfBytesRead; + FileOffset += NumberOfBytesRead; + Data = reinterpret_cast(Data) + NumberOfBytesRead; } } @@ -263,7 +274,14 @@ BasicFile::FileSize() #if ZEN_PLATFORM_WINDOWS ULARGE_INTEGER liFileSize; liFileSize.LowPart = ::GetFileSize(m_FileHandle, &liFileSize.HighPart); - + if (liFileSize.LowPart == INVALID_FILE_SIZE) + { + int Error = GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to get file size from file '{}'", zen::PathFromHandle(m_FileHandle))); + } + } return uint64_t(liFileSize.QuadPart); #else int Fd = int(uintptr_t(m_FileHandle)); @@ -351,9 +369,7 @@ TemporaryFile::CreateTemporary(std::filesystem::path TempDirName, std::error_cod m_TempPath = TempDirName / TempName.c_str(); - const bool IsCreate = true; - - Open(m_TempPath, IsCreate, Ec); + Open(m_TempPath, BasicFile::EMode::kTruncateDelete, Ec); } void @@ -453,8 +469,8 @@ TEST_CASE("BasicFile") ScopedCurrentDirectoryChange _; BasicFile File1; - CHECK_THROWS(File1.Open("zonk", false)); - CHECK_NOTHROW(File1.Open("zonk", true)); + CHECK_THROWS(File1.Open("zonk", BasicFile::EMode::kRead)); + CHECK_NOTHROW(File1.Open("zonk", BasicFile::EMode::kTruncate)); CHECK_NOTHROW(File1.Write("abcd", 4, 0)); CHECK(File1.FileSize() == 4); { -- cgit v1.2.3 From 6a166635b5c1d12aae5e58a04fbe423cf9995f6f Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 25 Mar 2022 12:04:59 +0100 Subject: incremental migration with optional clean of source add more fine-grained access modes for BasicFile --- zenstore/basicfile.cpp | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index fc7282941..44a7fc77f 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -44,13 +44,33 @@ void BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_code& Ec) { Ec.clear(); - uint32_t ModeFlags = static_cast(Mode); #if ZEN_PLATFORM_WINDOWS - const DWORD dwCreationDisposition = (ModeFlags & static_cast(kAccessTruncate)) ? CREATE_ALWAYS : OPEN_EXISTING; - DWORD dwDesiredAccess = GENERIC_READ; - dwDesiredAccess |= (ModeFlags & static_cast(kAccessWrite)) ? GENERIC_WRITE : 0; - dwDesiredAccess |= (ModeFlags & static_cast(kAccessDelete)) ? DELETE : 0; + DWORD dwCreationDisposition = 0; + DWORD dwDesiredAccess = 0; + switch (Mode) + { + case EMode::kRead: + dwCreationDisposition |= OPEN_EXISTING; + dwDesiredAccess |= GENERIC_READ; + break; + case EMode::kWrite: + dwCreationDisposition |= OPEN_ALWAYS; + dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE); + break; + case EMode::kDelete: + dwCreationDisposition |= OPEN_ALWAYS; + dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE | DELETE); + break; + case EMode::kTruncate: + dwCreationDisposition |= CREATE_ALWAYS; + dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE); + break; + case EMode::kTruncateDelete: + dwCreationDisposition |= CREATE_ALWAYS; + dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE | DELETE); + break; + } const DWORD dwShareMode = FILE_SHARE_READ; const DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; @@ -72,8 +92,20 @@ BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_co } #else int OpenFlags = O_CLOEXEC; - OpenFlags |= (ModeFlags & static_cast(kAccessWrite)) ? O_RDWR : 0; - OpenFlags |= (ModeFlags & static_cast(kAccessTruncate)) ? (O_CREAT | O_TRUNC) : 0; + switch (Mode) + { + case EMode::kRead: + OpenFlags |= O_RDONLY; + break; + case EMode::kWrite: + case EMode::kDelete: + OpenFlags |= (O_RDWR | O_CREAT); + break; + case EMode::kTruncate: + case EMode::kTruncateDelete: + OpenFlags |= (O_RDWR | O_CREAT | O_TRUNC); + break; + } int Fd = open(FileName.c_str(), OpenFlags, 0666); if (Fd < 0) -- cgit v1.2.3 From b4fab3c2d5ff2426489d42ad07f302f90587cc28 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 25 Mar 2022 13:17:13 +0100 Subject: cleanup --- zenstore/basicfile.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 44a7fc77f..0d410c745 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -142,33 +142,27 @@ BasicFile::Close() void BasicFile::Read(void* Data, uint64_t BytesToRead, uint64_t FileOffset) { - const uint64_t MaxChunkSize = 2u * 1024 * 1024 * 1024; - uint64_t TotalFileSize = FileSize(); - ZEN_ASSERT((FileOffset + BytesToRead) <= TotalFileSize); + const uint64_t MaxChunkSize = 2u * 1024 * 1024 * 1024; while (BytesToRead) { const uint64_t NumberOfBytesToRead = Min(BytesToRead, MaxChunkSize); - uint64_t NumberOfBytesRead; #if ZEN_PLATFORM_WINDOWS OVERLAPPED Ovl{}; Ovl.Offset = DWORD(FileOffset & 0xffff'ffffu); Ovl.OffsetHigh = DWORD(FileOffset >> 32); - ZEN_ASSERT((FileOffset + NumberOfBytesToRead) <= TotalFileSize); DWORD dwNumberOfBytesRead = 0; BOOL Success = ::ReadFile(m_FileHandle, Data, DWORD(NumberOfBytesToRead), &dwNumberOfBytesRead, &Ovl); - ZEN_ASSERT((dwNumberOfBytesRead <= NumberOfBytesToRead) && (dwNumberOfBytesRead > 0)); - NumberOfBytesRead = dwNumberOfBytesRead; + ZEN_ASSERT(dwNumberOfBytesRead == NumberOfBytesToRead); #else static_assert(sizeof(off_t) >= sizeof(uint64_t), "sizeof(off_t) does not support large files"); - int Fd = int(uintptr_t(m_FileHandle)); - int BytesRead = pread(Fd, Data, NumberOfBytesToRead, FileOffset); - bool Success = (BytesRead > 0); - NumberOfBytesRead = static_cast(BytesRead); + int Fd = int(uintptr_t(m_FileHandle)); + int BytesRead = pread(Fd, Data, NumberOfBytesToRead, FileOffset); + bool Success = (BytesRead > 0); #endif if (!Success) @@ -176,9 +170,9 @@ BasicFile::Read(void* Data, uint64_t BytesToRead, uint64_t FileOffset) ThrowLastError(fmt::format("Failed to read from file '{}'", zen::PathFromHandle(m_FileHandle))); } - BytesToRead -= NumberOfBytesRead; - FileOffset += NumberOfBytesRead; - Data = reinterpret_cast(Data) + NumberOfBytesRead; + BytesToRead -= NumberOfBytesToRead; + FileOffset += NumberOfBytesToRead; + Data = reinterpret_cast(Data) + NumberOfBytesToRead; } } @@ -366,6 +360,14 @@ BasicFile::MarkAsDeleteOnClose(std::error_code& Ec) #endif } +void* +BasicFile::Detach() +{ + void* FileHandle = m_FileHandle; + m_FileHandle = 0; + return FileHandle; +} + ////////////////////////////////////////////////////////////////////////// TemporaryFile::~TemporaryFile() -- cgit v1.2.3 From c0dc629f367e6bae0eb81ff2febd0ff467f1cd27 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 1 Apr 2022 15:20:57 +0200 Subject: fix BasicFile::Open on Linux --- zenstore/basicfile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 0d410c745..042f0aa43 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -113,7 +113,7 @@ BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_co Ec = zen::MakeErrorCodeFromLastError(); return; } - if (IsCreate) + if (Mode != EMode::kRead) { fchmod(Fd, 0666); } -- cgit v1.2.3 From 98c7fd153f4d25f13b8e4bc81648b02f80698c93 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 1 Apr 2022 15:39:49 +0200 Subject: make sure we allocate disk space when we set file size --- zenstore/basicfile.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 042f0aa43..93d4ded13 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -329,9 +329,11 @@ BasicFile::SetFileSize(uint64_t FileSize) #elif ZEN_PLATFORM_MAC int Fd = int(intptr_t(m_FileHandle)); ftruncate(Fd, (off_t)FileSize); + posix_fallocate(Fd, 0, (off_t)FileSize); #else - int Fd = int(intptr_t(m_FileHandle)); + posix_fallocate(Fd, 0, ) int Fd = int(intptr_t(m_FileHandle)); ftruncate64(Fd, (off64_t)FileSize); + posix_fallocate64(Fd, 0, (off64_t)FileSize); #endif } -- cgit v1.2.3 From 345cf462e89ba37867abe4c0722ce2549dddc4ca Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 1 Apr 2022 16:08:53 +0200 Subject: linux fixes --- zenstore/basicfile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 93d4ded13..8b3ee2844 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -331,7 +331,7 @@ BasicFile::SetFileSize(uint64_t FileSize) ftruncate(Fd, (off_t)FileSize); posix_fallocate(Fd, 0, (off_t)FileSize); #else - posix_fallocate(Fd, 0, ) int Fd = int(intptr_t(m_FileHandle)); + int Fd = int(intptr_t(m_FileHandle)); ftruncate64(Fd, (off64_t)FileSize); posix_fallocate64(Fd, 0, (off64_t)FileSize); #endif -- cgit v1.2.3 From 6e9f6df8e4d51852327d05b254813b7b792f40a8 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 1 Apr 2022 23:46:04 +0200 Subject: error handling in BasicFile::SetFileSize --- zenstore/basicfile.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 8b3ee2844..eb93638f2 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -324,16 +324,38 @@ BasicFile::SetFileSize(uint64_t FileSize) #if ZEN_PLATFORM_WINDOWS LARGE_INTEGER liFileSize; liFileSize.QuadPart = FileSize; - ::SetFilePointerEx(m_FileHandle, liFileSize, 0, FILE_BEGIN); - ::SetEndOfFile(m_FileHandle); + BOOL OK = ::SetFilePointerEx(m_FileHandle, liFileSize, 0, FILE_BEGIN); + if (OK == FALSE) + { + ThrowLastError(fmt::format("Failed to set file pointer to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } + OK = ::SetEndOfFile(m_FileHandle); + if (OK == FALSE) + { + ThrowLastError(fmt::format("Failed to set end of file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } #elif ZEN_PLATFORM_MAC int Fd = int(intptr_t(m_FileHandle)); - ftruncate(Fd, (off_t)FileSize); - posix_fallocate(Fd, 0, (off_t)FileSize); + if (ftruncate(Fd, (off_t)FileSize) < 0) + { + ThrowLastError(fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } + std::error_code Ec = posix_fallocate(Fd, 0, (off_t)FileSize); + if (Ec) + { + ThrowSystemError(Ec, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + } #else int Fd = int(intptr_t(m_FileHandle)); - ftruncate64(Fd, (off64_t)FileSize); - posix_fallocate64(Fd, 0, (off64_t)FileSize); + if (ftruncate64(Fd, (off64_t)FileSize) < 0) + { + ThrowLastError(fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } + std::error_code Ec = posix_fallocate64(Fd, 0, (off64_t)FileSize); + if (Ec) + { + ThrowSystemError(Ec, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + } #endif } -- cgit v1.2.3 From 34235f5628f8ec50a01d5d16b8af56792292147e Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 1 Apr 2022 23:49:09 +0200 Subject: rename EMode to Mode --- zenstore/basicfile.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index eb93638f2..f518e0491 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -29,7 +29,7 @@ BasicFile::~BasicFile() } void -BasicFile::Open(const std::filesystem::path& FileName, EMode Mode) +BasicFile::Open(const std::filesystem::path& FileName, Mode Mode) { std::error_code Ec; Open(FileName, Mode, Ec); @@ -41,7 +41,7 @@ BasicFile::Open(const std::filesystem::path& FileName, EMode Mode) } void -BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_code& Ec) +BasicFile::Open(const std::filesystem::path& FileName, Mode Mode, std::error_code& Ec) { Ec.clear(); @@ -50,23 +50,23 @@ BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_co DWORD dwDesiredAccess = 0; switch (Mode) { - case EMode::kRead: + case Mode::kRead: dwCreationDisposition |= OPEN_EXISTING; dwDesiredAccess |= GENERIC_READ; break; - case EMode::kWrite: + case Mode::kWrite: dwCreationDisposition |= OPEN_ALWAYS; dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE); break; - case EMode::kDelete: + case Mode::kDelete: dwCreationDisposition |= OPEN_ALWAYS; dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE | DELETE); break; - case EMode::kTruncate: + case Mode::kTruncate: dwCreationDisposition |= CREATE_ALWAYS; dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE); break; - case EMode::kTruncateDelete: + case Mode::kTruncateDelete: dwCreationDisposition |= CREATE_ALWAYS; dwDesiredAccess |= (GENERIC_READ | GENERIC_WRITE | DELETE); break; @@ -94,15 +94,15 @@ BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_co int OpenFlags = O_CLOEXEC; switch (Mode) { - case EMode::kRead: + case Mode::kRead: OpenFlags |= O_RDONLY; break; - case EMode::kWrite: - case EMode::kDelete: + case Mode::kWrite: + case Mode::kDelete: OpenFlags |= (O_RDWR | O_CREAT); break; - case EMode::kTruncate: - case EMode::kTruncateDelete: + case Mode::kTruncate: + case Mode::kTruncateDelete: OpenFlags |= (O_RDWR | O_CREAT | O_TRUNC); break; } @@ -113,7 +113,7 @@ BasicFile::Open(const std::filesystem::path& FileName, EMode Mode, std::error_co Ec = zen::MakeErrorCodeFromLastError(); return; } - if (Mode != EMode::kRead) + if (Mode != Mode::kRead) { fchmod(Fd, 0666); } @@ -427,7 +427,7 @@ TemporaryFile::CreateTemporary(std::filesystem::path TempDirName, std::error_cod m_TempPath = TempDirName / TempName.c_str(); - Open(m_TempPath, BasicFile::EMode::kTruncateDelete, Ec); + Open(m_TempPath, BasicFile::Mode::kTruncateDelete, Ec); } void @@ -527,8 +527,8 @@ TEST_CASE("BasicFile") ScopedCurrentDirectoryChange _; BasicFile File1; - CHECK_THROWS(File1.Open("zonk", BasicFile::EMode::kRead)); - CHECK_NOTHROW(File1.Open("zonk", BasicFile::EMode::kTruncate)); + CHECK_THROWS(File1.Open("zonk", BasicFile::Mode::kRead)); + CHECK_NOTHROW(File1.Open("zonk", BasicFile::Mode::kTruncate)); CHECK_NOTHROW(File1.Write("abcd", 4, 0)); CHECK(File1.FileSize() == 4); { -- cgit v1.2.3 From c2603f19ae2ac57b68eb8d6187cda668c70f2b2b Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Sat, 2 Apr 2022 00:08:26 +0200 Subject: proper error handling when setting file size --- zenstore/basicfile.cpp | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index f518e0491..731aacc69 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -86,7 +86,7 @@ BasicFile::Open(const std::filesystem::path& FileName, Mode Mode, std::error_cod if (FileHandle == INVALID_HANDLE_VALUE) { - Ec = zen::MakeErrorCodeFromLastError(); + Ec = MakeErrorCodeFromLastError(); return; } @@ -110,7 +110,7 @@ BasicFile::Open(const std::filesystem::path& FileName, Mode Mode, std::error_cod int Fd = open(FileName.c_str(), OpenFlags, 0666); if (Fd < 0) { - Ec = zen::MakeErrorCodeFromLastError(); + Ec = MakeErrorCodeFromLastError(); return; } if (Mode != Mode::kRead) @@ -302,10 +302,10 @@ BasicFile::FileSize() liFileSize.LowPart = ::GetFileSize(m_FileHandle, &liFileSize.HighPart); if (liFileSize.LowPart == INVALID_FILE_SIZE) { - int Error = GetLastError(); + int Error = zen::GetLastError(); if (Error) { - ThrowSystemError(Error, fmt::format("Failed to get file size from file '{}'", zen::PathFromHandle(m_FileHandle))); + ThrowSystemError(Error, fmt::format("Failed to get file size from file '{}'", PathFromHandle(m_FileHandle))); } } return uint64_t(liFileSize.QuadPart); @@ -327,18 +327,30 @@ BasicFile::SetFileSize(uint64_t FileSize) BOOL OK = ::SetFilePointerEx(m_FileHandle, liFileSize, 0, FILE_BEGIN); if (OK == FALSE) { - ThrowLastError(fmt::format("Failed to set file pointer to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set file pointer to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } OK = ::SetEndOfFile(m_FileHandle); if (OK == FALSE) { - ThrowLastError(fmt::format("Failed to set end of file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set end of file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } #elif ZEN_PLATFORM_MAC int Fd = int(intptr_t(m_FileHandle)); if (ftruncate(Fd, (off_t)FileSize) < 0) { - ThrowLastError(fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } std::error_code Ec = posix_fallocate(Fd, 0, (off_t)FileSize); if (Ec) @@ -349,7 +361,11 @@ BasicFile::SetFileSize(uint64_t FileSize) int Fd = int(intptr_t(m_FileHandle)); if (ftruncate64(Fd, (off64_t)FileSize) < 0) { - ThrowLastError(fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } std::error_code Ec = posix_fallocate64(Fd, 0, (off64_t)FileSize); if (Ec) -- cgit v1.2.3 From 35ab521ce481d25215582c9fea2dacb98e0820c1 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Sat, 2 Apr 2022 00:12:03 +0200 Subject: more linux fixes --- zenstore/basicfile.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 731aacc69..6e60c6e9c 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -352,10 +352,10 @@ BasicFile::SetFileSize(uint64_t FileSize) ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); } } - std::error_code Ec = posix_fallocate(Fd, 0, (off_t)FileSize); - if (Ec) + int Error = posix_fallocate(Fd, 0, (off_t)FileSize); + if (Error) { - ThrowSystemError(Ec, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + ThrowSystemError(Error, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) } #else int Fd = int(intptr_t(m_FileHandle)); @@ -367,10 +367,10 @@ BasicFile::SetFileSize(uint64_t FileSize) ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); } } - std::error_code Ec = posix_fallocate64(Fd, 0, (off64_t)FileSize); - if (Ec) + int Error = posix_fallocate64(Fd, 0, (off64_t)FileSize); + if (Error) { - ThrowSystemError(Ec, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + ThrowSystemError(Error, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) } #endif } -- cgit v1.2.3 From 65a1ec5ba5d58eaa8929e126fca12c1c00d69edf Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Sat, 2 Apr 2022 00:19:24 +0200 Subject: don't try to allocate file space for a zero size file --- zenstore/basicfile.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 6e60c6e9c..e795b67eb 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -352,10 +352,13 @@ BasicFile::SetFileSize(uint64_t FileSize) ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); } } - int Error = posix_fallocate(Fd, 0, (off_t)FileSize); - if (Error) + if (FileSize > 0) { - ThrowSystemError(Error, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + int Error = posix_fallocate(Fd, 0, (off_t)FileSize); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } #else int Fd = int(intptr_t(m_FileHandle)); @@ -367,10 +370,13 @@ BasicFile::SetFileSize(uint64_t FileSize) ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); } } - int Error = posix_fallocate64(Fd, 0, (off64_t)FileSize); - if (Error) + if (FileSize > 0) { - ThrowSystemError(Error, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + int Error = posix_fallocate64(Fd, 0, (off64_t)FileSize); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } #endif } -- cgit v1.2.3