| 1 | #include <regex> |
| 2 | |
| 3 | #include <nlohmann/json.hpp> |
| 4 | |
| 5 | #include <nix/util/signals.hh> |
| 6 | #include <nix/util/thread-pool.hh> |
| 7 | |
| 8 | #include <nix/store/nar-info.hh> |
| 9 | #include <nix/store/s3-binary-cache-store.hh> |
| 10 | #include <nix/store/sqlite.hh> |
| 11 | |
| 12 | #include <nix/main/shared.hh> |
| 13 | |
| 14 | // cache.nixos.org/debuginfo/<build-id> |
| 15 | // => redirect to NAR |
| 16 | |
| 17 | using namespace nix; |
| 18 | |
| 19 | void mainWrapped(int argc, char * * argv) |
| 20 | { |
| 21 | initNix(); |
| 22 | |
| 23 | if (argc != 3) throw Error("usage: index-debuginfo DEBUG-DB BINARY-CACHE-URI"); |
| 24 | |
| 25 | Path debugDbPath = argv[1]; |
| 26 | std::string binaryCacheUri = argv[2]; |
| 27 | |
| 28 | if (hasSuffix(binaryCacheUri, "/")) binaryCacheUri.pop_back(); |
| 29 | auto binaryCache = openStore(binaryCacheUri).cast<S3BinaryCacheStore>(); |
| 30 | |
| 31 | ThreadPool threadPool(25); |
| 32 | |
| 33 | auto doFile = [&](std::string build_id, std::string url, std::string filename) { |
| 34 | checkInterrupt(); |
| 35 | |
| 36 | nlohmann::json json; |
| 37 | json["archive"] = url; |
| 38 | json["member"] = filename; |
| 39 | |
| 40 | std::string key = "debuginfo/" + build_id; |
| 41 | |
| 42 | // FIXME: or should we overwrite? The previous link may point |
| 43 | // to a GC'ed file, so overwriting might be useful... |
| 44 | if (binaryCache->fileExists(key)) return; |
| 45 | |
| 46 | printError("redirecting ‘%s’ to ‘%s’", key, filename); |
| 47 | |
| 48 | binaryCache->upsertFile(key, json.dump(), "application/json"); |
| 49 | }; |
| 50 | |
| 51 | auto db = SQLite(debugDbPath); |
| 52 | |
| 53 | auto stmt = SQLiteStmt(db, "select build_id, url, filename from DebugInfo;"); |
| 54 | auto query = stmt.use(); |
| 55 | |
| 56 | while (query.next()) { |
| 57 | threadPool.enqueue(std::bind(doFile, query.getStr(0), query.getStr(1), query.getStr(2))); |
| 58 | } |
| 59 | |
| 60 | threadPool.process(); |
| 61 | } |
| 62 | |
| 63 | int main(int argc, char * * argv) |
| 64 | { |
| 65 | return handleExceptions(argv[0], [&]() { |
| 66 | mainWrapped(argc, argv); |
| 67 | }); |
| 68 | } |