Skip to content

Commit 6ca2efc

Browse files
authored
Merge pull request #14254 from roberth/upstream-RossComputerGuy/feat/expose-computefsclosure
libstore-c: add nix_store_get_fs_closure #14025 with tests and realise error fix
2 parents a4a49a9 + 6036aaf commit 6ca2efc

File tree

3 files changed

+607
-1
lines changed

3 files changed

+607
-1
lines changed

src/libstore-c/nix_api_store.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@ StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const c
126126
NIXC_CATCH_ERRS_NULL
127127
}
128128

129+
nix_err nix_store_get_fs_closure(
130+
nix_c_context * context,
131+
Store * store,
132+
const StorePath * store_path,
133+
bool flip_direction,
134+
bool include_outputs,
135+
bool include_derivers,
136+
void * userdata,
137+
void (*callback)(nix_c_context * context, void * userdata, const StorePath * store_path))
138+
{
139+
if (context)
140+
context->last_err_code = NIX_OK;
141+
try {
142+
const auto nixStore = store->ptr;
143+
144+
nix::StorePathSet set;
145+
nixStore->computeFSClosure(store_path->path, set, flip_direction, include_outputs, include_derivers);
146+
147+
if (callback) {
148+
for (const auto & path : set) {
149+
const StorePath tmp{path};
150+
callback(context, userdata, &tmp);
151+
if (context && context->last_err_code != NIX_OK)
152+
return context->last_err_code;
153+
}
154+
}
155+
}
156+
NIXC_CATCH_ERRS
157+
}
158+
129159
nix_err nix_store_realise(
130160
nix_c_context * context,
131161
Store * store,
@@ -143,6 +173,14 @@ nix_err nix_store_realise(
143173
const auto nixStore = store->ptr;
144174
auto results = nixStore->buildPathsWithResults(paths, nix::bmNormal, nixStore);
145175

176+
assert(results.size() == 1);
177+
178+
// Check if any builds failed
179+
for (auto & result : results) {
180+
if (auto * failureP = result.tryGetFailure())
181+
failureP->rethrow();
182+
}
183+
146184
if (callback) {
147185
for (const auto & result : results) {
148186
if (auto * success = result.tryGetSuccess()) {

src/libstore-c/nix_api_store.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ nix_err nix_store_real_path(
186186
* @param[in] path Path to build
187187
* @param[in] userdata data to pass to every callback invocation
188188
* @param[in] callback called for every realised output
189+
* @return NIX_OK if the build succeeded, or an error code if the build/scheduling/outputs/copying/etc failed.
190+
* On error, the callback is never invoked and error information is stored in context.
189191
*/
190192
nix_err nix_store_realise(
191193
nix_c_context * context,
@@ -245,6 +247,35 @@ void nix_derivation_free(nix_derivation * drv);
245247
*/
246248
nix_err nix_store_copy_closure(nix_c_context * context, Store * srcStore, Store * dstStore, StorePath * path);
247249

250+
/**
251+
* @brief Gets the closure of a specific store path
252+
*
253+
* @note The callback borrows each StorePath only for the duration of the call.
254+
*
255+
* @param[out] context Optional, stores error information
256+
* @param[in] store nix store reference
257+
* @param[in] store_path The path to compute from
258+
* @param[in] flip_direction If false, compute the forward closure (paths referenced by any store path in the closure).
259+
* If true, compute the backward closure (paths that reference any store path in the closure).
260+
* @param[in] include_outputs If flip_direction is false: for any derivation in the closure, include its outputs.
261+
* If flip_direction is true: for any output in the closure, include derivations that produce
262+
* it.
263+
* @param[in] include_derivers If flip_direction is false: for any output in the closure, include the derivation that
264+
* produced it.
265+
* If flip_direction is true: for any derivation in the closure, include its outputs.
266+
* @param[in] callback The function to call for every store path, in no particular order
267+
* @param[in] userdata The userdata to pass to the callback
268+
*/
269+
nix_err nix_store_get_fs_closure(
270+
nix_c_context * context,
271+
Store * store,
272+
const StorePath * store_path,
273+
bool flip_direction,
274+
bool include_outputs,
275+
bool include_derivers,
276+
void * userdata,
277+
void (*callback)(nix_c_context * context, void * userdata, const StorePath * store_path));
278+
248279
// cffi end
249280
#ifdef __cplusplus
250281
}

0 commit comments

Comments
 (0)