2014-11-25 02:17:11 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MuldeR's Utilities for Qt
|
2016-02-20 16:30:17 +01:00
|
|
|
// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
|
2014-11-25 02:17:11 +01:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/lgpl-2.1.txt
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
|
|
|
|
|
|
|
//StdLib
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
//Qt
|
|
|
|
#include <QString>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Directory Locker
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace MUtils
|
|
|
|
{
|
|
|
|
namespace Internal
|
|
|
|
{
|
|
|
|
class DirLockException : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DirLockException(const char *const message)
|
|
|
|
:
|
|
|
|
std::runtime_error(message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DirLock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DirLock(const QString dirPath)
|
|
|
|
:
|
|
|
|
m_dirPath(dirPath)
|
|
|
|
{
|
2014-12-20 23:47:06 +01:00
|
|
|
bool okay = false;
|
|
|
|
const QByteArray testData = QByteArray(TEST_DATA);
|
2014-11-25 02:17:11 +01:00
|
|
|
if(m_dirPath.isEmpty())
|
|
|
|
{
|
|
|
|
throw DirLockException("Path must not be empty!");
|
|
|
|
}
|
|
|
|
for(int i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
m_lockFile.reset(new QFile(QString("%1/~%2.lck").arg(m_dirPath, MUtils::rand_str())));
|
2014-12-20 23:47:06 +01:00
|
|
|
if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered))
|
2014-11-25 02:17:11 +01:00
|
|
|
{
|
|
|
|
if(m_lockFile->write(testData) >= testData.size())
|
|
|
|
{
|
2015-01-04 17:09:12 +01:00
|
|
|
if(m_lockFile->error() == QFile::NoError)
|
|
|
|
{
|
|
|
|
okay = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-25 02:17:11 +01:00
|
|
|
}
|
|
|
|
m_lockFile->remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!okay)
|
|
|
|
{
|
|
|
|
throw DirLockException("Locking has failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~DirLock(void)
|
|
|
|
{
|
2014-12-20 23:47:06 +01:00
|
|
|
bool okay = false;
|
2014-11-25 02:17:11 +01:00
|
|
|
if(!m_lockFile.isNull())
|
|
|
|
{
|
2015-01-02 23:50:14 +01:00
|
|
|
for(int i = 0; i < 16; i++)
|
2014-12-20 23:47:06 +01:00
|
|
|
{
|
2015-01-04 17:09:12 +01:00
|
|
|
if(m_lockFile->remove() || (!m_lockFile->exists()))
|
2014-12-20 23:47:06 +01:00
|
|
|
{
|
2015-01-04 17:09:12 +01:00
|
|
|
okay = true;
|
2014-12-20 23:47:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-01-02 23:50:14 +01:00
|
|
|
OS::sleep_ms(125);
|
2014-12-20 23:47:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!okay)
|
|
|
|
{
|
2015-01-04 17:09:12 +01:00
|
|
|
qWarning("DirLock: The lock file could not be removed!");
|
2014-11-25 02:17:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 23:47:06 +01:00
|
|
|
inline const QString &getPath(void) const
|
2014-11-25 02:17:11 +01:00
|
|
|
{
|
|
|
|
return m_dirPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const char *const TEST_DATA;
|
|
|
|
const QString m_dirPath;
|
|
|
|
QScopedPointer<QFile> m_lockFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *const DirLock::TEST_DATA = "7QtDxPWotHGQYv1xHyQHFKjTB5u5VYKHE20NMDAgLRYoy16CZN7mEYijbjCJpORnoBbtHCzqyy1a6BLCMMiTUZpLzgjg4qnN505egUBqk3wMhPsYjFpkng9i37mWd1iF";
|
|
|
|
}
|
|
|
|
}
|