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