Change CLockFile so that it doesn't leak fds when you call Open() twice

git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@891 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2007-12-02 09:34:49 +00:00
parent f74f501d5a
commit 4e0ccf8331
+21 -10
View File
@@ -70,29 +70,34 @@ protected:
class CLockFile {
public:
CLockFile() {
m_bCreated = false;
m_fd = -1;
m_pid = 0;
Init();
}
CLockFile(const CString& sFile) {
Init();
Open(sFile);
}
virtual ~CLockFile() {
if (getpid() == m_pid) {
if (m_fd > -1) {
UnLock();
close(m_fd);
Close();
}
if (m_bCreated) {
unlink(m_sFileName.c_str());
}
void Close() {
if (getpid() == m_pid && m_fd > -1) {
UnLock();
close(m_fd);
if (m_bCreated) {
unlink(m_sFileName.c_str());
}
Init();
}
}
void Open(const CString& sFile, bool bRw = false) {
Close();
m_fd = open(sFile.c_str(), bRw ? O_RDWR : O_RDONLY);
m_bCreated = false;
@@ -176,6 +181,12 @@ private:
return true;
}
void Init() {
m_bCreated = false;
m_fd = -1;
m_pid = 0;
}
int m_fd;
int m_pid;
bool m_bCreated;