2016-02-29 15:14:39 +00:00
|
|
|
#include "binary-cache-store.hh"
|
2016-03-04 16:23:42 +00:00
|
|
|
#include "globals.hh"
|
2016-05-30 12:53:57 +00:00
|
|
|
#include "nar-info-disk-cache.hh"
|
2016-02-24 13:48:16 +00:00
|
|
|
|
2021-03-26 16:10:15 +00:00
|
|
|
#include <atomic>
|
|
|
|
|
2016-02-24 13:48:16 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2020-09-10 08:55:51 +00:00
|
|
|
struct LocalBinaryCacheStoreConfig : virtual BinaryCacheStoreConfig
|
|
|
|
{
|
|
|
|
using BinaryCacheStoreConfig::BinaryCacheStoreConfig;
|
2020-09-14 12:04:02 +00:00
|
|
|
|
|
|
|
const std::string name() override { return "Local Binary Cache Store"; }
|
2020-09-10 08:55:51 +00:00
|
|
|
};
|
|
|
|
|
2020-12-20 15:33:12 +00:00
|
|
|
class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public virtual BinaryCacheStore
|
2016-02-29 15:14:39 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
Path binaryCacheDir;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-05-04 18:15:41 +00:00
|
|
|
LocalBinaryCacheStore(
|
2020-09-14 12:12:47 +00:00
|
|
|
const std::string scheme,
|
2020-09-08 12:50:23 +00:00
|
|
|
const Path & binaryCacheDir,
|
|
|
|
const Params & params)
|
2020-09-11 09:06:18 +00:00
|
|
|
: StoreConfig(params)
|
2020-12-20 15:33:12 +00:00
|
|
|
, BinaryCacheStoreConfig(params)
|
|
|
|
, LocalBinaryCacheStoreConfig(params)
|
|
|
|
, Store(params)
|
2020-09-10 08:55:51 +00:00
|
|
|
, BinaryCacheStore(params)
|
2016-04-29 14:47:20 +00:00
|
|
|
, binaryCacheDir(binaryCacheDir)
|
|
|
|
{
|
|
|
|
}
|
2016-02-29 15:14:39 +00:00
|
|
|
|
|
|
|
void init() override;
|
|
|
|
|
2016-04-29 14:26:16 +00:00
|
|
|
std::string getUri() override
|
|
|
|
{
|
|
|
|
return "file://" + binaryCacheDir;
|
|
|
|
}
|
|
|
|
|
2020-09-11 09:11:05 +00:00
|
|
|
static std::set<std::string> uriSchemes();
|
2020-09-08 12:50:23 +00:00
|
|
|
|
2016-02-29 15:14:39 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
bool fileExists(const std::string & path) override;
|
|
|
|
|
2017-03-14 14:26:01 +00:00
|
|
|
void upsertFile(const std::string & path,
|
2020-07-13 18:07:19 +00:00
|
|
|
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
|
|
const std::string & mimeType) override
|
2020-07-10 18:58:02 +00:00
|
|
|
{
|
|
|
|
auto path2 = binaryCacheDir + "/" + path;
|
2021-03-26 16:10:15 +00:00
|
|
|
static std::atomic<int> counter{0};
|
|
|
|
Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
|
2020-07-10 18:58:02 +00:00
|
|
|
AutoDelete del(tmp, false);
|
2020-07-13 18:07:19 +00:00
|
|
|
StreamToSourceAdapter source(istream);
|
2020-07-10 18:58:02 +00:00
|
|
|
writeFile(tmp, source);
|
|
|
|
if (rename(tmp.c_str(), path2.c_str()))
|
|
|
|
throw SysError("renaming '%1%' to '%2%'", tmp, path2);
|
|
|
|
del.cancel();
|
|
|
|
}
|
2016-02-29 15:14:39 +00:00
|
|
|
|
2018-03-27 21:12:31 +00:00
|
|
|
void getFile(const std::string & path, Sink & sink) override
|
2016-09-16 16:54:14 +00:00
|
|
|
{
|
2018-03-27 20:16:01 +00:00
|
|
|
try {
|
2018-03-27 21:12:31 +00:00
|
|
|
readFile(binaryCacheDir + "/" + path, sink);
|
2018-03-27 20:16:01 +00:00
|
|
|
} catch (SysError & e) {
|
2018-03-27 21:12:31 +00:00
|
|
|
if (e.errNo == ENOENT)
|
|
|
|
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path);
|
|
|
|
}
|
2016-09-16 16:54:14 +00:00
|
|
|
}
|
2016-02-29 15:14:39 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet queryAllValidPaths() override
|
2016-04-29 15:34:31 +00:00
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet paths;
|
2016-04-29 15:34:31 +00:00
|
|
|
|
|
|
|
for (auto & entry : readDirectory(binaryCacheDir)) {
|
|
|
|
if (entry.name.size() != 40 ||
|
|
|
|
!hasSuffix(entry.name, ".narinfo"))
|
|
|
|
continue;
|
2020-07-13 12:35:01 +00:00
|
|
|
paths.insert(parseStorePath(
|
|
|
|
storeDir + "/" + entry.name.substr(0, entry.name.size() - 8)
|
|
|
|
+ "-" + MissingName));
|
2016-04-29 15:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2016-02-29 15:14:39 +00:00
|
|
|
};
|
|
|
|
|
2016-02-24 13:48:16 +00:00
|
|
|
void LocalBinaryCacheStore::init()
|
|
|
|
{
|
|
|
|
createDirs(binaryCacheDir + "/nar");
|
2021-05-04 08:35:34 +00:00
|
|
|
createDirs(binaryCacheDir + "/" + realisationsPrefix);
|
2019-09-04 17:24:35 +00:00
|
|
|
if (writeDebugInfo)
|
|
|
|
createDirs(binaryCacheDir + "/debuginfo");
|
2022-01-18 15:14:01 +00:00
|
|
|
createDirs(binaryCacheDir + "/log");
|
2016-02-24 13:48:16 +00:00
|
|
|
BinaryCacheStore::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalBinaryCacheStore::fileExists(const std::string & path)
|
|
|
|
{
|
|
|
|
return pathExists(binaryCacheDir + "/" + path);
|
|
|
|
}
|
|
|
|
|
2020-09-11 09:11:05 +00:00
|
|
|
std::set<std::string> LocalBinaryCacheStore::uriSchemes()
|
2016-04-29 14:26:16 +00:00
|
|
|
{
|
2021-12-22 10:31:14 +00:00
|
|
|
if (getEnv("_NIX_FORCE_HTTP") == "1")
|
2020-09-08 12:50:23 +00:00
|
|
|
return {};
|
|
|
|
else
|
|
|
|
return {"file"};
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterStoreImplementation<LocalBinaryCacheStore, LocalBinaryCacheStoreConfig> regLocalBinaryCacheStore;
|
2016-02-29 15:11:11 +00:00
|
|
|
|
2016-02-24 13:48:16 +00:00
|
|
|
}
|