[PATCH] find: implement -empty

walter harms wharms at bfs.de
Wed Sep 11 07:46:45 UTC 2019



Am 10.09.2019 22:06, schrieb Aaro Koskinen:
> Implement -empty.
> 
> Signed-off-by: Aaro Koskinen <aaro.koskinen at iki.fi>
> ---
>  findutils/find.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 45 insertions(+)
> 
> diff --git a/findutils/find.c b/findutils/find.c
> index d6679bd08..3e4ae3266 100644
> --- a/findutils/find.c
> +++ b/findutils/find.c
> @@ -203,6 +203,14 @@
>  //config:	WARNING: This option can do much harm if used wrong. Busybox will not
>  //config:	try to protect the user from doing stupid things. Use with care.
>  //config:
> +//config:config FEATURE_FIND_EMPTY
> +//config:	bool "Enable -empty: match empty files or directories"
> +//config:	default y
> +//config:	depends on FIND
> +//config:	help
> +//config:	  Support the 'find -empty' option to find empty regular files
> +//config:	  or directories.
> +//config:
>  //config:config FEATURE_FIND_PATH
>  //config:	bool "Enable -path: match pathname with shell pattern"
>  //config:	default y
> @@ -333,6 +341,9 @@
>  //usage:	IF_FEATURE_FIND_DELETE(
>  //usage:     "\n	-delete		Delete current file/directory. Turns on -depth option"
>  //usage:	)
> +//usage:	IF_FEATURE_FIND_EMPTY(
> +//usage:     "\n	-empty		Match empty file/directory."
> +//usage:	)
>  //usage:	IF_FEATURE_FIND_QUIT(
>  //usage:     "\n	-quit		Exit"
>  //usage:	)
> @@ -396,6 +407,7 @@ IF_FEATURE_FIND_PAREN(  ACTS(paren, action ***subexpr;))
>  IF_FEATURE_FIND_PRUNE(  ACTS(prune))
>  IF_FEATURE_FIND_QUIT(   ACTS(quit))
>  IF_FEATURE_FIND_DELETE( ACTS(delete))
> +IF_FEATURE_FIND_EMPTY(  ACTS(empty))
>  IF_FEATURE_FIND_EXEC(   ACTS(exec,
>  				char **exec_argv; /* -exec ARGS */
>  				unsigned *subst_count;
> @@ -824,6 +836,31 @@ ACTF(delete)
>  	return TRUE;
>  }
>  #endif
> +#if ENABLE_FEATURE_FIND_EMPTY
> +ACTF(empty)
> +{
> +	if (S_ISDIR(statbuf->st_mode)) {
> +		DIR *dir;
> +
> +		dir = opendir(fileName);
> +		if (!dir) {
> +			bb_simple_perror_msg(fileName);
> +			return FALSE;
> +		} else {
> +			struct dirent *dent;
> +			char n = 0;
> +
> +			while ((dent = readdir(dir)) != NULL &&
> +			       DOT_OR_DOTDOT(dent->d_name) &&
> +			       n++ < 2)
> +				;
> +			closedir(dir);
> +			return !dent;
> +		}
> +	}
> +	return S_ISREG(statbuf->st_mode) && !statbuf->st_size;
> +}
> +#endif


is seems possible to simply that a bit (untested version):

if ( ! S_ISDIR(statbuf->st_mode))
       return S_ISREG(statbuf->st_mode) && !statbuf->st_size;


DIR *dir;

dir = opendir(fileName);
if (!dir) {
	bb_simple_perror_msg(fileName);
	return FALSE;
}

struct dirent *dent;
char n = 0;

while ((dent = readdir(dir)) != NULL &&
	 DOT_OR_DOTDOT(dent->d_name) &&
          n++ < 2) ;				;
closedir(dir);
return !dent;

JM2C,

re,
 wh



>  #if ENABLE_FEATURE_FIND_CONTEXT
>  ACTF(context)
>  {
> @@ -989,6 +1026,7 @@ static action*** parse_params(char **argv)
>  	IF_FEATURE_FIND_PRUNE(  PARM_prune     ,)
>  	IF_FEATURE_FIND_QUIT(   PARM_quit      ,)
>  	IF_FEATURE_FIND_DELETE( PARM_delete    ,)
> +	IF_FEATURE_FIND_EMPTY(	PARM_empty     ,)
>  	IF_FEATURE_FIND_EXEC(   PARM_exec      ,)
>  	IF_FEATURE_FIND_EXECUTABLE(PARM_executable,)
>  	IF_FEATURE_FIND_PAREN(  PARM_char_brace,)
> @@ -1034,6 +1072,7 @@ static action*** parse_params(char **argv)
>  	IF_FEATURE_FIND_PRUNE(  "-prune\0"  )
>  	IF_FEATURE_FIND_QUIT(   "-quit\0"  )
>  	IF_FEATURE_FIND_DELETE( "-delete\0" )
> +	IF_FEATURE_FIND_EMPTY(	"-empty\0"  )
>  	IF_FEATURE_FIND_EXEC(   "-exec\0"   )
>  	IF_FEATURE_FIND_EXECUTABLE("-executable\0")
>  	IF_FEATURE_FIND_PAREN(  "(\0"       )
> @@ -1203,6 +1242,12 @@ static action*** parse_params(char **argv)
>  			(void) ALLOC_ACTION(delete);
>  		}
>  #endif
> +#if ENABLE_FEATURE_FIND_EMPTY
> +		else if (parm == PARM_empty) {
> +			dbg("%d", __LINE__);
> +			(void) ALLOC_ACTION(empty);
> +		}
> +#endif
>  #if ENABLE_FEATURE_FIND_EXEC
>  		else if (parm == PARM_exec) {
>  			int i;


More information about the busybox mailing list