2016-09-02 20:24:34 +02:00
|
|
|
#include "remote-fs-accessor.hh"
|
|
|
|
#include "nar-accessor.hh"
|
2017-12-07 00:50:46 +01:00
|
|
|
#include "json.hh"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2016-09-02 20:24:34 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2017-10-17 21:15:33 +02:00
|
|
|
RemoteFSAccessor::RemoteFSAccessor(ref<Store> store, const Path & cacheDir)
|
2016-09-02 20:24:34 +02:00
|
|
|
: store(store)
|
2017-10-17 21:15:33 +02:00
|
|
|
, cacheDir(cacheDir)
|
2016-09-02 20:24:34 +02:00
|
|
|
{
|
2017-10-17 21:15:33 +02:00
|
|
|
if (cacheDir != "")
|
|
|
|
createDirs(cacheDir);
|
2016-09-02 20:24:34 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:50:46 +01:00
|
|
|
Path RemoteFSAccessor::makeCacheFile(const Path & storePath, const std::string & ext)
|
2017-10-17 21:39:48 +02:00
|
|
|
{
|
|
|
|
assert(cacheDir != "");
|
2017-12-07 00:50:46 +01:00
|
|
|
return fmt("%s/%s.%s", cacheDir, storePathToHash(storePath), ext);
|
2017-10-17 21:39:48 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:50:46 +01:00
|
|
|
void RemoteFSAccessor::addToCache(const Path & storePath, const std::string & nar,
|
|
|
|
ref<FSAccessor> narAccessor)
|
2017-10-17 21:39:48 +02:00
|
|
|
{
|
2017-12-07 00:50:46 +01: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 15:41:14 +02:00
|
|
|
}
|
2017-10-17 21:39:48 +02:00
|
|
|
}
|
|
|
|
|
2016-09-02 20:24:34 +02: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());
|
|
|
|
|
|
|
|
if (!store->isValidPath(storePath))
|
2017-07-30 13:27:57 +02:00
|
|
|
throw InvalidPath(format("path '%1%' is not a valid store path") % storePath);
|
2016-09-02 20:24:34 +02:00
|
|
|
|
|
|
|
auto i = nars.find(storePath);
|
|
|
|
if (i != nars.end()) return {i->second, restPath};
|
|
|
|
|
|
|
|
StringSink sink;
|
2017-12-07 00:50:46 +01: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 21:15:33 +02:00
|
|
|
|
2017-12-07 00:50:46 +01:00
|
|
|
auto narAccessor = makeNarAccessor(sink.s);
|
|
|
|
nars.emplace(storePath, narAccessor);
|
|
|
|
return {narAccessor, restPath};
|
2017-10-17 21:15:33 +02:00
|
|
|
|
2017-12-07 00:50:46 +01:00
|
|
|
} catch (SysError &) { }
|
2017-10-17 21:15:33 +02:00
|
|
|
}
|
2016-09-02 20:24:34 +02:00
|
|
|
|
2017-12-07 00:50:46 +01:00
|
|
|
store->narFromPath(storePath, sink);
|
|
|
|
auto narAccessor = makeNarAccessor(sink.s);
|
|
|
|
addToCache(storePath, *sink.s, narAccessor);
|
|
|
|
return {narAccessor, restPath};
|
2016-09-02 20:24:34 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|