1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 14:51:16 +02:00

Replace symlink_exists with pathExists

As it turns out the orignal implementation of symlink_exists cannot be
used in Nix because it did now std::filesystem::filesystem_error.
The new implementation fixes that but is now actually the same as
pathExists except for the path type.
This commit is contained in:
Jörg Thalheim 2025-05-01 11:43:37 +02:00
parent 143fb88ceb
commit 5b59be914d
6 changed files with 9 additions and 38 deletions

View file

@ -533,7 +533,7 @@ struct GitInputScheme : InputScheme
static MakeNotAllowedError makeNotAllowedError(std::filesystem::path repoPath) static MakeNotAllowedError makeNotAllowedError(std::filesystem::path repoPath)
{ {
return [repoPath{std::move(repoPath)}](const CanonPath & path) -> RestrictedPathError { return [repoPath{std::move(repoPath)}](const CanonPath & path) -> RestrictedPathError {
if (fs::symlink_exists(repoPath / path.rel())) if (pathExists(repoPath / path.rel()))
return RestrictedPathError( return RestrictedPathError(
"Path '%1%' in the repository %2% is not tracked by Git.\n" "Path '%1%' in the repository %2% is not tracked by Git.\n"
"\n" "\n"

View file

@ -811,7 +811,7 @@ LockedFlake lockFlake(
auto relPath = (topRef.subdir == "" ? "" : topRef.subdir + "/") + "flake.lock"; auto relPath = (topRef.subdir == "" ? "" : topRef.subdir + "/") + "flake.lock";
auto outputLockFilePath = *sourcePath / relPath; auto outputLockFilePath = *sourcePath / relPath;
bool lockFileExists = fs::symlink_exists(outputLockFilePath); bool lockFileExists = pathExists(outputLockFilePath);
auto s = chomp(diff); auto s = chomp(diff);
if (lockFileExists) { if (lockFileExists) {

View file

@ -1969,9 +1969,10 @@ void DerivationBuilderImpl::runChild()
if (pathExists(path)) if (pathExists(path))
ss.push_back(path); ss.push_back(path);
if (settings.caFile != "" && pathExists(settings.caFile)) { if (settings.caFile != "") {
Path caFile = settings.caFile; Path caFile = settings.caFile;
pathsInChroot.try_emplace("/etc/ssl/certs/ca-certificates.crt", canonPath(caFile, true), true); if (pathExists(caFile))
pathsInChroot.try_emplace("/etc/ssl/certs/ca-certificates.crt", canonPath(caFile, true), true);
} }
} }

View file

@ -31,18 +31,6 @@
namespace nix { namespace nix {
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);
}
}
}
DirectoryIterator::DirectoryIterator(const std::filesystem::path& p) { DirectoryIterator::DirectoryIterator(const std::filesystem::path& p) {
try { try {
// **Attempt to create the underlying directory_iterator** // **Attempt to create the underlying directory_iterator**
@ -243,9 +231,9 @@ std::optional<struct stat> maybeLstat(const Path & path)
} }
bool pathExists(const Path & path) bool pathExists(const std::filesystem::path & path)
{ {
return maybeLstat(path).has_value(); return maybeLstat(path.string()).has_value();
} }
bool pathAccessible(const std::filesystem::path & path) bool pathAccessible(const std::filesystem::path & path)

View file

@ -126,26 +126,8 @@ std::optional<struct stat> maybeLstat(const Path & path);
/** /**
* @return true iff the given path exists. * @return true iff the given path exists.
*
* In the process of being deprecated for `fs::symlink_exists`.
*/ */
bool pathExists(const Path & path); bool pathExists(const std::filesystem::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))
* ```
* Missing convenience analogous to
* ```
* std::filesystem::exists(p) = std::filesystem::exists(std::filesystem::status(p))
* ```
*/
bool symlink_exists(const std::filesystem::path & path);
} // namespace fs
/** /**
* Canonicalize a path except for the last component. * Canonicalize a path except for the last component.

View file

@ -76,7 +76,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
if (writeTo) { if (writeTo) {
logger->stop(); logger->stop();
if (fs::symlink_exists(*writeTo)) if (pathExists(*writeTo))
throw Error("path '%s' already exists", writeTo->string()); throw Error("path '%s' already exists", writeTo->string());
std::function<void(Value & v, const PosIdx pos, const fs::path & path)> recurse; std::function<void(Value & v, const PosIdx pos, const fs::path & path)> recurse;