libexpr-c: Add nix_store_path_name

This commit is contained in:
Robert Hensing 2024-04-08 13:14:03 +02:00
parent a512f4eebc
commit f2522d4ecd
3 changed files with 22 additions and 2 deletions

View File

@ -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;

View File

@ -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
*

View File

@ -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 <gtest/gtest.h>
@ -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<std::string_view> names;
std::vector<std::string> 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());