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
|
|
|
|
2021-03-26 18:10:15 +02:00
|
|
|
#include <atomic>
|
|
|
|
|
2016-02-24 15:48:16 +02:00
|
|
|
namespace nix {
|
|
|
|
|
2020-09-10 11:55:51 +03:00
|
|
|
struct LocalBinaryCacheStoreConfig : virtual BinaryCacheStoreConfig
|
|
|
|
{
|
|
|
|
using BinaryCacheStoreConfig::BinaryCacheStoreConfig;
|
2020-09-14 15:04:02 +03:00
|
|
|
|
|
|
|
const std::string name() override { return "Local Binary Cache Store"; }
|
2023-03-21 15:03:05 +02:00
|
|
|
|
|
|
|
std::string doc() override
|
|
|
|
{
|
|
|
|
return
|
|
|
|
#include "local-binary-cache-store.md"
|
|
|
|
;
|
|
|
|
}
|
2020-09-10 11:55:51 +03:00
|
|
|
};
|
|
|
|
|
2020-12-20 17:33:12 +02:00
|
|
|
class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public virtual BinaryCacheStore
|
2016-02-29 17:14:39 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
Path binaryCacheDir;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-05-04 21:15:41 +03:00
|
|
|
LocalBinaryCacheStore(
|
2020-09-14 15:12:47 +03:00
|
|
|
const std::string scheme,
|
2020-09-08 15:50:23 +03:00
|
|
|
const Path & binaryCacheDir,
|
|
|
|
const Params & params)
|
2020-09-11 12:06:18 +03:00
|
|
|
: StoreConfig(params)
|
2020-12-20 17:33:12 +02:00
|
|
|
, BinaryCacheStoreConfig(params)
|
|
|
|
, LocalBinaryCacheStoreConfig(params)
|
|
|
|
, Store(params)
|
2020-09-10 11:55:51 +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;
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:11:05 +03:00
|
|
|
static std::set<std::string> uriSchemes();
|
2020-09-08 15:50:23 +03:00
|
|
|
|
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,
|
2020-07-13 21:07:19 +03:00
|
|
|
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
|
|
const std::string & mimeType) override
|
2020-07-10 21:58:02 +03:00
|
|
|
{
|
|
|
|
auto path2 = binaryCacheDir + "/" + path;
|
2021-03-26 18:10:15 +02:00
|
|
|
static std::atomic<int> counter{0};
|
|
|
|
Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
|
2020-07-10 21:58:02 +03:00
|
|
|
AutoDelete del(tmp, false);
|
2020-07-13 21:07:19 +03:00
|
|
|
StreamToSourceAdapter source(istream);
|
2020-07-10 21:58:02 +03:00
|
|
|
writeFile(tmp, source);
|
2024-05-12 16:10:16 +03:00
|
|
|
std::filesystem::rename(tmp, path2);
|
2020-07-10 21:58:02 +03:00
|
|
|
del.cancel();
|
|
|
|
}
|
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);
|
2022-06-10 20:49:38 +03:00
|
|
|
throw;
|
2018-03-28 00:12:31 +03:00
|
|
|
}
|
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
|
|
|
|
2024-05-12 15:12:18 +03:00
|
|
|
for (auto & entry : std::filesystem::directory_iterator{binaryCacheDir}) {
|
2024-05-07 18:29:33 +03:00
|
|
|
auto name = entry.path().filename().string();
|
|
|
|
if (name.size() != 40 ||
|
|
|
|
!hasSuffix(name, ".narinfo"))
|
2016-04-29 18:34:31 +03:00
|
|
|
continue;
|
2020-07-13 15:35:01 +03:00
|
|
|
paths.insert(parseStorePath(
|
2024-05-07 18:29:33 +03:00
|
|
|
storeDir + "/" + name.substr(0, name.size() - 8)
|
2020-07-13 15:35:01 +03:00
|
|
|
+ "-" + MissingName));
|
2016-04-29 18:34:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2022-12-26 22:21:08 +02:00
|
|
|
std::optional<TrustedFlag> isTrustedClient() override
|
|
|
|
{
|
|
|
|
return Trusted;
|
|
|
|
}
|
2016-02-29 17:14:39 +02:00
|
|
|
};
|
|
|
|
|
2016-02-24 15:48:16 +02:00
|
|
|
void LocalBinaryCacheStore::init()
|
|
|
|
{
|
|
|
|
createDirs(binaryCacheDir + "/nar");
|
2021-05-04 11:35:34 +03:00
|
|
|
createDirs(binaryCacheDir + "/" + realisationsPrefix);
|
2019-09-04 20:24:35 +03:00
|
|
|
if (writeDebugInfo)
|
|
|
|
createDirs(binaryCacheDir + "/debuginfo");
|
2022-01-18 17:14:01 +02:00
|
|
|
createDirs(binaryCacheDir + "/log");
|
2016-02-24 15:48:16 +02:00
|
|
|
BinaryCacheStore::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalBinaryCacheStore::fileExists(const std::string & path)
|
|
|
|
{
|
|
|
|
return pathExists(binaryCacheDir + "/" + path);
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:11:05 +03:00
|
|
|
std::set<std::string> LocalBinaryCacheStore::uriSchemes()
|
2016-04-29 17:26:16 +03:00
|
|
|
{
|
2021-12-22 12:31:14 +02:00
|
|
|
if (getEnv("_NIX_FORCE_HTTP") == "1")
|
2020-09-08 15:50:23 +03:00
|
|
|
return {};
|
|
|
|
else
|
|
|
|
return {"file"};
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static RegisterStoreImplementation<LocalBinaryCacheStore, LocalBinaryCacheStoreConfig> regLocalBinaryCacheStore;
|
2016-02-29 17:11:11 +02:00
|
|
|
|
2016-02-24 15:48:16 +02:00
|
|
|
}
|