25#include <wx/filename.h>
56 LOCKFILE(
const wxString &filename,
bool aRemoveOnRelease =
true ) :
59 if( filename.IsEmpty() )
67 wxLogTrace(
traceLockFile,
"File is not writable: %s", filename );
78 wxLogTrace(
traceLockFile,
"Could not open a lock file for %s", filename );
100 wxLogTrace(
traceLockFile,
"Reclaimed the abandoned lock on %s", filename );
104 wxLogTrace(
traceLockFile,
"Existing Lock for %s", filename );
116 bool heldByAnother =
false;
118 if( !aFilename.IsEmpty() )
136 wxFileName fn( aFilename );
140 return fn.GetFullPath();
147 m_token( std::move( other.m_token ) ),
148 m_lock( std::move( other.m_lock ) ),
154 other.m_owned =
false;
245 std::random_device rd;
247 uint64_t high = (
static_cast<uint64_t
>( rd() ) << 32 ) | rd();
248 uint64_t low = (
static_cast<uint64_t
>( rd() ) << 32 ) | rd();
250 return wxString::Format( wxS(
"%016llx%016llx" ),
251 static_cast<unsigned long long>( high ),
252 static_cast<unsigned long long>( low ) );
262 j[
"username"] = std::string(
m_username.mb_str() );
263 j[
"hostname"] = std::string(
m_hostname.mb_str() );
264 j[
"token"] = std::string(
m_token.mb_str() );
266 if(
m_lock.Rewrite( j.dump() ) )
284 std::string contents;
286 if( !
m_lock.ReadAll( contents ) )
289 aRecord = nlohmann::json::parse( contents,
nullptr,
false );
291 if( aRecord.is_discarded() )
308 nlohmann::json record;
312 m_username = wxString( record.value(
"username", std::string() ) );
313 m_hostname = wxString( record.value(
"hostname", std::string() ) );
314 m_token = wxString( record.value(
"token", std::string() ) );
334 nlohmann::json record;
339 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.