From 9f1863d45fe596e258596a4b4cc9a4064bcb11d3 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 4 Feb 2017 21:03:59 -0500 Subject: Implement -nouser and -nogroup --- bfs.h | 2 ++ eval.c | 26 ++++++++++++++++++++++++++ parse.c | 16 ++++++++++++++++ tests.sh | 19 ++++++++++++++++++- tests/test_0128.out | 0 tests/test_0129.out | 0 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/test_0128.out create mode 100644 tests/test_0129.out diff --git a/bfs.h b/bfs.h index e5882ac..84874d6 100644 --- a/bfs.h +++ b/bfs.h @@ -288,6 +288,8 @@ bool eval_used(const struct expr *expr, struct eval_state *state); bool eval_gid(const struct expr *expr, struct eval_state *state); bool eval_uid(const struct expr *expr, struct eval_state *state); +bool eval_nogroup(const struct expr *expr, struct eval_state *state); +bool eval_nouser(const struct expr *expr, struct eval_state *state); bool eval_depth(const struct expr *expr, struct eval_state *state); bool eval_empty(const struct expr *expr, struct eval_state *state); diff --git a/eval.c b/eval.c index 4494cff..35501e0 100644 --- a/eval.c +++ b/eval.c @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include #include #include @@ -225,6 +227,30 @@ bool eval_uid(const struct expr *expr, struct eval_state *state) { return do_cmp(expr, statbuf->st_uid); } +/** + * -nogroup test. + */ +bool eval_nogroup(const struct expr *expr, struct eval_state *state) { + const struct stat *statbuf = fill_statbuf(state); + if (!statbuf) { + return false; + } + + return getgrgid(statbuf->st_gid) == NULL; +} + +/** + * -nouser test. + */ +bool eval_nouser(const struct expr *expr, struct eval_state *state) { + const struct stat *statbuf = fill_statbuf(state); + if (!statbuf) { + return false; + } + + return getpwuid(statbuf->st_uid) == NULL; +} + /** * -delete action. */ diff --git a/parse.c b/parse.c index 7f656c3..d0168a9 100644 --- a/parse.c +++ b/parse.c @@ -1205,6 +1205,13 @@ static struct expr *parse_newerxy(struct parser_state *state, int arg1, int arg2 return expr; } +/** + * Parse -nogroup. + */ +static struct expr *parse_nogroup(struct parser_state *state, int arg1, int arg2) { + return parse_nullary_test(state, eval_nogroup); +} + /** * Parse -nohidden. */ @@ -1225,6 +1232,13 @@ static struct expr *parse_noleaf(struct parser_state *state, int arg1, int arg2) return parse_nullary_option(state); } +/** + * Parse -nouser. + */ +static struct expr *parse_nouser(struct parser_state *state, int arg1, int arg2) { + return parse_nullary_test(state, eval_nouser); +} + /** * Parse a permission mode like chmod(1). */ @@ -1857,10 +1871,12 @@ static const struct table_entry parse_table[] = { {"newer", false, parse_acnewer, MTIME}, {"newer", true, parse_newerxy}, {"nocolor", false, parse_color, false}, + {"nogroup", false, parse_nogroup}, {"nohidden", false, parse_nohidden}, {"noignore_readdir_race", false, parse_ignore_races, false}, {"noleaf", false, parse_noleaf}, {"not"}, + {"nouser", false, parse_nouser}, {"nowarn", false, parse_warn, false}, {"o"}, {"ok", false, parse_exec, EXEC_CONFIRM}, diff --git a/tests.sh b/tests.sh index 06d319f..5a7e650 100755 --- a/tests.sh +++ b/tests.sh @@ -787,9 +787,26 @@ function test_0127() { bfs_diff basic -inum "$inode" } +function test_0127() { + [ "$BSD" -o "$GNU" ] || return 0 + + local inode="$(ls -id basic/k/foo/bar | cut -f1 -d' ')" + bfs_diff basic -inum "$inode" +} + +function test_0128() { + [ "$BSD" -o "$GNU" ] || return 0 + bfs_diff basic -nogroup +} + +function test_0129() { + [ "$BSD" -o "$GNU" ] || return 0 + bfs_diff basic -nouser +} + result=0 -for i in {1..127}; do +for i in {1..129}; do test="test_$(printf '%04d' $i)" if [ -t 1 ]; then diff --git a/tests/test_0128.out b/tests/test_0128.out new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_0129.out b/tests/test_0129.out new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3