summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2015-09-08 17:04:13 -0400
committerTavian Barnes <tavianator@tavianator.com>2015-09-08 17:11:39 -0400
commit72460c098f31c87deefc7daf9eeea3a4ad8224ab (patch)
tree6f5af25602bf0f8b84e8ecc54168e7d0f262064b
parent12b0b1f572cba2556722d282bb884a52269fe965 (diff)
downloadbfs-72460c098f31c87deefc7daf9eeea3a4ad8224ab.tar.xz
Implement -delete.
Related to #5.
-rw-r--r--bfs.c25
-rw-r--r--color.c4
-rw-r--r--color.h6
3 files changed, 29 insertions, 6 deletions
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 <errno.h>
+#include <fcntl.h>
#include <fnmatch.h>
#include <limits.h>
#include <stdbool.h>
@@ -283,6 +285,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.
*/
static bool eval_prune(const expression *expr, eval_state *state) {
@@ -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;
}
diff --git a/color.c b/color.c
index d994d17..2928fe2 100644
--- a/color.c
+++ b/color.c
@@ -309,7 +309,7 @@ void pretty_print(const color_table *colors, const char *fpath, const struct BFT
fputs("\n", stdout);
}
-void print_error(const color_table *colors, const char *fpath, const struct BFTW *ftwbuf) {
+void print_error(const color_table *colors, const char *fpath, int error) {
const char *color = NULL;
if (colors) {
color = colors->orphan;
@@ -318,7 +318,7 @@ void print_error(const color_table *colors, const char *fpath, const struct BFTW
if (color) {
print_esc(color, stderr);
}
- fprintf(stderr, "Error at %s: %s\n", fpath, strerror(ftwbuf->error));
+ fprintf(stderr, "Error at %s: %s\n", fpath, strerror(error));
if (color) {
print_esc(colors->reset, stderr);
}
diff --git a/color.h b/color.h
index ad2b53c..25523de 100644
--- a/color.h
+++ b/color.h
@@ -47,10 +47,10 @@ void pretty_print(const color_table *colors, const char *fpath, const struct BFT
* The color table to use.
* @param fpath
* The file path in error.
- * @param ftwbuf
- * The bftw() data for fpath.
+ * @param error
+ * The error code that occurred.
*/
-void print_error(const color_table *colors, const char *fpath, const struct BFTW *ftwbuf);
+void print_error(const color_table *colors, const char *fpath, int error);
/**
* Free a color table.