1 #include "access_rx_cache.h"
3 #include "io_uring_engine.h"
12 void AccessRXCache::check_access(const char *filename, bool allow_async, function<void(bool)> cb)
14 lock_guard<mutex> lock(mu);
15 if (engine == nullptr || !engine->get_supports_stat()) {
19 for (const char *end = strchr(filename + 1, '/'); end != nullptr; end = strchr(end + 1, '/')) {
20 string parent_path(filename, end - filename); // string_view from C++20.
21 auto cache_it = cache.find(parent_path);
22 if (cache_it != cache.end()) {
23 // Found in the cache.
24 if (!cache_it->second) {
32 bool ok = access(parent_path.c_str(), R_OK | X_OK) == 0;
33 cache.emplace(parent_path, ok);
41 // We want to call access(), but it could block on I/O. io_uring doesn't support
42 // access(), but we can do a dummy asynchonous statx() to populate the kernel's cache,
43 // which nearly always makes the next access() instantaneous.
45 // See if there's already a pending stat that matches this,
46 // or is a subdirectory.
47 auto it = pending_stats.lower_bound(parent_path);
48 if (it != pending_stats.end() && it->first.size() >= parent_path.size() &&
49 it->first.compare(0, parent_path.size(), parent_path) == 0) {
50 it->second.emplace_back(PendingStat{ filename, move(cb) });
52 it = pending_stats.emplace(filename, vector<PendingStat>{}).first;
53 engine->submit_stat(filename, [this, it, filename{ strdup(filename) }, cb{ move(cb) }] {
54 // The stat returned, so now do the actual access() calls.
55 // All of them should be in cache, so don't fire off new statx()
56 // calls during that check.
57 check_access(filename, /*allow_async=*/false, move(cb));
60 // Call all others that waited for the same stat() to finish.
61 // They may fire off new stat() calls if needed.
62 vector<PendingStat> pending = move(it->second);
63 pending_stats.erase(it);
64 for (PendingStat &ps : pending) {
65 check_access(ps.filename.c_str(), /*allow_async=*/true, move(ps.cb));
69 return; // The rest will happen in async context.