Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystem: Fix handling of symlinks #11771

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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