-
-void apply_limit()
-{
- if (--limit_left > 0) {
- return;
- }
- dprintf("Done in %.1f ms, found %" PRId64 " matches.\n",
- 1e3 * duration<float>(steady_clock::now() - start).count(), limit_matches);
- if (only_count) {
- printf("%ld\n", limit_matches);
- }
- exit(0);
-}
-
-class Serializer {
-public:
- ~Serializer() { assert(limit_left <= 0 || pending.empty()); }
- void print(uint64_t seq, uint64_t skip, const string msg);
-
-private:
- uint64_t next_seq = 0;
- struct Element {
- uint64_t seq, skip;
- string msg;
-
- bool operator<(const Element &other) const
- {
- return seq > other.seq;
- }
- };
- priority_queue<Element> pending;
-};
-
-void Serializer::print(uint64_t seq, uint64_t skip, const string msg)
-{
- if (only_count) {
- if (!msg.empty()) {
- apply_limit();
- }
- return;
- }
-
- if (next_seq != seq) {
- pending.push(Element{ seq, skip, move(msg) });
- return;
- }
-
- if (!msg.empty()) {
- if (print_nul) {
- printf("%s%c", msg.c_str(), 0);
- } else {
- printf("%s\n", msg.c_str());
- }
- apply_limit();
- }
- next_seq += skip;
-
- // See if any delayed prints can now be dealt with.
- while (!pending.empty() && pending.top().seq == next_seq) {
- if (!pending.top().msg.empty()) {
- if (print_nul) {
- printf("%s%c", pending.top().msg.c_str(), 0);
- } else {
- printf("%s\n", pending.top().msg.c_str());
- }
- apply_limit();
- }
- next_seq += pending.top().skip;
- pending.pop();
- }
-}
-
-struct Needle {
- enum { STRSTR,
- REGEX,
- GLOB } type;
- string str; // Filled in no matter what.
- regex_t re; // For REGEX.
-};
-
-bool matches(const Needle &needle, const char *haystack)
-{
- if (needle.type == Needle::STRSTR) {
- return strstr(haystack, needle.str.c_str()) != nullptr;
- } else if (needle.type == Needle::GLOB) {
- int flags = ignore_case ? FNM_CASEFOLD : 0;
- return fnmatch(needle.str.c_str(), haystack, flags) == 0;
- } else {
- assert(needle.type == Needle::REGEX);
- return regexec(&needle.re, haystack, /*nmatch=*/0, /*pmatch=*/nullptr, /*flags=*/0) == 0;
- }
-}
-
-class AccessRXCache {
-public:
- AccessRXCache(IOUringEngine *engine)
- : engine(engine) {}
- void check_access(const char *filename, bool allow_async, function<void(bool)> cb);
-
-private:
- unordered_map<string, bool> cache;
- struct PendingStat {
- string filename;
- function<void(bool)> cb;
- };
- map<string, vector<PendingStat>> pending_stats;
- IOUringEngine *engine;
-};
-
-void AccessRXCache::check_access(const char *filename, bool allow_async, function<void(bool)> cb)
-{
- if (!engine->get_supports_stat()) {
- allow_async = false;
- }
-
- for (const char *end = strchr(filename + 1, '/'); end != nullptr; end = strchr(end + 1, '/')) {
- string parent_path(filename, end - filename); // string_view from C++20.
- auto cache_it = cache.find(parent_path);
- if (cache_it != cache.end()) {
- // Found in the cache.
- if (!cache_it->second) {
- cb(false);
- return;
- }
- continue;
- }
-
- if (!allow_async) {
- bool ok = access(parent_path.c_str(), R_OK | X_OK) == 0;
- cache.emplace(parent_path, ok);
- if (!ok) {
- cb(false);
- return;
- }
- continue;
- }
-
- // We want to call access(), but it could block on I/O. io_uring doesn't support
- // access(), but we can do a dummy asynchonous statx() to populate the kernel's cache,
- // which nearly always makes the next access() instantaneous.
-
- // See if there's already a pending stat that matches this,
- // or is a subdirectory.
- auto it = pending_stats.lower_bound(parent_path);
- if (it != pending_stats.end() && it->first.size() >= parent_path.size() &&
- it->first.compare(0, parent_path.size(), parent_path) == 0) {
- it->second.emplace_back(PendingStat{ filename, move(cb) });
- } else {
- it = pending_stats.emplace(filename, vector<PendingStat>{}).first;
- engine->submit_stat(filename, [this, it, filename{ strdup(filename) }, cb{ move(cb) }] {
- // The stat returned, so now do the actual access() calls.
- // All of them should be in cache, so don't fire off new statx()
- // calls during that check.
- check_access(filename, /*allow_async=*/false, move(cb));
- free(filename);
-
- // Call all others that waited for the same stat() to finish.
- // They may fire off new stat() calls if needed.
- vector<PendingStat> pending = move(it->second);
- pending_stats.erase(it);
- for (PendingStat &ps : pending) {
- check_access(ps.filename.c_str(), /*allow_async=*/true, move(ps.cb));
- }
- });
- }
- return; // The rest will happen in async context.
- }
-
- // Passed all checks.
- cb(true);
-}