From cf63a35e944994f845c90acb8e327e055562647f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 26 Apr 2012 18:19:12 +1000 Subject: [PATCH] Give fd_ref a fs::path constructor --- io.cpp | 10 +++++++++- io.hpp | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/io.cpp b/io.cpp index 69e8eae0..dd3b0729 100644 --- a/io.cpp +++ b/io.cpp @@ -35,7 +35,7 @@ using namespace util; //---------------------------------------------------------------------------- std::unique_ptr util::slurp (const boost::filesystem::path& path) { - fd_ref fd(open (path.string ().c_str (), O_RDONLY)); // | O_CLOEXEC)); + fd_ref fd(path); // Calculate the total file size off_t size = lseek (fd, 0, SEEK_END); @@ -77,6 +77,14 @@ fd_ref::fd_ref (int _fd): } +fd_ref::fd_ref (const boost::filesystem::path &path): + fd (open (path.string ().c_str (), O_RDONLY)) +{ + if (fd < 0) + throw path_error (path); +} + + fd_ref::~fd_ref () { check (fd >= 0); close (fd); diff --git a/io.hpp b/io.hpp index b7003bc9..a42f0fc4 100644 --- a/io.hpp +++ b/io.hpp @@ -49,7 +49,8 @@ namespace util { public: int fd; - fd_ref (int _fd); + explicit fd_ref (int _fd); + explicit fd_ref (const boost::filesystem::path &); ~fd_ref (); operator int (void) const; @@ -100,6 +101,13 @@ namespace util { size_t size (void) const; }; #endif + + class path_error : public std::runtime_error { + public: + path_error (const boost::filesystem::path &path): + runtime_error ("Invalid path " + path.string ()) + { ; } + }; } #endif