2016-02-29 17:14:39 +02:00
|
|
|
#include "binary-cache-store.hh"
|
2016-03-04 18:23:42 +02:00
|
|
|
#include "globals.hh"
|
2016-05-30 15:53:57 +03:00
|
|
|
#include "nar-info-disk-cache.hh"
|
2016-02-24 15:48:16 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-02-29 17:14:39 +02:00
|
|
|
class LocalBinaryCacheStore : public BinaryCacheStore
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
Path binaryCacheDir;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-05-04 21:15:41 +03:00
|
|
|
LocalBinaryCacheStore(
|
2016-06-01 15:49:12 +03:00
|
|
|
const Params & params, const Path & binaryCacheDir)
|
2016-05-04 21:15:41 +03:00
|
|
|
: BinaryCacheStore(params)
|
2016-04-29 17:47:20 +03:00
|
|
|
, binaryCacheDir(binaryCacheDir)
|
|
|
|
{
|
|
|
|
}
|
2016-02-29 17:14:39 +02:00
|
|
|
|
|
|
|
void init() override;
|
|
|
|
|
2016-04-29 17:26:16 +03:00
|
|
|
std::string getUri() override
|
|
|
|
{
|
|
|
|
return "file://" + binaryCacheDir;
|
|
|
|
}
|
|
|
|
|
2016-02-29 17:14:39 +02:00
|
|
|
protected:
|
|
|
|
|
|
|
|
bool fileExists(const std::string & path) override;
|
|
|
|
|
2017-03-14 16:26:01 +02:00
|
|
|
void upsertFile(const std::string & path,
|
|
|
|
const std::string & data,
|
|
|
|
const std::string & mimeType) override;
|
2016-02-29 17:14:39 +02:00
|
|
|
|
2018-03-28 00:12:31 +03:00
|
|
|
void getFile(const std::string & path, Sink & sink) override
|
2016-09-16 19:54:14 +03:00
|
|
|
{
|
2018-03-27 23:16:01 +03:00
|
|
|
try {
|
2018-03-28 00:12:31 +03:00
|
|
|
readFile(binaryCacheDir + "/" + path, sink);
|
2018-03-27 23:16:01 +03:00
|
|
|
} catch (SysError & e) {
|
2018-03-28 00:12:31 +03:00
|
|
|
if (e.errNo == ENOENT)
|
|
|
|
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path);
|
|
|
|
}
|
2016-09-16 19:54:14 +03:00
|
|
|
}
|
2016-02-29 17:14:39 +02:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet queryAllValidPaths() override
|
2016-04-29 18:34:31 +03:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet paths;
|
2016-04-29 18:34:31 +03:00
|
|
|
|
|
|
|
for (auto & entry : readDirectory(binaryCacheDir)) {
|
|
|
|
if (entry.name.size() != 40 ||
|
|
|
|
!hasSuffix(entry.name, ".narinfo"))
|
|
|
|
continue;
|
2019-12-05 20:11:09 +02:00
|
|
|
paths.insert(parseStorePath(storeDir + "/" + entry.name.substr(0, entry.name.size() - 8)));
|
2016-04-29 18:34:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2016-02-29 17:14:39 +02:00
|
|
|
};
|
|
|
|
|
2016-02-24 15:48:16 +02:00
|
|
|
void LocalBinaryCacheStore::init()
|
|
|
|
{
|
|
|
|
createDirs(binaryCacheDir + "/nar");
|
2019-09-04 20:24:35 +03:00
|
|
|
if (writeDebugInfo)
|
|
|
|
createDirs(binaryCacheDir + "/debuginfo");
|
2016-02-24 15:48:16 +02:00
|
|
|
BinaryCacheStore::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void atomicWrite(const Path & path, const std::string & s)
|
|
|
|
{
|
|
|
|
Path tmp = path + ".tmp." + std::to_string(getpid());
|
|
|
|
AutoDelete del(tmp, false);
|
|
|
|
writeFile(tmp, s);
|
|
|
|
if (rename(tmp.c_str(), path.c_str()))
|
2020-04-22 02:07:07 +03:00
|
|
|
throw SysError("renaming '%1%' to '%2%'", tmp, path);
|
2016-02-24 15:48:16 +02:00
|
|
|
del.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalBinaryCacheStore::fileExists(const std::string & path)
|
|
|
|
{
|
|
|
|
return pathExists(binaryCacheDir + "/" + path);
|
|
|
|
}
|
|
|
|
|
2017-03-14 16:26:01 +02:00
|
|
|
void LocalBinaryCacheStore::upsertFile(const std::string & path,
|
|
|
|
const std::string & data,
|
|
|
|
const std::string & mimeType)
|
2016-02-24 15:48:16 +02:00
|
|
|
{
|
|
|
|
atomicWrite(binaryCacheDir + "/" + path, data);
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:26:16 +03:00
|
|
|
static RegisterStoreImplementation regStore([](
|
2016-06-01 15:49:12 +03:00
|
|
|
const std::string & uri, const Store::Params & params)
|
2016-04-29 17:26:16 +03:00
|
|
|
-> std::shared_ptr<Store>
|
|
|
|
{
|
2016-06-01 16:15:21 +03:00
|
|
|
if (getEnv("_NIX_FORCE_HTTP_BINARY_CACHE_STORE") == "1" ||
|
|
|
|
std::string(uri, 0, 7) != "file://")
|
|
|
|
return 0;
|
2016-05-04 21:15:41 +03:00
|
|
|
auto store = std::make_shared<LocalBinaryCacheStore>(params, std::string(uri, 7));
|
2016-04-29 17:47:20 +03:00
|
|
|
store->init();
|
|
|
|
return store;
|
2016-02-29 17:11:11 +02:00
|
|
|
});
|
|
|
|
|
2016-02-24 15:48:16 +02:00
|
|
|
}
|