From 21a863e5fdf1dbda13d70f37f0321002489a1a85 Mon Sep 17 00:00:00 2001
From: Gilad Arnold <[email protected]>
Date: Fri, 13 Nov 2015 13:16:14 -0800
Subject: [PATCH] Enable matching any perm bits.

Includes tests for the new feature, and a failure case for the minimal
perms test as well.

Also some typo fixing / massaging the help text so it fits in 80
columns.
---
 tests/find.test   |  8 +++++++-
 toys/posix/find.c | 30 ++++++++++++++++--------------
 2 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/tests/find.test b/tests/find.test
index 2f17bf7..71a3506 100755
--- a/tests/find.test
+++ b/tests/find.test
@@ -58,8 +58,14 @@ testing "find -perm (exact success)" \
   "find perm -type f -perm 0444" "perm/all-read-only\n" "" ""
 testing "find -perm (exact failure)" \
   "find perm -type f -perm 0400" "" "" ""
-testing "find -perm (at least)" \
+testing "find -perm (min success)" \
   "find perm -type f -perm -0400" "perm/all-read-only\n" "" ""
+testing "find -perm (min failure)" \
+  "find perm -type f -perm -0600" "" "" ""
+testing "find -perm (any success)" \
+  "find perm -type f -perm -0444" "perm/all-read-only\n" "" ""
+testing "find -perm (any failure)" \
+  "find perm -type f -perm -0222" "" "" ""
 
 # Still fails
 
diff --git a/toys/posix/find.c b/toys/posix/find.c
index f670184..3019cb6 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -22,17 +22,17 @@ config FIND
     -H  Follow command line symlinks         -L  Follow all symlinks
 
     Match filters:
-    -name  PATTERN filename with wildcards   -iname      case insensitive -name
-    -path  PATTERN path name with wildcards  -ipath      case insensitive -path
-    -user  UNAME   belongs to user UNAME     -nouser     user not in /etc/passwd
-    -group GROUP   belongs to group GROUP    -nogroup    group not in /etc/group
-    -perm  [-]MODE permissons (-=at least)   -prune      ignore contents of dir
-    -size  N[c]    512 byte blocks (c=bytes) -xdev       stay in this filesystem
-    -links N       hardlink count            -atime N    accessed N days ago
-    -ctime N       created N days ago        -mtime N    modified N days ago
-    -newer FILE    newer mtime than FILE     -mindepth # at least # dirs down
-    -depth         ignore contents of dir    -maxdepth # at most # dirs down
-    -inum  N       inode number N
+    -name  PATTERN  filename with wildcards   -iname      case insensitive -name
+    -path  PATTERN  path name with wildcards  -ipath      case insensitive -path
+    -user  UNAME    belongs to user UNAME     -nouser     user ID not known
+    -group GROUP    belongs to group GROUP    -nogroup    group ID not known
+    -perm  [-/]MODE permissions (-=min /=any) -prune      ignore contents of dir
+    -size  N[c]     512 byte blocks (c=bytes) -xdev       only this filesystem
+    -links N        hardlink count            -atime N    accessed N days ago
+    -ctime N        created N days ago        -mtime N    modified N days ago
+    -newer FILE     newer mtime than FILE     -mindepth # at least # dirs down
+    -depth          ignore contents of dir    -maxdepth # at most # dirs down
+    -inum  N        inode number N
     -type [bcdflps] (block, char, dir, file, symlink, pipe, socket)
 
     Numbers N may be prefixed by a - (less than) or + (greater than):
@@ -278,11 +278,13 @@ static int do_find(struct dirtree *new)
       } else if (!strcmp(s, "perm")) {
         if (check) {
           char *m = ss[1];
-          mode_t m1 = string_to_mode(m+(*m == '-'), 0),
+          int match_min = *m == '-',
+              match_any = *m == '/';
+          mode_t m1 = string_to_mode(m+(match_min || match_any), 0),
                  m2 = new->st.st_mode & 07777;
 
-          if (*m == '-') m2 &= m1;
-          test = m1 == m2;
+          if (match_min || match_any) m2 &= m1;
+          test = match_any ? !m1 || m2 : m1 == m2;
         }
       } else if (!strcmp(s, "type")) {
         if (check) {
-- 
2.6.0.rc2.230.g3dd15c0

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to