Add more detailed information to any file I/O errors and return true from FileOps::removeFile() if the file does not exist.

This commit is contained in:
Robert Knight 2011-08-20 00:46:07 +01:00
parent 71f197deb7
commit ceb3b7d6a0
2 changed files with 23 additions and 5 deletions

View File

@ -19,6 +19,21 @@
#include <libgen.h> #include <libgen.h>
#endif #endif
FileOps::IOException::IOException(const std::string& error)
: m_errno(0)
{
m_error = error;
#ifdef PLATFORM_UNIX
m_errno = errno;
if (m_errno > 0)
{
m_error += " details: " + std::string(strerror(m_errno));
}
#endif
}
FileOps::IOException::~IOException() throw () FileOps::IOException::~IOException() throw ()
{ {
} }
@ -153,7 +168,10 @@ void FileOps::removeFile(const char* src) throw (IOException)
#ifdef PLATFORM_UNIX #ifdef PLATFORM_UNIX
if (unlink(src) != 0) if (unlink(src) != 0)
{ {
throw IOException("Unable to remove file " + std::string(src)); if (errno != ENOENT)
{
throw IOException("Unable to remove file " + std::string(src));
}
} }
#else #else
throw IOException("not implemented"); throw IOException("not implemented");

View File

@ -3,16 +3,15 @@
#include <exception> #include <exception>
#include <string> #include <string>
#include "StringUtils.h"
class FileOps class FileOps
{ {
public: public:
class IOException : public std::exception class IOException : public std::exception
{ {
public: public:
IOException(const std::string& error) IOException(const std::string& error);
: m_error(error)
{
}
virtual ~IOException() throw (); virtual ~IOException() throw ();
@ -23,6 +22,7 @@ class FileOps
private: private:
std::string m_error; std::string m_error;
int m_errno;
}; };
static bool fileExists(const char* path) throw (IOException); static bool fileExists(const char* path) throw (IOException);