Skip to content

Commit

Permalink
C API: Add nix_store_get_storedir
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth authored and Mic92 committed Dec 14, 2024
1 parent e125d90 commit 5f76536
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/libstore-c/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ nix_err nix_store_get_uri(nix_c_context * context, Store * store, nix_get_string
NIXC_CATCH_ERRS
}

nix_err
nix_store_get_storedir(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data)
{
if (context)
context->last_err_code = NIX_OK;
try {
return call_nix_get_string_callback(store->ptr->storeDir, callback, user_data);
}
NIXC_CATCH_ERRS
}

nix_err
nix_store_get_version(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data)
{
Expand Down
13 changes: 12 additions & 1 deletion src/libstore-c/nix_api_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ void nix_store_free(Store * store);
*/
nix_err nix_store_get_uri(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data);

// returns: owned StorePath*
/**
* @brief get the storeDir of a Nix store, typically `"/nix/store"`
* @param[out] context Optional, stores error information
* @param[in] store nix store reference
* @param[in] callback Called with the URI.
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called.
* @see nix_get_string_callback
* @return error code, NIX_OK on success.
*/
nix_err
nix_store_get_storedir(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data);

/**
* @brief Parse a Nix store path into a StorePath
*
Expand Down
5 changes: 4 additions & 1 deletion src/libstore-tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ cxx = meson.get_compiler('cpp')

subdir('nix-meson-build-support/deps-lists')

nix_store = dependency('nix-store')

deps_private_maybe_subproject = [
dependency('nix-store'),
nix_store,
dependency('nix-store-c'),
dependency('nix-store-test-support'),
]
Expand Down Expand Up @@ -90,6 +92,7 @@ this_exe = executable(
include_directories : include_dirs,
# TODO: -lrapidcheck, see ../libutil-support/build.meson
link_args: linker_export_flags + ['-lrapidcheck'],
cpp_args : [ '-DNIX_STORE_DIR="' + nix_store.get_variable('storedir') + '"' ],
# get main from gtest
install : true,
)
Expand Down
33 changes: 33 additions & 0 deletions src/libstore-tests/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ TEST_F(nix_api_store_test, nix_store_get_uri)
ASSERT_STREQ("local", str.c_str());
}

TEST_F(nix_api_util_context, nix_store_get_storedir_default)
{
if (nix::getEnv("HOME").value_or("") == "/homeless-shelter") {
// skipping test in sandbox because nix_store_open tries to create /nix/var/nix/profiles
GTEST_SKIP();
}
nix_libstore_init(ctx);
Store * store = nix_store_open(ctx, nullptr, nullptr);
assert_ctx_ok();
ASSERT_NE(store, nullptr);

std::string str;
auto ret = nix_store_get_storedir(ctx, store, OBSERVE_STRING(str));
assert_ctx_ok();
ASSERT_EQ(NIX_OK, ret);

// These tests run with a unique storeDir, but not a relocated store
ASSERT_STREQ(NIX_STORE_DIR, str.c_str());

nix_store_free(store);
}

TEST_F(nix_api_store_test, nix_store_get_storedir)
{
std::string str;
auto ret = nix_store_get_storedir(ctx, store, OBSERVE_STRING(str));
assert_ctx_ok();
ASSERT_EQ(NIX_OK, ret);

// These tests run with a unique storeDir, but not a relocated store
ASSERT_STREQ(nixStoreDir.c_str(), str.c_str());
}

TEST_F(nix_api_store_test, InvalidPathFails)
{
nix_store_parse_path(ctx, store, "invalid-path");
Expand Down

0 comments on commit 5f76536

Please sign in to comment.