2016-09-02 18:24:34 +00:00
|
|
|
#include "remote-fs-accessor.hh"
|
|
|
|
#include "nar-accessor.hh"
|
2017-12-06 23:50:46 +00:00
|
|
|
#include "json.hh"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2016-09-02 18:24:34 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2017-10-17 19:15:33 +00:00
|
|
|
RemoteFSAccessor::RemoteFSAccessor(ref<Store> store, const Path & cacheDir)
|
2016-09-02 18:24:34 +00:00
|
|
|
: store(store)
|
2017-10-17 19:15:33 +00:00
|
|
|
, cacheDir(cacheDir)
|
2016-09-02 18:24:34 +00:00
|
|
|
{
|
2017-10-17 19:15:33 +00:00
|
|
|
if (cacheDir != "")
|
|
|
|
createDirs(cacheDir);
|
2016-09-02 18:24:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 14:19:37 +00:00
|
|
|
Path RemoteFSAccessor::makeCacheFile(std::string_view hashPart, const std::string & ext)
|
2017-10-17 19:39:48 +00:00
|
|
|
{
|
|
|
|
assert(cacheDir != "");
|
2020-07-13 14:19:37 +00:00
|
|
|
return fmt("%s/%s.%s", cacheDir, hashPart, ext);
|
2017-10-17 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 14:19:37 +00:00
|
|
|
void RemoteFSAccessor::addToCache(std::string_view hashPart, const std::string & nar,
|
2017-12-06 23:50:46 +00:00
|
|
|
ref<FSAccessor> narAccessor)
|
2017-10-17 19:39:48 +00:00
|
|
|
{
|
2020-07-13 14:19:37 +00:00
|
|
|
nars.emplace(hashPart, narAccessor);
|
2017-12-06 23:50:46 +00:00
|
|
|
|
|
|
|
if (cacheDir != "") {
|
|
|
|
try {
|
|
|
|
std::ostringstream str;
|
|
|
|
JSONPlaceholder jsonRoot(str);
|
|
|
|
listNar(jsonRoot, narAccessor, "", true);
|
2020-07-13 14:19:37 +00:00
|
|
|
writeFile(makeCacheFile(hashPart, "ls"), str.str());
|
2017-12-06 23:50:46 +00:00
|
|
|
|
|
|
|
/* FIXME: do this asynchronously. */
|
2020-07-13 14:19:37 +00:00
|
|
|
writeFile(makeCacheFile(hashPart, "nar"), nar);
|
2017-12-06 23:50:46 +00:00
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
2017-10-18 13:41:14 +00:00
|
|
|
}
|
2017-10-17 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 09:54:24 +00:00
|
|
|
std::pair<ref<FSAccessor>, Path> RemoteFSAccessor::fetch(const Path & path_, bool requireValidPath)
|
2016-09-02 18:24:34 +00:00
|
|
|
{
|
|
|
|
auto path = canonPath(path_);
|
|
|
|
|
2020-07-13 14:19:37 +00:00
|
|
|
auto [storePath, restPath] = store->toStorePath(path);
|
2016-09-02 18:24:34 +00:00
|
|
|
|
2020-12-15 09:54:24 +00:00
|
|
|
if (requireValidPath && !store->isValidPath(storePath))
|
2020-07-13 14:19:37 +00:00
|
|
|
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
|
2016-09-02 18:24:34 +00:00
|
|
|
|
2020-07-13 14:19:37 +00:00
|
|
|
auto i = nars.find(std::string(storePath.hashPart()));
|
2016-09-02 18:24:34 +00:00
|
|
|
if (i != nars.end()) return {i->second, restPath};
|
|
|
|
|
|
|
|
StringSink sink;
|
2017-12-06 23:50:46 +00:00
|
|
|
std::string listing;
|
|
|
|
Path cacheFile;
|
|
|
|
|
2020-07-13 14:19:37 +00:00
|
|
|
if (cacheDir != "" && pathExists(cacheFile = makeCacheFile(storePath.hashPart(), "nar"))) {
|
2017-12-06 23:50:46 +00:00
|
|
|
|
|
|
|
try {
|
2020-07-13 14:19:37 +00:00
|
|
|
listing = nix::readFile(makeCacheFile(storePath.hashPart(), "ls"));
|
2017-12-06 23:50:46 +00: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 13:10:56 +00:00
|
|
|
readFull(fd.get(), buf.data(), length);
|
2017-12-06 23:50:46 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
|
2020-07-13 14:19:37 +00:00
|
|
|
nars.emplace(storePath.hashPart(), narAccessor);
|
2017-12-06 23:50:46 +00:00
|
|
|
return {narAccessor, restPath};
|
|
|
|
|
|
|
|
} catch (SysError &) { }
|
|
|
|
|
|
|
|
try {
|
|
|
|
*sink.s = nix::readFile(cacheFile);
|
2017-10-17 19:15:33 +00:00
|
|
|
|
2017-12-06 23:50:46 +00:00
|
|
|
auto narAccessor = makeNarAccessor(sink.s);
|
2020-07-13 14:19:37 +00:00
|
|
|
nars.emplace(storePath.hashPart(), narAccessor);
|
2017-12-06 23:50:46 +00:00
|
|
|
return {narAccessor, restPath};
|
2017-10-17 19:15:33 +00:00
|
|
|
|
2017-12-06 23:50:46 +00:00
|
|
|
} catch (SysError &) { }
|
2017-10-17 19:15:33 +00:00
|
|
|
}
|
2016-09-02 18:24:34 +00:00
|
|
|
|
2020-07-13 14:19:37 +00:00
|
|
|
store->narFromPath(storePath, sink);
|
2017-12-06 23:50:46 +00:00
|
|
|
auto narAccessor = makeNarAccessor(sink.s);
|
2020-07-13 14:19:37 +00:00
|
|
|
addToCache(storePath.hashPart(), *sink.s, narAccessor);
|
2017-12-06 23:50:46 +00:00
|
|
|
return {narAccessor, restPath};
|
2016-09-02 18:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FSAccessor::Stat RemoteFSAccessor::stat(const Path & path)
|
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
|
|
|
return res.first->stat(res.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringSet RemoteFSAccessor::readDirectory(const Path & path)
|
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
|
|
|
return res.first->readDirectory(res.second);
|
|
|
|
}
|
|
|
|
|
2020-12-15 09:54:24 +00:00
|
|
|
std::string RemoteFSAccessor::readFile(const Path & path, bool requireValidPath)
|
2016-09-02 18:24:34 +00:00
|
|
|
{
|
2020-12-15 09:54:24 +00:00
|
|
|
auto res = fetch(path, requireValidPath);
|
2016-09-02 18:24:34 +00:00
|
|
|
return res.first->readFile(res.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RemoteFSAccessor::readLink(const Path & path)
|
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
|
|
|
return res.first->readLink(res.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|