From e1ebd204225b528e057fc9b657e61447853178d6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 17 Oct 2016 20:30:45 +0100 Subject: Add file handle constructor that accepts mode. Add SystemException that includes errno --- libadhocutil/fileUtils.cpp | 15 ++++++++++++--- libadhocutil/fileUtils.h | 9 +++++++++ libadhocutil/sys.ice | 9 +++++++++ libadhocutil/sysImpl.cpp | 10 ++++++++++ libadhocutil/unittests/Jamfile.jam | 1 + libadhocutil/unittests/testFileUtils.cpp | 5 +++-- 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 libadhocutil/sys.ice create mode 100644 libadhocutil/sysImpl.cpp diff --git a/libadhocutil/fileUtils.cpp b/libadhocutil/fileUtils.cpp index 7b90e4b..c6acc53 100644 --- a/libadhocutil/fileUtils.cpp +++ b/libadhocutil/fileUtils.cpp @@ -1,5 +1,6 @@ #include "fileUtils.h" #include +#include #include namespace AdHoc { @@ -15,7 +16,15 @@ namespace AdHoc { fh(open(path.c_str(), flags)) { if (fh < 0) { - throw std::runtime_error("Failed to open " + path.string()); + throw SystemException("Failed to open " + path.string(), strerror(errno), errno); + } + } + + FileHandle::FileHandle(const boost::filesystem::path & path, int flags, int mode) : + fh(open(path.c_str(), flags, mode)) + { + if (fh < 0) { + throw SystemException("Failed to open " + path.string(), strerror(errno), errno); } } @@ -29,7 +38,7 @@ namespace AdHoc { { if (fstat(fh, &st)) { // LCOV_EXCL_START can't think of a way to test open succeeding and fstat failing - throw std::runtime_error("Failed to stat " + path.string()); + throw SystemException("Failed to fstat " + path.string(), strerror(errno), errno); // LCOV_EXCL_STOP } } @@ -45,7 +54,7 @@ namespace AdHoc { data(mmap(0, st.st_size, PROT_READ, MAP_SHARED, fh, 0)) { if (data == (void*)-1) { - throw std::runtime_error("Failed to mmap " + path.string()); + throw SystemException("Failed to mmap " + path.string(), strerror(errno), errno); } } diff --git a/libadhocutil/fileUtils.h b/libadhocutil/fileUtils.h index 939a8ba..6ab20d3 100644 --- a/libadhocutil/fileUtils.h +++ b/libadhocutil/fileUtils.h @@ -27,6 +27,15 @@ namespace AdHoc { * @param flags File handle flags */ FileHandle(const boost::filesystem::path & path, int flags = O_RDONLY); + + /** + * Open a new file handle. + * @param path Path of file to open. + * @param flags File handle flags + * @param mode File handle mode + */ + FileHandle(const boost::filesystem::path & path, int flags, int mode); + virtual ~FileHandle(); /// The file handle. diff --git a/libadhocutil/sys.ice b/libadhocutil/sys.ice new file mode 100644 index 0000000..2704aa6 --- /dev/null +++ b/libadhocutil/sys.ice @@ -0,0 +1,9 @@ +module AdHoc { + ["cpp:ice_print"] + exception SystemException { + string task; + string message; + int errNo; + }; +}; + diff --git a/libadhocutil/sysImpl.cpp b/libadhocutil/sysImpl.cpp new file mode 100644 index 0000000..711c098 --- /dev/null +++ b/libadhocutil/sysImpl.cpp @@ -0,0 +1,10 @@ +#include +#include + +namespace AdHoc { + static boost::format e("%s (%d:%s)"); + void SystemException::ice_print(std::ostream & s) const + { + s << e % task % errNo % message; + } +} diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index b27f1d6..bf86e9c 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -243,6 +243,7 @@ run : : : BOOST_TEST_DYN_LINK ..//adhocutil + ..//adhocutil boost_utf ROOT=\"$(me)\" boost_system diff --git a/libadhocutil/unittests/testFileUtils.cpp b/libadhocutil/unittests/testFileUtils.cpp index 88521d3..8d4b38f 100644 --- a/libadhocutil/unittests/testFileUtils.cpp +++ b/libadhocutil/unittests/testFileUtils.cpp @@ -3,6 +3,7 @@ #include #include +#include BOOST_AUTO_TEST_CASE( memmap ) { @@ -16,14 +17,14 @@ BOOST_AUTO_TEST_CASE( openfail ) { BOOST_REQUIRE_THROW({ AdHoc::FileUtils::MemMap f("/tmp/nothere"); - }, std::runtime_error); + }, AdHoc::SystemException); } BOOST_AUTO_TEST_CASE( mapfail ) { BOOST_REQUIRE_THROW({ AdHoc::FileUtils::MemMap f("/dev/null"); - }, std::runtime_error); + }, AdHoc::SystemException); } BOOST_AUTO_TEST_CASE( pathPart ) -- cgit v1.2.3