29#ifndef INCLUDE__LOCK_FILE_H_
30#define INCLUDE__LOCK_FILE_H_
36#include <wx/filename.h>
40#define LCK "KICAD_LOCKING"
45 LOCKFILE(
const wxString &filename,
bool aRemoveOnRelease =
true ) :
49 if( filename.IsEmpty() )
52 wxLogTrace(
LCK,
"Trying to lock %s", filename );
53 wxFileName fn( filename );
57 if( !fn.IsDirWritable() )
59 wxLogTrace(
LCK,
"File is not writable: %s", filename );
70 bool lock_success =
false;
71 bool rw_success =
false;
74 wxLogNull suppressExpectedErrorMessages;
89 j[
"username"] = std::string(
m_username.mb_str() );
90 j[
"hostname"] = std::string(
m_hostname.mb_str() );
91 std::string lock_info = j.dump();
92 file.Write( lock_info );
94 wxLogTrace(
LCK,
"Locked %s", filename );
99 file.ReadAll( &lock_info );
106 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
107 m_username = wxString( j.at(
"username" ).get<std::string>() );
108 m_hostname = wxString( j.at(
"hostname" ).get<std::string>() );
110 catch(
const std::exception& parseError )
114 wxLogTrace(
LCK,
"Unreadable lock contents for %s: %s", filename,
119 wxLogTrace(
LCK,
"Existing Lock for %s", filename );
123 throw std::runtime_error(
"Failed to open lock file" );
126 catch( std::exception& e )
128 wxLogError(
"Got an error trying to lock %s: %s", filename, e.what() );
152 other.m_fileCreated =
false;
192 bool success =
false;
195 wxLogNull suppressExpectedErrorMessages;
204 j[
"username"] = std::string(
m_username.mb_str() );
205 j[
"hostname"] = std::string(
m_hostname.mb_str() );
206 std::string lock_info = j.dump();
207 file.Write( lock_info );
219 catch( std::exception& e )
221 wxLogError(
"Got exception trying to override lock on %s: %s",
273 explicit operator bool()
const
292 if( !fileName.FileExists() )
305 file.ReadAll( &lock_info );
306 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
308 if(
m_username == wxString( j[
"username"].get<std::string>() )
309 &&
m_hostname == wxString( j[
"hostname"].get<std::string>() ) )
316 catch( std::exception &e )
318 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.