Unify FSAccessor::Type and SourceAccessor::Type

This commit is contained in:
Eelco Dolstra 2023-11-01 14:43:20 +01:00
parent b2ac6fc040
commit 8ffd1695ce
4 changed files with 22 additions and 19 deletions

View file

@ -208,7 +208,7 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
std::string buildIdDir = "/lib/debug/.build-id"; std::string buildIdDir = "/lib/debug/.build-id";
if (auto st = narAccessor->stat(buildIdDir); st && st->type == FSAccessor::tDirectory) { if (auto st = narAccessor->stat(buildIdDir); st && st->type == SourceAccessor::tDirectory) {
ThreadPool threadPool(25); ThreadPool threadPool(25);
@ -234,14 +234,14 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
for (auto & s1 : narAccessor->readDirectory(buildIdDir)) { for (auto & s1 : narAccessor->readDirectory(buildIdDir)) {
auto dir = buildIdDir + "/" + s1; auto dir = buildIdDir + "/" + s1;
if (auto st = narAccessor->stat(dir); !st || st->type != FSAccessor::tDirectory if (auto st = narAccessor->stat(dir); !st || st->type != SourceAccessor::tDirectory
|| !std::regex_match(s1, regex1)) || !std::regex_match(s1, regex1))
continue; continue;
for (auto & s2 : narAccessor->readDirectory(dir)) { for (auto & s2 : narAccessor->readDirectory(dir)) {
auto debugPath = dir + "/" + s2; auto debugPath = dir + "/" + s2;
if (auto st = narAccessor->stat(debugPath); !st || st->type != FSAccessor::tRegular if (auto st = narAccessor->stat(debugPath); !st || st->type != SourceAccessor::tRegular
|| !std::regex_match(s2, regex2)) || !std::regex_match(s2, regex2))
continue; continue;

View file

@ -2,6 +2,7 @@
///@file ///@file
#include "types.hh" #include "types.hh"
#include "source-accessor.hh"
#include <optional> #include <optional>
@ -14,7 +15,7 @@ namespace nix {
class FSAccessor class FSAccessor
{ {
public: public:
enum Type { tRegular, tSymlink, tDirectory }; using Type = SourceAccessor::Type;
struct Stat struct Stat
{ {

View file

@ -51,7 +51,7 @@ struct NarAccessor : public FSAccessor
acc.root = std::move(member); acc.root = std::move(member);
parents.push(&acc.root); parents.push(&acc.root);
} else { } else {
if (parents.top()->stat.type != FSAccessor::Type::tDirectory) if (parents.top()->stat.type != Type::tDirectory)
throw Error("NAR file missing parent directory of path '%s'", path); throw Error("NAR file missing parent directory of path '%s'", path);
auto result = parents.top()->children.emplace(baseNameOf(path), std::move(member)); auto result = parents.top()->children.emplace(baseNameOf(path), std::move(member));
parents.push(&result.first->second); parents.push(&result.first->second);
@ -60,12 +60,12 @@ struct NarAccessor : public FSAccessor
void createDirectory(const Path & path) override void createDirectory(const Path & path) override
{ {
createMember(path, {FSAccessor::Type::tDirectory, false, 0, 0}); createMember(path, {Type::tDirectory, false, 0, 0});
} }
void createRegularFile(const Path & path) override void createRegularFile(const Path & path) override
{ {
createMember(path, {FSAccessor::Type::tRegular, false, 0, 0}); createMember(path, {Type::tRegular, false, 0, 0});
} }
void closeRegularFile() override void closeRegularFile() override
@ -91,7 +91,7 @@ struct NarAccessor : public FSAccessor
{ {
createMember(path, createMember(path,
NarMember{ NarMember{
.stat = {.type = FSAccessor::Type::tSymlink}, .stat = {.type = Type::tSymlink},
.target = target}); .target = target});
} }
@ -127,20 +127,20 @@ struct NarAccessor : public FSAccessor
std::string type = v["type"]; std::string type = v["type"];
if (type == "directory") { if (type == "directory") {
member.stat = {.type = FSAccessor::Type::tDirectory}; member.stat = {.type = Type::tDirectory};
for (auto i = v["entries"].begin(); i != v["entries"].end(); ++i) { for (auto i = v["entries"].begin(); i != v["entries"].end(); ++i) {
std::string name = i.key(); std::string name = i.key();
recurse(member.children[name], i.value()); recurse(member.children[name], i.value());
} }
} else if (type == "regular") { } else if (type == "regular") {
member.stat = { member.stat = {
.type = FSAccessor::Type::tRegular, .type = Type::tRegular,
.fileSize = v["size"], .fileSize = v["size"],
.isExecutable = v.value("executable", false), .isExecutable = v.value("executable", false),
.narOffset = v["narOffset"] .narOffset = v["narOffset"]
}; };
} else if (type == "symlink") { } else if (type == "symlink") {
member.stat = {.type = FSAccessor::Type::tSymlink}; member.stat = {.type = Type::tSymlink};
member.target = v.value("target", ""); member.target = v.value("target", "");
} else return; } else return;
}; };
@ -157,7 +157,7 @@ struct NarAccessor : public FSAccessor
for (auto it = path.begin(); it != end; ) { for (auto it = path.begin(); it != end; ) {
// because it != end, the remaining component is non-empty so we need // because it != end, the remaining component is non-empty so we need
// a directory // a directory
if (current->stat.type != FSAccessor::Type::tDirectory) return nullptr; if (current->stat.type != Type::tDirectory) return nullptr;
// skip slash (canonPath above ensures that this is always a slash) // skip slash (canonPath above ensures that this is always a slash)
assert(*it == '/'); assert(*it == '/');
@ -194,7 +194,7 @@ struct NarAccessor : public FSAccessor
{ {
auto i = get(path); auto i = get(path);
if (i.stat.type != FSAccessor::Type::tDirectory) if (i.stat.type != Type::tDirectory)
throw Error("path '%1%' inside NAR file is not a directory", path); throw Error("path '%1%' inside NAR file is not a directory", path);
StringSet res; StringSet res;
@ -207,7 +207,7 @@ struct NarAccessor : public FSAccessor
std::string readFile(const Path & path, bool requireValidPath = true) override std::string readFile(const Path & path, bool requireValidPath = true) override
{ {
auto i = get(path); auto i = get(path);
if (i.stat.type != FSAccessor::Type::tRegular) if (i.stat.type != Type::tRegular)
throw Error("path '%1%' inside NAR file is not a regular file", path); throw Error("path '%1%' inside NAR file is not a regular file", path);
if (getNarBytes) return getNarBytes(i.stat.narOffset, i.stat.fileSize); if (getNarBytes) return getNarBytes(i.stat.narOffset, i.stat.fileSize);
@ -219,7 +219,7 @@ struct NarAccessor : public FSAccessor
std::string readLink(const Path & path) override std::string readLink(const Path & path) override
{ {
auto i = get(path); auto i = get(path);
if (i.stat.type != FSAccessor::Type::tSymlink) if (i.stat.type != Type::tSymlink)
throw Error("path '%1%' inside NAR file is not a symlink", path); throw Error("path '%1%' inside NAR file is not a symlink", path);
return i.target; return i.target;
} }
@ -251,7 +251,7 @@ json listNar(ref<FSAccessor> accessor, const Path & path, bool recurse)
json obj = json::object(); json obj = json::object();
switch (st->type) { switch (st->type) {
case FSAccessor::Type::tRegular: case SourceAccessor::Type::tRegular:
obj["type"] = "regular"; obj["type"] = "regular";
obj["size"] = st->fileSize; obj["size"] = st->fileSize;
if (st->isExecutable) if (st->isExecutable)
@ -259,7 +259,7 @@ json listNar(ref<FSAccessor> accessor, const Path & path, bool recurse)
if (st->narOffset) if (st->narOffset)
obj["narOffset"] = st->narOffset; obj["narOffset"] = st->narOffset;
break; break;
case FSAccessor::Type::tDirectory: case SourceAccessor::Type::tDirectory:
obj["type"] = "directory"; obj["type"] = "directory";
{ {
obj["entries"] = json::object(); obj["entries"] = json::object();
@ -272,10 +272,12 @@ json listNar(ref<FSAccessor> accessor, const Path & path, bool recurse)
} }
} }
break; break;
case FSAccessor::Type::tSymlink: case SourceAccessor::Type::tSymlink:
obj["type"] = "symlink"; obj["type"] = "symlink";
obj["target"] = accessor->readLink(path); obj["target"] = accessor->readLink(path);
break; break;
case SourceAccessor::Type::tMisc:
assert(false); // cannot happen for NARs
} }
return obj; return obj;
} }

View file

@ -120,7 +120,7 @@ struct CmdShell : InstallablesCommand, MixEnvironment
unixPath.push_front(store->printStorePath(path) + "/bin"); unixPath.push_front(store->printStorePath(path) + "/bin");
auto propPath = store->printStorePath(path) + "/nix-support/propagated-user-env-packages"; auto propPath = store->printStorePath(path) + "/nix-support/propagated-user-env-packages";
if (auto st = accessor->stat(propPath); st && st->type == FSAccessor::tRegular) { if (auto st = accessor->stat(propPath); st && st->type == SourceAccessor::tRegular) {
for (auto & p : tokenizeString<Paths>(readFile(propPath))) for (auto & p : tokenizeString<Paths>(readFile(propPath)))
todo.push(store->parseStorePath(p)); todo.push(store->parseStorePath(p));
} }