It turns out that it was quite simple to implement.
I used libposix from dryproject.
#include <posix++.h>
class Walker {
public:
void walk(posix::directory dir) {
dir.for_each([this, dir](auto& dirent) {
if (dirent.name == "." or dirent.name == "..")
return;
if (!handle_dirent(dirent))
return;
struct stat stat;
if (dirent.type == DT_DIR || dirent.type == DT_UNKNOWN) {
int fd = openat(
dir.fd(), dirent.name.c_str(), O_DIRECTORY|O_NOFOLLOW|O_NOATIME);
if (fd < 0) {
if (errno == ENOTDIR || errno == ELOOP)
goto enotdir;
if (errno == ENOENT)
goto enoent;
posix::throw_error(
"openat", "%d, \"%s\"", dir.fd(), dirent.name);
}
posix::directory dir1(fd);
fstat(fd, &stat);
if (handle_directory(dirent, fd, stat))
walk(dir1);
close(fd);
return;
}
enotdir:
try {
dir.stat(dirent.name.c_str(), stat, AT_SYMLINK_NOFOLLOW);
} catch (const posix::runtime_error &error) {
if (error.number() == ENOENT)
goto enoent;
throw;
}
handle_file(dirent, stat);
return;
enoent:
handle_missing(dirent);
});
}
protected:
virtual bool handle_dirent(const posix::directory::entry&) { return true; }
virtual bool handle_directory(
const posix::directory::entry &dirent,
const int fd, const struct stat&) { return true; }
virtual void handle_file(
const posix::directory::entry &dirent,
const struct stat&) {}
virtual void handle_missing(
const posix::directory::entry &dirent) {}
};
GNU find ( , -size $RANDOM find stat , DT_DIR ).