find: add support for -delete, -path (by Natanael Copa)
This commit is contained in:
parent
945bd3dee8
commit
62f0479cf1
@ -135,6 +135,22 @@ config FEATURE_FIND_PRUNE
|
|||||||
If the file is a directory, dont descend into it. Useful for
|
If the file is a directory, dont descend into it. Useful for
|
||||||
exclusion .svn and CVS directories.
|
exclusion .svn and CVS directories.
|
||||||
|
|
||||||
|
config FEATURE_FIND_DELETE
|
||||||
|
bool "Enable -delete option allowing to delete files"
|
||||||
|
default n
|
||||||
|
depends on FIND && FEATURE_FIND_DEPTH
|
||||||
|
help
|
||||||
|
Support the 'find -delete' option for deleting files and direcotries.
|
||||||
|
WARNING: This option can do much harm if used wrong. Busybox will not
|
||||||
|
try to protect the user from doing stupid things. Use with care.
|
||||||
|
|
||||||
|
config FEATURE_FIND_PATH
|
||||||
|
bool "Enable -path option allowing to match pathname patterns"
|
||||||
|
default y
|
||||||
|
depends on FIND
|
||||||
|
help
|
||||||
|
The -path option matches whole pathnames instead of just filenames.
|
||||||
|
|
||||||
config GREP
|
config GREP
|
||||||
bool "grep"
|
bool "grep"
|
||||||
default n
|
default n
|
||||||
|
@ -79,6 +79,8 @@ USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
|
|||||||
USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
|
USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
|
||||||
USE_FEATURE_FIND_SIZE( ACTS(size, off_t size;))
|
USE_FEATURE_FIND_SIZE( ACTS(size, off_t size;))
|
||||||
USE_FEATURE_FIND_PRUNE( ACTS(prune))
|
USE_FEATURE_FIND_PRUNE( ACTS(prune))
|
||||||
|
USE_FEATURE_FIND_DELETE(ACTS(delete))
|
||||||
|
USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
|
||||||
|
|
||||||
static action ***actions;
|
static action ***actions;
|
||||||
static bool need_print = 1;
|
static bool need_print = 1;
|
||||||
@ -305,6 +307,28 @@ ACTF(prune)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if ENABLE_FEATURE_FIND_DELETE
|
||||||
|
ACTF(delete)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
if (S_ISDIR(statbuf->st_mode)) {
|
||||||
|
rc = rmdir(fileName);
|
||||||
|
} else {
|
||||||
|
rc = unlink(fileName);
|
||||||
|
}
|
||||||
|
if (rc < 0)
|
||||||
|
bb_perror_msg("%s", fileName);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if ENABLE_FEATURE_FIND_PATH
|
||||||
|
ACTF(path)
|
||||||
|
{
|
||||||
|
return fnmatch(ap->pattern, fileName, 0) == 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if ENABLE_FEATURE_FIND_SIZE
|
#if ENABLE_FEATURE_FIND_SIZE
|
||||||
ACTF(size)
|
ACTF(size)
|
||||||
{
|
{
|
||||||
@ -393,6 +417,8 @@ static action*** parse_params(char **argv)
|
|||||||
USE_FEATURE_FIND_PAREN( PARM_char_brace,)
|
USE_FEATURE_FIND_PAREN( PARM_char_brace,)
|
||||||
USE_FEATURE_FIND_SIZE( PARM_size ,)
|
USE_FEATURE_FIND_SIZE( PARM_size ,)
|
||||||
USE_FEATURE_FIND_PRUNE( PARM_prune ,)
|
USE_FEATURE_FIND_PRUNE( PARM_prune ,)
|
||||||
|
USE_FEATURE_FIND_DELETE(PARM_delete ,)
|
||||||
|
USE_FEATURE_FIND_PATH( PARM_path ,)
|
||||||
#if ENABLE_DESKTOP
|
#if ENABLE_DESKTOP
|
||||||
PARM_and ,
|
PARM_and ,
|
||||||
PARM_or ,
|
PARM_or ,
|
||||||
@ -420,6 +446,8 @@ static action*** parse_params(char **argv)
|
|||||||
USE_FEATURE_FIND_PAREN( "(" ,)
|
USE_FEATURE_FIND_PAREN( "(" ,)
|
||||||
USE_FEATURE_FIND_SIZE( "-size" ,)
|
USE_FEATURE_FIND_SIZE( "-size" ,)
|
||||||
USE_FEATURE_FIND_PRUNE( "-prune" ,)
|
USE_FEATURE_FIND_PRUNE( "-prune" ,)
|
||||||
|
USE_FEATURE_FIND_DELETE("-delete",)
|
||||||
|
USE_FEATURE_FIND_PATH( "-path" ,)
|
||||||
#if ENABLE_DESKTOP
|
#if ENABLE_DESKTOP
|
||||||
"-and" ,
|
"-and" ,
|
||||||
"-or" ,
|
"-or" ,
|
||||||
@ -656,6 +684,22 @@ static action*** parse_params(char **argv)
|
|||||||
(void) ALLOC_ACTION(prune);
|
(void) ALLOC_ACTION(prune);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#if ENABLE_FEATURE_FIND_DELETE
|
||||||
|
else if (parm == PARM_delete) {
|
||||||
|
need_print = 0;
|
||||||
|
recurse_flags |= ACTION_DEPTHFIRST;
|
||||||
|
(void) ALLOC_ACTION(delete);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if ENABLE_FEATURE_FIND_PATH
|
||||||
|
else if (parm == PARM_path) {
|
||||||
|
action_path *ap;
|
||||||
|
if (!*++argv)
|
||||||
|
bb_error_msg_and_die(bb_msg_requires_arg, arg);
|
||||||
|
ap = ALLOC_ACTION(path);
|
||||||
|
ap->pattern = arg1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#if ENABLE_FEATURE_FIND_SIZE
|
#if ENABLE_FEATURE_FIND_SIZE
|
||||||
else if (parm == PARM_size) {
|
else if (parm == PARM_size) {
|
||||||
action_size *ap;
|
action_size *ap;
|
||||||
|
@ -968,6 +968,10 @@
|
|||||||
"\n -size N File size is N" \
|
"\n -size N File size is N" \
|
||||||
) USE_FEATURE_FIND_PRUNE( \
|
) USE_FEATURE_FIND_PRUNE( \
|
||||||
"\n -prune Stop traversing current subtree" \
|
"\n -prune Stop traversing current subtree" \
|
||||||
|
) USE_FEATURE_FIND_DELETE( \
|
||||||
|
"\n -delete Delete files; Turns on -depth option" \
|
||||||
|
) USE_FEATURE_FIND_PATH( \
|
||||||
|
"\n -path Path matches PATTERN" \
|
||||||
) USE_FEATURE_FIND_PAREN( \
|
) USE_FEATURE_FIND_PAREN( \
|
||||||
"\n (EXPR) Group an expression" \
|
"\n (EXPR) Group an expression" \
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user