summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2016-02-04 17:25:04 -0500
committerTavian Barnes <tavianator@tavianator.com>2016-02-04 17:25:04 -0500
commit43b7d485d2ab9e320fd32fc47080cd60c626b530 (patch)
tree0216c757761c1ea91661119a6c80613ed43705e4
parentff87f3cdf1e3c5a7d4661e785f4d96be9d59291c (diff)
downloadbfs-43b7d485d2ab9e320fd32fc47080cd60c626b530.tar.xz
Implement -links.
-rw-r--r--bfs.h1
-rw-r--r--eval.c12
-rw-r--r--parse.c2
-rwxr-xr-xtests.sh18
4 files changed, 32 insertions, 1 deletions
diff --git a/bfs.h b/bfs.h
index 4b558d8..24aeed8 100644
--- a/bfs.h
+++ b/bfs.h
@@ -154,6 +154,7 @@ bool eval_uid(const struct expr *expr, struct eval_state *state);
bool eval_empty(const struct expr *expr, struct eval_state *state);
bool eval_hidden(const struct expr *expr, struct eval_state *state);
bool eval_inum(const struct expr *expr, struct eval_state *state);
+bool eval_links(const struct expr *expr, struct eval_state *state);
bool eval_type(const struct expr *expr, struct eval_state *state);
bool eval_name(const struct expr *expr, struct eval_state *state);
diff --git a/eval.c b/eval.c
index a60bd30..543a98a 100644
--- a/eval.c
+++ b/eval.c
@@ -285,6 +285,18 @@ bool eval_inum(const struct expr *expr, struct eval_state *state) {
}
/**
+ * -links test.
+ */
+bool eval_links(const struct expr *expr, struct eval_state *state) {
+ const struct stat *statbuf = fill_statbuf(state);
+ if (!statbuf) {
+ return false;
+ }
+
+ return do_cmp(expr, statbuf->st_nlink);
+}
+
+/**
* -name test.
*/
bool eval_name(const struct expr *expr, struct eval_state *state) {
diff --git a/parse.c b/parse.c
index 0cceef4..296fca7 100644
--- a/parse.c
+++ b/parse.c
@@ -501,6 +501,8 @@ static struct expr *parse_literal(struct parser_state *state) {
return new_action(state, eval_nohidden);
} else if (strcmp(arg, "-inum") == 0) {
return parse_test_icmp(state, arg, eval_inum);
+ } else if (strcmp(arg, "-links") == 0) {
+ return parse_test_icmp(state, arg, eval_links);
} else if (strcmp(arg, "-mindepth") == 0) {
return parse_depth(state, arg, &state->cl->mindepth);
} else if (strcmp(arg, "-maxdepth") == 0) {
diff --git a/tests.sh b/tests.sh
index a45af5f..ffb110d 100755
--- a/tests.sh
+++ b/tests.sh
@@ -28,6 +28,15 @@ function perms_structure() {
install -Dm755 /dev/null "$1/rwx"
}
+# Creates a file+directory structure with various symbolic and hard links
+function links_structure() {
+ touchp "$1/a"
+ ln -s a "$1/b"
+ ln "$1/a" "$1/c"
+ mkdir -p "$1/d/e"
+ ln -s ../../d "$1/d/e/f"
+}
+
# Checks for any (order-independent) differences between bfs and find
function find_diff() {
diff -u <(./bfs "$@" | sort) <(find "$@" | sort)
@@ -134,7 +143,14 @@ function test_0019() {
find_diff "$1" -cnewer "$1/e/f"
}
-for i in {1..19}; do
+function test_0020() {
+ links_structure "$1"
+ find_diff "$1" -links 2 && \
+ find_diff "$1" -links -2 && \
+ find_diff "$1" -links +1
+}
+
+for i in {1..20}; do
dir="$(mktemp -d "${TMPDIR:-/tmp}"/bfs.XXXXXXXXXX)"
test="test_$(printf '%04d' $i)"
"$test" "$dir"