diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-10-17 20:30:45 +0100 | 
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-10-17 20:30:45 +0100 | 
| commit | e1ebd204225b528e057fc9b657e61447853178d6 (patch) | |
| tree | a0bfb06f05987aef34b97e602527cf7ccfd4c96c | |
| parent | Test and fix multibyte behaviour in lexer-regex (diff) | |
| download | libadhocutil-e1ebd204225b528e057fc9b657e61447853178d6.tar.bz2 libadhocutil-e1ebd204225b528e057fc9b657e61447853178d6.tar.xz libadhocutil-e1ebd204225b528e057fc9b657e61447853178d6.zip  | |
Add file handle constructor that accepts mode.libadhocutil-0.3.7
Add SystemException that includes errno
| -rw-r--r-- | libadhocutil/fileUtils.cpp | 15 | ||||
| -rw-r--r-- | libadhocutil/fileUtils.h | 9 | ||||
| -rw-r--r-- | libadhocutil/sys.ice | 9 | ||||
| -rw-r--r-- | libadhocutil/sysImpl.cpp | 10 | ||||
| -rw-r--r-- | libadhocutil/unittests/Jamfile.jam | 1 | ||||
| -rw-r--r-- | libadhocutil/unittests/testFileUtils.cpp | 5 | 
6 files changed, 44 insertions, 5 deletions
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 <unistd.h> +#include <sys.h>  #include <sys/mman.h>  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 <sys.h> +#include <boost/format.hpp> + +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  	: : :  	<define>BOOST_TEST_DYN_LINK  	<library>..//adhocutil +	<implicit-dependency>..//adhocutil  	<library>boost_utf  	<define>ROOT=\"$(me)\"  	<library>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 <fileUtils.h>  #include <definedDirs.h> +#include <sys.h>  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 )  | 
