mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-11 00:36:20 +02:00
bbe97dff8b
Most functions now take a StorePath argument rather than a Path (which is just an alias for std::string). The StorePath constructor ensures that the path is syntactically correct (i.e. it looks like <store-dir>/<base32-hash>-<name>). Similarly, functions like buildPaths() now take a StorePathWithOutputs, rather than abusing Path by adding a '!<outputs>' suffix. Note that the StorePath type is implemented in Rust. This involves some hackery to allow Rust values to be used directly in C++, via a helper type whose destructor calls the Rust type's drop() function. The main issue is the dynamic nature of C++ move semantics: after we have moved a Rust value, we should not call the drop function on the original value. So when we move a value, we set the original value to bitwise zero, and the destructor only calls drop() if the value is not bitwise zero. This should be sufficient for most types. Also lots of minor cleanups to the C++ API to make it more modern (e.g. using std::optional and std::string_view in some places).
129 lines
3.5 KiB
C++
129 lines
3.5 KiB
C++
#include "archive.hh"
|
|
#include "fs-accessor.hh"
|
|
#include "store-api.hh"
|
|
#include "globals.hh"
|
|
#include "compression.hh"
|
|
#include "derivations.hh"
|
|
|
|
namespace nix {
|
|
|
|
LocalFSStore::LocalFSStore(const Params & params)
|
|
: Store(params)
|
|
{
|
|
}
|
|
|
|
struct LocalStoreAccessor : public FSAccessor
|
|
{
|
|
ref<LocalFSStore> store;
|
|
|
|
LocalStoreAccessor(ref<LocalFSStore> store) : store(store) { }
|
|
|
|
Path toRealPath(const Path & path)
|
|
{
|
|
Path storePath = store->toStorePath(path);
|
|
if (!store->isValidPath(store->parseStorePath(storePath)))
|
|
throw InvalidPath(format("path '%1%' is not a valid store path") % storePath);
|
|
return store->getRealStoreDir() + std::string(path, store->storeDir.size());
|
|
}
|
|
|
|
FSAccessor::Stat stat(const Path & path) override
|
|
{
|
|
auto realPath = toRealPath(path);
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
|
|
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
|
|
{
|
|
auto realPath = toRealPath(path);
|
|
|
|
auto entries = nix::readDirectory(realPath);
|
|
|
|
StringSet res;
|
|
for (auto & entry : entries)
|
|
res.insert(entry.name);
|
|
|
|
return res;
|
|
}
|
|
|
|
std::string readFile(const Path & path) override
|
|
{
|
|
return nix::readFile(toRealPath(path));
|
|
}
|
|
|
|
std::string readLink(const Path & path) override
|
|
{
|
|
return nix::readLink(toRealPath(path));
|
|
}
|
|
};
|
|
|
|
ref<FSAccessor> LocalFSStore::getFSAccessor()
|
|
{
|
|
return make_ref<LocalStoreAccessor>(ref<LocalFSStore>(
|
|
std::dynamic_pointer_cast<LocalFSStore>(shared_from_this())));
|
|
}
|
|
|
|
void LocalFSStore::narFromPath(const StorePath & path, Sink & sink)
|
|
{
|
|
if (!isValidPath(path))
|
|
throw Error("path '%s' is not valid", printStorePath(path));
|
|
dumpPath(getRealStoreDir() + std::string(printStorePath(path), storeDir.size()), sink);
|
|
}
|
|
|
|
const string LocalFSStore::drvsLogDir = "drvs";
|
|
|
|
|
|
|
|
std::shared_ptr<std::string> LocalFSStore::getBuildLog(const StorePath & path_)
|
|
{
|
|
auto path = path_.clone();
|
|
|
|
if (!path.isDerivation()) {
|
|
try {
|
|
auto info = queryPathInfo(path);
|
|
if (!info->deriver) return nullptr;
|
|
path = info->deriver->clone();
|
|
} catch (InvalidPath &) {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
auto baseName = std::string(baseNameOf(printStorePath(path)));
|
|
|
|
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";
|
|
|
|
if (pathExists(logPath))
|
|
return std::make_shared<std::string>(readFile(logPath));
|
|
|
|
else if (pathExists(logBz2Path)) {
|
|
try {
|
|
return decompress("bzip2", readFile(logBz2Path));
|
|
} catch (Error &) { }
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
}
|