diff options
| author | Stefan Boberg <[email protected]> | 2021-10-27 21:01:50 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-27 21:01:50 +0200 |
| commit | 3bbb0d326bec3b3379c831b6a4cdc00438ac67f5 (patch) | |
| tree | 7e94bcb4bb4077aeb68663017695b582ebc9b149 /zenstore/basicfile.cpp | |
| parent | Fixed crash at startup when updating manifest. (diff) | |
| download | zen-3bbb0d326bec3b3379c831b6a4cdc00438ac67f5.tar.xz zen-3bbb0d326bec3b3379c831b6a4cdc00438ac67f5.zip | |
Lockfile implementation (#24)
Implemented lockfile synchronization
To be used instead of or in conjunction with existing events to coordinate launching and discovery of server instances
Diffstat (limited to 'zenstore/basicfile.cpp')
| -rw-r--r-- | zenstore/basicfile.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index bbb9e1036..9ed70a5ec 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -2,6 +2,7 @@ #include "zenstore/basicfile.h" +#include <zencore/compactbinary.h> #include <zencore/except.h> #include <zencore/filesystem.h> #include <zencore/fmtutils.h> @@ -266,6 +267,53 @@ TemporaryFile::MoveTemporaryIntoPlace(std::filesystem::path FinalFileName, std:: std::filesystem::rename(m_TempPath, FinalFileName, Ec); } +////////////////////////////////////////////////////////////////////////// + +LockFile::LockFile() +{ +} + +LockFile::~LockFile() +{ +} + +void +LockFile::Create(std::filesystem::path FileName, CbObject Payload, std::error_code& Ec) +{ + Ec.clear(); + + const DWORD dwCreationDisposition = CREATE_ALWAYS; + DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE | DELETE; + const DWORD dwShareMode = FILE_SHARE_READ; + const DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE; + HANDLE hTemplateFile = nullptr; + + HANDLE FileHandle = CreateFile(FileName.c_str(), + dwDesiredAccess, + dwShareMode, + /* lpSecurityAttributes */ nullptr, + dwCreationDisposition, + dwFlagsAndAttributes, + hTemplateFile); + + if (FileHandle == INVALID_HANDLE_VALUE) + { + Ec = zen::MakeErrorCodeFromLastError(); + + return; + } + + m_FileHandle = FileHandle; + + BasicFile::Write(Payload.GetBuffer(), 0, Ec); +} + +void +LockFile::Update(CbObject Payload, std::error_code& Ec) +{ + BasicFile::Write(Payload.GetBuffer(), 0, Ec); +} + /* ___________ __ \__ ___/___ _______/ |_ ______ |