lib/realpath.cpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7 ** Platform Check
8 *****************************************************************************/
9 
10 #include <ecl/filesystem/config.hpp>
11 
12 #if defined(ECL_PRIVATE_HAS_POSIX_REALPATH)
13 
14 /*****************************************************************************
15 ** Includes
16 *****************************************************************************/
17 
18 #include <iostream>
19 #include <errno.h>
20 #include <climits> // limits.h
21 #include <cstdlib> // stdlib.h
22 #include <string>
23 #include <ecl/errors/handlers.hpp>
24 #include "../../include/ecl/filesystem/realpath.hpp"
25 
26 /*****************************************************************************
27 ** Namespaces
28 *****************************************************************************/
29 
30 namespace ecl {
31 
32 /*****************************************************************************
33 ** Implementation [realpath]
34 *****************************************************************************/
35 
36 ecl_filesystem_PUBLIC ecl::Error realpath(const std::string& path, std::string& absolute_path) {
37  int path_max;
38  // see the man page for realpath for details
39  #ifdef PATH_MAX
40  path_max = PATH_MAX; /* PATH_MAX from limits.h, but not always defined */
41  #else
42  path_max = pathconf(path, _PC_PATH_MAX);
43  if (path_max <= 0) { path_max = 4096; } /* Not guaranteed to give you results */
44  #endif
45 
46  char buffer[path_max];
47  char *result = ::realpath(path.c_str(), buffer);
48  absolute_path = buffer;
49  if ( result != NULL ) {
50  absolute_path = buffer;
51  } else {
52  switch(errno) {
53  case(EACCES) : { return Error(PermissionsError); } // Read or search permission was denied for a component of the path prefix.
54  case(EINVAL) : { return Error(InvalidArgError); } // path is NULL
55  case(EIO) : { return Error(ReadError); } // An I/O error occurred while reading from the filesystem.
56  case(ELOOP) : { return Error(InvalidObjectError); } // too many symbolic links were encountered in translating the pathname
57  case(ENAMETOOLONG) : { return Error(InvalidArgError); } // A component of a pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX characters.
58  case(ENOENT) : { return Error(NotFoundError); } // The named file does not exist
59  case(ENOTDIR) : { return Error(InvalidObjectError); } // A component of the path prefix is not a directory.
60  default : { return Error(UnknownError); }
61  }
62  }
63  return Error(NoError);
64 }
65 
66 }; // namespace ecl
67 
68 #endif /* ECL_PRIVATE_HAS_POSIX_REALPATH */
69 
InvalidObjectError
#define ecl_filesystem_PUBLIC
Definition: macros.hpp:33
PermissionsError
InvalidArgError
UnknownError
NotFoundError


ecl_filesystem
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:13