FileSystem: Fix handling of symlinks

Revert of c8a3e5a9ec
This commit is contained in:
KamFretoZ 2024-09-02 19:49:09 +07:00 committed by Ty
parent 90463a4a6c
commit 1ea512655a
2 changed files with 32 additions and 7 deletions

View File

@ -1650,6 +1650,21 @@ bool FileSystem::DirectoryExists(const char* path)
return false; 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) bool FileSystem::DirectoryIsEmpty(const char* path)
{ {
std::wstring wpath = GetWin32Path(path); std::wstring wpath = GetWin32Path(path);
@ -1962,7 +1977,7 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co
outData.Attributes = 0; outData.Attributes = 0;
struct stat sDir; struct stat sDir;
if (lstat(full_path.c_str(), &sDir) < 0) if (stat(full_path.c_str(), &sDir) < 0)
continue; continue;
if (S_ISDIR(sDir.st_mode)) if (S_ISDIR(sDir.st_mode))
@ -2101,7 +2116,7 @@ bool FileSystem::StatFile(const char* path, FILESYSTEM_STAT_DATA* sd)
// stat file // stat file
struct stat sysStatData; struct stat sysStatData;
if (lstat(path, &sysStatData) < 0) if (stat(path, &sysStatData) < 0)
return false; return false;
// parse attributes // parse attributes
@ -2157,7 +2172,7 @@ bool FileSystem::FileExists(const char* path)
// stat file // stat file
struct stat sysStatData; struct stat sysStatData;
if (lstat(path, &sysStatData) < 0) if (stat(path, &sysStatData) < 0)
return false; return false;
if (S_ISDIR(sysStatData.st_mode)) if (S_ISDIR(sysStatData.st_mode))
@ -2174,7 +2189,7 @@ bool FileSystem::DirectoryExists(const char* path)
// stat file // stat file
struct stat sysStatData; struct stat sysStatData;
if (lstat(path, &sysStatData) < 0) if (stat(path, &sysStatData) < 0)
return false; return false;
if (S_ISDIR(sysStatData.st_mode)) if (S_ISDIR(sysStatData.st_mode))
@ -2183,6 +2198,15 @@ bool FileSystem::DirectoryExists(const char* path)
return false; 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) bool FileSystem::DirectoryIsEmpty(const char* path)
{ {
DIR* pDir = opendir(path); DIR* pDir = opendir(path);
@ -2224,7 +2248,7 @@ bool FileSystem::CreateDirectoryPath(const char* path, bool recursive, Error* er
{ {
// check the attributes // check the attributes
struct stat sysStatData; 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; return true;
} }
@ -2294,7 +2318,7 @@ bool FileSystem::DeleteFilePath(const char* path, Error* error)
} }
struct stat sysStatData; 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."); Error::SetStringView(error, "File does not exist.");
return false; return false;
@ -2334,7 +2358,7 @@ bool FileSystem::DeleteDirectory(const char* path)
return false; return false;
struct stat sysStatData; 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 false;
return (rmdir(path) == 0); return (rmdir(path) == 0);

View File

@ -84,6 +84,7 @@ namespace FileSystem
/// Directory exists? /// Directory exists?
bool DirectoryExists(const char* path); bool DirectoryExists(const char* path);
bool IsRealDirectory(const char* path);
/// Directory does not contain any files? /// Directory does not contain any files?
bool DirectoryIsEmpty(const char* path); bool DirectoryIsEmpty(const char* path);