25#ifndef INCLUDE__LOCK_FILE_H_
26#define INCLUDE__LOCK_FILE_H_
31#include <wx/filename.h>
62 LOCKFILE(
const wxString &filename,
bool aRemoveOnRelease =
true ) :
65 if( filename.IsEmpty() )
73 wxLogTrace(
traceLockFile,
"File is not writable: %s", filename );
84 wxLogTrace(
traceLockFile,
"Could not open a lock file for %s", filename );
106 wxLogTrace(
traceLockFile,
"Reclaimed the abandoned lock on %s", filename );
110 wxLogTrace(
traceLockFile,
"Existing Lock for %s", filename );
122 bool heldByAnother =
false;
124 if( !aFilename.IsEmpty() )
142 wxFileName fn( aFilename );
146 return fn.GetFullPath();
153 m_token( std::move( other.m_token ) ),
154 m_lock( std::move( other.m_lock ) ),
160 other.m_owned =
false;
257 std::random_device rd;
259 uint64_t high = (
static_cast<uint64_t
>( rd() ) << 32 ) | rd();
260 uint64_t low = (
static_cast<uint64_t
>( rd() ) << 32 ) | rd();
262 return wxString::Format( wxS(
"%016llx%016llx" ),
263 static_cast<unsigned long long>( high ),
264 static_cast<unsigned long long>( low ) );
274 j[
"username"] = std::string(
m_username.mb_str() );
275 j[
"hostname"] = std::string(
m_hostname.mb_str() );
276 j[
"token"] = std::string(
m_token.mb_str() );
278 if(
m_lock.Rewrite( j.dump() ) )
296 std::string contents;
298 if( !
m_lock.ReadAll( contents ) )
301 aRecord = nlohmann::json::parse( contents,
nullptr,
false );
303 if( aRecord.is_discarded() )
314 nlohmann::json record;
318 m_username = wxString( record.value(
"username", std::string() ) );
319 m_hostname = wxString( record.value(
"hostname", std::string() ) );
320 m_token = wxString( record.value(
"token", std::string() ) );
336 nlohmann::json record;
341 if(
m_token == wxString( record.value(
"token", std::string() ) ) )
LOCKFILE(const wxString &filename, bool aRemoveOnRelease=true)
static wxString LockPathFor(const wxString &aFilename)
LOCKFILE(LOCKFILE &&other) noexcept
KIPLATFORM::IO::FILE_LOCK m_lock
bool OverrideLock(bool aRemoveOnRelease=true)
Force the lock, overwriting the data that existed already.
static wxString newToken()
bool readRecord(nlohmann::json &aRecord) const
static LOCKFILE Inspect(const wxString &aFilename)
Look at a lock without taking it: nothing is created, nothing is claimed and nothing is removed on re...
void UnlockFile()
Unlock and remove the file from the filesystem as long as we still own it.
static const std::string LockFileExtension
static const std::string LockFilePrefix
static const wxChar * traceLockFile
Flag to enable project lock file debug tracing.
Definition of file extensions used in Kicad.