Skip to content

Commit 3c7576b

Browse files
domenkozarclaude
andcommitted
libstore-c: Add C FFI for computing filesystem closures
Adds nix_store_compute_fs_closure to compute the set of all paths reachable from a given set of store paths through references. This provides the foundational API needed for closure-based GC operations. Includes: - nix_store_path_callback typedef for result iteration - nix_store_compute_fs_closure function with options for: - Reverse dependency computation (flip_direction) - Including derivation outputs - Including derivers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e7d4064 commit 3c7576b

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/libstore-c/nix_api_store.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "nix/store/local-fs-store.hh"
1111
#include "nix/store/indirect-root-store.hh"
1212
#include "nix/store/local-store.hh"
13+
#include "nix/store/gc-store.hh"
1314

1415
#include "nix/store/globals.hh"
1516

@@ -341,4 +342,51 @@ nix_err nix_store_delete_path(nix_c_context * context, Store * store, const char
341342
NIXC_CATCH_ERRS
342343
}
343344

345+
nix_err nix_store_compute_fs_closure(
346+
nix_c_context * context,
347+
Store * store,
348+
StorePath ** paths,
349+
size_t num_paths,
350+
bool flip_direction,
351+
bool include_outputs,
352+
bool include_derivers,
353+
nix_store_path_callback callback,
354+
void * user_data)
355+
{
356+
if (context)
357+
context->last_err_code = NIX_OK;
358+
try {
359+
if (!store)
360+
return context ? context->last_err_code = NIX_ERR_KEY : NIX_ERR_KEY;
361+
362+
if (num_paths == 0 || !paths) {
363+
// Empty input, nothing to do
364+
return NIX_OK;
365+
}
366+
367+
// Convert StorePath** array to StorePathSet
368+
nix::StorePathSet startPaths;
369+
for (size_t i = 0; i < num_paths; i++) {
370+
if (!paths[i])
371+
return context ? context->last_err_code = NIX_ERR_KEY : NIX_ERR_KEY;
372+
startPaths.insert(paths[i]->path);
373+
}
374+
375+
// Compute the closure
376+
nix::StorePathSet closure;
377+
store->ptr->computeFSClosure(startPaths, closure, flip_direction, include_outputs, include_derivers);
378+
379+
// Invoke callback for each path in the closure
380+
if (callback) {
381+
for (const auto & path : closure) {
382+
StorePath sp{path};
383+
callback(&sp, user_data);
384+
}
385+
}
386+
387+
return NIX_OK;
388+
}
389+
NIXC_CATCH_ERRS
390+
}
391+
344392
} // extern "C"

src/libstore-c/nix_api_store.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "nix_api_util.h"
1515
#include <stdbool.h>
16+
#include <stddef.h>
1617
#include <stdint.h>
1718

1819
#ifdef __cplusplus
@@ -375,6 +376,45 @@ nix_err nix_store_add_indirect_root(nix_c_context * context, Store * store, cons
375376
*/
376377
nix_err nix_store_delete_path(nix_c_context * context, Store * store, const char * path, uint64_t * bytes_freed);
377378

379+
/**
380+
* @brief Callback for iterating over store paths
381+
*
382+
* Called once for each store path in a result set.
383+
*
384+
* @param[in] path The store path
385+
* @param[in] user_data User-provided data passed to the function
386+
*/
387+
typedef void (*nix_store_path_callback)(const StorePath * path, void * user_data);
388+
389+
/**
390+
* @brief Compute the filesystem closure of store paths.
391+
*
392+
* The closure is the set of all paths reachable from the input paths
393+
* through references. This function is useful for determining all dependencies
394+
* of given paths before performing operations on them.
395+
*
396+
* @param[out] context Optional, stores error information
397+
* @param[in] store Nix Store reference
398+
* @param[in] paths Array of starting store paths (cannot be NULL if num_paths > 0)
399+
* @param[in] num_paths Number of paths in the array
400+
* @param[in] flip_direction If true, compute reverse dependencies (dependents) instead of forward
401+
* @param[in] include_outputs If true, include outputs of derivations
402+
* @param[in] include_derivers If true, include derivers of store paths
403+
* @param[in] callback Function called for each path in the computed closure
404+
* @param[in] user_data Arbitrary data passed to the callback
405+
* @return NIX_OK on success, error code on failure
406+
*/
407+
nix_err nix_store_compute_fs_closure(
408+
nix_c_context * context,
409+
Store * store,
410+
StorePath ** paths,
411+
size_t num_paths,
412+
bool flip_direction,
413+
bool include_outputs,
414+
bool include_derivers,
415+
nix_store_path_callback callback,
416+
void * user_data);
417+
378418
// cffi end
379419
#ifdef __cplusplus
380420
}

0 commit comments

Comments
 (0)