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 <[email protected]>
5 * Copyright (C) 2014-2022 KiCad Developers, see AUTHORS.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 <memory>
26
27#include <build_version.h>
28#include <lockfile.h>
29
30#include <wx/filename.h>
31#include <wx/snglinst.h>
32
33
34std::unique_ptr<wxSingleInstanceChecker> LockFile( const wxString& aFileName )
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, GetKicadLockFilePath() );
50
51 if( p->IsAnotherRunning() )
52 p = nullptr;
53
54 return p;
55}
56
57
59{
60 wxFileName lockpath;
61 lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
62
63#if defined( __WXMAC__ )
64 // In OSX use the standard per user cache directory
65 lockpath.AppendDir( "Library" );
66 lockpath.AppendDir( "Caches" );
67 lockpath.AppendDir( "kicad" );
68#elif defined( __UNIX__ )
69 wxString envstr;
70 // Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
71 if( wxGetEnv( "XDG_RUNTIME_DIR", &envstr ) && !envstr.IsEmpty() )
72 {
73 lockpath.AssignDir( envstr );
74 }
75 else if( wxGetEnv( "XDG_CACHE_HOME", &envstr ) && !envstr.IsEmpty() )
76 {
77 lockpath.AssignDir( envstr );
78 }
79 else
80 {
81 // If all fails, just use ~/.cache
82 lockpath.AppendDir( ".cache" );
83 }
84
85 lockpath.AppendDir( wxString::Format( "kicad_v%s", GetMajorMinorVersion() ) );
86#endif
87
88#if defined( __WXMAC__ ) || defined( __UNIX__ )
89 if( !lockpath.DirExists() )
90 {
91 // Lockfiles should be only readable by the user
92 lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
93 }
94#endif
95 return lockpath.GetPath();
96}
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:34
wxString GetKicadLockFilePath()
Definition: lockfile.cpp:58
File locking utilities.
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