From 834c5cda13316ee19ff7088138971f6f716f6b8b Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Wed, 13 Oct 2021 11:08:10 +0200 Subject: Moved zencore/windows.h include from basicfile.h to basicfile.cpp --- zenstore/basicfile.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index cfc77e2a5..e2fe68c4d 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -8,6 +8,10 @@ #include #include +#if ZEN_PLATFORM_WINDOWS +# include +#endif + #include #include -- cgit v1.2.3 From 24d6ae3dd08aa11f2bb062ff44bdf56ed4458e03 Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Thu, 14 Oct 2021 13:27:43 +0200 Subject: Implemented basicfile.cpp for POSIX platforms --- zenstore/basicfile.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index e2fe68c4d..7c4c4f99d 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -10,6 +10,9 @@ #if ZEN_PLATFORM_WINDOWS # include +#else +# include +# include #endif #include @@ -41,6 +44,7 @@ BasicFile::Open(std::filesystem::path FileName, bool IsCreate, std::error_code& { Ec.clear(); +#if ZEN_PLATFORM_WINDOWS const DWORD dwCreationDisposition = IsCreate ? CREATE_ALWAYS : OPEN_EXISTING; DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; const DWORD dwShareMode = FILE_SHARE_READ; @@ -66,6 +70,19 @@ BasicFile::Open(std::filesystem::path FileName, bool IsCreate, std::error_code& return; } +#else + int OpenFlags = O_RDWR; + OpenFlags |= IsCreate ? O_CREAT|O_TRUNC : 0; + + int Fd = open(FileName.c_str(), O_RDWR, 0666); + if (Fd < 0) + { + Ec = zen::MakeErrorCodeFromLastError(); + return; + } + + void* FileHandle = (void*)(uintptr_t(Fd)); +#endif m_FileHandle = FileHandle; } @@ -75,7 +92,12 @@ BasicFile::Close() { if (m_FileHandle) { +#if ZEN_PLATFORM_WINDOWS ::CloseHandle(m_FileHandle); +#else + int Fd = int(uintptr_t(m_FileHandle)); + close(Fd); +#endif m_FileHandle = nullptr; } } @@ -89,6 +111,7 @@ BasicFile::Read(void* Data, uint64_t BytesToRead, uint64_t FileOffset) { const uint64_t NumberOfBytesToRead = Min(BytesToRead, MaxChunkSize); +#if ZEN_PLATFORM_WINDOWS OVERLAPPED Ovl{}; Ovl.Offset = DWORD(FileOffset & 0xffff'ffffu); @@ -98,6 +121,12 @@ BasicFile::Read(void* Data, uint64_t BytesToRead, uint64_t FileOffset) BOOL Success = ::ReadFile(m_FileHandle, Data, DWORD(NumberOfBytesToRead), &dwNumberOfBytesRead, &Ovl); 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); +#endif if (!Success) { @@ -164,6 +193,7 @@ BasicFile::Write(const void* Data, uint64_t Size, uint64_t FileOffset, std::erro { const uint64_t NumberOfBytesToWrite = Min(Size, MaxChunkSize); +#if ZEN_PLATFORM_WINDOWS OVERLAPPED Ovl{}; Ovl.Offset = DWORD(FileOffset & 0xffff'ffffu); @@ -172,6 +202,12 @@ BasicFile::Write(const void* Data, uint64_t Size, uint64_t FileOffset, std::erro DWORD dwNumberOfBytesWritten = 0; BOOL Success = ::WriteFile(m_FileHandle, Data, DWORD(NumberOfBytesToWrite), &dwNumberOfBytesWritten, &Ovl); +#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 BytesWritten = pwrite(Fd, Data, NumberOfBytesToWrite, FileOffset); + bool Success = (BytesWritten > 0); +#endif if (!Success) { @@ -213,16 +249,29 @@ BasicFile::WriteAll(IoBuffer Data, std::error_code& Ec) void BasicFile::Flush() { +#if ZEN_PLATFORM_WINDOWS FlushFileBuffers(m_FileHandle); +#else + int Fd = int(uintptr_t(m_FileHandle)); + fsync(Fd); +#endif } uint64_t BasicFile::FileSize() { +#if ZEN_PLATFORM_WINDOWS ULARGE_INTEGER liFileSize; liFileSize.LowPart = ::GetFileSize(m_FileHandle, &liFileSize.HighPart); return uint64_t(liFileSize.QuadPart); +#else + int Fd = int(uintptr_t(m_FileHandle)); + static_assert(sizeof(decltype(stat::st_size)) == sizeof(uint64_t), "fstat() doesn't support large files"); + struct stat Stat; + fstat(Fd, &Stat); + return uint64_t(Stat.st_size); +#endif } ////////////////////////////////////////////////////////////////////////// @@ -237,11 +286,16 @@ TemporaryFile::Close() { if (m_FileHandle) { +#if ZEN_PLATFORM_WINDOWS // Mark file for deletion when final handle is closed FILE_DISPOSITION_INFO Fdi{.DeleteFile = TRUE}; SetFileInformationByHandle(m_FileHandle, FileDispositionInfo, &Fdi, sizeof Fdi); +#else + std::filesystem::path FilePath = zen::PathFromHandle(m_FileHandle); + unlink(FilePath.c_str()); +#endif BasicFile::Close(); } -- cgit v1.2.3 From 7728c367fdbcab816632c93e83ca3ef300a7ae1a Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Wed, 20 Oct 2021 15:50:21 +0200 Subject: open() flags were incorrect and it would not create new files --- 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 dd3598319..a1b9109b3 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -74,7 +74,7 @@ BasicFile::Open(std::filesystem::path FileName, bool IsCreate, std::error_code& int OpenFlags = O_RDWR; OpenFlags |= IsCreate ? O_CREAT|O_TRUNC : 0; - int Fd = open(FileName.c_str(), O_RDWR, 0666); + int Fd = open(FileName.c_str(), OpenFlags, 0666); if (Fd < 0) { Ec = zen::MakeErrorCodeFromLastError(); -- cgit v1.2.3 From 2afc202be27827fe0b81a6f13758ba5b051b451e Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Thu, 28 Oct 2021 10:29:13 +0200 Subject: LockFile implementation for Linux --- zenstore/basicfile.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index c6cf9fc43..598cfd54c 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -14,6 +14,7 @@ #else # include # include +# include #endif #include @@ -338,6 +339,7 @@ LockFile::~LockFile() void LockFile::Create(std::filesystem::path FileName, CbObject Payload, std::error_code& Ec) { +#if ZEN_PLATFORM_WINDOWS Ec.clear(); const DWORD dwCreationDisposition = CREATE_ALWAYS; @@ -360,6 +362,26 @@ LockFile::Create(std::filesystem::path FileName, CbObject Payload, std::error_co return; } +#elif ZEN_PLATFORM_LINUX + int Fd = open(FileName.c_str(), O_RDWR|O_CREAT, 0666); + if (Fd < 0) + { + Ec = zen::MakeErrorCodeFromLastError(); + return; + } + + int LockRet = flock(Fd, LOCK_EX); + if (LockRet < 0) + { + Ec = zen::MakeErrorCodeFromLastError(); + close(Fd); + return; + } + + void* FileHandle = (void*)uintptr_t(Fd); +#else +# error check flock() support +#endif m_FileHandle = FileHandle; -- cgit v1.2.3 From 2e43bf8c83c5026637201e99f698578a5464a8ed Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Thu, 28 Oct 2021 10:29:29 +0200 Subject: Alphebetical include order --- 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 598cfd54c..938c0f9e2 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -12,9 +12,9 @@ #if ZEN_PLATFORM_WINDOWS # include #else -# include # include # include +# include #endif #include -- cgit v1.2.3 From 71a5ab8781a596daad70eef596e8fdc2f0a5f818 Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Tue, 30 Nov 2021 16:25:31 +0100 Subject: Remove a LockFile's lock on destruction --- zenstore/basicfile.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 938c0f9e2..413c78674 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -334,6 +334,12 @@ LockFile::LockFile() LockFile::~LockFile() { +#if ZEN_PLATFORM_LINUX + int Fd = int(intptr_t(m_FileHandle)); + flock(Fd, LOCK_UN|LOCK_NB); +#elif ZEN_PLATFORM_MAC +# error check flock() support +#endif } void -- cgit v1.2.3 From 7ffa95f4d8f4520636a75496e267fef417fc1fb0 Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Tue, 30 Nov 2021 16:26:05 +0100 Subject: Do not block when trying to lock a lock file (POSIX) --- 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 413c78674..3edfc15ee 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -376,7 +376,7 @@ LockFile::Create(std::filesystem::path FileName, CbObject Payload, std::error_co return; } - int LockRet = flock(Fd, LOCK_EX); + int LockRet = flock(Fd, LOCK_EX|LOCK_NB); if (LockRet < 0) { Ec = zen::MakeErrorCodeFromLastError(); -- cgit v1.2.3