From 2cf24a2df0f07479be57afe2391da2ae3d25be10 Mon Sep 17 00:00:00 2001 From: siddhantCodes Date: Wed, 3 Jul 2024 17:34:02 +0530 Subject: [PATCH] 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 --- src/libstore/nar-accessor.cc | 6 ++++-- src/libutil/archive.cc | 4 ++-- src/libutil/archive.hh | 2 +- src/libutil/file-system.cc | 2 +- src/libutil/fs-sink.cc | 12 +++++++++--- src/libutil/git.cc | 2 +- tests/unit/libutil/git.cc | 6 +++--- 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/libstore/nar-accessor.cc b/src/libstore/nar-accessor.cc index 33dde48e5..b1079b027 100644 --- a/src/libstore/nar-accessor.cc +++ b/src/libstore/nar-accessor.cc @@ -73,7 +73,9 @@ struct NarAccessor : public SourceAccessor 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(); if (parents.empty()) { @@ -83,7 +85,7 @@ struct NarAccessor : public SourceAccessor } else { if (parents.top()->stat.type != Type::tDirectory) 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; parents.push(&ref); return ref; diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index 3693a1ffd..e2ebcda0c 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -290,11 +290,11 @@ void parseDump(FileSystemObjectSink & sink, Source & source) } if (version != narVersionMagic1) 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; sink.dstPath = path; diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh index bd70072ce..2da8f0cb1 100644 --- a/src/libutil/archive.hh +++ b/src/libutil/archive.hh @@ -75,7 +75,7 @@ void dumpString(std::string_view s, Sink & sink); 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'. diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 9307a0b53..f75851bbd 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -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()) return ""; diff --git a/src/libutil/fs-sink.cc b/src/libutil/fs-sink.cc index b4900cf8b..597272ec9 100644 --- a/src/libutil/fs-sink.cc +++ b/src/libutil/fs-sink.cc @@ -84,8 +84,12 @@ struct RestoreRegularFile : CreateRegularFileSink { void RestoreSink::createRegularFile(const CanonPath & path, std::function func) { - std::cout << "SCREAM!!!====== " << dstPath / path.rel() << std::endl; - std::filesystem::path p = dstPath / path.rel(); + auto p = dstPath; + + if (!path.rel().empty()) { + p = p / path.rel(); + } + RestoreRegularFile crf; crf.fd = #ifdef _WIN32 @@ -136,7 +140,9 @@ void RestoreRegularFile::operator () (std::string_view data) 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); } diff --git a/src/libutil/git.cc b/src/libutil/git.cc index f23df566a..a6968a43e 100644 --- a/src/libutil/git.cc +++ b/src/libutil/git.cc @@ -208,7 +208,7 @@ std::optional convertMode(SourceAccessor::Type type) void restore(FileSystemObjectSink & sink, Source & source, std::function 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 stat = accessor->lstat(from); auto gotOpt = convertMode(stat.type); diff --git a/tests/unit/libutil/git.cc b/tests/unit/libutil/git.cc index 9454bb675..a0125d023 100644 --- a/tests/unit/libutil/git.cc +++ b/tests/unit/libutil/git.cc @@ -67,7 +67,7 @@ TEST_F(GitTest, blob_read) { StringSink out; RegularFileSink out2 { out }; 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")); @@ -132,7 +132,7 @@ TEST_F(GitTest, tree_read) { NullFileSystemObjectSink out; Tree got; 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()}; if (entry.mode == Mode::Directory) name2 += '/'; @@ -227,7 +227,7 @@ TEST_F(GitTest, both_roundrip) { mockXpSettings); }; - mkSinkHook(CanonPath{""}, root.hash, BlobMode::Regular); + mkSinkHook(CanonPath::root, root.hash, BlobMode::Regular); ASSERT_EQ(*files, *files2); }