KiCad PCB EDA Suite
lockfile.cpp File Reference
#include <memory>
#include <build_version.h>
#include <lockfile.h>
#include <wx/filename.h>
#include <wx/snglinst.h>

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 ()
 

Function Documentation

◆ GetKicadLockFilePath()

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

Definition at line 61 of file lockfile.cpp.

62 {
63  wxFileName lockpath;
64  lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
65 
66 #if defined( __WXMAC__ )
67  // In OSX use the standard per user cache directory
68  lockpath.AppendDir( "Library" );
69  lockpath.AppendDir( "Caches" );
70  lockpath.AppendDir( "kicad" );
71 #elif defined( __UNIX__ )
72  wxString envstr;
73  // Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
74  if( wxGetEnv( "XDG_RUNTIME_DIR", &envstr ) && !envstr.IsEmpty() )
75  {
76  lockpath.AssignDir( envstr );
77  }
78  else if( wxGetEnv( "XDG_CACHE_HOME", &envstr ) && !envstr.IsEmpty() )
79  {
80  lockpath.AssignDir( envstr );
81  }
82  else
83  {
84  // If all fails, just use ~/.cache
85  lockpath.AppendDir( ".cache" );
86  }
87 
88  lockpath.AppendDir( wxString::Format( "kicad_v%s", GetMajorMinorVersion() ) );
89 #endif
90 
91 #if defined( __WXMAC__ ) || defined( __UNIX__ )
92  if( !lockpath.DirExists() )
93  {
94  // Lockfiles should be only readable by the user
95  lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
96  }
97 #endif
98  return lockpath.GetPath();
99 }
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,
51 
52  if( p->IsAnotherRunning() )
53  {
54  p = nullptr;
55  }
56 
57  return p;
58 }
wxString GetKicadLockFilePath()
Definition: lockfile.cpp:61

References GetKicadLockFilePath().

Referenced by SETTINGS_MANAGER::LoadProject().