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