summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-11-26 21:47:31 -0500
committerTavian Barnes <tavianator@tavianator.com>2020-11-28 11:33:18 -0500
commitc7c8a03a3783cfeb342f033a7cac0dcbe948bbb9 (patch)
treed41b20ca530e8193b83d5046cb677c0bdd845649
parent457fcee54828527ee2a6819cac2b51892b673069 (diff)
downloadbfs-c7c8a03a3783cfeb342f033a7cac0dcbe948bbb9.tar.xz
eval: Make -hidden behave consistently
Previously there was an unexpected difference between $ bfs .hidden -hidden and $ bfs ./.hidden -hidden ./.hidden The intent of the code was to avoid considering `.`, the default starting point, as hidden and thus pruning a whole search with -nohidden. Fix it to do that explicitly, and handle `..` too.
-rw-r--r--eval.c9
-rwxr-xr-xtests.sh6
-rw-r--r--tests/test_hidden_root.out5
3 files changed, 19 insertions, 1 deletions
diff --git a/eval.c b/eval.c
index 0503b14..bb93c12 100644
--- a/eval.c
+++ b/eval.c
@@ -467,7 +467,14 @@ bool eval_fstype(const struct expr *expr, struct eval_state *state) {
*/
bool eval_hidden(const struct expr *expr, struct eval_state *state) {
const struct BFTW *ftwbuf = state->ftwbuf;
- return ftwbuf->nameoff > 0 && ftwbuf->path[ftwbuf->nameoff] == '.';
+ const char *name = ftwbuf->path + ftwbuf->nameoff;
+
+ // Don't treat "." or ".." as hidden directories. Otherwise we'd filter
+ // out everything when given
+ //
+ // $ bfs . -nohidden
+ // $ bfs .. -nohidden
+ return name[0] == '.' && strcmp(name, ".") != 0 && strcmp(name, "..") != 0;
}
/**
diff --git a/tests.sh b/tests.sh
index 65eb104..0e1a964 100755
--- a/tests.sh
+++ b/tests.sh
@@ -681,6 +681,7 @@ bfs_tests=(
test_help
test_hidden
+ test_hidden_root
test_newerma_nonexistent
test_newermt_invalid
@@ -1890,6 +1891,11 @@ function test_hidden() {
bfs_diff weirdnames -hidden
}
+function test_hidden_root() {
+ cd weirdnames
+ bfs_diff . ./. ... ./... .../.. -hidden
+}
+
function test_nohidden() {
bfs_diff weirdnames -nohidden
}
diff --git a/tests/test_hidden_root.out b/tests/test_hidden_root.out
new file mode 100644
index 0000000..199ae5f
--- /dev/null
+++ b/tests/test_hidden_root.out
@@ -0,0 +1,5 @@
+...
+./...
+./...
+.../../...
+././...