KiCad PCB EDA Suite
Loading...
Searching...
No Matches
history_lock.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 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 3
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
20#include <history_lock.h>
21#include <lockfile.h>
22#include <advanced_config.h>
23#include <pgm_base.h>
25#include <wx/filename.h>
26#include <wx/filefn.h>
27#include <wx/ffile.h>
28#include <wx/log.h>
29#include <wx/datetime.h>
30
31#include <git2.h>
32
33#define HISTORY_LOCK_TRACE wxT( "KICAD_HISTORY_LOCK" )
34
35
36static wxString historyPath( const wxString& aProjectPath )
37{
38 return Pgm().GetSettingsManager().GetLocalHistoryDirForPath( aProjectPath );
39}
40
41
42HISTORY_LOCK_MANAGER::HISTORY_LOCK_MANAGER( const wxString& aProjectPath, int aStaleTimeoutSec ) :
43 m_projectPath( aProjectPath ),
44 m_historyPath( historyPath( aProjectPath ) ),
45 m_repo( nullptr ),
46 m_index( nullptr ),
47 m_repoOwned( false ),
48 m_indexOwned( false ),
49 m_staleTimeoutSec( aStaleTimeoutSec <= 0 ? ADVANCED_CFG::GetCfg().m_HistoryLockStaleTimeout : aStaleTimeoutSec )
50{
51 wxLogTrace( HISTORY_LOCK_TRACE, "Attempting to acquire lock for project: %s (timeout: %d sec)",
52 aProjectPath, m_staleTimeoutSec );
53
54 // Layer 1: Acquire file-based lock to prevent cross-instance conflicts
55 if( !acquireFileLock() )
56 {
57 wxLogTrace( HISTORY_LOCK_TRACE, "Failed to acquire file lock: %s", m_lockError );
58 return;
59 }
60
61 // Layer 2: Open git repository
62 if( !openRepository() )
63 {
64 wxLogTrace( HISTORY_LOCK_TRACE, "Failed to open repository: %s", m_lockError );
65 return;
66 }
67
68 // Layer 3: Acquire git index lock for fine-grained operation safety
69 if( !acquireIndexLock() )
70 {
71 wxLogTrace( HISTORY_LOCK_TRACE, "Failed to acquire index lock: %s", m_lockError );
72 return;
73 }
74
75 wxLogTrace( HISTORY_LOCK_TRACE, "Successfully acquired all locks for project: %s", aProjectPath );
76}
77
78
80{
81 wxLogTrace( HISTORY_LOCK_TRACE, "Releasing locks for project: %s", m_projectPath );
82
83 // Release in reverse order of acquisition
84 if( m_indexOwned && m_index )
85 {
86 git_index_free( m_index );
87 m_index = nullptr;
88 }
89
90 if( m_repoOwned && m_repo )
91 {
92 git_repository_free( m_repo );
93 m_repo = nullptr;
94 }
95
96 // m_fileLock automatically releases via RAII destructor
97}
98
99
101{
102 return m_fileLock && m_fileLock->Locked() && m_repo && m_index;
103}
104
105
107{
108 return m_lockError;
109}
110
111
113{
114 if( m_fileLock && !m_fileLock->Locked() )
115 {
116 return wxString::Format( wxS( "%s@%s" ),
117 m_fileLock->GetUsername(),
118 m_fileLock->GetHostname() );
119 }
120 return wxEmptyString;
121}
122
123
125{
126 // Ensure history directory exists
127 if( !wxDirExists( m_historyPath ) )
128 {
129 if( !wxFileName::Mkdir( m_historyPath, 0777, wxPATH_MKDIR_FULL ) )
130 {
131 m_lockError = wxString::Format(
132 _( "Cannot create history directory: %s" ), m_historyPath );
133 return false;
134 }
135 }
136
137 // Check for stale lock before attempting acquisition
139 {
140 wxLogWarning( "Detected stale lock file, removing it" );
142 }
143
144 // Create lock file path: .history/.repo.lock
145 wxFileName lockPath( m_historyPath, wxS( ".repo" ) );
146
147 // Use existing LOCKFILE infrastructure
148 m_fileLock = std::make_unique<LOCKFILE>( lockPath.GetFullPath(), true );
149
150 if( !m_fileLock->Locked() )
151 {
152 m_lockError = wxString::Format(
153 _( "History repository is locked by %s@%s" ),
154 m_fileLock->GetUsername(),
155 m_fileLock->GetHostname() );
156 return false;
157 }
158
159 return true;
160}
161
162
164{
165 if( !wxDirExists( m_historyPath ) )
166 {
167 m_lockError = wxString::Format(
168 _( "History directory does not exist: %s" ), m_historyPath );
169 return false;
170 }
171
172 // Attempt to open existing repository
173 int rc = git_repository_open( &m_repo, m_historyPath.mb_str().data() );
174
175 if( rc != 0 )
176 {
177 const git_error* err = git_error_last();
178 m_lockError = wxString::Format(
179 _( "Failed to open git repository: %s" ),
180 err ? wxString::FromUTF8( err->message ) : wxString( "Unknown error" ) );
181 return false;
182 }
183
184 // Attempt to set workdir
185 rc = git_repository_set_workdir( m_repo, m_projectPath.mb_str().data(), false );
186
187 if( rc != 0 )
188 {
189 const git_error* err = git_error_last();
190 m_lockError = wxString::Format( _( "Failed to set git repository workdir to %s: %s" ), m_projectPath,
191 err ? wxString::FromUTF8( err->message ) : wxString( "Unknown error" ) );
192 return false;
193 }
194
195 // libgit2 will not read .history/.gitignore on its own (it sits outside the workdir).
196 // Inject its rules so users can edit that file and have them honoured.
197 wxFileName ignoreFile( m_historyPath, wxS( ".gitignore" ) );
198
199 if( ignoreFile.FileExists() )
200 {
201 wxFFile f( ignoreFile.GetFullPath(), wxT( "rb" ) );
202 wxString content;
203
204 if( f.IsOpened() && f.ReadAll( &content ) )
205 git_ignore_add_rule( m_repo, content.utf8_str() );
206 }
207
208 m_repoOwned = true;
209 return true;
210}
211
212
214{
215 if( !m_repo )
216 {
217 m_lockError = _( "Cannot acquire index lock: repository not open" );
218 return false;
219 }
220
221 // git_repository_index will fail if another process has .git/index.lock
222 int rc = git_repository_index( &m_index, m_repo );
223
224 if( rc != 0 )
225 {
226 const git_error* err = git_error_last();
227 m_lockError = wxString::Format(
228 _( "Failed to acquire git index lock (another operation in progress?): %s" ),
229 err ? wxString::FromUTF8( err->message ) : wxString( "Unknown error" ) );
230 return false;
231 }
232
233 m_indexOwned = true;
234 return true;
235}
236
237
238bool HISTORY_LOCK_MANAGER::IsLockStale( const wxString& aProjectPath, int aStaleTimeoutSec )
239{
240 // Use config value if not explicitly specified
241 if( aStaleTimeoutSec <= 0 )
243
244 wxString histPath = historyPath( aProjectPath );
245 wxFileName lockPath( histPath, wxS( ".repo.lock" ) );
246
247 if( !lockPath.FileExists() )
248 return false; // No lock file exists
249
250 wxDateTime modTime;
251 if( !lockPath.GetTimes( nullptr, &modTime, nullptr ) )
252 return false; // Can't determine age
253
254 wxDateTime now = wxDateTime::Now();
255 wxTimeSpan age = now - modTime;
256
257 wxLogTrace( HISTORY_LOCK_TRACE, "Lock file age: %d seconds (stale threshold: %d)",
258 (int)age.GetSeconds().ToLong(), aStaleTimeoutSec );
259
260 return age.GetSeconds().ToLong() > aStaleTimeoutSec;
261}
262
263
264bool HISTORY_LOCK_MANAGER::BreakStaleLock( const wxString& aProjectPath )
265{
266 wxString histPath = historyPath( aProjectPath );
267 wxFileName lockPath( histPath, wxS( ".repo.lock" ) );
268
269 if( !lockPath.FileExists() )
270 return true; // Already removed
271
272 // Also remove the LOCKFILE-style lock file if it exists
273 wxFileName lockFilePath( histPath, wxS( ".repo" ) );
274 lockFilePath.SetName( FILEEXT::LockFilePrefix + lockFilePath.GetName() );
275 lockFilePath.SetExt( lockFilePath.GetExt() + wxS( "." ) + FILEEXT::LockFileExtension );
276
277 bool result = true;
278
279 if( lockPath.FileExists() )
280 {
281 result = wxRemoveFile( lockPath.GetFullPath() );
282 if( result )
283 wxLogTrace( HISTORY_LOCK_TRACE, "Removed stale lock: %s", lockPath.GetFullPath() );
284 else
285 wxLogError( "Failed to remove stale lock: %s", lockPath.GetFullPath() );
286 }
287
288 if( lockFilePath.FileExists() )
289 {
290 bool lockFileResult = wxRemoveFile( lockFilePath.GetFullPath() );
291 if( lockFileResult )
292 wxLogTrace( HISTORY_LOCK_TRACE, "Removed stale lockfile: %s", lockFilePath.GetFullPath() );
293 result = result && lockFileResult;
294 }
295
296 return result;
297}
298
299
301{
302 if( m_indexOwned && m_index )
303 {
304 git_index_free( m_index );
305 m_index = nullptr;
306 m_indexOwned = false;
307 }
308
309 if( m_repoOwned && m_repo )
310 {
311 git_repository_free( m_repo );
312 m_repo = nullptr;
313 m_repoOwned = false;
314 }
315}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
wxString GetLockHolder() const
Get information about who currently holds the lock.
HISTORY_LOCK_MANAGER(const wxString &aProjectPath, int aStaleTimeoutSec=0)
Construct a lock manager and attempt to acquire locks.
void ReleaseRepository()
Release git repository and index handles early, but keep the file lock.
~HISTORY_LOCK_MANAGER()
Destructor releases all locks and closes git repository.
wxString GetLockError() const
Get error message describing why lock could not be acquired.
git_repository * m_repo
static bool BreakStaleLock(const wxString &aProjectPath)
Forcibly remove a stale lock file.
std::unique_ptr< LOCKFILE > m_fileLock
static bool IsLockStale(const wxString &aProjectPath, int aStaleTimeoutSec=0)
Check if a lock file exists and is stale (older than timeout).
bool IsLocked() const
Check if locks were successfully acquired.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
wxString GetLocalHistoryDirForPath(const wxString &aProjectPath) const
Resolve the local-history directory for a project given by its on-disk path.
#define _(s)
int m_HistoryLockStaleTimeout
Stale lock timeout for local history repository locks, in seconds.
static const std::string LockFileExtension
static const std::string LockFilePrefix
static wxString historyPath(const wxString &aProjectPath)
#define HISTORY_LOCK_TRACE
File locking utilities.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxString result
Test unit parsing edge cases and error handling.