KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lockfile.h
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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
24
25#ifndef INCLUDE__LOCK_FILE_H_
26#define INCLUDE__LOCK_FILE_H_
27
28#include <wx/wx.h>
29#include <wx/file.h>
30#include <wx/filefn.h>
31#include <wx/log.h>
32#include <wx/filename.h>
33#include <json_common.h>
35
36#define LCK "KICAD_LOCKING"
37
39{
40public:
41 LOCKFILE( const wxString &filename, bool aRemoveOnRelease = true ) :
42 m_originalFile( filename ), m_fileCreated( false ), m_status( false ),
43 m_removeOnRelease( aRemoveOnRelease ), m_errorMsg( "" )
44 {
45 if( filename.IsEmpty() )
46 return;
47
48 wxLogTrace( LCK, "Trying to lock %s", filename );
49 wxFileName fn( filename );
50 fn.SetName( FILEEXT::LockFilePrefix + fn.GetName() );
51 fn.SetExt( fn.GetExt() + '.' + FILEEXT::LockFileExtension );
52
53 if( !fn.IsDirWritable() )
54 {
55 wxLogTrace( LCK, "File is not writable: %s", filename );
56 m_status = true;
57 m_removeOnRelease = false;
58 return;
59 }
60
61 m_lockFilename = fn.GetFullPath();
62
63 wxFile file;
64 try
65 {
66 bool lock_success = false;
67 bool rw_success = false;
68
69 {
70 wxLogNull suppressExpectedErrorMessages;
71
72 lock_success = file.Open( m_lockFilename, wxFile::write_excl );
73
74 if( !lock_success )
75 rw_success = file.Open( m_lockFilename, wxFile::read );
76 }
77
78 if( lock_success )
79 {
80 m_fileCreated = true;
81 m_status = true;
82 m_username = wxGetUserId();
83 m_hostname = wxGetHostName();
84 nlohmann::json j;
85 j["username"] = std::string( m_username.mb_str() );
86 j["hostname"] = std::string( m_hostname.mb_str() );
87 std::string lock_info = j.dump();
88 file.Write( lock_info );
89 file.Close();
90 wxLogTrace( LCK, "Locked %s", filename );
91 }
92 else if( rw_success )
93 {
94 wxString lock_info;
95 file.ReadAll( &lock_info );
96 file.Close();
97
98 // Cloud-synced drives can present a lock file before its contents finish syncing.
99 // Treat an empty or corrupt file as unknown-owner stale rather than a hard error.
100 try
101 {
102 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
103 m_username = wxString( j.at( "username" ).get<std::string>() );
104 m_hostname = wxString( j.at( "hostname" ).get<std::string>() );
105 }
106 catch( const std::exception& parseError )
107 {
108 m_username = wxEmptyString;
109 m_hostname = wxEmptyString;
110 wxLogTrace( LCK, "Unreadable lock contents for %s: %s", filename,
111 parseError.what() );
112 }
113
114 m_errorMsg = _( "Lock file already exists" );
115 wxLogTrace( LCK, "Existing Lock for %s", filename );
116 }
117 else
118 {
119 throw std::runtime_error( "Failed to open lock file" );
120 }
121 }
122 catch( std::exception& e )
123 {
124 wxLogError( "Got an error trying to lock %s: %s", filename, e.what() );
125
126 if( m_fileCreated )
127 {
128 wxRemoveFile( m_lockFilename );
129 m_fileCreated = false;
130 }
131
132 m_errorMsg = _( "Failed to access lock file" );
133 m_status = false;
134 }
135 }
136
137 LOCKFILE( LOCKFILE&& other ) noexcept :
138 m_originalFile( std::move( other.m_originalFile ) ),
139 m_lockFilename( std::move( other.m_lockFilename ) ),
140 m_username( std::move( other.m_username ) ),
141 m_hostname( std::move( other.m_hostname ) ),
142 m_fileCreated( other.m_fileCreated ),
143 m_status( other.m_status ),
144 m_removeOnRelease( other.m_removeOnRelease ),
145 m_errorMsg( std::move( other.m_errorMsg ) )
146 {
147 // Disable unlock in the moved-from object
148 other.m_fileCreated = false;
149 }
150
152 {
153 UnlockFile();
154 }
155
160 {
161 wxLogTrace( LCK, "Unlocking %s", m_lockFilename );
162
164 {
166 wxRemoveFile( m_lockFilename );
167
168 m_fileCreated = false;
169 m_status = false;
170 m_errorMsg = wxEmptyString;
171 }
172 }
173
179 bool OverrideLock( bool aRemoveOnRelease = true )
180 {
181 wxLogTrace( LCK, "Overriding lock on %s", m_lockFilename );
182
183 if( !m_fileCreated )
184 {
185 try
186 {
187 wxFile file;
188 bool success = false;
189
190 {
191 wxLogNull suppressExpectedErrorMessages;
192 success = file.Open( m_lockFilename, wxFile::write );
193 }
194
195 if( success )
196 {
197 m_username = wxGetUserId();
198 m_hostname = wxGetHostName();
199 nlohmann::json j;
200 j["username"] = std::string( m_username.mb_str() );
201 j["hostname"] = std::string( m_hostname.mb_str() );
202 std::string lock_info = j.dump();
203 file.Write( lock_info );
204 file.Close();
205 m_fileCreated = true;
206 m_status = true;
207 m_removeOnRelease = aRemoveOnRelease;
208 m_errorMsg = wxEmptyString;
209 wxLogTrace( LCK, "Successfully overrode lock on %s", m_lockFilename );
210 return true;
211 }
212
213 return false;
214 }
215 catch( std::exception& e )
216 {
217 wxLogError( "Got exception trying to override lock on %s: %s",
218 m_lockFilename, e.what() );
219
220 return false;
221 }
222 }
223 else
224 {
225 wxLogTrace( LCK, "Upgraded lock on %s to delete on release", m_lockFilename );
226 m_removeOnRelease = aRemoveOnRelease;
227 }
228
229 return true;
230 }
231
233 {
234 // Empty owner means the lock file could not be parsed (e.g. partial cloud sync).
235 // We cannot prove another user owns it, so treat it as reclaimable.
236 if( m_username.IsEmpty() && m_hostname.IsEmpty() )
237 return true;
238
239 return m_username == wxGetUserId() && m_hostname == wxGetHostName();
240 }
241
246 wxString GetUsername(){ return m_username; }
247
252 wxString GetHostname(){ return m_hostname; }
253
257 wxString GetErrorMsg(){ return m_errorMsg; }
258
259 bool Locked() const
260 {
261 return m_fileCreated;
262 }
263
264 bool Valid() const
265 {
266 return m_status;
267 }
268
269 explicit operator bool() const
270 {
271 return m_status;
272 }
273
274private:
277 wxString m_username;
278 wxString m_hostname;
282 wxString m_errorMsg;
283
285 {
286 wxFileName fileName( m_lockFilename );
287
288 if( !fileName.FileExists() )
289 {
290 wxLogTrace( LCK, "File does not exist: %s", m_lockFilename );
291 return false;
292 }
293
294 wxFile file;
295
296 try
297 {
298 if( file.Open( m_lockFilename, wxFile::read ) )
299 {
300 wxString lock_info;
301 file.ReadAll( &lock_info );
302 nlohmann::json j = nlohmann::json::parse( std::string( lock_info.mb_str() ) );
303
304 if( m_username == wxString( j["username"].get<std::string>() )
305 && m_hostname == wxString( j["hostname"].get<std::string>() ) )
306 {
307 wxLogTrace( LCK, "User and host match for lock %s", m_lockFilename );
308 return true;
309 }
310 }
311 }
312 catch( std::exception &e )
313 {
314 wxLogError( "Got exception trying to check user/host for lock on %s: %s",
316 e.what() );
317 }
318
319 wxLogTrace( LCK, "User and host DID NOT match for lock %s", m_lockFilename );
320 return false;
321 }
322};
323
324
325#endif // INCLUDE__LOCK_FILE_H_
bool m_removeOnRelease
Definition lockfile.h:281
LOCKFILE(const wxString &filename, bool aRemoveOnRelease=true)
Definition lockfile.h:41
bool m_fileCreated
Definition lockfile.h:279
LOCKFILE(LOCKFILE &&other) noexcept
Definition lockfile.h:137
wxString m_errorMsg
Definition lockfile.h:282
bool m_status
Definition lockfile.h:280
wxString m_lockFilename
Definition lockfile.h:276
bool OverrideLock(bool aRemoveOnRelease=true)
Force the lock, overwriting the data that existed already.
Definition lockfile.h:179
bool Valid() const
Definition lockfile.h:264
~LOCKFILE()
Definition lockfile.h:151
wxString m_originalFile
Definition lockfile.h:275
wxString m_hostname
Definition lockfile.h:278
wxString GetUsername()
Definition lockfile.h:246
wxString GetErrorMsg()
Definition lockfile.h:257
wxString GetHostname()
Definition lockfile.h:252
bool checkUserAndHost()
Definition lockfile.h:284
bool Locked() const
Definition lockfile.h:259
wxString m_username
Definition lockfile.h:277
bool IsLockedByMe()
Definition lockfile.h:232
void UnlockFile()
Unlock and remove the file from the filesystem as long as we still own it.
Definition lockfile.h:159
#define _(s)
static const std::string LockFileExtension
static const std::string LockFilePrefix
#define LCK
Definition lockfile.h:36
Definition of file extensions used in Kicad.