make rmrfdir use lstat() when d_type == DT_UNKNOWN

This commit is contained in:
rtldg 2022-07-30 01:45:18 +00:00
parent 066ed2b2b2
commit 9b02a91311

View file

@ -30,9 +30,11 @@
#include "libc/x/x.h" #include "libc/x/x.h"
static int rmrfdir(const char *dirpath) { static int rmrfdir(const char *dirpath) {
int rc; int rc, err;
DIR *d; DIR *d;
char *path; char *path;
bool isdir;
struct stat st;
struct dirent *e; struct dirent *e;
if (!(d = opendir(dirpath))) return -1; if (!(d = opendir(dirpath))) return -1;
while ((e = readdir(d))) { while ((e = readdir(d))) {
@ -40,11 +42,26 @@ static int rmrfdir(const char *dirpath) {
if (!strcmp(e->d_name, "..")) continue; if (!strcmp(e->d_name, "..")) continue;
assert(!strchr(e->d_name, '/')); assert(!strchr(e->d_name, '/'));
path = xjoinpaths(dirpath, 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); rc = rmrfdir(path);
} else { } else {
rc = unlink(path); rc = unlink(path);
} }
errcheck:
free(path); free(path);
if (rc == -1) { if (rc == -1) {
closedir(d); closedir(d);