General cleanup.

- actually print error for bad parent
- declare functions as static when necessary
- clean up headers
- clear up error message
This commit is contained in:
Érico Nogueira 2021-09-18 23:48:56 -03:00
parent 5fcae83d95
commit fdf8fa3c30
3 changed files with 16 additions and 28 deletions

17
erm.c
View File

@ -41,14 +41,14 @@ int main(int argc, char **argv)
}
file_action action = recursive ? recurse_into : single_file;
file_action callback = recursive ? (void*)1 : NULL;
callback_action callback = recursive ? run_queue : NULL;
const char *err_fmt = recursive ?
"failed to delve into '%s': %s\n" : "failed to remove '%s': %s\n";
"failed to queue '%s': %s\n" : "failed to remove '%s': %s\n";
int rv = 0;
for (int i = 0; i < argc; i++) {
const char *path = argv[i];
if (action(path, i)) {
if (action(path)) {
fprintf(stderr, err_fmt, path, strerror(errno));
if (stop_at_error) {
return 1;
@ -57,16 +57,7 @@ int main(int argc, char **argv)
}
}
}
if (callback) {
/*for (int i = 0; i < argc; i++) {
const char *path = argv[i];
if (callback(path, i)) {
fprintf(stderr, err_fmt, path, strerror(errno));
rv = 1;
}
}*/
run_queue();
}
if (callback) callback();
return rv;
}

9
erm.h
View File

@ -1,8 +1,7 @@
typedef int (*file_action)(const char *path, int position);
struct task;
typedef int (*file_action)(const char *path);
typedef int (*callback_action)(void);
/* remove.c */
int recurse_into(const char *, int);
int join_thread(const char *, int);
int single_file(const char *, int);
int recurse_into(const char *);
int single_file(const char *);
int run_queue(void);

View File

@ -63,7 +63,7 @@ error:
static long nproc;
int queue_remove(struct queue *q, struct task *t)
static int queue_remove(struct queue *q, struct task *t)
{
int rv = 0;
pthread_mutex_lock(&q->mtx);
@ -93,12 +93,6 @@ error:
return rv;
}
int recurse_into(const char *path, int c)
{
(void)c;
return queue_add(&queue, strdup(path), DT_UNKNOWN, NULL);
}
static void *process_queue_item(void *arg)
{
struct queue *q = arg;
@ -180,7 +174,7 @@ end:
int rc = atomic_fetch_sub(&recurse->rc, 1);
printf("parent: %04d '%s'\n", rc, recurse->path);
if (rc < 1) {
printf("bad parent: %04d '%s'\n", rc, recurse->path);
fprintf(stderr, "bad parent: %04d '%s'\n", rc, recurse->path);
abort();
}
if (rc == 1) {
@ -252,8 +246,12 @@ int run_queue(void)
return 0;
}
int single_file(const char *path, int c)
int single_file(const char *path)
{
(void)c;
return remove(path);
}
int recurse_into(const char *path)
{
return queue_add(&queue, strdup(path), DT_UNKNOWN, NULL);
}