2023-07-14 16:53:01 +03:00
|
|
|
#include "nix_api_store.h"
|
|
|
|
#include "nix_api_store_internal.h"
|
|
|
|
#include "nix_api_util.h"
|
|
|
|
#include "nix_api_util_internal.h"
|
|
|
|
|
2023-08-28 18:27:05 +03:00
|
|
|
#include "path.hh"
|
2023-07-14 16:53:01 +03:00
|
|
|
#include "store-api.hh"
|
2024-02-29 17:32:49 +02:00
|
|
|
#include "build-result.hh"
|
2023-07-14 16:53:01 +03:00
|
|
|
|
|
|
|
#include "globals.hh"
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
nix_err nix_libstore_init(nix_c_context * context)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
context->last_err_code = NIX_OK;
|
|
|
|
try {
|
|
|
|
nix::initLibStore();
|
|
|
|
}
|
|
|
|
NIXC_CATCH_ERRS
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
nix_err nix_init_plugins(nix_c_context * context)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
context->last_err_code = NIX_OK;
|
|
|
|
try {
|
|
|
|
nix::initPlugins();
|
|
|
|
}
|
|
|
|
NIXC_CATCH_ERRS
|
2023-08-07 16:54:46 +03:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
Store * nix_store_open(nix_c_context * context, const char * uri, const char *** params)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
context->last_err_code = NIX_OK;
|
|
|
|
try {
|
2024-01-11 23:41:57 +02:00
|
|
|
std::string uri_str = uri ? uri : "";
|
|
|
|
|
|
|
|
if (uri_str.empty())
|
2023-08-28 17:45:02 +03:00
|
|
|
return new Store{nix::openStore()};
|
2024-01-11 23:41:57 +02:00
|
|
|
|
|
|
|
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];
|
2023-08-28 17:45:02 +03:00
|
|
|
}
|
2024-01-11 23:41:57 +02:00
|
|
|
return new Store{nix::openStore(uri_str, params_map)};
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
2023-08-28 17:45:02 +03:00
|
|
|
NIXC_CATCH_ERRS_NULL
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2024-02-25 15:20:57 +02:00
|
|
|
void nix_store_free(Store * store)
|
2023-08-28 17:45:02 +03:00
|
|
|
{
|
|
|
|
delete store;
|
|
|
|
}
|
2023-07-14 16:53:01 +03:00
|
|
|
|
2024-04-15 13:05:57 +03:00
|
|
|
nix_err nix_store_get_uri(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data)
|
2023-08-28 17:45:02 +03:00
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
context->last_err_code = NIX_OK;
|
|
|
|
try {
|
|
|
|
auto res = store->ptr->getUri();
|
2024-03-29 11:01:16 +02:00
|
|
|
return call_nix_get_string_callback(res, callback, user_data);
|
2023-08-28 17:45:02 +03:00
|
|
|
}
|
|
|
|
NIXC_CATCH_ERRS
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2024-04-15 13:05:57 +03:00
|
|
|
nix_err
|
|
|
|
nix_store_get_version(nix_c_context * context, Store * store, nix_get_string_callback callback, void * user_data)
|
2023-08-28 17:45:02 +03:00
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
context->last_err_code = NIX_OK;
|
|
|
|
try {
|
|
|
|
auto res = store->ptr->getVersion();
|
2024-03-29 11:01:16 +02:00
|
|
|
return call_nix_get_string_callback(res.value_or(""), callback, user_data);
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
2023-08-28 17:45:02 +03:00
|
|
|
NIXC_CATCH_ERRS
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
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);
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
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
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2024-02-27 19:09:38 +02:00
|
|
|
nix_err nix_store_realise(
|
2023-08-28 17:45:02 +03:00
|
|
|
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 {
|
2024-02-29 17:32:49 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
if (callback) {
|
2024-02-29 17:32:49 +02:00
|
|
|
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());
|
|
|
|
}
|
2023-08-28 17:45:02 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
2023-08-28 17:45:02 +03:00
|
|
|
NIXC_CATCH_ERRS
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2024-04-15 13:05:57 +03:00
|
|
|
void nix_store_path_name(const StorePath * store_path, nix_get_string_callback callback, void * user_data)
|
2024-04-08 14:14:03 +03:00
|
|
|
{
|
|
|
|
std::string_view name = store_path->path.name();
|
2024-04-15 13:05:57 +03:00
|
|
|
callback(name.data(), name.size(), user_data);
|
2024-04-08 14:14:03 +03:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
void nix_store_path_free(StorePath * sp)
|
|
|
|
{
|
|
|
|
delete sp;
|
|
|
|
}
|
2024-04-08 11:34:58 +03:00
|
|
|
|
|
|
|
StorePath * nix_store_path_clone(const StorePath * p)
|
|
|
|
{
|
|
|
|
return new StorePath{p->path};
|
|
|
|
}
|