From f17f3a22c671ce93774b1ea39ad4404fa111b46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 2 Apr 2025 21:22:43 +0200 Subject: [PATCH] symlink_exists: wrap exceptions into nix exception (cherry picked from commit 779687854f62adfdf448f4ccb37b33887f368621) --- src/libutil/file-system.cc | 12 +++++++++++- src/libutil/file-system.hh | 5 ++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 0adafc0e4..a609aa083 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -29,7 +29,17 @@ namespace nix { -namespace fs { using namespace std::filesystem; } +namespace fs { + using namespace std::filesystem; + + bool symlink_exists(const std::filesystem::path & path) { + try { + return std::filesystem::exists(std::filesystem::symlink_status(path)); + } catch (const std::filesystem::filesystem_error & e) { + throw SysError("cannot check existence of %1%", path); + } + } +} bool isAbsolute(PathView path) { diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 49d120cb7..61bfe6b30 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -134,6 +134,7 @@ bool pathExists(const Path & path); namespace fs { /** + * TODO: we may actually want to use pathExists instead of this function * ``` * symlink_exists(p) = std::filesystem::exists(std::filesystem::symlink_status(p)) * ``` @@ -142,9 +143,7 @@ namespace fs { * std::filesystem::exists(p) = std::filesystem::exists(std::filesystem::status(p)) * ``` */ -inline bool symlink_exists(const std::filesystem::path & path) { - return std::filesystem::exists(std::filesystem::symlink_status(path)); -} +bool symlink_exists(const std::filesystem::path & path); } // namespace fs