2016-03-21 17:55:57 +01:00
|
|
|
#include "archive.hh"
|
2017-03-13 12:07:50 +01:00
|
|
|
#include "compression.hh"
|
|
|
|
#include "derivations.hh"
|
2020-05-17 17:31:57 +02:00
|
|
|
#include "fs-accessor.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "store-api.hh"
|
2016-02-25 17:43:19 +01:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
LocalFSStore::LocalFSStore(const Params& params) : Store(params) {}
|
2016-06-02 13:33:49 +02:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
struct LocalStoreAccessor : public FSAccessor {
|
|
|
|
ref<LocalFSStore> store;
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
LocalStoreAccessor(ref<LocalFSStore> store) : store(store) {}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
Path toRealPath(const Path& path) {
|
|
|
|
Path storePath = store->toStorePath(path);
|
|
|
|
if (!store->isValidPath(storePath))
|
|
|
|
throw InvalidPath(format("path '%1%' is not a valid store path") %
|
|
|
|
storePath);
|
|
|
|
return store->getRealStoreDir() + std::string(path, store->storeDir.size());
|
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
FSAccessor::Stat stat(const Path& path) override {
|
|
|
|
auto realPath = toRealPath(path);
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(realPath.c_str(), &st)) {
|
|
|
|
if (errno == ENOENT || errno == ENOTDIR)
|
|
|
|
return {Type::tMissing, 0, false};
|
|
|
|
throw SysError(format("getting status of '%1%'") % path);
|
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode))
|
|
|
|
throw Error(format("file '%1%' has unsupported type") % path);
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
return {S_ISREG(st.st_mode)
|
|
|
|
? Type::tRegular
|
|
|
|
: S_ISLNK(st.st_mode) ? Type::tSymlink : Type::tDirectory,
|
|
|
|
S_ISREG(st.st_mode) ? (uint64_t)st.st_size : 0,
|
2016-02-25 17:43:19 +01:00
|
|
|
S_ISREG(st.st_mode) && st.st_mode & S_IXUSR};
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
StringSet readDirectory(const Path& path) override {
|
|
|
|
auto realPath = toRealPath(path);
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
auto entries = nix::readDirectory(realPath);
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
StringSet res;
|
2020-05-19 20:04:08 +02:00
|
|
|
for (auto& entry : entries) {
|
|
|
|
res.insert(entry.name);
|
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
return res;
|
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
std::string readFile(const Path& path) override {
|
|
|
|
return nix::readFile(toRealPath(path));
|
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
std::string readLink(const Path& path) override {
|
|
|
|
return nix::readLink(toRealPath(path));
|
|
|
|
}
|
2016-02-25 17:43:19 +01:00
|
|
|
};
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
ref<FSAccessor> LocalFSStore::getFSAccessor() {
|
|
|
|
return make_ref<LocalStoreAccessor>(ref<LocalFSStore>(
|
|
|
|
std::dynamic_pointer_cast<LocalFSStore>(shared_from_this())));
|
2016-02-25 17:43:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
void LocalFSStore::narFromPath(const Path& path, Sink& sink) {
|
2020-05-19 19:55:58 +02:00
|
|
|
if (!isValidPath(path)) {
|
|
|
|
throw Error(format("path '%s' is not valid") % path);
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
dumpPath(getRealStoreDir() + std::string(path, storeDir.size()), sink);
|
2016-03-21 17:55:57 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 12:07:50 +01:00
|
|
|
const string LocalFSStore::drvsLogDir = "drvs";
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
std::shared_ptr<std::string> LocalFSStore::getBuildLog(const Path& path_) {
|
|
|
|
auto path(path_);
|
2017-04-13 15:55:38 +02:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
assertStorePath(path);
|
2017-04-13 15:55:38 +02:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
if (!isDerivation(path)) {
|
|
|
|
try {
|
|
|
|
path = queryPathInfo(path)->deriver;
|
|
|
|
} catch (InvalidPath&) {
|
|
|
|
return nullptr;
|
2017-03-13 12:07:50 +01:00
|
|
|
}
|
2020-05-19 19:55:58 +02:00
|
|
|
if (path == "") {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2017-03-13 12:07:50 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
string baseName = baseNameOf(path);
|
2017-03-13 12:07:50 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
Path logPath = j == 0 ? fmt("%s/%s/%s/%s", logDir, drvsLogDir,
|
|
|
|
string(baseName, 0, 2), string(baseName, 2))
|
|
|
|
: fmt("%s/%s/%s", logDir, drvsLogDir, baseName);
|
|
|
|
Path logBz2Path = logPath + ".bz2";
|
2017-03-13 12:07:50 +01:00
|
|
|
|
2020-05-19 18:38:04 +02:00
|
|
|
if (pathExists(logPath)) {
|
2020-05-17 17:31:57 +02:00
|
|
|
return std::make_shared<std::string>(readFile(logPath));
|
2017-03-21 19:23:07 +01:00
|
|
|
|
2020-05-19 18:38:04 +02:00
|
|
|
} else if (pathExists(logBz2Path)) {
|
2020-05-17 17:31:57 +02:00
|
|
|
try {
|
|
|
|
return decompress("bzip2", readFile(logBz2Path));
|
|
|
|
} catch (Error&) {
|
|
|
|
}
|
2017-03-13 12:07:50 +01:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2017-03-13 12:07:50 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
return nullptr;
|
2017-03-13 12:07:50 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
} // namespace nix
|