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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <history_lock.h>
25#include <lockfile.h>
26#include <advanced_config.h>
27#include <wx/filename.h>
28#include <wx/filefn.h>
29#include <wx/log.h>
30#include <wx/datetime.h>
31
32#include <git2.h>
33
34#define HISTORY_LOCK_TRACE wxT( "KICAD_HISTORY_LOCK" )
35
36
37static wxString historyPath( const wxString& aProjectPath )
38{
39 wxFileName p( aProjectPath, wxEmptyString );
40 p.AppendDir( wxS( ".history" ) );
41 return p.GetPath();
42}
43
44
45HISTORY_LOCK_MANAGER::HISTORY_LOCK_MANAGER( const wxString& aProjectPath, int aStaleTimeoutSec ) :
46 m_projectPath( aProjectPath ),
47 m_historyPath( historyPath( aProjectPath ) ),
48 m_repo( nullptr ),
49 m_index( nullptr ),
50 m_repoOwned( false ),
51 m_indexOwned( false ),
52 m_staleTimeoutSec( aStaleTimeoutSec <= 0 ? ADVANCED_CFG::GetCfg().m_HistoryLockStaleTimeout : aStaleTimeoutSec )
53{
54 wxLogTrace( HISTORY_LOCK_TRACE, "Attempting to acquire lock for project: %s (timeout: %d sec)",
55 aProjectPath, m_staleTimeoutSec );
56
57 // Layer 1: Acquire file-based lock to prevent cross-instance conflicts
58 if( !acquireFileLock() )
59 {
60 wxLogTrace( HISTORY_LOCK_TRACE, "Failed to acquire file lock: %s", m_lockError );
61 return;
62 }
63
64 // Layer 2: Open git repository
65 if( !openRepository() )
66 {
67 wxLogTrace( HISTORY_LOCK_TRACE, "Failed to open repository: %s", m_lockError );
68 return;
69 }
70
71 // Layer 3: Acquire git index lock for fine-grained operation safety
72 if( !acquireIndexLock() )
73 {
74 wxLogTrace( HISTORY_LOCK_TRACE, "Failed to acquire index lock: %s", m_lockError );
75 return;
76 }
77
78 wxLogTrace( HISTORY_LOCK_TRACE, "Successfully acquired all locks for project: %s", aProjectPath );
79}
80
81
83{
84 wxLogTrace( HISTORY_LOCK_TRACE, "Releasing locks for project: %s", m_projectPath );
85
86 // Release in reverse order of acquisition
87 if( m_indexOwned && m_index )
88 {
89 git_index_free( m_index );
90 m_index = nullptr;
91 }
92
93 if( m_repoOwned && m_repo )
94 {
95 git_repository_free( m_repo );
96 m_repo = nullptr;
97 }
98
99 // m_fileLock automatically releases via RAII destructor
100}
101
102
104{
105 return m_fileLock && m_fileLock->Locked() && m_repo && m_index;
106}
107
108
110{
111 return m_lockError;
112}
113
114
116{
117 if( m_fileLock && !m_fileLock->Locked() )
118 {
119 return wxString::Format( wxS( "%s@%s" ),
120 m_fileLock->GetUsername(),
121 m_fileLock->GetHostname() );
122 }
123 return wxEmptyString;
124}
125
126
128{
129 // Ensure history directory exists
130 if( !wxDirExists( m_historyPath ) )
131 {
132 if( !wxFileName::Mkdir( m_historyPath, 0777, wxPATH_MKDIR_FULL ) )
133 {
134 m_lockError = wxString::Format(
135 _( "Cannot create history directory: %s" ), m_historyPath );
136 return false;
137 }
138 }
139
140 // Check for stale lock before attempting acquisition
142 {
143 wxLogWarning( "Detected stale lock file, removing it" );
145 }
146
147 // Create lock file path: .history/.repo.lock
148 wxFileName lockPath( m_historyPath, wxS( ".repo" ) );
149
150 // Use existing LOCKFILE infrastructure
151 m_fileLock = std::make_unique<LOCKFILE>( lockPath.GetFullPath(), true );
152
153 if( !m_fileLock->Locked() )
154 {
155 m_lockError = wxString::Format(
156 _( "History repository is locked by %s@%s" ),
157 m_fileLock->GetUsername(),
158 m_fileLock->GetHostname() );
159 return false;
160 }
161
162 return true;
163}
164
165
167{
168 if( !wxDirExists( m_historyPath ) )
169 {
170 m_lockError = wxString::Format(
171 _( "History directory does not exist: %s" ), m_historyPath );
172 return false;
173 }
174
175 // Attempt to open existing repository
176 int rc = git_repository_open( &m_repo, m_historyPath.mb_str().data() );
177
178 if( rc != 0 )
179 {
180 const git_error* err = git_error_last();
181 m_lockError = wxString::Format(
182 _( "Failed to open git repository: %s" ),
183 err ? wxString::FromUTF8( err->message ) : wxString( "Unknown error" ) );
184 return false;
185 }
186
187 m_repoOwned = true;
188 return true;
189}
190
191
193{
194 if( !m_repo )
195 {
196 m_lockError = _( "Cannot acquire index lock: repository not open" );
197 return false;
198 }
199
200 // git_repository_index will fail if another process has .git/index.lock
201 int rc = git_repository_index( &m_index, m_repo );
202
203 if( rc != 0 )
204 {
205 const git_error* err = git_error_last();
206 m_lockError = wxString::Format(
207 _( "Failed to acquire git index lock (another operation in progress?): %s" ),
208 err ? wxString::FromUTF8( err->message ) : wxString( "Unknown error" ) );
209 return false;
210 }
211
212 m_indexOwned = true;
213 return true;
214}
215
216
217bool HISTORY_LOCK_MANAGER::IsLockStale( const wxString& aProjectPath, int aStaleTimeoutSec )
218{
219 // Use config value if not explicitly specified
220 if( aStaleTimeoutSec <= 0 )
222
223 wxString histPath = historyPath( aProjectPath );
224 wxFileName lockPath( histPath, wxS( ".repo.lock" ) );
225
226 if( !lockPath.FileExists() )
227 return false; // No lock file exists
228
229 wxDateTime modTime;
230 if( !lockPath.GetTimes( nullptr, &modTime, nullptr ) )
231 return false; // Can't determine age
232
233 wxDateTime now = wxDateTime::Now();
234 wxTimeSpan age = now - modTime;
235
236 wxLogTrace( HISTORY_LOCK_TRACE, "Lock file age: %d seconds (stale threshold: %d)",
237 (int)age.GetSeconds().ToLong(), aStaleTimeoutSec );
238
239 return age.GetSeconds().ToLong() > aStaleTimeoutSec;
240}
241
242
243bool HISTORY_LOCK_MANAGER::BreakStaleLock( const wxString& aProjectPath )
244{
245 wxString histPath = historyPath( aProjectPath );
246 wxFileName lockPath( histPath, wxS( ".repo.lock" ) );
247
248 if( !lockPath.FileExists() )
249 return true; // Already removed
250
251 // Also remove the LOCKFILE-style lock file if it exists
252 wxFileName lockFilePath( histPath, wxS( ".repo" ) );
253 lockFilePath.SetName( FILEEXT::LockFilePrefix + lockFilePath.GetName() );
254 lockFilePath.SetExt( lockFilePath.GetExt() + wxS( "." ) + FILEEXT::LockFileExtension );
255
256 bool result = true;
257
258 if( lockPath.FileExists() )
259 {
260 result = wxRemoveFile( lockPath.GetFullPath() );
261 if( result )
262 wxLogTrace( HISTORY_LOCK_TRACE, "Removed stale lock: %s", lockPath.GetFullPath() );
263 else
264 wxLogError( "Failed to remove stale lock: %s", lockPath.GetFullPath() );
265 }
266
267 if( lockFilePath.FileExists() )
268 {
269 bool lockFileResult = wxRemoveFile( lockFilePath.GetFullPath() );
270 if( lockFileResult )
271 wxLogTrace( HISTORY_LOCK_TRACE, "Removed stale lockfile: %s", lockFilePath.GetFullPath() );
272 result = result && lockFileResult;
273 }
274
275 return result;
276}
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.
~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.
#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.
wxString result
Test unit parsing edge cases and error handling.