]> git.sesse.net Git - plocate/blob - access_rx_cache.cpp
Fix detection of -latomic (it doesn't come from pkg-config).
[plocate] / access_rx_cache.cpp
1 #include "access_rx_cache.h"
2
3 #include "io_uring_engine.h"
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <utility>
9
10 using namespace std;
11
12 void AccessRXCache::check_access(const char *filename, bool allow_async, function<void(bool)> cb)
13 {
14         lock_guard<mutex> lock(mu);
15         if (engine == nullptr || !engine->get_supports_stat()) {
16                 allow_async = false;
17         }
18
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) {
25                                 cb(false);
26                                 return;
27                         }
28                         continue;
29                 }
30
31                 if (!allow_async) {
32                         bool ok = access(parent_path.c_str(), R_OK | X_OK) == 0;
33                         cache.emplace(parent_path, ok);
34                         if (!ok) {
35                                 cb(false);
36                                 return;
37                         }
38                         continue;
39                 }
40
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.
44
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) });
51                 } else {
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));
58                                 free(filename);
59
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));
66                                 }
67                         });
68                 }
69                 return;  // The rest will happen in async context.
70         }
71
72         // Passed all checks.
73         cb(true);
74 }