KiCad PCB EDA Suite
lockfile.h File Reference
#include <wx/string.h>
#include <memory>

Go to the source code of this file.

Functions

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

Function Documentation

wxString GetKicadLockFilePath ( )

Function GetKicadLockFilePath.

Returns
A wxString containing the path for lockfiles in Kicad

Definition at line 59 of file lockfile.cpp.

Referenced by PGM_BASE::InitPgm(), and LockFile().

60 {
61  wxFileName lockpath;
62  lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
63 
64 #if defined( __WXMAC__ )
65  // In OSX use the standard per user cache directory
66  lockpath.AppendDir( "Library" );
67  lockpath.AppendDir( "Caches" );
68  lockpath.AppendDir( "kicad" );
69 #elif defined( __UNIX__ )
70  wxString envstr;
71  // Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
72  if( wxGetEnv( "XDG_RUNTIME_DIR", &envstr ) && !envstr.IsEmpty() )
73  {
74  lockpath.AssignDir( envstr );
75  }
76  else if( wxGetEnv( "XDG_CACHE_HOME", &envstr ) && !envstr.IsEmpty() )
77  {
78  lockpath.AssignDir( envstr );
79  }
80  else
81  {
82  // If all fails, just use ~/.cache
83  lockpath.AppendDir( ".cache" );
84  }
85 
86  lockpath.AppendDir( "kicad" );
87 #endif
88 
89 #if defined( __WXMAC__ ) || defined( __UNIX__ )
90  if( !lockpath.DirExists() )
91  {
92  // Lockfiles should be only readable by the user
93  lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
94  }
95 #endif
96  return lockpath.GetPath();
97 }
std::unique_ptr<wxSingleInstanceChecker> LockFile ( const wxString &  aFileName)

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

Definition at line 32 of file lockfile.cpp.

References GetKicadLockFilePath().

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