createSymlinkIfSupportedByFilesystem(): Add support for directory symlinks used on Windows

This commit is contained in:
Kamil Śliwak 2021-07-03 00:02:58 +02:00
parent 1f953486ee
commit 618ba2fb9a
3 changed files with 16 additions and 7 deletions

View File

@ -39,7 +39,7 @@ void solidity::test::createFilesWithParentDirs(set<boost::filesystem::path> cons
} }
} }
void solidity::test::createFileWithContent(boost::filesystem::path const& _path, string const& content) void solidity::test::createFileWithContent(boost::filesystem::path const& _path, string const& _content)
{ {
if (boost::filesystem::is_regular_file(_path)) if (boost::filesystem::is_regular_file(_path))
BOOST_THROW_EXCEPTION(runtime_error("File already exists: \"" + _path.string() + "\".")); \ BOOST_THROW_EXCEPTION(runtime_error("File already exists: \"" + _path.string() + "\".")); \
@ -49,16 +49,21 @@ void solidity::test::createFileWithContent(boost::filesystem::path const& _path,
if (newFile.fail() || !boost::filesystem::is_regular_file(_path)) if (newFile.fail() || !boost::filesystem::is_regular_file(_path))
BOOST_THROW_EXCEPTION(runtime_error("Failed to create a file: \"" + _path.string() + "\".")); \ BOOST_THROW_EXCEPTION(runtime_error("Failed to create a file: \"" + _path.string() + "\".")); \
newFile << content; newFile << _content;
} }
bool solidity::test::createSymlinkIfSupportedByFilesystem( bool solidity::test::createSymlinkIfSupportedByFilesystem(
boost::filesystem::path const& _targetPath, boost::filesystem::path const& _targetPath,
boost::filesystem::path const& _linkName boost::filesystem::path const& _linkName,
bool _directorySymlink
) )
{ {
boost::system::error_code symlinkCreationError; boost::system::error_code symlinkCreationError;
boost::filesystem::create_symlink(_targetPath, _linkName, symlinkCreationError);
if (_directorySymlink)
boost::filesystem::create_directory_symlink(_targetPath, _linkName, symlinkCreationError);
else
boost::filesystem::create_symlink(_targetPath, _linkName, symlinkCreationError);
if (!symlinkCreationError) if (!symlinkCreationError)
return true; return true;

View File

@ -35,16 +35,20 @@ void createFilesWithParentDirs(std::set<boost::filesystem::path> const& _paths,
/// Creates a file with the exact content specified in the second argument. /// Creates a file with the exact content specified in the second argument.
/// Throws an exception if the file already exists or if the parent directory of the file does not. /// Throws an exception if the file already exists or if the parent directory of the file does not.
void createFileWithContent(boost::filesystem::path const& _path, std::string const& content); void createFileWithContent(boost::filesystem::path const& _path, std::string const& _content);
/// Creates a symlink between two paths. /// Creates a symlink between two paths.
/// The target does not have to exist. /// The target does not have to exist.
/// If @p directorySymlink is true, indicate to the operating system that this is a directory
/// symlink. On some systems (e.g. Windows) it's possible to create a non-directory symlink pointing
/// at a directory, which makes such a symlinks unusable.
/// @returns true if the symlink has been successfully created, false if the filesystem does not /// @returns true if the symlink has been successfully created, false if the filesystem does not
/// support symlinks. /// support symlinks.
/// Throws an exception of the operation fails for a different reason. /// Throws an exception of the operation fails for a different reason.
bool createSymlinkIfSupportedByFilesystem( bool createSymlinkIfSupportedByFilesystem(
boost::filesystem::path const& _targetPath, boost::filesystem::path const& _targetPath,
boost::filesystem::path const& _linkName boost::filesystem::path const& _linkName,
bool _directorySymlink
); );
} }

View File

@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE(readFileAsString_symlink)
TemporaryDirectory tempDir("common-io-test-"); TemporaryDirectory tempDir("common-io-test-");
createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n"); createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n");
if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt")) if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt", false))
return; return;
BOOST_TEST(readFileAsString(tempDir.path() / "symlink.txt") == "ABC\ndef\n"); BOOST_TEST(readFileAsString(tempDir.path() / "symlink.txt") == "ABC\ndef\n");