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..28792e7cc42 --- /dev/null +++ b/doc/manual/rl-next/c-api-new-store-methods.md @@ -0,0 +1,9 @@ +--- +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 +- `nix_store_copy_path()` - Copy a single store path between two stores, allows repairs and configuring signature checking diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index 80fcf10cb0d..4133d769f21 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -453,4 +453,42 @@ nix_err nix_derivation_get_outputs_and_optpaths( NIXC_CATCH_ERRS } +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 +} + +nix_err nix_store_copy_path( + nix_c_context * context, Store * srcStore, Store * dstStore, const StorePath * path, bool repair, bool checkSigs) +{ + if (context) + context->last_err_code = NIX_OK; + try { + if (srcStore == nullptr) + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Source store is null"); + + if (dstStore == nullptr) + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Destination store is null"); + + if (path == nullptr) + return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Store path is null"); + + auto repairFlag = repair ? nix::RepairFlag::Repair : nix::RepairFlag::NoRepair; + auto checkSigsFlag = checkSigs ? nix::CheckSigsFlag::CheckSigs : nix::CheckSigsFlag::NoCheckSigs; + nix::copyStorePath(*srcStore->ptr, *dstStore->ptr, path->path, repairFlag, checkSigsFlag); + } + NIXC_CATCH_ERRS +} + } // extern "C" diff --git a/src/libstore-c/nix_api_store.h b/src/libstore-c/nix_api_store.h index 5e542b0caaf..964c4066154 100644 --- a/src/libstore-c/nix_api_store.h +++ b/src/libstore-c/nix_api_store.h @@ -357,6 +357,30 @@ nix_err nix_derivation_get_outputs_and_optpaths( nix_err nix_derivation_to_json( nix_c_context * context, const nix_derivation * drv, nix_get_string_callback callback, void * userdata); +/** + * @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); + +/** + * @brief Copy a path from one store to another. + * + * @param[out] context Optional, stores error information + * @param[in] srcStore nix source store reference + * @param[in] dstStore nix destination store reference + * @param[in] path The path to copy + * @param[in] repair Whether to repair the path + * @param[in] checkSigs Whether to check path signatures are trusted before copying + */ +nix_err nix_store_copy_path( + nix_c_context * context, Store * srcStore, Store * dstStore, const StorePath * path, bool repair, bool checkSigs); + // cffi end #ifdef __cplusplus }