2022-11-16 17:49:49 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2016-09-02 21:24:34 +03:00
|
|
|
#include "remote-fs-accessor.hh"
|
|
|
|
#include "nar-accessor.hh"
|
2017-12-07 01:50:46 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2016-09-02 21:24:34 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
RemoteFSAccessor::RemoteFSAccessor(ref<Store> store, bool requireValidPath, const Path & cacheDir)
|
2016-09-02 21:24:34 +03:00
|
|
|
: store(store)
|
2023-11-01 18:09:28 +02:00
|
|
|
, requireValidPath(requireValidPath)
|
2017-10-17 22:15:33 +03:00
|
|
|
, cacheDir(cacheDir)
|
2016-09-02 21:24:34 +03:00
|
|
|
{
|
2017-10-17 22:15:33 +03:00
|
|
|
if (cacheDir != "")
|
|
|
|
createDirs(cacheDir);
|
2016-09-02 21:24:34 +03:00
|
|
|
}
|
|
|
|
|
2020-07-13 17:19:37 +03:00
|
|
|
Path RemoteFSAccessor::makeCacheFile(std::string_view hashPart, const std::string & ext)
|
2017-10-17 22:39:48 +03:00
|
|
|
{
|
|
|
|
assert(cacheDir != "");
|
2020-07-13 17:19:37 +03:00
|
|
|
return fmt("%s/%s.%s", cacheDir, hashPart, ext);
|
2017-10-17 22:39:48 +03:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
ref<SourceAccessor> RemoteFSAccessor::addToCache(std::string_view hashPart, std::string && nar)
|
2017-10-17 22:39:48 +03:00
|
|
|
{
|
2022-01-17 23:20:05 +02:00
|
|
|
if (cacheDir != "") {
|
|
|
|
try {
|
|
|
|
/* FIXME: do this asynchronously. */
|
|
|
|
writeFile(makeCacheFile(hashPart, "nar"), nar);
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto narAccessor = makeNarAccessor(std::move(nar));
|
2020-07-13 17:19:37 +03:00
|
|
|
nars.emplace(hashPart, narAccessor);
|
2017-12-07 01:50:46 +02:00
|
|
|
|
|
|
|
if (cacheDir != "") {
|
|
|
|
try {
|
2023-11-01 18:09:28 +02:00
|
|
|
nlohmann::json j = listNar(narAccessor, CanonPath::root, true);
|
2022-11-16 17:49:49 +02:00
|
|
|
writeFile(makeCacheFile(hashPart, "ls"), j.dump());
|
2017-12-07 01:50:46 +02:00
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
2017-10-18 16:41:14 +03:00
|
|
|
}
|
2022-01-17 23:20:05 +02:00
|
|
|
|
|
|
|
return narAccessor;
|
2017-10-17 22:39:48 +03:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
std::pair<ref<SourceAccessor>, CanonPath> RemoteFSAccessor::fetch(const CanonPath & path)
|
2016-09-02 21:24:34 +03:00
|
|
|
{
|
2023-11-01 18:09:28 +02:00
|
|
|
auto [storePath, restPath_] = store->toStorePath(path.abs());
|
|
|
|
auto restPath = CanonPath(restPath_);
|
2016-09-02 21:24:34 +03:00
|
|
|
|
2020-12-15 11:54:24 +02:00
|
|
|
if (requireValidPath && !store->isValidPath(storePath))
|
2020-07-13 17:19:37 +03:00
|
|
|
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
|
2016-09-02 21:24:34 +03:00
|
|
|
|
2020-07-13 17:19:37 +03:00
|
|
|
auto i = nars.find(std::string(storePath.hashPart()));
|
2016-09-02 21:24:34 +03:00
|
|
|
if (i != nars.end()) return {i->second, restPath};
|
|
|
|
|
2017-12-07 01:50:46 +02:00
|
|
|
std::string listing;
|
|
|
|
Path cacheFile;
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
if (cacheDir != "" && nix::pathExists(cacheFile = makeCacheFile(storePath.hashPart(), "nar"))) {
|
2017-12-07 01:50:46 +02:00
|
|
|
|
|
|
|
try {
|
2020-07-13 17:19:37 +03:00
|
|
|
listing = nix::readFile(makeCacheFile(storePath.hashPart(), "ls"));
|
2017-12-07 01:50:46 +02:00
|
|
|
|
|
|
|
auto narAccessor = makeLazyNarAccessor(listing,
|
|
|
|
[cacheFile](uint64_t offset, uint64_t length) {
|
|
|
|
|
|
|
|
AutoCloseFD fd = open(cacheFile.c_str(), O_RDONLY | O_CLOEXEC);
|
|
|
|
if (!fd)
|
|
|
|
throw SysError("opening NAR cache file '%s'", cacheFile);
|
|
|
|
|
|
|
|
if (lseek(fd.get(), offset, SEEK_SET) != (off_t) offset)
|
|
|
|
throw SysError("seeking in '%s'", cacheFile);
|
|
|
|
|
|
|
|
std::string buf(length, 0);
|
2020-12-02 15:10:56 +02:00
|
|
|
readFull(fd.get(), buf.data(), length);
|
2017-12-07 01:50:46 +02:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
|
2020-07-13 17:19:37 +03:00
|
|
|
nars.emplace(storePath.hashPart(), narAccessor);
|
2017-12-07 01:50:46 +02:00
|
|
|
return {narAccessor, restPath};
|
|
|
|
|
2023-12-02 00:03:28 +02:00
|
|
|
} catch (SystemError &) { }
|
2017-12-07 01:50:46 +02:00
|
|
|
|
|
|
|
try {
|
2022-01-17 23:20:05 +02:00
|
|
|
auto narAccessor = makeNarAccessor(nix::readFile(cacheFile));
|
2020-07-13 17:19:37 +03:00
|
|
|
nars.emplace(storePath.hashPart(), narAccessor);
|
2017-12-07 01:50:46 +02:00
|
|
|
return {narAccessor, restPath};
|
2023-12-02 00:03:28 +02:00
|
|
|
} catch (SystemError &) { }
|
2017-10-17 22:15:33 +03:00
|
|
|
}
|
2016-09-02 21:24:34 +03:00
|
|
|
|
2022-01-17 23:20:05 +02:00
|
|
|
StringSink sink;
|
2020-07-13 17:19:37 +03:00
|
|
|
store->narFromPath(storePath, sink);
|
2022-01-17 23:20:05 +02:00
|
|
|
return {addToCache(storePath.hashPart(), std::move(sink.s)), restPath};
|
2016-09-02 21:24:34 +03:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
std::optional<SourceAccessor::Stat> RemoteFSAccessor::maybeLstat(const CanonPath & path)
|
2016-09-02 21:24:34 +03:00
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
2023-11-01 18:09:28 +02:00
|
|
|
return res.first->maybeLstat(res.second);
|
2016-09-02 21:24:34 +03:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
SourceAccessor::DirEntries RemoteFSAccessor::readDirectory(const CanonPath & path)
|
2016-09-02 21:24:34 +03:00
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
|
|
|
return res.first->readDirectory(res.second);
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
std::string RemoteFSAccessor::readFile(const CanonPath & path)
|
2016-09-02 21:24:34 +03:00
|
|
|
{
|
2023-11-01 18:09:28 +02:00
|
|
|
auto res = fetch(path);
|
2016-09-02 21:24:34 +03:00
|
|
|
return res.first->readFile(res.second);
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
std::string RemoteFSAccessor::readLink(const CanonPath & path)
|
2016-09-02 21:24:34 +03:00
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
|
|
|
return res.first->readLink(res.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|