[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] find: implement -ok
From:       David Leonard <d+busybox () adaptive-enterprises ! com>
Date:       2022-03-28 1:30:18
Message-ID: 9p395o38-9o30-q397-83nn-612r2orors5s () nqncgvir-ragrecevfrf ! pbz
[Download RAW message or body]

Implements POSIX's -ok primary for find.

This is the same as -exec, execpt that the user is prompted before
running the command.

function                                             old     new   delta
do_exec                                              782     904    +122
parse_params                                        3333    3363     +30
packed_usage                                       34437   34464     +27
.rodata                                           119440  119448      +8
static.params                                        271     275      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/0 up/down: 191/0)             Total: 191 bytes
["ok.patch" (text/x-diff)]

From 7ac4795a2767e1fc2a5fbc759290f8ce22000864 Mon Sep 17 00:00:00 2001
From: David Leonard <d+busybox@adaptive-enterprises.com>
Date: Mon, 28 Mar 2022 10:55:18 +1000
Subject: [PATCH 1/2] find: implement -ok

function                                             old     new   delta
do_exec                                              782     904    +122
parse_params                                        3333    3363     +30
packed_usage                                       34437   34464     +27
.rodata                                           119440  119448      +8
static.params                                        271     275      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/0 up/down: 191/0)             Total: 191 bytes
   text	   data	    bss	    dec	    hex	filename
1734640	  16476	   1816	1752932	 1abf64	busybox_old
1734831	  16476	   1816	1753123	 1ac023	busybox_unstripped
---
 findutils/find.c     | 32 ++++++++++++++++++++++++++++++--
 testsuite/find.tests |  6 ++++++
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/findutils/find.c b/findutils/find.c
index bb6ad31e5..40f66ab2e 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -180,6 +180,13 @@
 //config:	Without this option, -exec + is a synonym for -exec ;
 //config:	(IOW: it works correctly, but without expected speedup)
 //config:
+//config:config FEATURE_FIND_EXEC_OK
+//config:	bool "Enable -ok: execute confirmed commands"
+//config:	default y
+//config:	depends on FEATURE_FIND_EXEC
+//config:	help
+//config:	Support the 'find -ok' option which prompts before executing.
+//config:
 //config:config FEATURE_FIND_USER
 //config:	bool "Enable -user: username/uid matching"
 //config:	default y
@@ -395,6 +402,9 @@
 //usage:	IF_FEATURE_FIND_EXEC_PLUS(
 //usage:     "\n	-exec CMD ARG + Run CMD with {} replaced by list of file names"
 //usage:	)
+//usage:	IF_FEATURE_FIND_EXEC_OK(
+//usage:     "\n	-ok CMD ARG ;   Prompt and run CMD with {} replaced"
+//usage:	)
 //usage:	IF_FEATURE_FIND_DELETE(
 //usage:     "\n	-delete		Delete current file/directory. Turns on -depth option"
 //usage:	)
@@ -467,6 +477,9 @@ IF_FEATURE_FIND_EXEC(   ACTS(exec,
 				char **exec_argv; /* -exec ARGS */
 				unsigned *subst_count;
 				int exec_argc; /* count of ARGS */
+				IF_FEATURE_FIND_EXEC_OK(
+				int ok;		/* -ok */
+				)
 				IF_FEATURE_FIND_EXEC_PLUS(
 					/*
 					 * filelist is NULL if "exec ;"
@@ -802,10 +815,22 @@ static int do_exec(action_exec *ap, const char *fileName)
 	}
 # endif
 
+# if ENABLE_FEATURE_FIND_EXEC_OK
+	if (ap->ok) {
+		for (i = 0; argv[i]; i++)
+			fprintf(stderr, "%s ", argv[i]);
+		fprintf(stderr, "?");
+		if (!bb_ask_y_confirmation())
+			goto not_ok;
+	}
+# endif
 	rc = spawn_and_wait(argv);
 	if (rc < 0)
 		bb_simple_perror_msg(argv[0]);
 
+# if ENABLE_FEATURE_FIND_EXEC_OK
+    not_ok:
+# endif
 	i = 0;
 	while (argv[i])
 		free(argv[i++]);
@@ -1120,6 +1145,7 @@ static action*** parse_params(char **argv)
 	IF_FEATURE_FIND_DELETE( PARM_delete    ,)
 	IF_FEATURE_FIND_EMPTY(	PARM_empty     ,)
 	IF_FEATURE_FIND_EXEC(   PARM_exec      ,)
+	IF_FEATURE_FIND_EXEC_OK(PARM_ok        ,)
 	IF_FEATURE_FIND_EXECUTABLE(PARM_executable,)
 	IF_FEATURE_FIND_PAREN(  PARM_char_brace,)
 	/* All options/actions starting from here require argument */
@@ -1171,6 +1197,7 @@ static action*** parse_params(char **argv)
 	IF_FEATURE_FIND_DELETE( "-delete\0" )
 	IF_FEATURE_FIND_EMPTY(	"-empty\0"  )
 	IF_FEATURE_FIND_EXEC(   "-exec\0"   )
+	IF_FEATURE_FIND_EXEC_OK("-ok\0"     )
 	IF_FEATURE_FIND_EXECUTABLE("-executable\0")
 	IF_FEATURE_FIND_PAREN(  "(\0"       )
 	/* All options/actions starting from here require argument */
@@ -1351,18 +1378,19 @@ static action*** parse_params(char **argv)
 		}
 #endif
 #if ENABLE_FEATURE_FIND_EXEC
-		else if (parm == PARM_exec) {
+		else if (parm == PARM_exec IF_FEATURE_FIND_EXEC_OK(|| parm == PARM_ok)) {
 			int i;
 			action_exec *ap;
 			IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
 			dbg("%d", __LINE__);
 			G.need_print = 0;
 			ap = ALLOC_ACTION(exec);
+			IF_FEATURE_FIND_EXEC_OK(ap->ok = (parm == PARM_ok);)
 			ap->exec_argv = ++argv; /* first arg after -exec */
 			/*ap->exec_argc = 0; - ALLOC_ACTION did it */
 			while (1) {
 				if (!*argv) /* did not see ';' or '+' until end */
-					bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
+					bb_error_msg_and_die(bb_msg_requires_arg, arg);
 				// find -exec echo Foo ">{}<" ";"
 				// executes "echo Foo >FILENAME<",
 				// find -exec echo Foo ">{}<" "+"
diff --git a/testsuite/find.tests b/testsuite/find.tests
index 138236c81..effdf60a6 100755
--- a/testsuite/find.tests
+++ b/testsuite/find.tests
@@ -28,6 +28,12 @@ testing "find -exec exitcode 2" \
 	"0\n" \
 	"" ""
 SKIP=
+optional FEATURE_FIND_EXEC_OK
+testing "find -ok" \
+	"cd find.tempdir && find testfile -ok true {} + 2>&1; echo \$?" \
+	"true testfile ?\n0\n" \
+	"" "y"
+SKIP=
 # Surprisingly, "-exec false ;" results in exitcode 0! "-exec false +" is different!!!
 optional FEATURE_FIND_EXEC
 testing "find -exec exitcode 3" \
-- 
2.32.0


From 48e78e55f367a427cf613f199cfa9060af08a304 Mon Sep 17 00:00:00 2001
From: David Leonard <d+busybox@adaptive-enterprises.com>
Date: Mon, 28 Mar 2022 11:14:13 +1000
Subject: [PATCH 2/2] find: implement -nouser, -nogroup

function                                             old     new   delta
parse_params                                        3363    3443     +80
func_nouser                                            -      52     +52
func_nogroup                                           -      52     +52
static.params                                        275     292     +17
packed_usage                                       34464   34473      +9
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 3/0 up/down: 210/0)             Total: 210 bytes
   text	   data	    bss	    dec	    hex	filename
1734831	  16476	   1816	1753123	 1ac023	busybox_old
1735041	  16476	   1816	1753333	 1ac0f5	busybox_unstripped
---
 findutils/find.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/findutils/find.c b/findutils/find.c
index 40f66ab2e..2a0a867e3 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -197,6 +197,16 @@
 //config:	default y
 //config:	depends on FIND
 //config:
+//config:config FEATURE_FIND_NOUSER
+//config:	bool "Enable -nouser matching"
+//config:	default y
+//config:	depends on FIND
+//config:
+//config:config FEATURE_FIND_NOGROUP
+//config:	bool "Enable -nogroup matching"
+//config:	default y
+//config:	depends on FIND
+//config:
 //config:config FEATURE_FIND_NOT
 //config:	bool "Enable the 'not' (!) operator"
 //config:	default y
@@ -373,6 +383,12 @@
 //usage:	IF_FEATURE_FIND_GROUP(
 //usage:     "\n	-group NAME/ID	File is owned by given group"
 //usage:	)
+//usage:	IF_FEATURE_FIND_NOUSER(
+//usage:     "\n	-nouser		File is owned by unknown uid"
+//usage:	)
+//usage:	IF_FEATURE_FIND_NOGROUP(
+//usage:     "\n	-nogroup	File is owned by unknown gid"
+//usage:	)
 //usage:	IF_FEATURE_FIND_SIZE(
 //usage:     "\n	-size N[bck]	File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
 //usage:     "\n			+/-N: file size is bigger/smaller than N"
@@ -466,6 +482,8 @@ IF_FEATURE_FIND_NEWER(  ACTS(newer, time_t newer_mtime;))
 IF_FEATURE_FIND_INUM(   ACTS(inum,  ino_t inode_num;))
 IF_FEATURE_FIND_SAMEFILE(ACTS(samefile, ino_t inode_num; dev_t device;))
 IF_FEATURE_FIND_USER(   ACTS(user,  uid_t uid;))
+IF_FEATURE_FIND_NOUSER( ACTS(nouser))
+IF_FEATURE_FIND_NOUSER( ACTS(nogroup))
 IF_FEATURE_FIND_SIZE(   ACTS(size,  char size_char; off_t size;))
 IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
 IF_FEATURE_FIND_PAREN(  ACTS(paren, action ***subexpr;))
@@ -891,6 +909,18 @@ ACTF(group)
 	return (statbuf->st_gid == ap->gid);
 }
 #endif
+#if ENABLE_FEATURE_FIND_NOUSER
+ACTF(nouser)
+{
+	return !getpwuid(statbuf->st_uid);
+}
+#endif
+#if ENABLE_FEATURE_FIND_NOGROUP
+ACTF(nogroup)
+{
+	return !getgrgid(statbuf->st_gid);
+}
+#endif
 #if ENABLE_FEATURE_FIND_PRINT0
 ACTF(print0)
 {
@@ -1144,6 +1174,8 @@ static action*** parse_params(char **argv)
 	IF_FEATURE_FIND_QUIT(   PARM_quit      ,)
 	IF_FEATURE_FIND_DELETE( PARM_delete    ,)
 	IF_FEATURE_FIND_EMPTY(	PARM_empty     ,)
+	IF_FEATURE_FIND_NOUSER( PARM_nouser    ,)
+	IF_FEATURE_FIND_NOGROUP(PARM_nogroup   ,)
 	IF_FEATURE_FIND_EXEC(   PARM_exec      ,)
 	IF_FEATURE_FIND_EXEC_OK(PARM_ok        ,)
 	IF_FEATURE_FIND_EXECUTABLE(PARM_executable,)
@@ -1196,6 +1228,8 @@ static action*** parse_params(char **argv)
 	IF_FEATURE_FIND_QUIT(   "-quit\0"  )
 	IF_FEATURE_FIND_DELETE( "-delete\0" )
 	IF_FEATURE_FIND_EMPTY(	"-empty\0"  )
+	IF_FEATURE_FIND_NOUSER( "-nouser\0"   )
+	IF_FEATURE_FIND_NOGROUP("-nogroup\0"  )
 	IF_FEATURE_FIND_EXEC(   "-exec\0"   )
 	IF_FEATURE_FIND_EXEC_OK("-ok\0"     )
 	IF_FEATURE_FIND_EXECUTABLE("-executable\0")
@@ -1594,6 +1628,18 @@ static action*** parse_params(char **argv)
 				ap->gid = xgroup2gid(arg1);
 		}
 #endif
+#if ENABLE_FEATURE_FIND_NOUSER
+		else if (parm == PARM_nouser) {
+			dbg("%d", __LINE__);
+			(void) ALLOC_ACTION(nouser);
+		}
+#endif
+#if ENABLE_FEATURE_FIND_NOGROUP
+		else if (parm == PARM_nogroup) {
+			dbg("%d", __LINE__);
+			(void) ALLOC_ACTION(nogroup);
+		}
+#endif
 #if ENABLE_FEATURE_FIND_SIZE
 		else if (parm == PARM_size) {
 /* -size n[bckw]: file uses n units of space
-- 
2.32.0



_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic