32HHP5CWAWJB2Q3Q6HYEC66YFGQAV6ESU7VEQ5ENBAQ5ZCRP4KGAC LocalBinaryCache::LocalBinaryCache(ref<Store> localStore, const Path & binaryCacheDir): localStore(localStore), binaryCacheDir(binaryCacheDir)
LocalBinaryCache::LocalBinaryCache(ref<Store> localStore, const Path & binaryCacheDir,const Path & secretKeyFile, const Path & publicKeyFile): localStore(localStore), binaryCacheDir(binaryCacheDir)
Path cacheInfoFile = binaryCacheDir + "/nix-cache-info";if (!pathExists(cacheInfoFile))writeFile(cacheInfoFile, "StoreDir: " + settings.nixStore + "\n");if (secretKeyFile != "")secretKey = std::unique_ptr<SecretKey>(new SecretKey(readFile(secretKeyFile)));if (publicKeyFile != "") {publicKeys = std::unique_ptr<PublicKeys>(new PublicKeys);auto key = PublicKey(readFile(publicKeyFile));publicKeys->emplace(key.name, key);}
string narFileRel = "nar/" + printHash32(narXzHash) + ".nar.xz";Path narFile = binaryCacheDir + "/" + narFileRel;
narInfo.url = "nar/" + printHash32(narInfo.fileHash) + ".nar.xz";Path narFile = binaryCacheDir + "/" + narInfo.url;
Strings refs;for (auto & r : info.references)refs.push_back(baseNameOf(r));std::string narInfo;narInfo += "StorePath: " + info.path + "\n";narInfo += "URL: " + narFileRel + "\n";narInfo += "Compression: xz\n";narInfo += "FileHash: sha256:" + printHash32(narXzHash) + "\n";narInfo += "FileSize: " + std::to_string(narXz.size()) + "\n";narInfo += "NarHash: sha256:" + printHash32(narHash) + "\n";narInfo += "NarSize: " + std::to_string(narSize) + "\n";narInfo += "References: " + concatStringsSep(" ", refs) + "\n";
if (secretKey) narInfo.sign(*secretKey);
if (!pathExists(narInfoFile))abort();std::string narInfo = readFile(narInfoFile);auto corrupt = [&]() {throw Error(format("corrupt NAR info file ‘%1%’") % narInfoFile);};size_t pos = 0;while (pos < narInfo.size()) {size_t colon = narInfo.find(':', pos);if (colon == std::string::npos) corrupt();std::string name(narInfo, pos, colon - pos);size_t eol = narInfo.find('\n', colon + 2);if (eol == std::string::npos) corrupt();std::string value(narInfo, colon + 2, eol - colon - 2);if (name == "StorePath") {res.info.path = value;if (value != storePath) corrupt();res.info.path = value;}else if (name == "References") {auto refs = tokenizeString<Strings>(value, " ");if (!res.info.references.empty()) corrupt();for (auto & r : refs)res.info.references.insert(settings.nixStore + "/" + r);}else if (name == "URL") {res.narUrl = value;}else if (name == "Compression") {res.compression = value;}
NarInfo narInfo = NarInfo(readFile(narInfoFile), narInfoFile);assert(narInfo.path == storePath);