mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Refactored followsymlink
This commit is contained in:
1 parent
64496d7bcc
commit
2a6b32ce5b
1 file changed
+24
-11
@@ -8,6 +8,7 @@
|
||||
#include <iostream>
|
||||
#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 );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user