From f8ca402859c2f9c9547c0374ea9a53dd6c6abc3c Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Sun, 29 Sep 2024 19:10:24 +0300 Subject: Fix find builtin to dereference symlink argument if terminated with directory separator (GH issue #434) --- libbutl/builtin.cxx | 8 +++++++- tests/builtin/find.testscript | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libbutl/builtin.cxx b/libbutl/builtin.cxx index a5861d4..e1a0fea 100644 --- a/libbutl/builtin.cxx +++ b/libbutl/builtin.cxx @@ -1070,7 +1070,13 @@ namespace butl try { - pe = path_entry (fp); + // Note that POSIX makes no distinction between symlink path with + // and without trailing directory separator and specify that it + // should not be deferenced. The major implementations, however, + // dereference symlink paths with the trailing directory separator. + // We will follow that behavior. + // + pe = path_entry (fp, p.to_directory () /* follow_symlinks */); } catch (const system_error& e) { diff --git a/tests/builtin/find.testscript b/tests/builtin/find.testscript index b09822c..5971108 100644 --- a/tests/builtin/find.testscript +++ b/tests/builtin/find.testscript @@ -44,6 +44,30 @@ $* . -mindepth 12a 2>"find: invalid value '12a' for primary '-mindepth'" == 1 : path : { + : dir-symlink + : + { + mkdir -p a/c; + ln -s a c; + + # If 'c' path is a symlink (may not be the case on Windows), then check + # that the find builtin only dereferences it if it is terminated with the + # directory separator. + # + $* c -type l | set p; + + if ($p == 'c') + $* c >>EOO + c + EOO + end; + + $* c/ -type d >>EOO + c/ + c/c + EOO + } + : relative : { -- cgit v1.1