From f2522d4ecdcd693a2f94cd118eb7fa509983a54e Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 8 Apr 2024 13:14:03 +0200 Subject: [PATCH] libexpr-c: Add nix_store_path_name --- src/libstore-c/nix_api_store.cc | 7 +++++++ src/libstore-c/nix_api_store.h | 9 +++++++++ tests/unit/libexpr/nix_api_expr.cc | 8 ++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index d044ba14f..511ba0fad 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -128,6 +128,13 @@ nix_err nix_store_realise( NIXC_CATCH_ERRS } +void nix_store_path_name(const StorePath *store_path, void * callback, void * user_data) +{ + std::string_view name = store_path->path.name(); + ((nix_get_string_callback) callback)(name.data(), name.size(), user_data); +} + + void nix_store_path_free(StorePath * sp) { delete sp; diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index ab86a81df..ca8996681 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -90,6 +90,15 @@ nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callbac */ StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path); +/** + * @brief Get the path name (e.g. "name" in /nix/store/...-name) + * + * @param[in] store_path the path to get the name from + * @param[in] callback called with the name + * @param[in] user_data arbitrary data, passed to the callback when it's called. + */ +void nix_store_path_name(const StorePath *store_path, void * callback, void * user_data); + /** * @brief Copy a StorePath * diff --git a/tests/unit/libexpr/nix_api_expr.cc b/tests/unit/libexpr/nix_api_expr.cc index fad078d0c..f5c66536d 100644 --- a/tests/unit/libexpr/nix_api_expr.cc +++ b/tests/unit/libexpr/nix_api_expr.cc @@ -6,6 +6,7 @@ #include "nix_api_value.h" #include "tests/nix_api_expr.hh" +#include "tests/string_callback.hh" #include "gmock/gmock.h" #include @@ -168,11 +169,14 @@ TEST_F(nix_api_expr_test, nix_expr_realise_context) EXPECT_THAT(s, testing::HasSubstr("a derivation path by itself:")); EXPECT_THAT(s, testing::EndsWith("-not-actually-built-yet.drv\n")); - std::vector names; + std::vector names; size_t n = nix_realised_string_get_store_path_count(r); for (size_t i = 0; i < n; ++i) { const StorePath * p = nix_realised_string_get_store_path(r, i); - names.push_back(p->path.name()); + ASSERT_NE(nullptr, p); + std::string name; + nix_store_path_name(p, OBSERVE_STRING(name)); + names.push_back(name); } std::sort(names.begin(), names.end()); ASSERT_EQ(3, names.size());