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.cxx | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'butl/path.cxx') diff --git a/butl/path.cxx b/butl/path.cxx index 5b68749..3e82190 100644 --- a/butl/path.cxx +++ b/butl/path.cxx @@ -6,14 +6,15 @@ #ifdef _WIN32 # include // _MAX_PATH -# include // _[w]getcwd, _[w]chdir +# include // _[w]getcwd(), _[w]chdir() #else # include // EINVAL -# include // mbstowcs, wcstombs +# include // mbstowcs(), wcstombs(), realpath() # include // PATH_MAX -# include // getcwd, chdir +# include // getcwd(), chdir() #endif +#include #include #ifndef _WIN32 @@ -68,6 +69,27 @@ namespace butl #endif } +#ifndef _WIN32 + template <> + void path_traits:: + realize (string_type& s) + { + char r[PATH_MAX]; + if (realpath (s.c_str (), r) == nullptr) + { + // @@ If there were a message in invalid_basic_path, we could have said + // what exactly is wrong with the path. + // + if (errno == EACCES || errno == ENOENT || errno == ENOTDIR) + throw invalid_basic_path (s); + else + throw system_error (errno, system_category ()); + } + + s = r; + } +#endif + // // wchar_t // @@ -112,4 +134,13 @@ namespace butl throw system_error (errno, system_category ()); #endif } + +#ifndef _WIN32 + template <> + void path_traits:: + realize (string_type&) + { + assert (false); // Implement if/when needed. + } +#endif } -- cgit v1.1