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
|
|
|
}
|
|
|
|
|
2017-12-06 23:50:46 +00:00
|
|
|
Path RemoteFSAccessor::makeCacheFile(const Path & storePath, const std::string & ext)
|
2017-10-17 19:39:48 +00:00
|
|
|
{
|
|
|
|
assert(cacheDir != "");
|
2020-06-16 12:16:39 +00:00
|
|
|
return fmt("%s/%s.%s", cacheDir, store->parseStorePath(storePath).hashPart(), ext);
|
2017-10-17 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:50:46 +00:00
|
|
|
void RemoteFSAccessor::addToCache(const Path & storePath, const std::string & nar,
|
|
|
|
ref<FSAccessor> narAccessor)
|
2017-10-17 19:39:48 +00:00
|
|
|
{
|
2017-12-06 23:50:46 +00:00
|
|
|
nars.emplace(storePath, narAccessor);
|
|
|
|
|
|
|
|
if (cacheDir != "") {
|
|
|
|
try {
|
|
|
|
std::ostringstream str;
|
|
|
|
JSONPlaceholder jsonRoot(str);
|
|
|
|
listNar(jsonRoot, narAccessor, "", true);
|
|
|
|
writeFile(makeCacheFile(storePath, "ls"), str.str());
|
|
|
|
|
|
|
|
/* FIXME: do this asynchronously. */
|
|
|
|
writeFile(makeCacheFile(storePath, "nar"), nar);
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
2017-10-18 13:41:14 +00:00
|
|
|
}
|
2017-10-17 19:39:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 18:24:34 +00:00
|
|
|
std::pair<ref<FSAccessor>, Path> RemoteFSAccessor::fetch(const Path & path_)
|
|
|
|
{
|
|
|
|
auto path = canonPath(path_);
|
|
|
|
|
|
|
|
auto storePath = store->toStorePath(path);
|
|
|
|
std::string restPath = std::string(path, storePath.size());
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
if (!store->isValidPath(store->parseStorePath(storePath)))
|
2020-04-21 23:07:07 +00:00
|
|
|
throw InvalidPath("path '%1%' is not a valid store path", storePath);
|
2016-09-02 18:24:34 +00:00
|
|
|
|
|
|
|
auto i = nars.find(storePath);
|
|
|
|
if (i != nars.end()) return {i->second, restPath};
|
|
|
|
|
|
|
|
StringSink sink;
|
2017-12-06 23:50:46 +00:00
|
|
|
std::string listing;
|
|
|
|
Path cacheFile;
|
|
|
|
|
|
|
|
if (cacheDir != "" && pathExists(cacheFile = makeCacheFile(storePath, "nar"))) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
listing = nix::readFile(makeCacheFile(storePath, "ls"));
|
|
|
|
|
|
|
|
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);
|
|
|
|
readFull(fd.get(), (unsigned char *) buf.data(), length);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
|
|
|
|
nars.emplace(storePath, narAccessor);
|
|
|
|
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);
|
|
|
|
nars.emplace(storePath, narAccessor);
|
|
|
|
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
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
store->narFromPath(store->parseStorePath(storePath), sink);
|
2017-12-06 23:50:46 +00:00
|
|
|
auto narAccessor = makeNarAccessor(sink.s);
|
|
|
|
addToCache(storePath, *sink.s, narAccessor);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RemoteFSAccessor::readFile(const Path & path)
|
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
|
|
|
return res.first->readFile(res.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RemoteFSAccessor::readLink(const Path & path)
|
|
|
|
{
|
|
|
|
auto res = fetch(path);
|
|
|
|
return res.first->readLink(res.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|