OSDN Git Service

Improved buildRandomList() function.
[mutilities/MUtilities.git] / src / DirLocker.h
index f4a683f..d06d86d 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -56,21 +56,24 @@ namespace MUtils
                        :
                                m_dirPath(dirPath)
                        {
+                               bool okay = false;
+                               const QByteArray testData = QByteArray(TEST_DATA);
                                if(m_dirPath.isEmpty())
                                {
                                        throw DirLockException("Path must not be empty!");
                                }
-                               const QByteArray testData = QByteArray(TEST_DATA);
-                               bool okay = false;
                                for(int i = 0; i < 32; i++)
                                {
-                                       m_lockFile.reset(new QFile(QString("%1/~%2.lck").arg(m_dirPath, MUtils::rand_str())));
-                                       if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate))
+                                       m_lockFile.reset(new QFile(QString("%1/~%2.lck").arg(m_dirPath, MUtils::next_rand_str())));
+                                       if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered))
                                        {
                                                if(m_lockFile->write(testData) >= testData.size())
                                                {
-                                                       okay = true;
-                                                       break;
+                                                       if(m_lockFile->error() == QFile::NoError)
+                                                       {
+                                                               okay = true;
+                                                               break;
+                                                       }
                                                }
                                                m_lockFile->remove();
                                        }
@@ -83,20 +86,26 @@ namespace MUtils
 
                        ~DirLock(void)
                        {
+                               bool okay = false;
                                if(!m_lockFile.isNull())
                                {
-                                       m_lockFile->remove();
-                               }
-                               for(int i = 0; i < 8; i++)
-                               {
-                                       if(MUtils::remove_directory(m_dirPath))
+                                       for(int i = 0; i < 16; i++)
                                        {
-                                               break;
+                                               if(m_lockFile->remove() || (!m_lockFile->exists()))
+                                               {
+                                                       okay = true;
+                                                       break;
+                                               }
+                                               OS::sleep_ms(125);
                                        }
                                }
+                               if(!okay)
+                               {
+                                       qWarning("DirLock: The lock file could not be removed!");
+                               }
                        }
 
-                       inline const QString &path(void) const
+                       inline const QString &getPath(void) const
                        {
                                return m_dirPath;
                        }