KiCad PCB EDA Suite
lockfile.h File Reference

File locking utilities. More...

#include <wx/string.h>
#include <memory>

Go to the source code of this file.

Functions

std::unique_ptr< wxSingleInstanceChecker > LockFile (const wxString &aFileName)
 Test to see if aFileName can be locked (is not already locked) and only then returns a wxSingleInstanceChecker protecting aFileName. More...
 
wxString GetKicadLockFilePath ()
 

Detailed Description

File locking utilities.

Definition in file lockfile.h.

Function Documentation

◆ GetKicadLockFilePath()

wxString GetKicadLockFilePath ( )
Returns
A wxString containing the path for lockfiles in Kicad.

Definition at line 58 of file lockfile.cpp.

59{
60 wxFileName lockpath;
61 lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
62
63#if defined( __WXMAC__ )
64 // In OSX use the standard per user cache directory
65 lockpath.AppendDir( "Library" );
66 lockpath.AppendDir( "Caches" );
67 lockpath.AppendDir( "kicad" );
68#elif defined( __UNIX__ )
69 wxString envstr;
70 // Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
71 if( wxGetEnv( "XDG_RUNTIME_DIR", &envstr ) && !envstr.IsEmpty() )
72 {
73 lockpath.AssignDir( envstr );
74 }
75 else if( wxGetEnv( "XDG_CACHE_HOME", &envstr ) && !envstr.IsEmpty() )
76 {
77 lockpath.AssignDir( envstr );
78 }
79 else
80 {
81 // If all fails, just use ~/.cache
82 lockpath.AppendDir( ".cache" );
83 }
84
85 lockpath.AppendDir( wxString::Format( "kicad_v%s", GetMajorMinorVersion() ) );
86#endif
87
88#if defined( __WXMAC__ ) || defined( __UNIX__ )
89 if( !lockpath.DirExists() )
90 {
91 // Lockfiles should be only readable by the user
92 lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
93 }
94#endif
95 return lockpath.GetPath();
96}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References Format(), and GetMajorMinorVersion().

Referenced by LockFile().

◆ LockFile()

std::unique_ptr< wxSingleInstanceChecker > LockFile ( const wxString &  aFileName)

Test to see if aFileName can be locked (is not already locked) and only then returns a wxSingleInstanceChecker protecting aFileName.

Definition at line 34 of file lockfile.cpp.

35{
36 // first make absolute and normalize, to avoid that different lock files
37 // for the same file can be created
38 wxFileName fn( aFileName );
39
40 fn.MakeAbsolute();
41
42 wxString lockFileName = fn.GetFullPath() + ".lock";
43
44 lockFileName.Replace( "/", "_" );
45
46 // We can have filenames coming from Windows, so also convert Windows separator
47 lockFileName.Replace( "\\", "_" );
48
49 auto p = std::make_unique<wxSingleInstanceChecker>( lockFileName, GetKicadLockFilePath() );
50
51 if( p->IsAnotherRunning() )
52 p = nullptr;
53
54 return p;
55}
wxString GetKicadLockFilePath()
Definition: lockfile.cpp:58

References GetKicadLockFilePath().

Referenced by SETTINGS_MANAGER::LoadProject().