diff options
-rw-r--r-- | lib/CommLayer/CommLayerServer.h | 2 | ||||
-rw-r--r-- | lib/Utils/DebugDump.cpp | 100 | ||||
-rw-r--r-- | lib/Utils/DebugDump.h | 13 | ||||
-rw-r--r-- | src/Daemon/CrashWatcher.cpp | 16 | ||||
-rw-r--r-- | src/Daemon/CrashWatcher.h | 1 |
5 files changed, 64 insertions, 68 deletions
diff --git a/lib/CommLayer/CommLayerServer.h b/lib/CommLayer/CommLayerServer.h index b67b5966..0acb7d70 100644 --- a/lib/CommLayer/CommLayerServer.h +++ b/lib/CommLayer/CommLayerServer.h @@ -20,7 +20,7 @@ to_string( T x ) } -class CCommLayerServer{ +class CCommLayerServer { protected: /* FIXME more observers? */ //std::vector<Observer *obs>; diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp index d239ba1b..3297579b 100644 --- a/lib/Utils/DebugDump.cpp +++ b/lib/Utils/DebugDump.cpp @@ -50,6 +50,21 @@ #define PID_STR_MAX 16 +/* Is it "." or ".."? */ +/* abrtlib candidate */ +static bool dot_or_dotdot(const char *filename) +{ + if (filename[0] != '.') return false; + if (filename[1] == '\0') return true; + if (filename[1] != '.') return false; + if (filename[2] == '\0') return true; + return false; +} + +static std::string RemoveBackSlashes(const std::string& pDir); +static bool ExistFileDir(const std::string& pPath); +static void LoadTextFile(const std::string& pPath, std::string& pData); + CDebugDump::CDebugDump() : m_sDebugDumpDir(""), m_bOpened(false), @@ -80,7 +95,7 @@ bool CDebugDump::Exist(const std::string& pPath) } -bool CDebugDump::ExistFileDir(const std::string& pPath) +static bool ExistFileDir(const std::string& pPath) { struct stat buf; if (stat(pPath.c_str(), &buf) == 0) @@ -220,49 +235,42 @@ void CDebugDump::Create(const std::string& pDir, const std::string& pUID) SaveTime(); } -void CDebugDump::DeleteFileDir(const std::string& pDir) +static void DeleteFileDir(const std::string& pDir) { - if (!ExistFileDir(pDir)) - { - return; - } DIR *dir = opendir(pDir.c_str()); - std::string fullPath; - if (dir != NULL) + if (!dir) + return; + + struct dirent *dent; + while ((dent = readdir(dir)) != NULL) { - struct dirent *dent; - while ((dent = readdir(dir)) != NULL) + if (dot_or_dotdot(dent->d_name)) + continue; + std::string fullPath = pDir + "/" + dent->d_name; + if (unlink(fullPath.c_str()) == -1) { - if (std::string(dent->d_name) != "." && std::string(dent->d_name) != "..") + if (errno != EISDIR) { - fullPath = pDir + "/" + dent->d_name; - if (dent->d_type == DT_DIR) - { - DeleteFileDir(fullPath); - } - if (remove(fullPath.c_str()) == -1) - { - closedir(dir); - throw CABRTException(EXCEP_DD_DELETE, "CDebugDump::DeleteFileDir(): Cannot remove file: " + fullPath); - } + closedir(dir); + throw CABRTException(EXCEP_DD_DELETE, std::string(__func__) + ": Cannot remove file: " + fullPath); } - } - closedir(dir); - if (remove(pDir.c_str()) == -1) - { - throw CABRTException(EXCEP_DD_DELETE, "CDebugDump::DeleteFileDir(): Cannot remove dir: " + fullPath); + DeleteFileDir(fullPath); } } + closedir(dir); + if (remove(pDir.c_str()) == -1) + { + throw CABRTException(EXCEP_DD_DELETE, std::string(__func__) + ": Cannot remove dir: " + pDir); + } } -bool CDebugDump::IsTextFile(const std::string& pName) +static bool IsTextFile(const std::string& pName) { - bool isText = false; magic_t m = magic_open(MAGIC_MIME_TYPE); if (m == NULL) { - throw CABRTException(EXCEP_ERROR, std::string("CDebugDump::IsTextFile(): Cannot open magic cookie: ") + magic_error(m)); + throw CABRTException(EXCEP_ERROR, std::string(__func__) + "Cannot open magic cookie: " + magic_error(m)); } int r = magic_load(m, NULL); @@ -270,26 +278,25 @@ bool CDebugDump::IsTextFile(const std::string& pName) if (r == -1) { magic_close(m); - throw CABRTException(EXCEP_ERROR, std::string("CDebugDump::IsTextFile(): Cannot load magic db: ") + magic_error(m)); + throw CABRTException(EXCEP_ERROR, std::string(__func__) + "Cannot load magic db: " + magic_error(m)); } char* ch = (char *) magic_file(m, pName.c_str()); if (ch == NULL) { - throw CABRTException(EXCEP_ERROR, std::string("CDebugDump::IsTextFile(): Cannot determine file type: ") + magic_error(m)); + magic_close(m); + throw CABRTException(EXCEP_ERROR, std::string(__func__) + "Cannot determine file type: " + magic_error(m)); } - if (!strncmp(ch, "text", 4)) - { - isText = true; - } + bool isText = (strncmp(ch, "text", 4) == 0); + magic_close(m); return isText; } -std::string CDebugDump::RemoveBackSlashes(const std::string& pDir) +static std::string RemoveBackSlashes(const std::string& pDir) { std::string ret = pDir; while (ret[ret.length() - 1] == '/') @@ -335,7 +342,7 @@ void CDebugDump::SaveTime() SaveText(FILENAME_TIME, ss.str()); } -void CDebugDump::LoadTextFile(const std::string& pPath, std::string& pData) +static void LoadTextFile(const std::string& pPath, std::string& pData) { std::ifstream fIn; pData = ""; @@ -359,11 +366,11 @@ void CDebugDump::LoadTextFile(const std::string& pPath, std::string& pData) } else { - throw CABRTException(EXCEP_DD_LOAD, "CDebugDump: LoadTextFile(): Cannot open file " + pPath); + throw CABRTException(EXCEP_DD_LOAD, std::string(__func__) + ": Cannot open file " + pPath); } } -void CDebugDump::LoadBinaryFile(const std::string& pPath, char** pData, unsigned int* pSize) +static void LoadBinaryFile(const std::string& pPath, char** pData, unsigned int* pSize) { std::ifstream fIn; fIn.open(pPath.c_str(), std::ios::binary | std::ios::ate); @@ -381,12 +388,11 @@ void CDebugDump::LoadBinaryFile(const std::string& pPath, char** pData, unsigned } else { - throw CABRTException(EXCEP_DD_LOAD, "CDebugDump: LoadBinaryFile(): Cannot open file " + pPath); + throw CABRTException(EXCEP_DD_LOAD, std::string(__func__) + ": Cannot open file " + pPath); } } - -void CDebugDump::SaveTextFile(const std::string& pPath, const std::string& pData) +static void SaveTextFile(const std::string& pPath, const std::string& pData) { std::ofstream fOut; fOut.open(pPath.c_str()); @@ -395,17 +401,17 @@ void CDebugDump::SaveTextFile(const std::string& pPath, const std::string& pData fOut << pData; if (!fOut.good()) { - throw CABRTException(EXCEP_DD_SAVE, "CDebugDump: SaveTextFile(): Cannot save file " + pPath); + throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot save file " + pPath); } fOut.close(); } else { - throw CABRTException(EXCEP_DD_SAVE, "CDebugDump: SaveTextFile(): Cannot open file " + pPath); + throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot open file " + pPath); } } -void CDebugDump::SaveBinaryFile(const std::string& pPath, const char* pData, const unsigned pSize) +static void SaveBinaryFile(const std::string& pPath, const char* pData, const unsigned pSize) { std::ofstream fOut; fOut.open(pPath.c_str(), std::ios::binary); @@ -414,13 +420,13 @@ void CDebugDump::SaveBinaryFile(const std::string& pPath, const char* pData, con fOut.write(pData, pSize); if (!fOut.good()) { - throw CABRTException(EXCEP_DD_SAVE, "CDebugDump: SaveBinaryFile(): Cannot save file " + pPath); + throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot save file " + pPath); } fOut.close(); } else { - throw CABRTException(EXCEP_DD_SAVE, "CDebugDump: SaveBinaryFile(): Cannot open file " + pPath); + throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot open file " + pPath); } } diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h index a09a913d..65cc7609 100644 --- a/lib/Utils/DebugDump.h +++ b/lib/Utils/DebugDump.h @@ -50,24 +50,11 @@ class CDebugDump void SaveKernelArchitectureRelease(); void SaveTime(); - void LoadTextFile(const std::string& pName, std::string& pData); - void LoadBinaryFile(const std::string& pName, char** pData, unsigned int* pSize); - - void SaveTextFile(const std::string& pName, const std::string& pData); - void SaveBinaryFile(const std::string& pName, const char* pData, const unsigned int pSize); - bool ExistFileDir(const std::string& pPath); - void Lock(); bool GetAndSetLock(const std::string& pLockFile, const std::string& pPID); void UnLock(); - void DeleteFileDir(const std::string& pDir); - - bool IsTextFile(const std::string& pName); - - std::string RemoveBackSlashes(const std::string& pDir); public: - CDebugDump(); void Open(const std::string& pDir); void Create(const std::string& pDir, const std::string& pUID); diff --git a/src/Daemon/CrashWatcher.cpp b/src/Daemon/CrashWatcher.cpp index 3163d851..1dc99dfa 100644 --- a/src/Daemon/CrashWatcher.cpp +++ b/src/Daemon/CrashWatcher.cpp @@ -42,6 +42,7 @@ to_string( T x ) */ /* Is it "." or ".."? */ +/* abrtlib candidate */ static bool dot_or_dotdot(const char *filename) { if (filename[0] != '.') return false; @@ -51,6 +52,8 @@ static bool dot_or_dotdot(const char *filename) return false; } +static double GetDirSize(const std::string &pPath); + gboolean CCrashWatcher::handle_event_cb(GIOChannel *gio, GIOCondition condition, gpointer daemon) { GIOError err; @@ -81,7 +84,8 @@ gboolean CCrashWatcher::handle_event_cb(GIOChannel *gio, GIOCondition condition, /* we want to ignore the lock files */ if (event->mask & IN_ISDIR) { - if (cc->GetDirSize(DEBUG_DUMPS_DIR)/(1024*1024.0) < cc->m_pSettings->GetMaxCrashReportsSize()){ + if (GetDirSize(DEBUG_DUMPS_DIR) / (1024*1024) < cc->m_pSettings->GetMaxCrashReportsSize()) + { //std::string sName = name; map_crash_info_t crashinfo; try @@ -402,13 +406,13 @@ void CCrashWatcher::Debug(const std::string& pMessage, const std::string& pDest) std::cout << "Debug: " + pMessage << std::endl; } -double CCrashWatcher::GetDirSize(const std::string &pPath) +static double GetDirSize(const std::string &pPath) { double size = 0; struct dirent *ep; struct stat stats; DIR *dp; - std::string dname; + dp = opendir(pPath.c_str()); if (dp != NULL) { @@ -416,7 +420,7 @@ double CCrashWatcher::GetDirSize(const std::string &pPath) { if (dot_or_dotdot(ep->d_name)) continue; - dname = pPath + "/" + ep->d_name; + std::string dname = pPath + "/" + ep->d_name; if (lstat(dname.c_str(), &stats) == 0) { if (S_ISDIR(stats.st_mode)) @@ -429,11 +433,11 @@ double CCrashWatcher::GetDirSize(const std::string &pPath) } } } - (void) closedir(dp); + closedir(dp); } else { - throw CABRTException(EXCEP_FATAL, "CCrashWatcher::GetDirSize(): Init Failed"); + throw CABRTException(EXCEP_FATAL, std::string(__func__) + ": Init Failed"); } return size; } diff --git a/src/Daemon/CrashWatcher.h b/src/Daemon/CrashWatcher.h index d8fc9aa8..66ca71f0 100644 --- a/src/Daemon/CrashWatcher.h +++ b/src/Daemon/CrashWatcher.h @@ -104,7 +104,6 @@ class CCrashWatcher /* finds dumps created when daemon wasn't running */ // FIXME: how to catch abrt itself without this? void FindNewDumps(const std::string& pPath); - double GetDirSize(const std::string &pPath); int m_nFd; GIOChannel* m_pGio; |