libutil/recurse: Use a single path buffer, and directory fd

This way, we don't use PATH_MAX bytes on the stack per path component,
and don't have to keep copying the complete path around.
This commit is contained in:
Michael Forney
2019-12-22 13:53:46 -08:00
parent 039b54aa51
commit edbcc223ea
10 changed files with 72 additions and 57 deletions

View File

@@ -2,6 +2,7 @@
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
@@ -11,20 +12,20 @@
int rm_status = 0;
void
rm(const char *path, struct stat *st, void *data, struct recursor *r)
rm(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
{
if (!r->maxdepth && S_ISDIR(st->st_mode)) {
recurse(path, NULL, r);
recurse(dirfd, name, NULL, r);
if (rmdir(path) < 0) {
if (unlinkat(dirfd, name, AT_REMOVEDIR) < 0) {
if (!(r->flags & SILENT))
weprintf("rmdir %s:", path);
weprintf("rmdir %s:", r->path);
if (!((r->flags & SILENT) && errno == ENOENT))
rm_status = 1;
}
} else if (unlink(path) < 0) {
} else if (unlinkat(dirfd, name, 0) < 0) {
if (!(r->flags & SILENT))
weprintf("unlink %s:", path);
weprintf("unlink %s:", r->path);
if (!((r->flags & SILENT) && errno == ENOENT))
rm_status = 1;
}