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

@@ -16,27 +16,30 @@
int recurse_status = 0;
void
recurse(const char *path, void *data, struct recursor *r)
recurse(int dirfd, const char *name, void *data, struct recursor *r)
{
struct dirent *d;
struct history *new, *h;
struct stat st, dst;
DIR *dp;
char subpath[PATH_MAX];
int flags = 0;
int flags = 0, fd;
size_t pathlen = r->pathlen;
if (dirfd == AT_FDCWD)
pathlen = estrlcpy(r->path, name, sizeof(r->path));
if (r->follow == 'P' || (r->follow == 'H' && r->depth))
flags |= AT_SYMLINK_NOFOLLOW;
if (fstatat(AT_FDCWD, path, &st, flags) < 0) {
if (fstatat(dirfd, name, &st, flags) < 0) {
if (!(r->flags & SILENT)) {
weprintf("stat %s:", path);
weprintf("stat %s:", r->path);
recurse_status = 1;
}
return;
}
if (!S_ISDIR(st.st_mode)) {
r->fn(path, &st, data, r);
r->fn(dirfd, name, &st, data, r);
return;
}
@@ -51,35 +54,39 @@ recurse(const char *path, void *data, struct recursor *r)
return;
if (!r->depth && (r->flags & DIRFIRST))
r->fn(path, &st, data, r);
r->fn(dirfd, name, &st, data, r);
if (!r->maxdepth || r->depth + 1 < r->maxdepth) {
if (!(dp = opendir(path))) {
fd = openat(dirfd, name, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
if (fd < 0) {
weprintf("open %s:", r->path);
recurse_status = 1;
}
if (!(dp = fdopendir(fd))) {
if (!(r->flags & SILENT)) {
weprintf("opendir %s:", path);
weprintf("fdopendir:");
recurse_status = 1;
}
return;
}
if (r->path[pathlen - 1] != '/')
pathlen += estrlcpy(r->path + pathlen, "/", sizeof(r->path) - pathlen);
if (r->follow == 'H')
flags |= AT_SYMLINK_NOFOLLOW;
while ((d = readdir(dp))) {
if (r->follow == 'H')
flags |= AT_SYMLINK_NOFOLLOW;
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
estrlcpy(subpath, path, sizeof(subpath));
if (path[strlen(path) - 1] != '/')
estrlcat(subpath, "/", sizeof(subpath));
estrlcat(subpath, d->d_name, sizeof(subpath));
if (fstatat(AT_FDCWD, subpath, &dst, flags) < 0) {
r->pathlen = pathlen + estrlcpy(r->path + pathlen, d->d_name, sizeof(r->path) - pathlen);
if (fstatat(fd, d->d_name, &dst, flags) < 0) {
if (!(r->flags & SILENT)) {
weprintf("stat %s:", subpath);
weprintf("stat %s:", r->path);
recurse_status = 1;
}
} else if ((r->flags & SAMEDEV) && dst.st_dev != st.st_dev) {
continue;
} else {
r->depth++;
r->fn(subpath, &dst, data, r);
r->fn(fd, d->d_name, &dst, data, r);
r->depth--;
}
}
@@ -88,7 +95,7 @@ recurse(const char *path, void *data, struct recursor *r)
if (!r->depth) {
if (!(r->flags & DIRFIRST))
r->fn(path, &st, data, r);
r->fn(dirfd, name, &st, data, r);
while (r->hist) {
h = r->hist;

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;
}