Fixed issue #223. Attempt to remove stale lock file.

--HG--
branch : develop
This commit is contained in:
dismine 2015-01-26 17:02:57 +02:00
parent 886f347ba6
commit aac096d897
3 changed files with 44 additions and 5 deletions

View File

@ -339,6 +339,43 @@ double VApplication::fromPixel(double pix) const
return fromPixel(pix, _patternUnit); return fromPixel(pix, _patternUnit);
} }
//---------------------------------------------------------------------------------------------------------------------
bool VApplication::TryLock(QLockFile *lock)
{
if (lock == nullptr)
{
return false;
}
if (lock->tryLock())
{
return true;
}
else
{
if (lock->error() == QLockFile::LockFailedError)
{
// This happens if a stale lock file exists and another process uses that PID.
// Try removing the stale file, which will fail if a real process is holding a
// file-level lock. A false error is more problematic than not locking properly
// on corner-case systems.
if (lock->removeStaleLockFile() == false || lock->tryLock() == false)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
return false;
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VApplication::pathToTables() const QString VApplication::pathToTables() const
{ {
@ -461,7 +498,7 @@ void VApplication::BeginLogging()
qInstallMessageHandler(noisyFailureMsgHandler); qInstallMessageHandler(noisyFailureMsgHandler);
logLock = new QLockFile(LogPath()+".lock"); logLock = new QLockFile(LogPath()+".lock");
logLock->setStaleLockTime(0); logLock->setStaleLockTime(0);
if (logLock->tryLock()) if (TryLock(logLock))
{ {
qCDebug(vApp) << "Log file"<<LogPath()<<"was locked."; qCDebug(vApp) << "Log file"<<LogPath()<<"was locked.";
} }
@ -495,7 +532,7 @@ void VApplication::ClearOldLogs() const
{ {
QFileInfo info(allFiles.at(i)); QFileInfo info(allFiles.at(i));
QLockFile *lock = new QLockFile(info.absoluteFilePath() + ".lock"); QLockFile *lock = new QLockFile(info.absoluteFilePath() + ".lock");
if (lock->tryLock()) if (TryLock(lock))
{ {
qCDebug(vApp) << "Locked file"<<info.absoluteFilePath(); qCDebug(vApp) << "Locked file"<<info.absoluteFilePath();
QFile oldLog(allFiles.at(i)); QFile oldLog(allFiles.at(i));
@ -2118,7 +2155,7 @@ void VApplication::GatherLogs() const
} }
QLockFile *logLock = new QLockFile(info.absoluteFilePath()+".lock"); QLockFile *logLock = new QLockFile(info.absoluteFilePath()+".lock");
logLock->setStaleLockTime(0); logLock->setStaleLockTime(0);
if (logLock->tryLock()) if (TryLock(logLock))
{ {
*out <<"--------------------------" << endl; *out <<"--------------------------" << endl;
QFile logFile(info.absoluteFilePath()); QFile logFile(info.absoluteFilePath());

View File

@ -70,6 +70,8 @@ public:
double fromPixel(double pix, const Unit &unit) const; double fromPixel(double pix, const Unit &unit) const;
double fromPixel(double pix) const; double fromPixel(double pix) const;
static bool TryLock(QLockFile *lock);
static const qreal PrintDPI; static const qreal PrintDPI;
QString translationsPath() const; QString translationsPath() const;
QString pathToTables() const; QString pathToTables() const;

View File

@ -2365,7 +2365,7 @@ void MainWindow::LoadPattern(const QString &fileName)
qCDebug(vMainWindow)<<"Loking file"; qCDebug(vMainWindow)<<"Loking file";
lock = new QLockFile(fileName+".lock"); lock = new QLockFile(fileName+".lock");
lock->setStaleLockTime(0); lock->setStaleLockTime(0);
if (lock->tryLock()) if (VApplication::TryLock(lock))
{ {
qCDebug(vMainWindow) << "Pattern file"<<fileName<<"was locked."; qCDebug(vMainWindow) << "Pattern file"<<fileName<<"was locked.";
} }
@ -2477,7 +2477,7 @@ QStringList MainWindow::GetUnlokedRestoreFileList() const
// Seeking file that realy need reopen // Seeking file that realy need reopen
QLockFile *lock = new QLockFile(files.at(i)+".lock"); QLockFile *lock = new QLockFile(files.at(i)+".lock");
lock->setStaleLockTime(0); lock->setStaleLockTime(0);
if (lock->tryLock()) if (VApplication::TryLock(lock))
{ {
restoreFiles.append(files.at(i)); restoreFiles.append(files.at(i));
} }