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