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"
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
if (!uri) {
|
|
|
|
return new Store{nix::openStore()};
|
|
|
|
} else {
|
|
|
|
std::string uri_str = uri;
|
|
|
|
if (!params)
|
|
|
|
return new Store{nix::openStore(uri_str)};
|
2023-07-14 16:53:01 +03:00
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
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)};
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
void nix_store_unref(Store * store)
|
|
|
|
{
|
|
|
|
delete store;
|
|
|
|
}
|
2023-07-14 16:53:01 +03:00
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
context->last_err_code = NIX_OK;
|
|
|
|
try {
|
|
|
|
auto res = store->ptr->getUri();
|
|
|
|
return nix_export_std_string(context, res, dest, n);
|
|
|
|
}
|
|
|
|
NIXC_CATCH_ERRS
|
2023-07-14 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
nix_err nix_store_get_version(nix_c_context * context, Store * store, char * dest, unsigned int n)
|
|
|
|
{
|
|
|
|
if (context)
|
|
|
|
context->last_err_code = NIX_OK;
|
|
|
|
try {
|
|
|
|
auto res = store->ptr->getVersion();
|
|
|
|
if (res) {
|
|
|
|
return nix_export_std_string(context, *res, dest, n);
|
|
|
|
} else {
|
|
|
|
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "store does not have a version");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-08-28 17:45:02 +03:00
|
|
|
nix_err nix_store_build(
|
|
|
|
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 {
|
|
|
|
store->ptr->buildPaths({
|
|
|
|
nix::DerivedPath::Built{
|
2023-08-28 18:27:05 +03:00
|
|
|
.drvPath = nix::makeConstantStorePathRef(path->path),
|
2023-08-28 17:45:02 +03:00
|
|
|
.outputs = nix::OutputsSpec::All{},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (callback) {
|
|
|
|
for (auto & [outputName, outputPath] : store->ptr->queryDerivationOutputMap(path->path)) {
|
|
|
|
auto op = store->ptr->printStorePath(outputPath);
|
|
|
|
callback(userdata, outputName.c_str(), op.c_str());
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
void nix_store_path_free(StorePath * sp)
|
|
|
|
{
|
|
|
|
delete sp;
|
|
|
|
}
|