Skip to content

Commit

Permalink
FileSystem: Add CreateSymLink function
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoticgd authored and F0bes committed Dec 14, 2024
1 parent b8ff171 commit 1ed3001
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
25 changes: 25 additions & 0 deletions common/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions common/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions tests/ctest/common/filesystem_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -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_"));
Expand Down

0 comments on commit 1ed3001

Please sign in to comment.