FileSystem: Add CreateSymLink function

This commit is contained in:
chaoticgd 2024-12-09 04:09:34 +00:00 committed by Ty
parent b8ff171127
commit 1ed3001358
3 changed files with 30 additions and 3 deletions

View File

@ -1961,6 +1961,26 @@ bool FileSystem::SetPathCompression(const char* path, bool enable)
return result;
}
bool FileSystem::CreateSymLink(const char* link, const char* target)
{
// convert to wide string
const std::wstring wlink = GetWin32Path(link);
if (wlink.empty())
return false;
const std::wstring wtarget = GetWin32Path(target);
if (wtarget.empty())
return false;
// check if it's a directory
DWORD flags = 0;
if (DirectoryExists(target))
flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
// create the symbolic link
return CreateSymbolicLinkW(wlink.c_str(), wtarget.c_str(), flags) != 0;
}
bool FileSystem::IsSymbolicLink(const char* path)
{
// convert to wide string
@ -2541,6 +2561,11 @@ bool FileSystem::SetPathCompression(const char* path, bool enable)
return false;
}
bool FileSystem::CreateSymLink(const char* link, const char* target)
{
return symlink(target, link) == 0;
}
bool FileSystem::IsSymbolicLink(const char* path)
{
struct stat sysStatData;

View File

@ -178,6 +178,10 @@ namespace FileSystem
/// Does nothing and returns false on non-Windows platforms.
bool SetPathCompression(const char* path, bool enable);
// Creates a symbolic link. Note that on Windows this requires elevated
// privileges so this is mostly useful for testing purposes.
bool CreateSymLink(const char* link, const char* target);
/// Checks if a file or directory is a symbolic link.
bool IsSymbolicLink(const char* path);

View File

@ -7,8 +7,6 @@
#ifdef __linux__
#include <unistd.h>
static std::optional<std::string> create_test_directory()
{
for (u16 i = 0; i < UINT16_MAX; i++)
@ -42,7 +40,7 @@ TEST(FileSystem, RecursiveDeleteDirectoryDontFollowSymbolicLinks)
std::string dir_to_delete = Path::Combine(*test_dir, "dir_to_delete");
ASSERT_TRUE(FileSystem::CreateDirectoryPath(dir_to_delete.c_str(), false));
std::string symlink_path = Path::Combine(dir_to_delete, "link");
ASSERT_EQ(symlink(target_dir.c_str(), symlink_path.c_str()), 0);
ASSERT_TRUE(FileSystem::CreateSymLink(symlink_path.c_str(), target_dir.c_str()));
// Delete the directory containing the symlink.
ASSERT_TRUE(dir_to_delete.starts_with("/tmp/pcsx2_filesystem_test_"));