diff options
author | Zdenek Prikryl <zdeny@dhcp-lab-218.englab.brq.redhat.com> | 2009-04-01 12:57:06 +0200 |
---|---|---|
committer | Zdenek Prikryl <zdeny@dhcp-lab-218.englab.brq.redhat.com> | 2009-04-01 12:57:06 +0200 |
commit | 99a0819d935bbdfb34226e54ff50ed78b1fc6da0 (patch) | |
tree | 60106dfbcff10b425793520ae02a60f159f4a71c /lib/Utils | |
parent | b3adc1a23a16b5d7a7d341d748bd3e618e5d225f (diff) | |
download | abrt-99a0819d935bbdfb34226e54ff50ed78b1fc6da0.tar.gz abrt-99a0819d935bbdfb34226e54ff50ed78b1fc6da0.tar.xz abrt-99a0819d935bbdfb34226e54ff50ed78b1fc6da0.zip |
rewritten CDebugDump and CrashTypes
Diffstat (limited to 'lib/Utils')
-rw-r--r-- | lib/Utils/DebugDump.cpp | 130 | ||||
-rw-r--r-- | lib/Utils/DebugDump.h | 25 | ||||
-rw-r--r-- | lib/Utils/Makefile.am | 1 |
3 files changed, 109 insertions, 47 deletions
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp index 883d6b91..198b668d 100644 --- a/lib/Utils/DebugDump.cpp +++ b/lib/Utils/DebugDump.cpp @@ -32,11 +32,14 @@ #include <ctype.h> #include <time.h> #include <unistd.h> +#include <magic.h> +#include <string.h> CDebugDump::CDebugDump() : m_sDebugDumpDir(""), m_bOpened(false), - m_bUnlock(true) + m_bUnlock(true), + m_pGetNextFileDir(NULL) {} void CDebugDump::Open(const std::string& pDir) @@ -157,7 +160,7 @@ void CDebugDump::Create(const std::string& pDir) throw "CDebugDump::Create(): Cannot create dir: " + pDir; } - SaveEnvironment(); + SaveKernelArchitectureRelease(); SaveTime(); } @@ -195,6 +198,39 @@ void CDebugDump::DeleteFileDir(const std::string& pDir) } } +bool CDebugDump::IsTextFile(const std::string& pName) +{ + bool isText = false; + magic_t m = magic_open(MAGIC_MIME); + + if (m == NULL) + { + throw std::string("CDebugDump::IsTextFile(): Cannot open magic cookie: ") + magic_error(m); + } + + int r = magic_load(m,NULL); + + if (r == -1) + { + throw std::string("CDebugDump::IsTextFile(): Cannot load magic db: ") + magic_error(m); + } + + char* ch = (char *) magic_file(m, pName.c_str()); + + if (ch == NULL) + { + throw std::string("CDebugDump::IsTextFile(): Cannot determine file type: ") + magic_error(m); + } + + if (!strncmp(ch, "text", 4)) + { + isText = true; + } + + magic_close(m); + return isText; +} + void CDebugDump::Delete() { if (!ExistFileDir(m_sDebugDumpDir)) @@ -210,7 +246,7 @@ void CDebugDump::Close() m_bOpened = false; } -void CDebugDump::SaveEnvironment() +void CDebugDump::SaveKernelArchitectureRelease() { struct utsname buf; if (uname(&buf) == 0) @@ -242,16 +278,18 @@ void CDebugDump::LoadTextFile(const std::string& pPath, std::string& pData) fIn.open(pPath.c_str()); if (fIn.is_open()) { - std::string line; - while (!fIn.eof()) + // TODO: rewrite this + int ch; + while ((ch = fIn.get())!= EOF) { - getline (fIn,line); - // TODO: remove this hack - if (pData != "") - { - pData += "\n"; - } - pData += line; + if (ch == 0) + { + pData += " "; + } + else if (isspace(ch) || (isascii(ch) && !iscntrl(ch))) + { + pData += ch; + } } fIn.close(); } @@ -291,6 +329,10 @@ void CDebugDump::SaveTextFile(const std::string& pPath, const std::string& pData if (fOut.is_open()) { fOut << pData; + if (!fOut.good()) + { + throw "CDebugDump: SaveTextFile(): Cannot save file " + pPath; + } fOut.close(); } else @@ -306,6 +348,10 @@ void CDebugDump::SaveBinaryFile(const std::string& pPath, const char* pData, con if (fOut.is_open()) { fOut.write(pData, pSize); + if (!fOut.good()) + { + throw "CDebugDump: SaveBinaryFile(): Cannot save file " + pPath; + } fOut.close(); } else @@ -336,36 +382,46 @@ void CDebugDump::SaveBinary(const std::string& pName, const char* pData, const u SaveBinaryFile(fullPath, pData, pSize); } - -void CDebugDump::SaveProc(const std::string& pPID) +void CDebugDump::InitGetNextFile() { - std::string path = "/proc/"+pPID+"/exe"; - std::string data; - char executable[PATH_MAX]; - int len; - - if ((len = readlink(path.c_str(), executable, PATH_MAX)) != -1) + if (m_pGetNextFileDir != NULL) { - executable[len] = '\0'; - SaveText(FILENAME_EXECUTABLE, executable); + closedir(m_pGetNextFileDir); + m_pGetNextFileDir= NULL; } + m_pGetNextFileDir = opendir(m_sDebugDumpDir.c_str()); + if (m_pGetNextFileDir == NULL) + { + throw "CDebugDump::InitGetNextFile(): Cannot open dir " + m_sDebugDumpDir; + } +} +bool CDebugDump::GetNextFile(std::string& pFileName, std::string& pContent, bool& pIsTextFile) +{ + static struct dirent *dent = NULL; - path = "/proc/"+pPID+"/status"; - std::string uid = ""; - int ii = 0; - - LoadTextFile(path, data); - data = data.substr(data.find("Uid:")+5); - - while (!isspace(data[ii])) + if (m_pGetNextFileDir == NULL) { - uid += data[ii]; - ii++; + false; } - SaveText(FILENAME_UID, uid); - - path = "/proc/"+pPID+"/cmdline"; - LoadTextFile(path, data); - SaveText(FILENAME_CMDLINE, data); + while ((dent = readdir(m_pGetNextFileDir)) != NULL) + { + if (dent->d_type == DT_REG) + { + pFileName = dent->d_name; + if (IsTextFile(m_sDebugDumpDir + "/" + pFileName)) + { + LoadText(pFileName, pContent); + pIsTextFile = true; + } + else + { + pContent = ""; + pIsTextFile = false; + } + return true; + } + } + return false; } + diff --git a/lib/Utils/DebugDump.h b/lib/Utils/DebugDump.h index 5805a82e..94323e4c 100644 --- a/lib/Utils/DebugDump.h +++ b/lib/Utils/DebugDump.h @@ -24,22 +24,23 @@ #define DEBUGDUMP_H_ #include <string> +#include <dirent.h> #define FILENAME_UUID "uuid" #define FILENAME_ARCHITECTURE "architecture" #define FILENAME_KERNEL "kernel" -#define FILENAME_RELEASE "release" -#define FILENAME_EXECUTABLE "executable" -#define FILENAME_CMDLINE "cmdline" #define FILENAME_TIME "time" #define FILENAME_UID "uid" #define FILENAME_PACKAGE "package" #define FILENAME_DESCRIPTION "description" #define FILENAME_ANALYZER "analyzer" -#define FILENAME_TEXTDATA1 "text_data1" -#define FILENAME_TEXTDATA2 "text_data2" -#define FILENAME_BINARYDATA1 "binary_data1" -#define FILENAME_BINARYDATA2 "binary_data2" +#define FILENAME_RELEASE "release" +#define FILENAME_EXECUTABLE "executable" +#define FILENAME_CMDLINE "cmdline" +#define FILENAME_TEXTDATA1 "text_data_1" +#define FILENAME_TEXTDATA2 "text_data_2" +#define FILENAME_BINARYDATA1 "binary_data_1" +#define FILENAME_BINARYDATA2 "binary_data_1" class CDebugDump { @@ -47,8 +48,9 @@ class CDebugDump std::string m_sDebugDumpDir; bool m_bOpened; bool m_bUnlock; + DIR* m_pGetNextFileDir; - void SaveEnvironment(); + void SaveKernelArchitectureRelease(); void SaveTime(); void LoadTextFile(const std::string& pName, std::string& pData); @@ -64,6 +66,8 @@ class CDebugDump void DeleteFileDir(const std::string& pDir); + bool IsTextFile(const std::string& pName); + public: CDebugDump(); @@ -72,8 +76,6 @@ class CDebugDump void Delete(); void Close(); - void SaveProc(const std::string& pPID); - bool Exist(const std::string& pFileName); void LoadText(const std::string& pName, std::string& pData); @@ -81,6 +83,9 @@ class CDebugDump void SaveText(const std::string& pName, const std::string& pData); void SaveBinary(const std::string& pName, const char* pData, const unsigned int pSize); + + void InitGetNextFile(); + bool GetNextFile(std::string& pFileName, std::string& pContent, bool& pIsTextFile); }; #endif /*DEBUGDUMP_H_*/ diff --git a/lib/Utils/Makefile.am b/lib/Utils/Makefile.am index c9bef6d4..5e255096 100644 --- a/lib/Utils/Makefile.am +++ b/lib/Utils/Makefile.am @@ -1,6 +1,7 @@ lib_LTLIBRARIES = libUtils.la libUtils_la_SOURCES = DebugDump.cpp DebugDump.h Settings.h libUtils_la_LDFLAGS = -version-info 0:1:0 +libUtils_la_LIBADD = -lmagic install-data-local: $(mkdir_p) '$(DESTDIR)/$(DEBUG_DUMPS_DIR)'
\ No newline at end of file |