diff --git a/doc/manual/rl-next/c-api-new-store-methods.md b/doc/manual/rl-next/c-api-new-store-methods.md new file mode 100644 index 00000000000..7269847a2c4 --- /dev/null +++ b/doc/manual/rl-next/c-api-new-store-methods.md @@ -0,0 +1,8 @@ +--- +synopsis: "C API: New store API methods" +prs: [14766] +--- + +The C API now includes additional methods: + +- `nix_store_query_path_from_hash_part()` - Get the full store path given its hash part diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index 4f71d0a3cae..d164efd596d 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -338,4 +338,20 @@ nix_derivation * nix_store_drv_from_store_path(nix_c_context * context, Store * NIXC_CATCH_ERRS_NULL } +StorePath * nix_store_query_path_from_hash_part(nix_c_context * context, Store * store, const char * hash) +{ + if (context) + context->last_err_code = NIX_OK; + try { + std::optional s = store->ptr->queryPathFromHashPart(hash); + + if (!s.has_value()) { + return nullptr; + } + + return new StorePath{std::move(s.value())}; + } + NIXC_CATCH_ERRS_NULL +} + } // extern "C" diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index 761fdf3c899..5c58356070f 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -259,6 +259,17 @@ nix_err nix_store_get_fs_closure( */ nix_derivation * nix_store_drv_from_store_path(nix_c_context * context, Store * store, const StorePath * path); +/** + * @brief Query the full store path given the hash part of a valid store + * path, or empty if no matching path is found. + * + * @param[out] context Optional, stores error information + * @param[in] store nix store reference + * @param[in] hash Hash part of path as a string + * @return Store path reference, NULL if no matching path is found. + */ +StorePath * nix_store_query_path_from_hash_part(nix_c_context * context, Store * store, const char * hash); + // cffi end #ifdef __cplusplus }