mirror of
https://github.com/NixOS/nix
synced 2025-06-27 00:11:17 +02:00
C API: Keep the structure flat
See https://github.com/NixOS/nix/pull/10329
This commit is contained in:
parent
d96b52bd8b
commit
c57de60522
26 changed files with 50 additions and 32 deletions
21
src/libstore-c/local.mk
Normal file
21
src/libstore-c/local.mk
Normal file
|
@ -0,0 +1,21 @@
|
|||
libraries += libstorec
|
||||
|
||||
libstorec_NAME = libnixstorec
|
||||
|
||||
libstorec_DIR := $(d)
|
||||
|
||||
libstorec_SOURCES := $(wildcard $(d)/*.cc)
|
||||
|
||||
libstorec_LIBS = libutil libstore libutilc
|
||||
|
||||
libstorec_LDFLAGS += -pthread
|
||||
|
||||
# Not just for this library itself, but also for downstream libraries using this library
|
||||
|
||||
INCLUDE_libstorec := -I $(d)
|
||||
libstorec_CXXFLAGS += $(INCLUDE_libutil) $(INCLUDE_libutilc) \
|
||||
$(INCLUDE_libstore) $(INCLUDE_libstorec)
|
||||
|
||||
$(eval $(call install-file-in, $(d)/nix-store-c.pc, $(libdir)/pkgconfig, 0644))
|
||||
|
||||
libstorec_FORCE_INSTALL := 1
|
9
src/libstore-c/nix-store-c.pc.in
Normal file
9
src/libstore-c/nix-store-c.pc.in
Normal file
|
@ -0,0 +1,9 @@
|
|||
prefix=@prefix@
|
||||
libdir=@libdir@
|
||||
includedir=@includedir@
|
||||
|
||||
Name: Nix
|
||||
Description: Nix Store - C API
|
||||
Version: @PACKAGE_VERSION@
|
||||
Libs: -L${libdir} -lnixstorec -lnixutilc
|
||||
Cflags: -I${includedir}/nix
|
134
src/libstore-c/nix_api_store.cc
Normal file
134
src/libstore-c/nix_api_store.cc
Normal file
|
@ -0,0 +1,134 @@
|
|||
#include "nix_api_store.h"
|
||||
#include "nix_api_store_internal.h"
|
||||
#include "nix_api_util.h"
|
||||
#include "nix_api_util_internal.h"
|
||||
|
||||
#include "path.hh"
|
||||
#include "store-api.hh"
|
||||
#include "build-result.hh"
|
||||
|
||||
#include "globals.hh"
|
||||
|
||||
nix_err nix_libstore_init(nix_c_context * context)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
nix::initLibStore();
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
nix_err nix_init_plugins(nix_c_context * context)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
nix::initPlugins();
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
Store * nix_store_open(nix_c_context * context, const char * uri, const char *** params)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
std::string uri_str = uri ? uri : "";
|
||||
|
||||
if (uri_str.empty())
|
||||
return new Store{nix::openStore()};
|
||||
|
||||
if (!params)
|
||||
return new Store{nix::openStore(uri_str)};
|
||||
|
||||
nix::Store::Params params_map;
|
||||
for (size_t i = 0; params[i] != nullptr; i++) {
|
||||
params_map[params[i][0]] = params[i][1];
|
||||
}
|
||||
return new Store{nix::openStore(uri_str, params_map)};
|
||||
}
|
||||
NIXC_CATCH_ERRS_NULL
|
||||
}
|
||||
|
||||
void nix_store_free(Store * store)
|
||||
{
|
||||
delete store;
|
||||
}
|
||||
|
||||
nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callback, void * user_data)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto res = store->ptr->getUri();
|
||||
return call_nix_observe_string(res, callback, user_data);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
nix_err nix_store_get_version(nix_c_context * context, Store * store, void * callback, void * user_data)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
auto res = store->ptr->getVersion();
|
||||
return call_nix_observe_string(res.value_or(""), callback, user_data);
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
return store->ptr->isValidPath(path->path);
|
||||
}
|
||||
NIXC_CATCH_ERRS_RES(false);
|
||||
}
|
||||
|
||||
StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path)
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
nix::StorePath s = store->ptr->parseStorePath(path);
|
||||
return new StorePath{std::move(s)};
|
||||
}
|
||||
NIXC_CATCH_ERRS_NULL
|
||||
}
|
||||
|
||||
nix_err nix_store_realise(
|
||||
nix_c_context * context,
|
||||
Store * store,
|
||||
StorePath * path,
|
||||
void * userdata,
|
||||
void (*callback)(void * userdata, const char *, const char *))
|
||||
{
|
||||
if (context)
|
||||
context->last_err_code = NIX_OK;
|
||||
try {
|
||||
|
||||
const std::vector<nix::DerivedPath> paths{nix::DerivedPath::Built{
|
||||
.drvPath = nix::makeConstantStorePathRef(path->path), .outputs = nix::OutputsSpec::All{}}};
|
||||
|
||||
const auto nixStore = store->ptr;
|
||||
auto results = nixStore->buildPathsWithResults(paths, nix::bmNormal, nixStore);
|
||||
|
||||
if (callback) {
|
||||
for (const auto & result : results) {
|
||||
for (const auto & [outputName, realisation] : result.builtOutputs) {
|
||||
auto op = store->ptr->printStorePath(realisation.outPath);
|
||||
callback(userdata, outputName.c_str(), op.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
NIXC_CATCH_ERRS
|
||||
}
|
||||
|
||||
void nix_store_path_free(StorePath * sp)
|
||||
{
|
||||
delete sp;
|
||||
}
|
148
src/libstore-c/nix_api_store.h
Normal file
148
src/libstore-c/nix_api_store.h
Normal file
|
@ -0,0 +1,148 @@
|
|||
#ifndef NIX_API_STORE_H
|
||||
#define NIX_API_STORE_H
|
||||
/**
|
||||
* @defgroup libstore libstore
|
||||
* @brief C bindings for nix libstore
|
||||
*
|
||||
* libstore is used for talking to a Nix store
|
||||
* @{
|
||||
*/
|
||||
/** @file
|
||||
* @brief Main entry for the libstore C bindings
|
||||
*/
|
||||
|
||||
#include "nix_api_util.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
// cffi start
|
||||
|
||||
/** @brief Reference to a Nix store */
|
||||
typedef struct Store Store;
|
||||
/** @brief Nix store path */
|
||||
typedef struct StorePath StorePath;
|
||||
|
||||
/**
|
||||
* @brief Initializes the Nix store library
|
||||
*
|
||||
* This function should be called before creating a Store
|
||||
* This function can be called multiple times.
|
||||
*
|
||||
* @param[out] context Optional, stores error information
|
||||
* @return NIX_OK if the initialization was successful, an error code otherwise.
|
||||
*/
|
||||
nix_err nix_libstore_init(nix_c_context * context);
|
||||
|
||||
/**
|
||||
* @brief Loads the plugins specified in Nix's plugin-files setting.
|
||||
*
|
||||
* Call this once, after calling your desired init functions and setting
|
||||
* relevant settings.
|
||||
*
|
||||
* @param[out] context Optional, stores error information
|
||||
* @return NIX_OK if the initialization was successful, an error code otherwise.
|
||||
*/
|
||||
nix_err nix_init_plugins(nix_c_context * context);
|
||||
|
||||
/**
|
||||
* @brief Open a nix store
|
||||
* Store instances may share state and resources behind the scenes.
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] uri URI of the nix store, copied
|
||||
* @param[in] params optional, array of key-value pairs, {{"endpoint",
|
||||
* "https://s3.local"}}
|
||||
* @return a Store pointer, NULL in case of errors
|
||||
* @see nix_store_free
|
||||
*/
|
||||
Store * nix_store_open(nix_c_context *, const char * uri, const char *** params);
|
||||
|
||||
/**
|
||||
* @brief Deallocate a nix store and free any resources if not also held by other Store instances.
|
||||
*
|
||||
* Does not fail.
|
||||
*
|
||||
* @param[in] store the store to free
|
||||
*/
|
||||
void nix_store_free(Store * store);
|
||||
|
||||
/**
|
||||
* @brief get the URI of a 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_observe_string
|
||||
* @return error code, NIX_OK on success.
|
||||
*/
|
||||
nix_err nix_store_get_uri(nix_c_context * context, Store * store, void * callback, void * user_data);
|
||||
|
||||
// returns: owned StorePath*
|
||||
/**
|
||||
* @brief Parse a Nix store path into a StorePath
|
||||
*
|
||||
* @note Don't forget to free this path using nix_store_path_free()!
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] store nix store reference
|
||||
* @param[in] path Path string to parse, copied
|
||||
* @return owned store path, NULL on error
|
||||
*/
|
||||
StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path);
|
||||
|
||||
/** @brief Deallocate a StorePath
|
||||
*
|
||||
* Does not fail.
|
||||
* @param[in] p the path to free
|
||||
*/
|
||||
void nix_store_path_free(StorePath * p);
|
||||
|
||||
/**
|
||||
* @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in
|
||||
* the store)
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] store Nix Store reference
|
||||
* @param[in] path Path to check
|
||||
* @return true or false, error info in context
|
||||
*/
|
||||
bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path);
|
||||
// nix_err nix_store_ensure(Store*, const char*);
|
||||
// nix_err nix_store_build_paths(Store*);
|
||||
/**
|
||||
* @brief Realise a Nix store path
|
||||
*
|
||||
* Blocking, calls callback once for each realised output
|
||||
*
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] store Nix Store reference
|
||||
* @param[in] path Path to build
|
||||
* @param[in] userdata data to pass to every callback invocation
|
||||
* @param[in] callback called for every realised output
|
||||
*/
|
||||
nix_err nix_store_realise(
|
||||
nix_c_context * context,
|
||||
Store * store,
|
||||
StorePath * path,
|
||||
void * userdata,
|
||||
void (*callback)(void * userdata, const char * outname, const char * out));
|
||||
|
||||
/**
|
||||
* @brief get the version of a nix store.
|
||||
* If the store doesn't have a version (like the dummy store), returns an empty string.
|
||||
* @param[out] context Optional, stores error information
|
||||
* @param[in] store nix store reference
|
||||
* @param[in] callback Called with the version.
|
||||
* @param[in] user_data optional, arbitrary data, passed to the callback when it's called.
|
||||
* @see nix_observe_string
|
||||
* @return error code, NIX_OK on success.
|
||||
*/
|
||||
nix_err nix_store_get_version(nix_c_context * context, Store * store, void * callback, void * user_data);
|
||||
|
||||
// cffi end
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
#endif // NIX_API_STORE_H
|
15
src/libstore-c/nix_api_store_internal.h
Normal file
15
src/libstore-c/nix_api_store_internal.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef NIX_API_STORE_INTERNAL_H
|
||||
#define NIX_API_STORE_INTERNAL_H
|
||||
#include "store-api.hh"
|
||||
|
||||
struct Store
|
||||
{
|
||||
nix::ref<nix::Store> ptr;
|
||||
};
|
||||
|
||||
struct StorePath
|
||||
{
|
||||
nix::StorePath path;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue