readdir_for_dirs: Actually filter out non-dirs

This function is supposed to return "the next directory". Because this
is imperfect, it only tries to.

Except it went to all the trouble of figuring out the type and then
just... returned it anyway.

This has nice speedups in globs with directory components like `*/` or
`**`. I have observed 1.1x to 2.0x.

We could also return when we know it's definitely a directory and then
skip a stat() later, but preliminary testing seemed to show that's not
worth much.
This commit is contained in:
Fabian Boehm 2022-07-12 16:50:00 +02:00
parent dbb4e05254
commit 526b7e3b1b

View File

@ -96,6 +96,10 @@ bool wreaddir(DIR *dir, wcstring &out_name) {
return true;
}
/// Wrapper for readdir that tries to only return directories.
/// This is only supported on some (file)systems,
/// and includes links,
/// so the caller still needs to check.
bool readdir_for_dirs(DIR *dir, std::string *out_name) {
struct dirent *result = nullptr;
while (!result) {
@ -110,7 +114,9 @@ bool readdir_for_dirs(DIR *dir, std::string *out_name) {
break; // these may be directories
}
default: {
break; // nothing else can
// these definitely aren't - skip
result = nullptr;
continue;
}
}
#else