KiCad PCB EDA Suite
lockfile.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014-2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2014-2017 KiCad Developers, see CHANGELOG.TXT for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <build_version.h>
26 #include <lockfile.h>
27 
28 #include <wx/filename.h>
29 #include <wx/snglinst.h>
30 
31 #include <common.h>
32 
33 std::unique_ptr<wxSingleInstanceChecker> LockFile( const wxString& aFileName )
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 }
58 
59 
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 GetKicadLockFilePath()
Definition: lockfile.cpp:60
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
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 wxSingleInstan...
Definition: lockfile.cpp:33
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
The common library.
File locking utilities.