From a7d954d98109ecb09c7440f5bba7deca3e4c538b Mon Sep 17 00:00:00 2001 From: Zdenek Prikryl Date: Thu, 12 Feb 2009 17:58:20 +0100 Subject: fixed lock functionality --- lib/Utils/DebugDump.cpp | 72 ++++++++++++++++++++++++++++++++++++------------- lib/Utils/DebugDump.h | 6 ++++- 2 files changed, 59 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp index 14b77906..19d3a2be 100644 --- a/lib/Utils/DebugDump.cpp +++ b/lib/Utils/DebugDump.cpp @@ -34,29 +34,22 @@ #include #include #include -#include CDebugDump::CDebugDump() : - m_sDebugDumpDir("") + m_sDebugDumpDir(""), + m_nFD(0), + m_bLockCreated(false) {} void CDebugDump::Open(const std::string& pDir) { - int fd; m_sDebugDumpDir = pDir; std::string lockPath = m_sDebugDumpDir + "/.lock"; if (!ExistFileDir(pDir)) { throw "CDebugDump::CDebugDump(): "+pDir+" does not exist."; } - if ((m_nFD = open(lockPath.c_str(), O_RDWR | O_CREAT, 0640)) < 0) - { - throw std::string("CDebugDump::Open(): can not create lock file"); - } - if (lockf(m_nFD,F_LOCK, 0) < 0) - { - throw std::string("CDebugDump::Open(): cannot lock DebugDump"); - } + Lock(); } bool CDebugDump::Exist(const std::string& pPath) @@ -79,13 +72,59 @@ bool CDebugDump::ExistFileDir(const std::string& pPath) return false; } +void CDebugDump::Lock() +{ + std::string lockPath = m_sDebugDumpDir + ".lock"; + if (ExistFileDir(lockPath)) + { + if ((m_nFD = open(lockPath.c_str(), O_RDWR | O_CREAT, 0640)) < 0) + { + throw std::string("CDebugDump::Create(): can not create lock file"); + } + m_bLockCreated = false; + } + else + { + if ((m_nFD = open(lockPath.c_str(), O_RDWR | O_CREAT, 0640)) < 0) + { + throw std::string("CDebugDump::Create(): can not create lock file"); + } + m_bLockCreated = true; + } + if (lockf(m_nFD,F_LOCK, 0) < 0) + { + throw std::string("CDebugDump::Create(): cannot lock DebugDump"); + } +} + +void CDebugDump::UnLock() +{ + std::string lockPath = m_sDebugDumpDir + ".lock"; + lockf(m_nFD,F_ULOCK, 0); + close(m_nFD); + if (m_bLockCreated) + { + remove(lockPath.c_str()); + m_bLockCreated = false; + } +} + void CDebugDump::Create(const std::string& pDir) { + m_sDebugDumpDir = pDir; + std::string lockPath = pDir + ".lock"; + if (ExistFileDir(pDir)) + { + throw "CDebugDump::CDebugDump(): "+pDir+" already exists."; + } + + Lock(); + if (mkdir(pDir.c_str(), 0755) == -1) { throw "CDebugDump::Create(): Cannot create dir: " + pDir; } - Open(pDir); + SaveEnvironment(); SaveTime(); } @@ -96,7 +135,7 @@ void CDebugDump::Delete(const std::string& pDir) { return; } - Open(pDir); + Lock(); DIR *dir = opendir(pDir.c_str()); std::string fullPath; struct dirent *dent = NULL; @@ -123,15 +162,12 @@ void CDebugDump::Delete(const std::string& pDir) throw "CDebugDump::DeleteDir(): Cannot remove dir: " + fullPath; } } - Close(); + UnLock(); } void CDebugDump::Close() { - std::string lockPath = m_sDebugDumpDir + "/.lock"; - lockf(m_nFD,F_ULOCK, 0); - close(m_nFD); - remove(lockPath.c_str()); + UnLock(); } void CDebugDump::SaveEnvironment() diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h index 1217136e..a6bb0511 100644 --- a/lib/Utils/DebugDump.h +++ b/lib/Utils/DebugDump.h @@ -43,8 +43,9 @@ class CDebugDump { private: std::string m_sDebugDumpDir; - int m_nFD; + bool m_bLockCreated; + void SaveEnvironment(); void SaveTime(); @@ -55,6 +56,9 @@ class CDebugDump void SaveBinaryFile(const std::string& pName, const char* pData, const unsigned int pSize); bool ExistFileDir(const std::string& pPath); + void Lock(); + void UnLock(); + public: CDebugDump(); -- cgit