From 1b4aa5c1ef7d3a04ca4ec01a98306a180636cfc8 Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Fri, 8 Aug 2025 01:20:50 +0300 Subject: [PATCH 1/2] libstore: Remove unused overload of scanForReferences This doesn't seem to be used anywhere at the moment. It might be used out-of-tree, but this is a small convenience function that is not worth keeping without in-tree usage. --- src/libstore/include/nix/store/path-references.hh | 2 -- src/libstore/path-references.cc | 8 -------- 2 files changed, 10 deletions(-) diff --git a/src/libstore/include/nix/store/path-references.hh b/src/libstore/include/nix/store/path-references.hh index fad1e57a3..55535f666 100644 --- a/src/libstore/include/nix/store/path-references.hh +++ b/src/libstore/include/nix/store/path-references.hh @@ -6,8 +6,6 @@ namespace nix { -std::pair scanForReferences(const Path & path, const StorePathSet & refs); - StorePathSet scanForReferences(Sink & toTee, const Path & path, const StorePathSet & refs); class PathRefScanSink : public RefScanSink diff --git a/src/libstore/path-references.cc b/src/libstore/path-references.cc index 2c71f437f..8b167e902 100644 --- a/src/libstore/path-references.cc +++ b/src/libstore/path-references.cc @@ -43,14 +43,6 @@ StorePathSet PathRefScanSink::getResultPaths() return found; } -std::pair scanForReferences(const std::string & path, const StorePathSet & refs) -{ - HashSink hashSink{HashAlgorithm::SHA256}; - auto found = scanForReferences(hashSink, path, refs); - auto hash = hashSink.finish(); - return std::pair(found, hash); -} - StorePathSet scanForReferences(Sink & toTee, const Path & path, const StorePathSet & refs) { PathRefScanSink refsSink = PathRefScanSink::fromPaths(refs); From 2e3ebfb82922cc19f1966a6d3a60272564fb811f Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Fri, 8 Aug 2025 01:35:34 +0300 Subject: [PATCH 2/2] libutil: Move references.{hh,cc} to libstore The implicit dependency on refLength (which is the StorePath::HashLen) is not good. Also the companion tests and benchmarks are already in libstore-tests. --- src/libstore-tests/ref-scan-bench.cc | 2 +- src/libstore-tests/references.cc | 39 +++++++++++++++- src/libstore/include/nix/store/meson.build | 1 + .../include/nix/store/path-references.hh | 2 +- .../include/nix/store}/references.hh | 0 src/libstore/local-store.cc | 2 +- src/libstore/make-content-addressed.cc | 2 +- src/libstore/meson.build | 1 + src/{libutil => libstore}/references.cc | 6 +-- src/libstore/store-api.cc | 2 - src/libutil-tests/meson.build | 1 - src/libutil-tests/references.cc | 45 ------------------- src/libutil/include/nix/util/meson.build | 1 - src/libutil/meson.build | 1 - src/nix/hash.cc | 2 +- 15 files changed, 48 insertions(+), 59 deletions(-) rename src/{libutil/include/nix/util => libstore/include/nix/store}/references.hh (100%) rename src/{libutil => libstore}/references.cc (96%) delete mode 100644 src/libutil-tests/references.cc diff --git a/src/libstore-tests/ref-scan-bench.cc b/src/libstore-tests/ref-scan-bench.cc index 011d53aec..ff0aa1815 100644 --- a/src/libstore-tests/ref-scan-bench.cc +++ b/src/libstore-tests/ref-scan-bench.cc @@ -1,4 +1,4 @@ -#include "nix/util/references.hh" +#include "nix/store/references.hh" #include "nix/store/path.hh" #include "nix/util/base-nix-32.hh" diff --git a/src/libstore-tests/references.cc b/src/libstore-tests/references.cc index c7b706c68..27ecad08f 100644 --- a/src/libstore-tests/references.cc +++ b/src/libstore-tests/references.cc @@ -1,9 +1,46 @@ -#include "nix/util/references.hh" +#include "nix/store/references.hh" #include namespace nix { +struct RewriteParams +{ + std::string originalString, finalString; + StringMap rewrites; + + friend std::ostream & operator<<(std::ostream & os, const RewriteParams & bar) + { + StringSet strRewrites; + for (auto & [from, to] : bar.rewrites) + strRewrites.insert(from + "->" + to); + return os << "OriginalString: " << bar.originalString << std::endl + << "Rewrites: " << dropEmptyInitThenConcatStringsSep(",", strRewrites) << std::endl + << "Expected result: " << bar.finalString; + } +}; + +class RewriteTest : public ::testing::TestWithParam +{}; + +TEST_P(RewriteTest, IdentityRewriteIsIdentity) +{ + RewriteParams param = GetParam(); + StringSink rewritten; + auto rewriter = RewritingSink(param.rewrites, rewritten); + rewriter(param.originalString); + rewriter.flush(); + ASSERT_EQ(rewritten.s, param.finalString); +} + +INSTANTIATE_TEST_CASE_P( + references, + RewriteTest, + ::testing::Values( + RewriteParams{"foooo", "baroo", {{"foo", "bar"}, {"bar", "baz"}}}, + RewriteParams{"foooo", "bazoo", {{"fou", "bar"}, {"foo", "baz"}}}, + RewriteParams{"foooo", "foooo", {}})); + TEST(references, scan) { std::string hash1 = "dc04vv14dak1c1r48qa0m23vr9jy8sm0"; diff --git a/src/libstore/include/nix/store/meson.build b/src/libstore/include/nix/store/meson.build index e883a89e4..e41a7da4d 100644 --- a/src/libstore/include/nix/store/meson.build +++ b/src/libstore/include/nix/store/meson.build @@ -62,6 +62,7 @@ headers = [ config_pub_h ] + files( 'posix-fs-canonicalise.hh', 'profiles.hh', 'realisation.hh', + 'references.hh', 'remote-fs-accessor.hh', 'remote-store-connection.hh', 'remote-store.hh', diff --git a/src/libstore/include/nix/store/path-references.hh b/src/libstore/include/nix/store/path-references.hh index 55535f666..66d0da268 100644 --- a/src/libstore/include/nix/store/path-references.hh +++ b/src/libstore/include/nix/store/path-references.hh @@ -1,7 +1,7 @@ #pragma once ///@file -#include "nix/util/references.hh" +#include "nix/store/references.hh" #include "nix/store/path.hh" namespace nix { diff --git a/src/libutil/include/nix/util/references.hh b/src/libstore/include/nix/store/references.hh similarity index 100% rename from src/libutil/include/nix/util/references.hh rename to src/libstore/include/nix/store/references.hh diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index d8540be86..685402cfe 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -7,7 +7,7 @@ #include "nix/store/derivations.hh" #include "nix/store/realisation.hh" #include "nix/store/nar-info.hh" -#include "nix/util/references.hh" +#include "nix/store/references.hh" #include "nix/util/callback.hh" #include "nix/util/topo-sort.hh" #include "nix/util/finally.hh" diff --git a/src/libstore/make-content-addressed.cc b/src/libstore/make-content-addressed.cc index 831542943..ce4a36849 100644 --- a/src/libstore/make-content-addressed.cc +++ b/src/libstore/make-content-addressed.cc @@ -1,5 +1,5 @@ #include "nix/store/make-content-addressed.hh" -#include "nix/util/references.hh" +#include "nix/store/references.hh" namespace nix { diff --git a/src/libstore/meson.build b/src/libstore/meson.build index 0b6471af3..ad76582d8 100644 --- a/src/libstore/meson.build +++ b/src/libstore/meson.build @@ -316,6 +316,7 @@ sources = files( 'posix-fs-canonicalise.cc', 'profiles.cc', 'realisation.cc', + 'references.cc', 'remote-fs-accessor.cc', 'remote-store.cc', 'restricted-store.cc', diff --git a/src/libutil/references.cc b/src/libstore/references.cc similarity index 96% rename from src/libutil/references.cc rename to src/libstore/references.cc index 42076acff..1620af26e 100644 --- a/src/libutil/references.cc +++ b/src/libstore/references.cc @@ -1,6 +1,6 @@ -#include "nix/util/references.hh" +#include "nix/store/references.hh" +#include "nix/store/path.hh" #include "nix/util/hash.hh" -#include "nix/util/archive.hh" #include "nix/util/base-nix-32.hh" #include @@ -10,7 +10,7 @@ namespace nix { -static size_t refLength = 32; /* characters */ +static constexpr auto refLength = StorePath::HashLen; static void search(std::string_view s, StringSet & hashes, StringSet & seen) { diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 3e2a8e553..b678833e6 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -9,7 +9,6 @@ #include "nix/util/util.hh" #include "nix/store/nar-info-disk-cache.hh" #include "nix/util/thread-pool.hh" -#include "nix/util/references.hh" #include "nix/util/archive.hh" #include "nix/util/callback.hh" #include "nix/util/git.hh" @@ -18,7 +17,6 @@ // `addMultipleToStore`. #include "nix/store/worker-protocol.hh" #include "nix/util/signals.hh" -#include "nix/util/users.hh" #include #include diff --git a/src/libutil-tests/meson.build b/src/libutil-tests/meson.build index 2dbb4d129..e4a060d3e 100644 --- a/src/libutil-tests/meson.build +++ b/src/libutil-tests/meson.build @@ -66,7 +66,6 @@ sources = files( 'pool.cc', 'position.cc', 'processes.cc', - 'references.cc', 'sort.cc', 'spawn.cc', 'strings.cc', diff --git a/src/libutil-tests/references.cc b/src/libutil-tests/references.cc deleted file mode 100644 index b76db67cf..000000000 --- a/src/libutil-tests/references.cc +++ /dev/null @@ -1,45 +0,0 @@ -#include "nix/util/references.hh" -#include - -namespace nix { - -using std::string; - -struct RewriteParams -{ - string originalString, finalString; - StringMap rewrites; - - friend std::ostream & operator<<(std::ostream & os, const RewriteParams & bar) - { - StringSet strRewrites; - for (auto & [from, to] : bar.rewrites) - strRewrites.insert(from + "->" + to); - return os << "OriginalString: " << bar.originalString << std::endl - << "Rewrites: " << dropEmptyInitThenConcatStringsSep(",", strRewrites) << std::endl - << "Expected result: " << bar.finalString; - } -}; - -class RewriteTest : public ::testing::TestWithParam -{}; - -TEST_P(RewriteTest, IdentityRewriteIsIdentity) -{ - RewriteParams param = GetParam(); - StringSink rewritten; - auto rewriter = RewritingSink(param.rewrites, rewritten); - rewriter(param.originalString); - rewriter.flush(); - ASSERT_EQ(rewritten.s, param.finalString); -} - -INSTANTIATE_TEST_CASE_P( - references, - RewriteTest, - ::testing::Values( - RewriteParams{"foooo", "baroo", {{"foo", "bar"}, {"bar", "baz"}}}, - RewriteParams{"foooo", "bazoo", {{"fou", "bar"}, {"foo", "baz"}}}, - RewriteParams{"foooo", "foooo", {}})); - -} // namespace nix diff --git a/src/libutil/include/nix/util/meson.build b/src/libutil/include/nix/util/meson.build index bc58b4d5e..bdf114259 100644 --- a/src/libutil/include/nix/util/meson.build +++ b/src/libutil/include/nix/util/meson.build @@ -55,7 +55,6 @@ headers = files( 'posix-source-accessor.hh', 'processes.hh', 'ref.hh', - 'references.hh', 'regex-combinators.hh', 'repair-flag.hh', 'serialise.hh', diff --git a/src/libutil/meson.build b/src/libutil/meson.build index afdddc6b5..ffd1ebd49 100644 --- a/src/libutil/meson.build +++ b/src/libutil/meson.build @@ -145,7 +145,6 @@ sources = [ config_priv_h ] + files( 'pos-table.cc', 'position.cc', 'posix-source-accessor.cc', - 'references.cc', 'serialise.cc', 'signature/local-keys.cc', 'signature/signer.cc', diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 9858386c5..d3c9ccb66 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -3,7 +3,7 @@ #include "nix/store/content-address.hh" #include "nix/cmd/legacy.hh" #include "nix/main/shared.hh" -#include "nix/util/references.hh" +#include "nix/store/references.hh" #include "nix/util/archive.hh" #include "nix/util/git.hh" #include "nix/util/posix-source-accessor.hh"