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