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-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-04-29 17:47:20 +03:00
|
|
|
|
const StoreParams & 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;
|
|
|
|
|
|
|
|
|
|
void upsertFile(const std::string & path, const std::string & data) override;
|
|
|
|
|
|
2016-04-15 16:11:34 +03:00
|
|
|
|
std::shared_ptr<std::string> getFile(const std::string & path) override;
|
2016-02-29 17:14:39 +02:00
|
|
|
|
|
2016-04-29 18:34:31 +03:00
|
|
|
|
PathSet queryAllValidPaths() override
|
|
|
|
|
{
|
|
|
|
|
PathSet paths;
|
|
|
|
|
|
|
|
|
|
for (auto & entry : readDirectory(binaryCacheDir)) {
|
|
|
|
|
if (entry.name.size() != 40 ||
|
|
|
|
|
!hasSuffix(entry.name, ".narinfo"))
|
|
|
|
|
continue;
|
|
|
|
|
paths.insert(settings.nixStore + "/" + entry.name.substr(0, entry.name.size() - 8));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 17:14:39 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-02-24 15:48:16 +02:00
|
|
|
|
void LocalBinaryCacheStore::init()
|
|
|
|
|
{
|
|
|
|
|
createDirs(binaryCacheDir + "/nar");
|
|
|
|
|
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()))
|
|
|
|
|
throw SysError(format("renaming ‘%1%’ to ‘%2%’") % tmp % path);
|
|
|
|
|
del.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LocalBinaryCacheStore::fileExists(const std::string & path)
|
|
|
|
|
{
|
|
|
|
|
return pathExists(binaryCacheDir + "/" + path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LocalBinaryCacheStore::upsertFile(const std::string & path, const std::string & data)
|
|
|
|
|
{
|
|
|
|
|
atomicWrite(binaryCacheDir + "/" + path, data);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 16:11:34 +03:00
|
|
|
|
std::shared_ptr<std::string> LocalBinaryCacheStore::getFile(const std::string & path)
|
2016-02-24 15:48:16 +02:00
|
|
|
|
{
|
2016-04-15 16:11:34 +03:00
|
|
|
|
try {
|
|
|
|
|
return std::make_shared<std::string>(readFile(binaryCacheDir + "/" + path));
|
|
|
|
|
} catch (SysError & e) {
|
|
|
|
|
if (e.errNo == ENOENT) return 0;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2016-02-24 15:48:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 17:26:16 +03:00
|
|
|
|
static RegisterStoreImplementation regStore([](
|
|
|
|
|
const std::string & uri, const StoreParams & params)
|
|
|
|
|
-> std::shared_ptr<Store>
|
|
|
|
|
{
|
2016-02-29 17:11:11 +02:00
|
|
|
|
if (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
|
|
|
|
}
|