29#ifndef INCLUDE__LOCK_FILE_H_
30#define INCLUDE__LOCK_FILE_H_
36#include <wx/filename.h>
37#include <nlohmann/json.hpp>
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;
90 j[
"username"] = std::string(
m_username.mb_str() );
91 j[
"hostname"] = std::string(
m_hostname.mb_str() );
92 std::string lock_info = j.dump();
93 file.Write( lock_info );
95 wxLogTrace(
LCK,
"Locked %s", filename );
101 file.ReadAll( &lock_info );
102 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
103 m_username = wxString( j[
"username"].get<std::string>() );
104 m_hostname = wxString( j[
"hostname"].get<std::string>() );
107 wxLogTrace(
LCK,
"Existing Lock for %s", filename );
111 throw std::runtime_error(
"Failed to open lock file" );
114 catch( std::exception& e )
116 wxLogError(
"Got an error trying to lock %s: %s", filename, e.what() );
169 bool success =
false;
172 wxLogNull suppressExpectedErrorMessages;
181 j[
"username"] = std::string(
m_username.mb_str() );
182 j[
"hostname"] = std::string(
m_hostname.mb_str() );
183 std::string lock_info = j.dump();
184 file.Write( lock_info );
196 catch( std::exception& e )
198 wxLogError(
"Got exception trying to override lock on %s: %s",
245 explicit operator bool()
const
264 if( !fileName.FileExists() )
277 file.ReadAll( &lock_info );
278 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
280 if(
m_username == wxString( j[
"username"].get<std::string>() )
281 &&
m_hostname == wxString( j[
"hostname"].get<std::string>() ) )
288 catch( std::exception &e )
290 wxLogError(
"Got exception trying to check user/host for lock on %s: %s",
LOCKFILE(const wxString &filename, bool aRemoveOnRelease=true)
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.