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() );
167 bool success =
false;
170 wxLogNull suppressExpectedErrorMessages;
179 j[
"username"] = std::string(
m_username.mb_str() );
180 j[
"hostname"] = std::string(
m_hostname.mb_str() );
181 std::string lock_info = j.dump();
182 file.Write( lock_info );
194 catch( std::exception& e )
196 wxLogError(
"Got exception trying to override lock on %s: %s",
241 explicit operator bool()
const
260 if( !fileName.FileExists() )
272 file.ReadAll( &lock_info );
273 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
275 if(
m_username == wxString( j[
"username"].get<std::string>() )
276 &&
m_hostname == wxString( j[
"hostname"].get<std::string>() ) )
283 catch( std::exception &e )
285 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)
Forces the lock, overwriting the data that existed already.
void UnlockFile()
Unlocks and removes 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.