From 9b02a913118ac418f3ab9edd906f532dd236b757 Mon Sep 17 00:00:00 2001 From: rtldg Date: Sat, 30 Jul 2022 01:45:18 +0000 Subject: [PATCH] make rmrfdir use lstat() when d_type == DT_UNKNOWN --- libc/x/rmrf.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/libc/x/rmrf.c b/libc/x/rmrf.c index b84d78191..249b5aa70 100644 --- a/libc/x/rmrf.c +++ b/libc/x/rmrf.c @@ -30,9 +30,11 @@ #include "libc/x/x.h" static int rmrfdir(const char *dirpath) { - int rc; + int rc, err; DIR *d; char *path; + bool isdir; + struct stat st; struct dirent *e; if (!(d = opendir(dirpath))) return -1; while ((e = readdir(d))) { @@ -40,11 +42,26 @@ static int rmrfdir(const char *dirpath) { if (!strcmp(e->d_name, "..")) continue; assert(!strchr(e->d_name, '/')); path = xjoinpaths(dirpath, e->d_name); - if (e->d_type == DT_DIR) { + if (e->d_type == DT_UNKNOWN) { + err = errno; + if ((rc = lstat(path, &st)) == -1) { + if (errno == ENOENT) { + errno = err; + rc = 0; + } + goto errcheck; + } else { + isdir = S_ISDIR(st.st_mode); + } + } else { + isdir = e->d_type == DT_DIR; + } + if (isdir) { rc = rmrfdir(path); } else { rc = unlink(path); } + errcheck: free(path); if (rc == -1) { closedir(d);