From 70358cabdd7b0b45992eb209a75cef2c5d86fa52 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 31 Mar 2011 18:00:07 +0200 Subject: [PATCH] CFile: Add an error flag Whenever an error happens with some file operation, the error flag is now set. This is especially interesting for tracking errors from Write(). Signed-off-by: Uli Schlachter --- FileUtils.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++-------- FileUtils.h | 4 ++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/FileUtils.cpp b/FileUtils.cpp index 34877914..c37e3e05 100644 --- a/FileUtils.cpp +++ b/FileUtils.cpp @@ -26,11 +26,13 @@ CString CFile::m_sHomePath; CFile::CFile() { m_iFD = -1; + ResetError(); } CFile::CFile(const CString& sLongName) { m_iFD = -1; + ResetError(); SetFileName(sLongName); } @@ -167,13 +169,25 @@ int CFile::GetInfo(const CString& sFile, struct stat& st) { // // Functions to manipulate the file on the filesystem // -bool CFile::Delete() { return CFile::Delete(m_sLongName); } +bool CFile::Delete() { + if (CFile::Delete(m_sLongName)) + return true; + m_bHadError = true; + return false; +} + bool CFile::Move(const CString& sNewFileName, bool bOverwrite) { - return CFile::Move(m_sLongName, sNewFileName, bOverwrite); + if (CFile::Move(m_sLongName, sNewFileName, bOverwrite)) + return true; + m_bHadError = true; + return false; } bool CFile::Copy(const CString& sNewFileName, bool bOverwrite) { - return CFile::Copy(m_sLongName, sNewFileName, bOverwrite); + if (CFile::Copy(m_sLongName, sNewFileName, bOverwrite)) + return true; + m_bHadError = true; + return false; } bool CFile::Delete(const CString& sFileName) { @@ -235,7 +249,11 @@ bool CFile::Chmod(mode_t mode) { if (m_iFD == -1) { return false; } - return (fchmod(m_iFD, mode) == 0); + if (fchmod(m_iFD, mode) != 0) { + m_bHadError = true; + return false; + } + return true; } bool CFile::Chmod(const CString& sFile, mode_t mode) { @@ -248,6 +266,8 @@ bool CFile::Seek(off_t uPos) { return true; } + m_bHadError = true; + return false; } @@ -257,11 +277,16 @@ bool CFile::Truncate() { return true; } + m_bHadError = true; + return false; } bool CFile::Sync() { - return (m_iFD != -1 && fsync(m_iFD) == 0); + if (m_iFD != -1 && fsync(m_iFD) == 0) + return true; + m_bHadError = true; + return false; } bool CFile::Open(const CString& sFileName, int iFlags, mode_t iMode) { @@ -271,6 +296,7 @@ bool CFile::Open(const CString& sFileName, int iFlags, mode_t iMode) { bool CFile::Open(int iFlags, mode_t iMode) { if (m_iFD != -1) { + m_bHadError = true; return false; } @@ -282,8 +308,10 @@ bool CFile::Open(int iFlags, mode_t iMode) { iMode |= O_BINARY; m_iFD = open(m_sLongName.c_str(), iFlags, iMode); - if (m_iFD < 0) + if (m_iFD < 0) { + m_bHadError = true; return false; + } /* Make sure this FD isn't given to childs */ SetFdCloseOnExec(m_iFD); @@ -296,7 +324,10 @@ int CFile::Read(char *pszBuffer, int iBytes) { return -1; } - return read(m_iFD, pszBuffer, iBytes); + int res = read(m_iFD, pszBuffer, iBytes); + if (res != iBytes) + m_bHadError = true; + return res; } bool CFile::ReadLine(CString& sData, const CString & sDelimiter) { @@ -366,7 +397,10 @@ int CFile::Write(const char *pszBuffer, u_int iBytes) { return -1; } - return write(m_iFD, pszBuffer, iBytes); + u_int res = write(m_iFD, pszBuffer, iBytes); + if (res != iBytes) + m_bHadError = true; + return res; } int CFile::Write(const CString & sData) { @@ -375,6 +409,7 @@ int CFile::Write(const CString & sData) { void CFile::Close() { if (m_iFD >= 0) { if (close(m_iFD) < 0) { + m_bHadError = true; DEBUG("CFile::Close(): close() failed with [" << strerror(errno) << "]"); } diff --git a/FileUtils.h b/FileUtils.h index a3c061bb..3323cb5c 100644 --- a/FileUtils.h +++ b/FileUtils.h @@ -120,6 +120,9 @@ public: CString GetShortName() const; CString GetDir() const; + bool HadError() const { return m_bHadError; } + void ResetError() { m_bHadError = false; } + static void InitHomePath(const CString& sFallback); static const CString& GetHomePath() { return m_sHomePath; } @@ -129,6 +132,7 @@ private: CString m_sBuffer; int m_iFD; + bool m_bHadError; static CString m_sHomePath;