realpath.cpp
Go to the documentation of this file.
00001 
00006 /*****************************************************************************
00007 ** Platform Check
00008 *****************************************************************************/
00009 
00010 #include <ecl/filesystem/config.hpp>
00011 
00012 #if defined(ECL_PRIVATE_HAS_POSIX_REALPATH)
00013 
00014 /*****************************************************************************
00015 ** Includes
00016 *****************************************************************************/
00017 
00018 #include <iostream>
00019 #include <errno.h>
00020 #include <climits>  // limits.h
00021 #include <cstdlib>  // stdlib.h
00022 #include <string>
00023 #include <ecl/errors/handlers.hpp>
00024 #include "../../include/ecl/filesystem/realpath.hpp"
00025 
00026 /*****************************************************************************
00027 ** Namespaces
00028 *****************************************************************************/
00029 
00030 namespace ecl {
00031 
00032 /*****************************************************************************
00033 ** Implementation [realpath]
00034 *****************************************************************************/
00035 
00036 ecl_filesystem_PUBLIC ecl::Error realpath(const std::string& path, std::string& absolute_path) {
00037   int path_max;
00038   // see the man page for realpath for details
00039   #ifdef PATH_MAX
00040     path_max = PATH_MAX; /* PATH_MAX from limits.h, but not always defined */
00041   #else
00042     path_max = pathconf(path, _PC_PATH_MAX);
00043     if (path_max <= 0) { path_max = 4096; } /* Not guaranteed to give you results */
00044   #endif
00045 
00046   char buffer[path_max];
00047   char *result = ::realpath(path.c_str(), buffer);
00048   absolute_path = buffer;
00049   if ( result != NULL ) {
00050     absolute_path = buffer;
00051   } else {
00052     switch(errno) {
00053       case(EACCES)       : { return Error(PermissionsError); }   // Read or search permission was denied for a component of the path prefix.
00054       case(EINVAL)       : { return Error(InvalidArgError); }    // path is NULL
00055       case(EIO)          : { return Error(ReadError); }          // An I/O error occurred while reading from the filesystem.
00056       case(ELOOP)        : { return Error(InvalidObjectError); } // too many symbolic links were encountered in translating the pathname
00057       case(ENAMETOOLONG) : { return Error(InvalidArgError); }    // A component of a pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX characters.
00058       case(ENOENT)       : { return Error(NotFoundError); }      // The named file does not exist
00059       case(ENOTDIR)      : { return Error(InvalidObjectError); } // A component of the path prefix is not a directory.
00060       default            : { return Error(UnknownError); }
00061     }
00062   }
00063   return Error(NoError);
00064 }
00065 
00066 }; // namespace ecl
00067 
00068 #endif /* ECL_PRIVATE_HAS_POSIX_REALPATH */
00069 


ecl_filesystem
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:17:27