2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/remote-fs-accessor.hh"
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2017-12-07 00:50:46 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/nar-accessor.hh"
|
|
|
|
#include "libutil/json.hh"
|
2016-09-02 20:24:34 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-05-20 23:58:43 +02:00
|
|
|
RemoteFSAccessor::RemoteFSAccessor(const ref<Store>& store,
|
|
|
|
const Path& cacheDir)
|
2017-10-17 21:15:33 +02:00
|
|
|
: store(store), cacheDir(cacheDir) {
|
2020-05-20 23:27:37 +02:00
|
|
|
if (!cacheDir.empty()) {
|
2017-10-17 21:15:33 +02:00
|
|
|
createDirs(cacheDir);
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
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) {
|
2020-05-20 23:27:37 +02:00
|
|
|
assert(!cacheDir.empty());
|
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,
|
2020-05-20 23:58:43 +02:00
|
|
|
const ref<FSAccessor>& narAccessor) {
|
2017-12-07 00:50:46 +01:00
|
|
|
nars.emplace(storePath, narAccessor);
|
|
|
|
|
2020-05-20 23:27:37 +02:00
|
|
|
if (!cacheDir.empty()) {
|
2017-12-07 00:50:46 +01:00
|
|
|
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
|
|
|
}
|
2020-05-17 17:31:57 +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_);
|
|
|
|
|
2017-07-30 13:27:57 +02:00
|
|
|
auto storePath = store->toStorePath(path);
|
|
|
|
std::string restPath = std::string(path, storePath.size());
|
2016-09-02 20:24:34 +02:00
|
|
|
|
|
|
|
if (!store->isValidPath(storePath)) {
|
|
|
|
throw InvalidPath(format("path '%1%' is not a valid store path") %
|
|
|
|
storePath);
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2016-09-02 20:24:34 +02:00
|
|
|
|
2017-12-07 00:50:46 +01:00
|
|
|
auto i = nars.find(storePath);
|
2016-09-02 20:24:34 +02:00
|
|
|
if (i != nars.end()) {
|
|
|
|
return {i->second, restPath};
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2017-12-07 00:50:46 +01:00
|
|
|
|
2016-09-02 20:24:34 +02:00
|
|
|
StringSink sink;
|
2017-12-07 00:50:46 +01:00
|
|
|
std::string listing;
|
|
|
|
Path cacheFile;
|
|
|
|
|
2020-05-20 23:27:37 +02:00
|
|
|
if (!cacheDir.empty() &&
|
2017-12-07 00:50:46 +01:00
|
|
|
pathExists(cacheFile = makeCacheFile(storePath, "nar"))) {
|
|
|
|
try {
|
|
|
|
listing = nix::readFile(makeCacheFile(storePath, "ls"));
|
|
|
|
|
|
|
|
auto narAccessor = makeLazyNarAccessor(
|
|
|
|
listing, [cacheFile](uint64_t offset, uint64_t length) {
|
2020-08-21 05:00:55 +02:00
|
|
|
AutoCloseFD fd(open(cacheFile.c_str(), O_RDONLY | O_CLOEXEC));
|
2017-12-07 00:50:46 +01:00
|
|
|
if (!fd) {
|
|
|
|
throw SysError("opening NAR cache file '%s'", cacheFile);
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2017-12-07 00:50:46 +01:00
|
|
|
|
2020-08-02 02:17:44 +02:00
|
|
|
if (lseek(fd.get(), offset, SEEK_SET) !=
|
|
|
|
static_cast<off_t>(offset)) {
|
2017-12-07 00:50:46 +01:00
|
|
|
throw SysError("seeking in '%s'", cacheFile);
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2017-12-07 00:50:46 +01:00
|
|
|
|
|
|
|
std::string buf(length, 0);
|
2020-08-02 02:17:44 +02:00
|
|
|
readFull(fd.get(), reinterpret_cast<unsigned char*>(buf.data()),
|
|
|
|
length);
|
2017-12-07 00:50:46 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-05-17 17:31:57 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nix
|