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

11
chgrp.c
View File

@ -14,18 +14,17 @@ static gid_t gid = -1;
static int ret = 0; static int ret = 0;
static void static void
chgrp(const char *path, struct stat *st, void *data, struct recursor *r) chgrp(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
{ {
int flags = 0; int flags = 0;
if ((r->maxdepth == 0 && r->follow == 'P') || (r->follow == 'H' && r->depth) || (hflag && !(r->depth))) if ((r->maxdepth == 0 && r->follow == 'P') || (r->follow == 'H' && r->depth) || (hflag && !(r->depth)))
flags |= AT_SYMLINK_NOFOLLOW; flags |= AT_SYMLINK_NOFOLLOW;
if (fchownat(dirfd, name, -1, gid, flags) < 0) {
if (fchownat(AT_FDCWD, path, -1, gid, flags) < 0) { weprintf("chown %s:", r->path);
weprintf("chown %s:", path);
ret = 1; ret = 1;
} else if (S_ISDIR(st->st_mode)) { } else if (S_ISDIR(st->st_mode)) {
recurse(path, NULL, r); recurse(dirfd, name, NULL, r);
} }
} }
@ -71,7 +70,7 @@ main(int argc, char *argv[])
} }
for (argc--, argv++; *argv; argc--, argv++) for (argc--, argv++; *argv; argc--, argv++)
recurse(*argv, NULL, &r); recurse(AT_FDCWD, *argv, NULL, &r);
return ret || recurse_status; return ret || recurse_status;
} }

11
chmod.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "fs.h" #include "fs.h"
@ -9,16 +10,16 @@ static mode_t mask = 0;
static int ret = 0; static int ret = 0;
static void static void
chmodr(const char *path, struct stat *st, void *data, struct recursor *r) chmodr(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
{ {
mode_t m; mode_t m;
m = parsemode(modestr, st->st_mode, mask); m = parsemode(modestr, st->st_mode, mask);
if (!S_ISLNK(st->st_mode) && chmod(path, m) < 0) { if (!S_ISLNK(st->st_mode) && fchmodat(dirfd, name, m, 0) < 0) {
weprintf("chmod %s:", path); weprintf("chmod %s:", r->path);
ret = 1; ret = 1;
} else if (S_ISDIR(st->st_mode)) { } else if (S_ISDIR(st->st_mode)) {
recurse(path, NULL, r); recurse(dirfd, name, NULL, r);
} }
} }
@ -71,7 +72,7 @@ done:
usage(); usage();
for (--argc, ++argv; *argv; argc--, argv++) for (--argc, ++argv; *argv; argc--, argv++)
recurse(*argv, NULL, &r); recurse(AT_FDCWD, *argv, NULL, &r);
return ret || recurse_status; return ret || recurse_status;
} }

10
chown.c
View File

@ -17,18 +17,18 @@ static gid_t gid = -1;
static int ret = 0; static int ret = 0;
static void static void
chownpwgr(const char *path, struct stat *st, void *data, struct recursor *r) chownpwgr(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
{ {
int flags = 0; int flags = 0;
if ((r->maxdepth == 0 && r->follow == 'P') || (r->follow == 'H' && r->depth) || (hflag && !(r->depth))) if ((r->maxdepth == 0 && r->follow == 'P') || (r->follow == 'H' && r->depth) || (hflag && !(r->depth)))
flags |= AT_SYMLINK_NOFOLLOW; flags |= AT_SYMLINK_NOFOLLOW;
if (fchownat(AT_FDCWD, path, uid, gid, flags) < 0) { if (fchownat(dirfd, name, uid, gid, flags) < 0) {
weprintf("chown %s:", path); weprintf("chown %s:", r->path);
ret = 1; ret = 1;
} else if (S_ISDIR(st->st_mode)) { } else if (S_ISDIR(st->st_mode)) {
recurse(path, NULL, r); recurse(dirfd, name, NULL, r);
} }
} }
@ -99,7 +99,7 @@ main(int argc, char *argv[])
usage(); usage();
for (argc--, argv++; *argv; argc--, argv++) for (argc--, argv++; *argv; argc--, argv++)
recurse(*argv, NULL, &r); recurse(AT_FDCWD, *argv, NULL, &r);
return ret || recurse_status; return ret || recurse_status;
} }

13
du.c
View File

@ -3,6 +3,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -35,19 +36,19 @@ nblks(blkcnt_t blocks)
} }
static void static void
du(const char *path, struct stat *st, void *data, struct recursor *r) du(int dirfd, const char *path, struct stat *st, void *data, struct recursor *r)
{ {
off_t *total = data, subtotal; off_t *total = data, subtotal;
subtotal = nblks(st->st_blocks); subtotal = nblks(st->st_blocks);
if (S_ISDIR(st->st_mode)) if (S_ISDIR(st->st_mode))
recurse(path, &subtotal, r); recurse(dirfd, path, &subtotal, r);
*total += subtotal; *total += subtotal;
if (!r->depth) if (!r->depth)
printpath(*total, path); printpath(*total, r->path);
else if (!sflag && r->depth <= maxdepth && (S_ISDIR(st->st_mode) || aflag)) else if (!sflag && r->depth <= maxdepth && (S_ISDIR(st->st_mode) || aflag))
printpath(subtotal, path); printpath(subtotal, r->path);
} }
static void static void
@ -104,11 +105,11 @@ main(int argc, char *argv[])
blksize = 1024; blksize = 1024;
if (!argc) { if (!argc) {
recurse(".", &n, &r); recurse(AT_FDCWD, ".", &n, &r);
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
n = 0; n = 0;
recurse(*argv, &n, &r); recurse(AT_FDCWD, *argv, &n, &r);
} }
} }

9
fs.h
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -9,7 +10,9 @@ struct history {
}; };
struct recursor { struct recursor {
void (*fn)(const char *, struct stat *st, void *, struct recursor *); void (*fn)(int, const char *, struct stat *, void *, struct recursor *);
char path[PATH_MAX];
size_t pathlen;
struct history *hist; struct history *hist;
int depth; int depth;
int maxdepth; int maxdepth;
@ -37,7 +40,7 @@ extern int rm_status;
extern int recurse_status; extern int recurse_status;
void recurse(const char *, void *, struct recursor *); void recurse(int, const char *, void *, struct recursor *);
int cp(const char *, const char *, int); int cp(const char *, const char *, int);
void rm(const char *, struct stat *st, void *, struct recursor *); void rm(int, const char *, struct stat *st, void *, struct recursor *);

View File

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

View File

@ -2,6 +2,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -11,20 +12,20 @@
int rm_status = 0; int rm_status = 0;
void 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)) { 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)) if (!(r->flags & SILENT))
weprintf("rmdir %s:", path); weprintf("rmdir %s:", r->path);
if (!((r->flags & SILENT) && errno == ENOENT)) if (!((r->flags & SILENT) && errno == ENOENT))
rm_status = 1; rm_status = 1;
} }
} else if (unlink(path) < 0) { } else if (unlinkat(dirfd, name, 0) < 0) {
if (!(r->flags & SILENT)) if (!(r->flags & SILENT))
weprintf("unlink %s:", path); weprintf("unlink %s:", r->path);
if (!((r->flags & SILENT) && errno == ENOENT)) if (!((r->flags & SILENT) && errno == ENOENT))
rm_status = 1; rm_status = 1;
} }

3
mv.c
View File

@ -2,6 +2,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include "fs.h" #include "fs.h"
@ -21,7 +22,7 @@ mv(const char *s1, const char *s2, int depth)
cp_aflag = cp_rflag = cp_pflag = 1; cp_aflag = cp_rflag = cp_pflag = 1;
cp_follow = 'P'; cp_follow = 'P';
cp(s1, s2, depth); cp(s1, s2, depth);
recurse(s1, NULL, &r); recurse(AT_FDCWD, s1, NULL, &r);
return (mv_status = cp_status || rm_status); return (mv_status = cp_status || rm_status);
} }
mv_status = 1; mv_status = 1;

4
rm.c
View File

@ -1,4 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <fcntl.h>
#include "fs.h" #include "fs.h"
#include "util.h" #include "util.h"
@ -34,7 +36,7 @@ main(int argc, char *argv[])
} }
for (; *argv; argc--, argv++) for (; *argv; argc--, argv++)
recurse(*argv, NULL, &r); recurse(AT_FDCWD, *argv, NULL, &r);
return rm_status || recurse_status; return rm_status || recurse_status;
} }

10
tar.c
View File

@ -368,14 +368,14 @@ print(char *fname, ssize_t l, char b[BLKSIZ])
} }
static void static void
c(const char *path, struct stat *st, void *data, struct recursor *r) c(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
{ {
archive(path); archive(r->path);
if (vflag) if (vflag)
puts(path); puts(r->path);
if (S_ISDIR(st->st_mode)) if (S_ISDIR(st->st_mode))
recurse(path, NULL, r); recurse(dirfd, name, NULL, r);
} }
static void static void
@ -578,7 +578,7 @@ main(int argc, char *argv[])
if (chdir(dir) < 0) if (chdir(dir) < 0)
eprintf("chdir %s:", dir); eprintf("chdir %s:", dir);
for (; *argv; argc--, argv++) for (; *argv; argc--, argv++)
recurse(*argv, NULL, &r); recurse(AT_FDCWD, *argv, NULL, &r);
break; break;
case 't': case 't':
case 'x': case 'x':