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;
 
   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() );
 
 
  141        other.m_fileCreated = 
false;
 
 
  183                bool success = 
false;
 
  186                    wxLogNull suppressExpectedErrorMessages;
 
  195                    j[
"username"] = std::string( 
m_username.mb_str() );
 
  196                    j[
"hostname"] = std::string( 
m_hostname.mb_str() );
 
  197                    std::string lock_info = j.dump();
 
  198                    file.Write( lock_info );
 
  210            catch( std::exception& e )
 
  212                wxLogError( 
"Got exception trying to override lock on %s: %s",
 
 
  259    explicit operator bool()
 const 
 
  278        if( !fileName.FileExists() )
 
  291                file.ReadAll( &lock_info );
 
  292                nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
 
  294                if( 
m_username == wxString( j[
"username"].get<std::string>() )
 
  295                        && 
m_hostname == wxString( j[
"hostname"].get<std::string>() ) )
 
  302        catch( std::exception &e )
 
  304            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.