25#ifndef INCLUDE__LOCK_FILE_H_
26#define INCLUDE__LOCK_FILE_H_
32#include <wx/filename.h>
36#define LCK "KICAD_LOCKING"
41 LOCKFILE(
const wxString &filename,
bool aRemoveOnRelease =
true ) :
45 if( filename.IsEmpty() )
48 wxLogTrace(
LCK,
"Trying to lock %s", filename );
49 wxFileName fn( filename );
53 if( !fn.IsDirWritable() )
55 wxLogTrace(
LCK,
"File is not writable: %s", filename );
66 bool lock_success =
false;
67 bool rw_success =
false;
70 wxLogNull suppressExpectedErrorMessages;
85 j[
"username"] = std::string(
m_username.mb_str() );
86 j[
"hostname"] = std::string(
m_hostname.mb_str() );
87 std::string lock_info = j.dump();
88 file.Write( lock_info );
90 wxLogTrace(
LCK,
"Locked %s", filename );
95 file.ReadAll( &lock_info );
102 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
103 m_username = wxString( j.at(
"username" ).get<std::string>() );
104 m_hostname = wxString( j.at(
"hostname" ).get<std::string>() );
106 catch(
const std::exception& parseError )
110 wxLogTrace(
LCK,
"Unreadable lock contents for %s: %s", filename,
115 wxLogTrace(
LCK,
"Existing Lock for %s", filename );
119 throw std::runtime_error(
"Failed to open lock file" );
122 catch( std::exception& e )
124 wxLogError(
"Got an error trying to lock %s: %s", filename, e.what() );
148 other.m_fileCreated =
false;
188 bool success =
false;
191 wxLogNull suppressExpectedErrorMessages;
200 j[
"username"] = std::string(
m_username.mb_str() );
201 j[
"hostname"] = std::string(
m_hostname.mb_str() );
202 std::string lock_info = j.dump();
203 file.Write( lock_info );
215 catch( std::exception& e )
217 wxLogError(
"Got exception trying to override lock on %s: %s",
269 explicit operator bool()
const
288 if( !fileName.FileExists() )
301 file.ReadAll( &lock_info );
302 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
304 if(
m_username == wxString( j[
"username"].get<std::string>() )
305 &&
m_hostname == wxString( j[
"hostname"].get<std::string>() ) )
312 catch( std::exception &e )
314 wxLogError(
"Got exception trying to check user/host for lock on %s: %s",
LOCKFILE(const wxString &filename, bool aRemoveOnRelease=true)
LOCKFILE(LOCKFILE &&other) noexcept
bool OverrideLock(bool aRemoveOnRelease=true)
Force the lock, overwriting the data that existed already.
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
Definition of file extensions used in Kicad.