From e4a63cd55c6347dd24938ec8a1ef203409be058e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 12 Feb 2016 09:08:32 +0200 Subject: Add path::realize(), which on POSIX calls realpath(3) --- butl/path | 80 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 33 deletions(-) (limited to 'butl/path') diff --git a/butl/path b/butl/path index c600e04..1fdc8b6 100644 --- a/butl/path +++ b/butl/path @@ -14,6 +14,33 @@ namespace butl { + class invalid_path_base: std::exception + { + public: + virtual char const* + what () const throw (); + }; + + template + class invalid_basic_path: public invalid_path_base + { + public: + typedef std::basic_string string_type; + + invalid_basic_path (C const* p): path_ (p) {} + invalid_basic_path (string_type const& p): path_ (p) {} + ~invalid_basic_path () throw () {} + + string_type const& + path () const + { + return path_; + } + + private: + string_type path_; + }; + template struct path_traits { @@ -123,8 +150,8 @@ namespace butl return ln < rn ? -1 : (ln > rn ? 1 : 0); } - // Get/set current working directory. Throw std::system_error - // to report the underlying OS errors. + // Get/set current working directory. Throw std::system_error to report + // the underlying OS errors. // static string_type current (); @@ -132,6 +159,15 @@ namespace butl static void current (string_type const&); + // Make the path real (by calling realpath(3)). Throw invalid_basic_path + // if the path is invalid (e.g., some components do not exist) and + // std::system_error to report other underlying OS errors. + // +#ifndef _WIN32 + static void + realize (string_type&); +#endif + private: #ifdef _WIN32 static C @@ -179,35 +215,6 @@ namespace butl typedef basic_path> dir_wpath; typedef invalid_basic_path invalid_wpath; - // - // - class invalid_path_base: std::exception - { - public: - virtual char const* - what () const throw (); - }; - - template - class invalid_basic_path: public invalid_path_base - { - public: - typedef std::basic_string string_type; - - invalid_basic_path (C const* p): path_ (p) {} - invalid_basic_path (string_type const& p): path_ (p) {} - ~invalid_basic_path () throw () {} - - string_type const& - path () const - { - return path_; - } - - private: - string_type path_; - }; - template class path_data { @@ -477,17 +484,24 @@ namespace butl // Normalize the path. This includes collapsing the '.' and '..' // directories if possible, collapsing multiple directory // separators, and converting all directory separators to the - // canonical form. Returns *this. + // canonical form. Return *this. // basic_path& normalize (); // Make the path absolute using the current directory unless - // it is already absolute. + // it is already absolute. Return *this. // basic_path& complete (); + // Make the path real, that is, absolute, normalized, and with resolved + // symlinks. On POSIX systems this is accomplished with the call to + // realpath(3). On Windows -- complete() and normalize(). Return *this. + // + basic_path& + realize (); + public: basic_path& operator/= (basic_path const&); -- cgit v1.1