Skip to content

Commit

Permalink
FileSystem: Fix handling of symlinks
Browse files Browse the repository at this point in the history
Revert of c8a3e5a
  • Loading branch information
kamfretoz authored and F0bes committed Sep 2, 2024
1 parent 90463a4 commit 1ea5126
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
38 changes: 31 additions & 7 deletions common/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,21 @@ bool FileSystem::DirectoryExists(const char* path)
return false;
}

bool FileSystem::IsRealDirectory(const char* path)
{
// convert to wide string
const std::wstring wpath = GetWin32Path(path);
if (wpath.empty())
return false;

// determine attributes for the path. if it's a directory, things have to be handled differently..
const DWORD fileAttributes = GetFileAttributesW(wpath.c_str());
if (fileAttributes == INVALID_FILE_ATTRIBUTES)
return false;

return ((fileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) != FILE_ATTRIBUTE_DIRECTORY);
}

bool FileSystem::DirectoryIsEmpty(const char* path)
{
std::wstring wpath = GetWin32Path(path);
Expand Down Expand Up @@ -1962,7 +1977,7 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
outData.Attributes = 0;

struct stat sDir;
if (lstat(full_path.c_str(), &sDir) < 0)
if (stat(full_path.c_str(), &sDir) < 0)
continue;

if (S_ISDIR(sDir.st_mode))
Expand Down Expand Up @@ -2101,7 +2116,7 @@ bool FileSystem::StatFile(const char* path, FILESYSTEM_STAT_DATA* sd)

// stat file
struct stat sysStatData;
if (lstat(path, &sysStatData) < 0)
if (stat(path, &sysStatData) < 0)
return false;

// parse attributes
Expand Down Expand Up @@ -2157,7 +2172,7 @@ bool FileSystem::FileExists(const char* path)

// stat file
struct stat sysStatData;
if (lstat(path, &sysStatData) < 0)
if (stat(path, &sysStatData) < 0)
return false;

if (S_ISDIR(sysStatData.st_mode))
Expand All @@ -2174,7 +2189,7 @@ bool FileSystem::DirectoryExists(const char* path)

// stat file
struct stat sysStatData;
if (lstat(path, &sysStatData) < 0)
if (stat(path, &sysStatData) < 0)
return false;

if (S_ISDIR(sysStatData.st_mode))
Expand All @@ -2183,6 +2198,15 @@ bool FileSystem::DirectoryExists(const char* path)
return false;
}

bool FileSystem::IsRealDirectory(const char* path)
{
struct stat sysStatData;
if (lstat(path, &sysStatData) < 0)
return false;

return (S_ISDIR(sysStatData.st_mode) && !S_ISLNK(sysStatData.st_mode));
}

bool FileSystem::DirectoryIsEmpty(const char* path)
{
DIR* pDir = opendir(path);
Expand Down Expand Up @@ -2224,7 +2248,7 @@ bool FileSystem::CreateDirectoryPath(const char* path, bool recursive, Error* er
{
// check the attributes
struct stat sysStatData;
if (lstat(path, &sysStatData) == 0 && S_ISDIR(sysStatData.st_mode))
if (stat(path, &sysStatData) == 0 && S_ISDIR(sysStatData.st_mode))
return true;
}

Expand Down Expand Up @@ -2294,7 +2318,7 @@ bool FileSystem::DeleteFilePath(const char* path, Error* error)
}

struct stat sysStatData;
if (lstat(path, &sysStatData) != 0 || S_ISDIR(sysStatData.st_mode))
if (stat(path, &sysStatData) != 0 || S_ISDIR(sysStatData.st_mode))
{
Error::SetStringView(error, "File does not exist.");
return false;
Expand Down Expand Up @@ -2334,7 +2358,7 @@ bool FileSystem::DeleteDirectory(const char* path)
return false;

struct stat sysStatData;
if (lstat(path, &sysStatData) != 0 || !S_ISDIR(sysStatData.st_mode))
if (stat(path, &sysStatData) != 0 || !S_ISDIR(sysStatData.st_mode))
return false;

return (rmdir(path) == 0);
Expand Down
1 change: 1 addition & 0 deletions common/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ namespace FileSystem

/// Directory exists?
bool DirectoryExists(const char* path);
bool IsRealDirectory(const char* path);

/// Directory does not contain any files?
bool DirectoryIsEmpty(const char* path);
Expand Down

0 comments on commit 1ea5126

Please sign in to comment.