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

Detailed Description

File locking utilities.

Definition in file lockfile.h.

Function Documentation

◆ GetKicadLockFilePath()

wxString GetKicadLockFilePath ( )

Function GetKicadLockFilePath.

Returns
A wxString containing the path for lockfiles in Kicad

Definition at line 60 of file lockfile.cpp.

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

References Format(), and GetMajorMinorVersion().

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

◆ LockFile()

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 33 of file lockfile.cpp.

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

References GetKicadLockFilePath().