fix tests and minor changes

- use the iterator in `CanonPath` to count `level`
- use the `CanonPath::basename` method
- use `CanonPath::root` instead of `CanonPath{""}`
- remove `Path` and `PathView`, use `std::filesystem::path` directly
This commit is contained in:
siddhantCodes 2024-07-03 17:34:02 +05:30
parent 72bb530141
commit 2cf24a2df0
7 changed files with 21 additions and 13 deletions

View File

@ -73,7 +73,9 @@ struct NarAccessor : public SourceAccessor
NarMember & createMember(const CanonPath & path, NarMember member) NarMember & createMember(const CanonPath & path, NarMember member)
{ {
size_t level = std::count(path.rel().begin(), path.rel().end(), '/'); size_t level = 0;
for (auto _ : path) ++level;
while (parents.size() > level) parents.pop(); while (parents.size() > level) parents.pop();
if (parents.empty()) { if (parents.empty()) {
@ -83,7 +85,7 @@ struct NarAccessor : public SourceAccessor
} else { } else {
if (parents.top()->stat.type != 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.rel()), std::move(member)); auto result = parents.top()->children.emplace(*path.baseName(), std::move(member));
auto & ref = result.first->second; auto & ref = result.first->second;
parents.push(&ref); parents.push(&ref);
return ref; return ref;

View File

@ -290,11 +290,11 @@ void parseDump(FileSystemObjectSink & sink, Source & source)
} }
if (version != narVersionMagic1) if (version != narVersionMagic1)
throw badArchive("input doesn't look like a Nix archive"); throw badArchive("input doesn't look like a Nix archive");
parse(sink, source, CanonPath{""}); parse(sink, source, CanonPath::root);
} }
void restorePath(const Path & path, Source & source) void restorePath(const std::filesystem::path & path, Source & source)
{ {
RestoreSink sink; RestoreSink sink;
sink.dstPath = path; sink.dstPath = path;

View File

@ -75,7 +75,7 @@ void dumpString(std::string_view s, Sink & sink);
void parseDump(FileSystemObjectSink & sink, Source & source); void parseDump(FileSystemObjectSink & sink, Source & source);
void restorePath(const Path & path, Source & source); void restorePath(const std::filesystem::path & path, Source & source);
/** /**
* Read a NAR from 'source' and write it to 'sink'. * Read a NAR from 'source' and write it to 'sink'.

View File

@ -127,7 +127,7 @@ Path dirOf(const PathView path)
} }
std::string_view baseNameOf(PathView path) std::string_view baseNameOf(std::string_view path)
{ {
if (path.empty()) if (path.empty())
return ""; return "";

View File

@ -84,8 +84,12 @@ struct RestoreRegularFile : CreateRegularFileSink {
void RestoreSink::createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func) void RestoreSink::createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func)
{ {
std::cout << "SCREAM!!!====== " << dstPath / path.rel() << std::endl; auto p = dstPath;
std::filesystem::path p = dstPath / path.rel();
if (!path.rel().empty()) {
p = p / path.rel();
}
RestoreRegularFile crf; RestoreRegularFile crf;
crf.fd = crf.fd =
#ifdef _WIN32 #ifdef _WIN32
@ -136,7 +140,9 @@ void RestoreRegularFile::operator () (std::string_view data)
void RestoreSink::createSymlink(const CanonPath & path, const std::string & target) void RestoreSink::createSymlink(const CanonPath & path, const std::string & target)
{ {
std::filesystem::path p = dstPath / path.rel(); auto p = dstPath;
if (!path.rel().empty())
p = dstPath / path.rel();
nix::createSymlink(target, p); nix::createSymlink(target, p);
} }

View File

@ -208,7 +208,7 @@ std::optional<Mode> convertMode(SourceAccessor::Type type)
void restore(FileSystemObjectSink & sink, Source & source, std::function<RestoreHook> hook) void restore(FileSystemObjectSink & sink, Source & source, std::function<RestoreHook> hook)
{ {
parse(sink, CanonPath{""}, source, BlobMode::Regular, [&](CanonPath name, TreeEntry entry) { parse(sink, CanonPath::root, source, BlobMode::Regular, [&](CanonPath name, TreeEntry entry) {
auto [accessor, from] = hook(entry.hash); auto [accessor, from] = hook(entry.hash);
auto stat = accessor->lstat(from); auto stat = accessor->lstat(from);
auto gotOpt = convertMode(stat.type); auto gotOpt = convertMode(stat.type);

View File

@ -67,7 +67,7 @@ TEST_F(GitTest, blob_read) {
StringSink out; StringSink out;
RegularFileSink out2 { out }; RegularFileSink out2 { out };
ASSERT_EQ(parseObjectType(in, mockXpSettings), ObjectType::Blob); ASSERT_EQ(parseObjectType(in, mockXpSettings), ObjectType::Blob);
parseBlob(out2, CanonPath{""}, in, BlobMode::Regular, mockXpSettings); parseBlob(out2, CanonPath::root, in, BlobMode::Regular, mockXpSettings);
auto expected = readFile(goldenMaster("hello-world.bin")); auto expected = readFile(goldenMaster("hello-world.bin"));
@ -132,7 +132,7 @@ TEST_F(GitTest, tree_read) {
NullFileSystemObjectSink out; NullFileSystemObjectSink out;
Tree got; Tree got;
ASSERT_EQ(parseObjectType(in, mockXpSettings), ObjectType::Tree); ASSERT_EQ(parseObjectType(in, mockXpSettings), ObjectType::Tree);
parseTree(out, CanonPath{""}, in, [&](auto & name, auto entry) { parseTree(out, CanonPath::root, in, [&](auto & name, auto entry) {
auto name2 = std::string{name.rel()}; auto name2 = std::string{name.rel()};
if (entry.mode == Mode::Directory) if (entry.mode == Mode::Directory)
name2 += '/'; name2 += '/';
@ -227,7 +227,7 @@ TEST_F(GitTest, both_roundrip) {
mockXpSettings); mockXpSettings);
}; };
mkSinkHook(CanonPath{""}, root.hash, BlobMode::Regular); mkSinkHook(CanonPath::root, root.hash, BlobMode::Regular);
ASSERT_EQ(*files, *files2); ASSERT_EQ(*files, *files2);
} }