Unify DirEntries types

This commit is contained in:
Eelco Dolstra 2023-11-01 15:33:19 +01:00
parent cdb27c1519
commit 5381123879
8 changed files with 16 additions and 14 deletions

View file

@ -231,14 +231,14 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
std::regex regex1("^[0-9a-f]{2}$"); std::regex regex1("^[0-9a-f]{2}$");
std::regex regex2("^[0-9a-f]{38}\\.debug$"); std::regex regex2("^[0-9a-f]{38}\\.debug$");
for (auto & s1 : narAccessor->readDirectory(buildIdDir)) { for (auto & [s1, _type] : narAccessor->readDirectory(buildIdDir)) {
auto dir = buildIdDir + "/" + s1; auto dir = buildIdDir + "/" + s1;
if (auto st = narAccessor->stat(dir); !st || st->type != SourceAccessor::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, _type] : narAccessor->readDirectory(dir)) {
auto debugPath = dir + "/" + s2; auto debugPath = dir + "/" + s2;
if (auto st = narAccessor->stat(debugPath); !st || st->type != SourceAccessor::tRegular if (auto st = narAccessor->stat(debugPath); !st || st->type != SourceAccessor::tRegular

View file

@ -39,7 +39,9 @@ public:
virtual std::optional<Stat> stat(const Path & path) = 0; virtual std::optional<Stat> stat(const Path & path) = 0;
virtual StringSet readDirectory(const Path & path) = 0; using DirEntries = SourceAccessor::DirEntries;
virtual DirEntries readDirectory(const Path & path) = 0;
/** /**
* Read a file inside the store. * Read a file inside the store.

View file

@ -48,15 +48,15 @@ struct LocalStoreAccessor : public FSAccessor
S_ISREG(st.st_mode) && st.st_mode & S_IXUSR}}; S_ISREG(st.st_mode) && st.st_mode & S_IXUSR}};
} }
StringSet readDirectory(const Path & path) override DirEntries readDirectory(const Path & path) override
{ {
auto realPath = toRealPath(path); auto realPath = toRealPath(path);
auto entries = nix::readDirectory(realPath); auto entries = nix::readDirectory(realPath);
StringSet res; DirEntries res;
for (auto & entry : entries) for (auto & entry : entries)
res.insert(entry.name); res.insert_or_assign(entry.name, std::nullopt);
return res; return res;
} }

View file

@ -190,16 +190,16 @@ struct NarAccessor : public FSAccessor
return i->stat; return i->stat;
} }
StringSet readDirectory(const Path & path) override DirEntries readDirectory(const Path & path) override
{ {
auto i = get(path); auto i = get(path);
if (i.stat.type != 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; DirEntries res;
for (auto & child : i.children) for (auto & child : i.children)
res.insert(child.first); res.insert_or_assign(child.first, std::nullopt);
return res; return res;
} }
@ -264,7 +264,7 @@ json listNar(ref<FSAccessor> accessor, const Path & path, bool recurse)
{ {
obj["entries"] = json::object(); obj["entries"] = json::object();
json &res2 = obj["entries"]; json &res2 = obj["entries"];
for (auto & name : accessor->readDirectory(path)) { for (auto & [name, type] : accessor->readDirectory(path)) {
if (recurse) { if (recurse) {
res2[name] = listNar(accessor, path + "/" + name, true); res2[name] = listNar(accessor, path + "/" + name, true);
} else } else

View file

@ -107,7 +107,7 @@ std::optional<FSAccessor::Stat> RemoteFSAccessor::stat(const Path & path)
return res.first->stat(res.second); return res.first->stat(res.second);
} }
StringSet RemoteFSAccessor::readDirectory(const Path & path) SourceAccessor::DirEntries RemoteFSAccessor::readDirectory(const Path & path)
{ {
auto res = fetch(path); auto res = fetch(path);
return res.first->readDirectory(res.second); return res.first->readDirectory(res.second);

View file

@ -30,7 +30,7 @@ public:
std::optional<Stat> stat(const Path & path) override; std::optional<Stat> stat(const Path & path) override;
StringSet readDirectory(const Path & path) override; DirEntries readDirectory(const Path & path) override;
std::string readFile(const Path & path, bool requireValidPath = true) override; std::string readFile(const Path & path, bool requireValidPath = true) override;

View file

@ -74,7 +74,7 @@ struct MixLs : virtual Args, MixJSON
{ {
if (st.type == FSAccessor::Type::tDirectory && !showDirectory) { if (st.type == FSAccessor::Type::tDirectory && !showDirectory) {
auto names = accessor->readDirectory(curPath); auto names = accessor->readDirectory(curPath);
for (auto & name : names) for (auto & [name, type] : names)
showFile(curPath + "/" + name, relPath + "/" + name); showFile(curPath + "/" + name, relPath + "/" + name);
} else } else
showFile(curPath, relPath); showFile(curPath, relPath);

View file

@ -224,7 +224,7 @@ struct CmdWhyDepends : SourceExprCommand, MixOperateOnOptions
if (st->type == FSAccessor::Type::tDirectory) { if (st->type == FSAccessor::Type::tDirectory) {
auto names = accessor->readDirectory(p); auto names = accessor->readDirectory(p);
for (auto & name : names) for (auto & [name, type] : names)
visitPath(p + "/" + name); visitPath(p + "/" + name);
} }