From 2a6b32ce5b342becee227b724efd2719ddb58dd1 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Thu, 7 Jul 2016 14:54:47 -0700 Subject: [PATCH] Refactored followsymlink --- src/libpsl-native/src/followsymlink.cpp | 35 +++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/libpsl-native/src/followsymlink.cpp b/src/libpsl-native/src/followsymlink.cpp index d4308f2df..24a544664 100644 --- a/src/libpsl-native/src/followsymlink.cpp +++ b/src/libpsl-native/src/followsymlink.cpp @@ -8,6 +8,7 @@ #include #include "followsymlink.h" #include "issymlink.h" + //! @brief Followsymlink determines target path of a sym link //! //! Followsymlink @@ -38,26 +39,30 @@ char* FollowSymLink(const char* fileName) { errno = 0; - // Check parameters + // if filename is null, return null value if (!fileName) { errno = ERROR_INVALID_PARAMETER; return NULL; } - char actualpath[PATH_MAX+1]; - char* realPath = realpath(fileName, actualpath); - - /* if realPath does not return a value, continue with previous implementation of symlink*/ - if (IsSymLink(fileName)) + //if lstat in IsSymLink returns -1, path does not exist + if (IsSymLink(fileName) == -1) { + printf("-1 %s", fileName); + return NULL; + } + + /*If the path is a symlink, the function return the absolute filepath if valid. if not valid, return null*/ + if (IsSymLink(fileName) == 1) + { + printf("readlink %s", fileName); char buffer[PATH_MAX]; ssize_t sz = readlink(fileName, buffer, PATH_MAX); if (sz == -1) { switch(errno) - { case EACCES: errno = ERROR_ACCESS_DENIED; @@ -89,16 +94,23 @@ char* FollowSymLink(const char* fileName) default: errno = ERROR_INVALID_FUNCTION; } + return NULL; } buffer[sz] = '\0'; return strndup(buffer, sz + 1); - } - -/*else realpath returned a valid resolved path*/ + + } + + /*else the path is not a symlink - but attempt to resolve*/ else { + printf("realpath %s", fileName); + //Attempt to resolve with the absolute filepath + char actualpath[PATH_MAX+1]; + char* realPath = realpath(fileName, actualpath); + if (sizeof(realPath) == -1) { switch(errno) @@ -133,9 +145,10 @@ char* FollowSymLink(const char* fileName) default: errno = ERROR_INVALID_FUNCTION; } + return NULL; } - return strndup(realPath, strlen(realPath) + 1 ); + return strndup(realPath, strlen(realPath) + 1 ); } }