Remove CLockFile and let CFile do its job

This shouldn't contain any major behaviour change, but there are some minor
ones. Also, the API for a shared lock wasn't used and thus is dropped.

Thanks to cnu for this idea.


git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1337 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2009-01-20 13:21:29 +00:00
parent 6345ce12fa
commit 127347825e
5 changed files with 45 additions and 118 deletions
+25
View File
@@ -372,6 +372,31 @@ void CFile::Close() {
}
void CFile::ClearBuffer() { m_sBuffer.clear(); }
bool CFile::TryExLock(const CString& sLockFile, int iFlags) {
Open(sLockFile, iFlags);
return TryExLock();
}
bool CFile::TryExLock() {
return Lock(LOCK_EX|LOCK_NB);
}
bool CFile::UnLock() {
return Lock(LOCK_UN);
}
bool CFile::Lock(int iOperation) {
if (m_iFD == -1) {
return false;
}
if (::flock(m_iFD, iOperation) != 0) {
return false;
}
return true;
}
bool CFile::IsOpen() const { return (m_iFD != -1); }
CString CFile::GetLongName() const { return m_sLongName; }
CString CFile::GetShortName() const { return m_sShortName; }
+7
View File
@@ -116,6 +116,10 @@ public:
void Close();
void ClearBuffer();
bool TryExLock(const CString& sLockFile, int iFlags = O_RDONLY);
bool TryExLock();
bool UnLock();
bool IsOpen() const;
CString GetLongName() const;
CString GetShortName() const;
@@ -123,6 +127,9 @@ public:
void SetFD(int iFD);
private:
// flock() wrapper
bool Lock(int iOperation);
CString m_sBuffer;
int m_iFD;
-103
View File
@@ -79,109 +79,6 @@ protected:
static bool stdoutIsTTY;
};
class CLockFile {
public:
CLockFile() {
Init();
}
CLockFile(const CString& sFile) {
Init();
Open(sFile);
}
virtual ~CLockFile() {
Close();
}
void Close() {
if (getpid() == m_pid && m_fd > -1) {
// This UnLock() also unlocks for all of our childs!
UnLock();
close(m_fd);
if (m_bCreated) {
unlink(m_sFileName.c_str());
}
} else if (m_fd > -1)
// Make sure we don't leak this fd
close(m_fd);
Init();
}
bool Open(const CString& sFile, bool bRw = false) {
Close();
m_fd = open(sFile.c_str(), bRw ? O_RDWR : O_RDONLY);
m_bCreated = false;
if (m_fd == -1) {
// I must create the file then
m_fd = open(sFile.c_str(), O_RDWR|O_CREAT, 0644);
if (m_fd == -1) {
return false;
}
m_bCreated = true;
}
// Thanks to broken POSIX, we shouldn't give this fd to anyone
SetFdCloseOnExec(m_fd);
m_pid = getpid(); // for destructor
m_sFileName = sFile;
return true;
}
//! timeout in milliseconds
bool TryExLock(const CString& sLockFile, bool bRw = false) {
Open(sLockFile, bRw);
return TryExLock();
}
bool TryExLock() {
return Lock(LOCK_EX|LOCK_NB);
}
bool TryShLock() {
return(Lock(LOCK_SH|LOCK_NB));
}
bool LockEx() { return Lock(LOCK_EX); }
bool LockSh() { return Lock(LOCK_SH); }
bool UnLock() { return Lock(LOCK_UN); }
CString GetFileName() { return m_sFileName; }
int GetFD() { return m_fd; }
private:
bool Lock(int iOperation) {
if (m_fd == -1) {
return false;
}
if (::flock(m_fd, iOperation) != 0) {
return false;
}
return true;
}
void Init() {
m_bCreated = false;
m_fd = -1;
m_pid = 0;
}
int m_fd;
int m_pid;
bool m_bCreated;
CString m_sFileName;
};
class CException {
public:
typedef enum {
+11 -13
View File
@@ -247,28 +247,26 @@ bool CZNC::WriteISpoof(CUser* pUser) {
return false;
if (!m_sISpoofFile.empty()) {
m_pISpoofLockFile = new CLockFile;
m_pISpoofLockFile = new CFile;
if (!m_pISpoofLockFile->TryExLock(m_sISpoofFile, true)) {
delete m_pISpoofLockFile;
m_pISpoofLockFile = NULL;
return false;
}
CFile File(m_pISpoofLockFile->GetFD(), m_pISpoofLockFile->GetFileName());
char buf[1024];
memset((char*) buf, 0, 1024);
File.Read(buf, 1023);
m_pISpoofLockFile->Read(buf, 1023);
m_sOrigISpoof = buf;
if (!File.Seek(0) || !File.Truncate()) {
if (!m_pISpoofLockFile->Seek(0) || !m_pISpoofLockFile->Truncate()) {
delete m_pISpoofLockFile;
m_pISpoofLockFile = NULL;
return false;
}
CString sData = m_sISpoofFormat.Token(0, false, "%") + pUser->GetIdent() + m_sISpoofFormat.Token(1, true, "%");
File.Write(sData + "\n");
m_pISpoofLockFile->Write(sData + "\n");
}
return true;
}
@@ -278,10 +276,8 @@ void CZNC::ReleaseISpoof() {
return;
if (!m_sISpoofFile.empty()) {
CFile File(m_pISpoofLockFile->GetFD(), m_pISpoofLockFile->GetFileName());
if (File.Seek(0) && File.Truncate()) {
File.Write(m_sOrigISpoof);
if (m_pISpoofLockFile->Seek(0) && m_pISpoofLockFile->Truncate()) {
m_pISpoofLockFile->Write(m_sOrigISpoof);
}
m_sOrigISpoof = "";
@@ -959,6 +955,10 @@ bool CZNC::DoRehash(CString& sError)
return false;
}
// (re)open the config file
if (m_LockFile.IsOpen())
m_LockFile.Close();
if (!m_LockFile.Open(m_sConfigFile)) {
sError = "Can not open config file";
CUtils::PrintStatus(false, sError);
@@ -971,7 +971,7 @@ bool CZNC::DoRehash(CString& sError)
return false;
}
CFile File(m_LockFile.GetFD(), m_sConfigFile);
CFile &File = m_LockFile;
// This fd is re-used for rehashing, so we must seek back to the beginning!
if (!File.Seek(0)) {
@@ -1579,8 +1579,6 @@ bool CZNC::DoRehash(CString& sError)
delete pUser;
}
File.Close();
if (m_msUsers.size() == 0) {
sError = "You must define at least one user in your config.";
CUtils::PrintError(sError);
+2 -2
View File
@@ -209,8 +209,8 @@ protected:
CString m_sPidFile;
VCString m_vsVHosts;
VCString m_vsMotd;
CLockFile m_LockFile;
CLockFile* m_pISpoofLockFile;
CFile m_LockFile;
CFile* m_pISpoofLockFile;
uint m_uiConnectDelay;
#ifdef _MODULES
CGlobalModules* m_pModules;