check if a symlink exists more carefully

This commit is contained in:
Alexander Tokmakov 2023-03-07 21:27:27 +01:00
parent e23f624968
commit f5cf039190
3 changed files with 12 additions and 0 deletions

View File

@ -383,6 +383,14 @@ bool isSymlink(const fs::path & path)
return fs::is_symlink(path); /// STYLE_CHECK_ALLOW_STD_FS_SYMLINK
}
bool isSymlinkNoThrow(const fs::path & path)
{
std::error_code dummy;
if (path.filename().empty())
return fs::is_symlink(path.parent_path(), dummy); /// STYLE_CHECK_ALLOW_STD_FS_SYMLINK
return fs::is_symlink(path, dummy); /// STYLE_CHECK_ALLOW_STD_FS_SYMLINK
}
fs::path readSymlink(const fs::path & path)
{
/// See the comment for isSymlink

View File

@ -95,6 +95,7 @@ void setModificationTime(const std::string & path, time_t time);
time_t getChangeTime(const std::string & path);
bool isSymlink(const fs::path & path);
bool isSymlinkNoThrow(const fs::path & path);
fs::path readSymlink(const fs::path & path);
}

View File

@ -509,6 +509,9 @@ void DatabaseAtomic::tryCreateMetadataSymlink()
{
try
{
/// fs::exists could return false for broken symlink
if (FS::isSymlinkNoThrow(metadata_symlink))
fs::remove(metadata_symlink);
fs::create_directory_symlink(metadata_path, path_to_metadata_symlink);
}
catch (...)