2024-07-18 06:32:27 +03:00
|
|
|
#include "local-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"
|
2024-06-04 17:05:40 +03:00
|
|
|
#include "signals.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 {
|
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
LocalBinaryCacheStoreConfig::LocalBinaryCacheStoreConfig(
|
|
|
|
std::string_view scheme,
|
|
|
|
PathView binaryCacheDir,
|
|
|
|
const Params & params)
|
|
|
|
: StoreConfig(params)
|
|
|
|
, BinaryCacheStoreConfig(params)
|
|
|
|
, binaryCacheDir(binaryCacheDir)
|
2020-09-10 11:55:51 +03:00
|
|
|
{
|
2024-07-18 06:32:27 +03:00
|
|
|
}
|
2020-09-14 15:04:02 +03:00
|
|
|
|
2023-03-21 15:03:05 +02:00
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
std::string LocalBinaryCacheStoreConfig::doc()
|
2016-02-29 17:14:39 +02:00
|
|
|
{
|
2024-07-18 06:32:27 +03:00
|
|
|
return
|
|
|
|
#include "local-binary-cache-store.md"
|
|
|
|
;
|
|
|
|
}
|
2016-02-29 17:14:39 +02:00
|
|
|
|
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
struct LocalBinaryCacheStore : virtual LocalBinaryCacheStoreConfig, virtual BinaryCacheStore
|
|
|
|
{
|
2024-01-25 17:31:52 +02:00
|
|
|
/**
|
|
|
|
* @param binaryCacheDir `file://` is a short-hand for `file:///`
|
|
|
|
* for now.
|
|
|
|
*/
|
2016-05-04 21:15:41 +03:00
|
|
|
LocalBinaryCacheStore(
|
2024-01-25 17:31:52 +02:00
|
|
|
std::string_view scheme,
|
|
|
|
PathView binaryCacheDir,
|
2020-09-08 15:50:23 +03:00
|
|
|
const Params & params)
|
2020-09-11 12:06:18 +03:00
|
|
|
: StoreConfig(params)
|
2020-12-20 17:33:12 +02:00
|
|
|
, BinaryCacheStoreConfig(params)
|
2024-07-18 06:32:27 +03:00
|
|
|
, LocalBinaryCacheStoreConfig(scheme, binaryCacheDir, params)
|
2020-12-20 17:33:12 +02:00
|
|
|
, Store(params)
|
2020-09-10 11:55:51 +03:00
|
|
|
, BinaryCacheStore(params)
|
2016-04-29 17:47:20 +03:00
|
|
|
{
|
|
|
|
}
|
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,
|
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-06-04 17:05:40 +03:00
|
|
|
checkInterrupt();
|
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);
|
|
|
|
}
|
|
|
|
|
2024-07-16 06:26:39 +03:00
|
|
|
std::set<std::string> LocalBinaryCacheStoreConfig::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
|
|
|
}
|