From 72460c098f31c87deefc7daf9eeea3a4ad8224ab Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 8 Sep 2015 17:04:13 -0400 Subject: Implement -delete. Related to #5. --- bfs.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'bfs.c') diff --git a/bfs.c b/bfs.c index 608cd34..1383589 100644 --- a/bfs.c +++ b/bfs.c @@ -11,6 +11,8 @@ #include "bftw.h" #include "color.h" +#include +#include #include #include #include @@ -282,6 +284,24 @@ static const char *skip_paths(parser_state *state) { } } +/** + * -delete action. + */ +static bool eval_delete(const expression *expr, eval_state *state) { + int flag = 0; + if (state->ftwbuf->typeflag == BFTW_DIR) { + flag |= AT_REMOVEDIR; + } + + // TODO: Have bftw() tell us a better base fd to use + if (unlinkat(AT_FDCWD, state->fpath, flag) != 0) { + print_error(state->cl->colors, state->fpath, errno); + state->ret = BFTW_STOP; + } + + return true; +} + /** * -prune action. */ @@ -479,6 +499,9 @@ static expression *parse_literal(parser_state *state) { } else if (strcmp(arg, "-nocolor") == 0) { state->cl->color = false; return new_option(state); + } else if (strcmp(arg, "-delete") == 0) { + state->cl->flags |= BFTW_DEPTH; + return new_action(state, eval_delete); } else if (strcmp(arg, "-depth") == 0) { state->cl->flags |= BFTW_DEPTH; return new_option(state); @@ -835,7 +858,7 @@ static int cmdline_callback(const char *fpath, const struct BFTW *ftwbuf, void * const cmdline *cl = ptr; if (ftwbuf->typeflag == BFTW_ERROR) { - print_error(cl->colors, fpath, ftwbuf); + print_error(cl->colors, fpath, ftwbuf->error); return BFTW_SKIP_SUBTREE; } -- cgit v1.2.3