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