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 <lockfile.h>
26 
27 #include <wx/filename.h>
28 #include <wx/snglinst.h>
29 
30 #include <common.h>
31 
32 std::unique_ptr<wxSingleInstanceChecker> LockFile( const wxString& aFileName )
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 }
57 
58 
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 }
wxString GetKicadLockFilePath()
Function GetKicadLockFilePath.
Definition: lockfile.cpp:59
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 retur...
Definition: lockfile.cpp:32
The common library.
File locking utilities.