From: Mikko Rasa Date: Thu, 28 Jul 2011 12:10:13 +0000 (+0300) Subject: Exception rework for fs components X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=19edaf3b45bc4c23459fc8ddd552dcfa33eecc71;p=libs%2Fcore.git Exception rework for fs components --- diff --git a/source/fs/dir.cpp b/source/fs/dir.cpp index 0832854..c53c156 100644 --- a/source/fs/dir.cpp +++ b/source/fs/dir.cpp @@ -5,7 +5,7 @@ #ifdef WIN32 #include #endif -#include +#include #include #include #include "dir.h" @@ -54,6 +54,12 @@ const Path &get_bin_dir(const string &argv0) } + +not_a_directory::not_a_directory(const Path &p): + runtime_error(p.str()) +{ } + + void mkdir(const Path &path, int mode) { int err; @@ -66,7 +72,7 @@ void mkdir(const Path &path, int mode) #endif if(err==-1) - throw SystemError("mkdir failed", errno); + throw system_error("mkdir"); } void mkpath(const Path &path, int mode) @@ -84,11 +90,11 @@ void mkpath(const Path &path, int mode) if(err==0) { if(!S_ISDIR(st.st_mode)) - throw Exception("A component exists and is not a directory"); + throw not_a_directory(p); continue; } else if(errno!=ENOENT) - throw SystemError("stat failed", errno); + throw system_error("mkpath:stat"); else mkdir(p, mode); } @@ -97,7 +103,7 @@ void mkpath(const Path &path, int mode) void rmdir(const Path &path) { if(::rmdir(path.str().c_str())==-1) - throw SystemError("rmdir failed", errno); + throw system_error("rmdir"); } void rmdirs(const Path &path) @@ -213,7 +219,7 @@ Path get_sys_lib_dir(const string &argv0, const string &appname) void chdir(const Path &path) { if(::chdir(path.str().c_str())==-1) - throw SystemError("chdir failed", errno); + throw system_error("chdir"); } } // namespace FS diff --git a/source/fs/dir.h b/source/fs/dir.h index 5544a31..6fa49f9 100644 --- a/source/fs/dir.h +++ b/source/fs/dir.h @@ -8,6 +8,13 @@ namespace Msp { namespace FS { +class not_a_directory: public std::runtime_error +{ +public: + not_a_directory(const Path &); + virtual ~not_a_directory() throw() { } +}; + /// Creates a directory void mkdir(const Path &path, int mode); diff --git a/source/fs/path.cpp b/source/fs/path.cpp index 52b1da2..784c6eb 100644 --- a/source/fs/path.cpp +++ b/source/fs/path.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include "path.h" #include "utils.h" @@ -103,7 +103,7 @@ string Path::operator[](int n) const } } - throw InvalidParameterValue("Path component index out of range"); + throw invalid_argument("Path::operator[]"); } bool Path::operator==(const Path &p) const diff --git a/source/fs/stat.cpp b/source/fs/stat.cpp index 867256e..ccd344b 100644 --- a/source/fs/stat.cpp +++ b/source/fs/stat.cpp @@ -1,8 +1,7 @@ -#include #ifdef WIN32 #include #endif -#include +#include #include "path.h" #include "stat.h" @@ -18,7 +17,7 @@ struct stat stat(const Path &fn) { struct stat st; if(stat(fn, st)==-1) - throw SystemError("stat failed", errno); + throw system_error("stat"); return st; } @@ -35,7 +34,7 @@ struct stat lstat(const Path &fn) { struct stat st; if(lstat(fn, st)==-1) - throw SystemError("lstat failed", errno); + throw system_error("lstat"); return st; } diff --git a/source/fs/utils.cpp b/source/fs/utils.cpp index 0c29a53..d34a750 100644 --- a/source/fs/utils.cpp +++ b/source/fs/utils.cpp @@ -1,6 +1,5 @@ -#include #include -#include +#include #ifndef WIN32 #include #else @@ -83,12 +82,12 @@ Path readlink(const Path &link) { #ifdef WIN32 (void)link; - throw Exception("No symbolic links on win32"); + throw logic_error("no symbolic links on win32"); #else char buf[4096]; int len = ::readlink(link.str().c_str(), buf, sizeof(buf)); if(len==-1) - throw SystemError("readlink failed", errno); + throw system_error("readlink"); return string(buf, len); #endif } @@ -119,7 +118,7 @@ Path realpath(const Path &path) if(S_ISLNK(st.st_mode)) { if(++n_links>64) - throw Exception("Ludicrous amount of symlinks detected in realpath, giving up"); + throw runtime_error("too many symbolic links"); Path link = readlink(next); queue.insert(queue.begin(), link.begin(), link.end()); } @@ -134,13 +133,13 @@ Path realpath(const Path &path) void rename(const Path &from, const Path &to) { if(::rename(from.str().c_str(), to.str().c_str())==-1) - throw SystemError("rename failed", errno); + throw system_error("rename"); } void unlink(const Path &path) { if(::unlink(path.str().c_str())==-1) - throw SystemError("unlink failed", errno); + throw system_error("unlink"); } Path relative(const Path &path, const Path &base)