summaryrefslogtreecommitdiffstats
path: root/lib/Utils/DebugDump.cpp
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-11-06 12:41:24 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-06 12:41:24 +0100
commitc8e9e69f96c2bd0b9248d6dfab91e2d27ab8e608 (patch)
tree237a9d3f821d01932111e9fa59af5d3e44b988c8 /lib/Utils/DebugDump.cpp
parent03bbbf8eff11cf68a7e2325ca310e02bf3757193 (diff)
downloadabrt-c8e9e69f96c2bd0b9248d6dfab91e2d27ab8e608.tar.gz
abrt-c8e9e69f96c2bd0b9248d6dfab91e2d27ab8e608.tar.xz
abrt-c8e9e69f96c2bd0b9248d6dfab91e2d27ab8e608.zip
mass replace of const string& params by const char*
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'lib/Utils/DebugDump.cpp')
-rw-r--r--lib/Utils/DebugDump.cpp153
1 files changed, 44 insertions, 109 deletions
diff --git a/lib/Utils/DebugDump.cpp b/lib/Utils/DebugDump.cpp
index b908acc..276c42f 100644
--- a/lib/Utils/DebugDump.cpp
+++ b/lib/Utils/DebugDump.cpp
@@ -48,7 +48,7 @@ static std::string RemoveBackSlashes(const char *pDir)
}
static bool ExistFileDir(const char* pPath);
-static void LoadTextFile(const std::string& pPath, std::string& pData);
+static void LoadTextFile(const char *pPath, std::string& pData);
CDebugDump::CDebugDump() :
m_sDebugDumpDir(""),
@@ -57,13 +57,13 @@ CDebugDump::CDebugDump() :
m_bLocked(false)
{}
-void CDebugDump::Open(const std::string& pDir)
+void CDebugDump::Open(const char *pDir)
{
if (m_bOpened)
{
throw CABRTException(EXCEP_ERROR, "CDebugDump::CDebugDump(): DebugDump is already opened.");
}
- m_sDebugDumpDir = RemoveBackSlashes(pDir.c_str());
+ m_sDebugDumpDir = RemoveBackSlashes(pDir);
if (!ExistFileDir(m_sDebugDumpDir.c_str()))
{
throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::CDebugDump(): " + m_sDebugDumpDir + " does not exist.");
@@ -215,17 +215,17 @@ void CDebugDump::UnLock()
}
}
-void CDebugDump::Create(const std::string& pDir, int64_t uid)
+void CDebugDump::Create(const char *pDir, int64_t uid)
{
if (m_bOpened)
{
- throw CABRTException(EXCEP_ERROR, "CDebugDump::CDebugDump(): DebugDump is already opened.");
+ throw CABRTException(EXCEP_ERROR, "DebugDump is already opened");
}
- m_sDebugDumpDir = RemoveBackSlashes(pDir.c_str());
+ m_sDebugDumpDir = RemoveBackSlashes(pDir);
if (ExistFileDir(m_sDebugDumpDir.c_str()))
{
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::CDebugDump(): "+m_sDebugDumpDir+" already exists.");
+ throw CABRTException(EXCEP_DD_OPEN, ssprintf("'%s' already exists", m_sDebugDumpDir.c_str()));
}
Lock();
@@ -235,13 +235,13 @@ void CDebugDump::Create(const std::string& pDir, int64_t uid)
{
UnLock();
m_bOpened = false;
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::Create(): Cannot create dir: " + pDir);
+ throw CABRTException(EXCEP_DD_OPEN, ssprintf("Can't create dir '%s'", pDir));
}
if (chmod(m_sDebugDumpDir.c_str(), 0700) == -1)
{
UnLock();
m_bOpened = false;
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::Create(): Cannot change permissions, dir: " + pDir);
+ throw CABRTException(EXCEP_DD_OPEN, ssprintf("Can't change mode of '%s'", pDir));
}
struct passwd* pw = getpwuid(uid);
gid_t gid = pw ? pw->pw_gid : uid;
@@ -252,9 +252,10 @@ void CDebugDump::Create(const std::string& pDir, int64_t uid)
perror_msg("can't change '%s' ownership to %u:%u", m_sDebugDumpDir.c_str(), (int)uid, (int)gid);
}
- SaveText(FILENAME_UID, ssprintf("%li", uid));
+ SaveText(FILENAME_UID, to_string(uid).c_str());
SaveKernelArchitectureRelease();
- SaveTime();
+ time_t t = time(NULL);
+ SaveText(FILENAME_TIME, to_string(t).c_str());
}
static void DeleteFileDir(const std::string& pDir)
@@ -274,7 +275,7 @@ static void DeleteFileDir(const std::string& pDir)
if (errno != EISDIR)
{
closedir(dir);
- throw CABRTException(EXCEP_DD_DELETE, std::string(__func__) + ": Cannot remove file: " + fullPath);
+ throw CABRTException(EXCEP_DD_DELETE, "Can't remove file: " + fullPath);
}
DeleteFileDir(fullPath);
}
@@ -282,7 +283,7 @@ static void DeleteFileDir(const std::string& pDir)
closedir(dir);
if (remove(pDir.c_str()) == -1)
{
- throw CABRTException(EXCEP_DD_DELETE, std::string(__func__) + ": Cannot remove dir: " + pDir);
+ throw CABRTException(EXCEP_DD_DELETE, "Can't remove dir: " + pDir);
}
}
@@ -371,100 +372,44 @@ void CDebugDump::SaveKernelArchitectureRelease()
const char *release_ptr = release.c_str();
unsigned len_1st_str = strchrnul(release_ptr, '\n') - release_ptr;
release.erase(len_1st_str); /* usually simply removes trailing '\n' */
- SaveText(FILENAME_RELEASE, release);
-}
-
-void CDebugDump::SaveTime()
-{
- time_t t = time(NULL);
- SaveText(FILENAME_TIME, to_string(t));
+ SaveText(FILENAME_RELEASE, release.c_str());
}
-static void LoadTextFile(const std::string& pPath, std::string& pData)
+static void LoadTextFile(const char *pPath, std::string& pData)
{
- std::ifstream fIn;
+ FILE *fp = fopen(pPath, "r");
+ if (!fp)
+ {
+ throw CABRTException(EXCEP_DD_LOAD, ssprintf("Can't open file '%s'", pPath));
+ }
pData = "";
- fIn.open(pPath.c_str());
- if (fIn.is_open())
+ int ch;
+ while ((ch = fgetc(fp)) != EOF)
{
- // TODO: rewrite this
- int ch;
- while ((ch = fIn.get())!= EOF)
+ if (ch == '\0')
{
- if (ch == 0)
- {
- pData += " ";
- }
- else if (isspace(ch) || (isascii(ch) && !iscntrl(ch)))
- {
- pData += ch;
- }
+ pData += ' ';
}
- fIn.close();
- }
- else
- {
- throw CABRTException(EXCEP_DD_LOAD, std::string(__func__) + ": Cannot open file " + pPath);
- }
-}
-
-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);
- unsigned int size;
- if (fIn.is_open())
- {
- size = fIn.tellg();
- char *data = new char [size];
- fIn.read(data, size);
-
- *pData = data;
- *pSize = size;
-
- fIn.close();
- }
- else
- {
- throw CABRTException(EXCEP_DD_LOAD, std::string(__func__) + ": Cannot open file " + pPath);
- }
-}
-
-static void SaveTextFile(const std::string& pPath, const std::string& pData)
-{
- std::ofstream fOut;
- fOut.open(pPath.c_str());
- if (fOut.is_open())
- {
- fOut << pData;
- if (!fOut.good())
+ else if (isspace(ch) || (isascii(ch) && !iscntrl(ch)))
{
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot save file " + pPath);
+ pData += ch;
}
- fOut.close();
- }
- else
- {
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot open file " + pPath);
}
+ fclose(fp);
}
-static void SaveBinaryFile(const std::string& pPath, const char* pData, const unsigned pSize)
+static void SaveBinaryFile(const char *pPath, const char* pData, unsigned pSize)
{
- std::ofstream fOut;
- fOut.open(pPath.c_str(), std::ios::binary);
- if (fOut.is_open())
+ int fd = open(pPath, O_WRONLY | O_TRUNC | O_CREAT, 0666);
+ if (fd < 0)
{
- fOut.write(pData, pSize);
- if (!fOut.good())
- {
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot save file " + pPath);
- }
- fOut.close();
+ throw CABRTException(EXCEP_DD_SAVE, ssprintf("Can't open file '%s'", pPath));
}
- else
+ unsigned r = full_write(fd, pData, pSize);
+ close(fd);
+ if (r != pSize)
{
- throw CABRTException(EXCEP_DD_SAVE, std::string(__func__) + ": Cannot open file " + pPath);
+ throw CABRTException(EXCEP_DD_SAVE, ssprintf("Can't save file '%s'", pPath));
}
}
@@ -474,36 +419,27 @@ void CDebugDump::LoadText(const char* pName, std::string& pData)
{
throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::LoadText(): DebugDump is not opened.");
}
- std::string fullPath = m_sDebugDumpDir + "/" + pName;
- LoadTextFile(fullPath, pData);
-}
-void CDebugDump::LoadBinary(const char* pName, char** pData, unsigned int* pSize)
-{
- if (!m_bOpened)
- {
- throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::LoadBinary(): DebugDump is not opened.");
- }
- std::string fullPath = m_sDebugDumpDir + "/" + pName;
- LoadBinaryFile(fullPath, pData, pSize);
+ std::string fullPath = m_sDebugDumpDir + '/' + pName;
+ LoadTextFile(fullPath.c_str(), pData);
}
-void CDebugDump::SaveText(const char* pName, const std::string& pData)
+void CDebugDump::SaveText(const char* pName, const char* pData)
{
if (!m_bOpened)
{
throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::SaveText(): DebugDump is not opened.");
}
std::string fullPath = m_sDebugDumpDir + "/" + pName;
- SaveTextFile(fullPath, pData);
+ SaveBinaryFile(fullPath.c_str(), pData, strlen(pData));
}
-void CDebugDump::SaveBinary(const char* pName, const char* pData, const unsigned int pSize)
+void CDebugDump::SaveBinary(const char* pName, const char* pData, unsigned pSize)
{
if (!m_bOpened)
{
throw CABRTException(EXCEP_DD_OPEN, "CDebugDump::SaveBinary(): DebugDump is not opened.");
}
std::string fullPath = m_sDebugDumpDir + "/" + pName;
- SaveBinaryFile(fullPath, pData, pSize);
+ SaveBinaryFile(fullPath.c_str(), pData, pSize);
}
void CDebugDump::InitGetNextFile()
@@ -535,7 +471,7 @@ bool CDebugDump::GetNextFile(std::string& pFileName, std::string& pContent, bool
{
if (is_regular_file(dent, m_sDebugDumpDir.c_str()))
{
- std::string fullname = m_sDebugDumpDir + "/" + dent->d_name;
+ std::string fullname = m_sDebugDumpDir + '/' + dent->d_name;
pFileName = dent->d_name;
if (IsTextFile(fullname.c_str()))
@@ -545,7 +481,7 @@ bool CDebugDump::GetNextFile(std::string& pFileName, std::string& pContent, bool
}
else
{
- pContent = "";
+ pContent.clear();
pIsTextFile = false;
}
return true;
@@ -555,4 +491,3 @@ bool CDebugDump::GetNextFile(std::string& pFileName, std::string& pContent, bool
m_pGetNextFileDir = NULL;
return false;
}
-