use cpp bindings to make nar

This commit is contained in:
cy 2025-04-26 21:08:01 -04:00
parent 0fedae9334
commit 85fefe9e77
Signed by: cy
SSH key fingerprint: SHA256:o/geVWV4om1QhUSkKvDQeW/eAihwnjyXkqMwrVdbuts
7 changed files with 66 additions and 50 deletions

View file

@ -23,6 +23,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use anyhow::Result;
use bytes::Bytes;
use futures::stream::{Stream, StreamExt};
use tokio::io::{AsyncWrite, AsyncWriteExt};
@ -125,7 +126,7 @@ impl AsyncWriteAdapter {
writer.write_all(&v).await?;
}
Err(e) => {
return Err(e);
return Err(e.into());
}
}
}
@ -139,7 +140,7 @@ impl AsyncWriteAdapter {
}
impl Stream for AsyncWriteAdapter {
type Item = Result<Vec<u8>>;
type Item = std::io::Result<Bytes>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.receiver.poll_recv(cx) {
@ -147,9 +148,12 @@ impl Stream for AsyncWriteAdapter {
Poll::Ready(Some(message)) => {
use AsyncWriteMessage::*;
match message {
Data(v) => Poll::Ready(Some(Ok(v))),
Data(v) => Poll::Ready(Some(Ok(v.into()))),
Error(exception) => {
let error = anyhow::Error::msg(format!("cxx error: {exception}"));
let error = std::io::Error::new(
io::ErrorKind::Other,
format!("cxx error: {exception}"),
);
Poll::Ready(Some(Err(error)))
}
Eof => {
@ -160,7 +164,7 @@ impl Stream for AsyncWriteAdapter {
}
Poll::Ready(None) => {
if !self.eof {
Poll::Ready(Some(Err(io::Error::from(io::ErrorKind::BrokenPipe).into())))
Poll::Ready(Some(Err(io::Error::from(io::ErrorKind::BrokenPipe))))
} else {
Poll::Ready(None)
}
@ -208,6 +212,13 @@ mod ffi {
include_derivers: bool,
) -> Result<UniquePtr<CxxVector<CxxString>>>;
/// Creates a NAR dump from a path.
fn nar_from_path(
self: Pin<&mut CNixStore>,
base_name: Vec<u8>,
sender: Box<AsyncWriteSender>,
) -> Result<()>;
/// Obtains a handle to the Nix store.
fn open_nix_store() -> Result<UniquePtr<CNixStore>>;

View file

@ -108,6 +108,17 @@ std::unique_ptr<std::vector<std::string>> CNixStore::compute_fs_closure(RBasePat
return std::make_unique<std::vector<std::string>>(result);
}
void CNixStore::nar_from_path(RVec<unsigned char> base_name, RBox<AsyncWriteSender> sender) {
RustSink sink(std::move(sender));
std::string_view sv((const char *)base_name.data(), base_name.size());
nix::StorePath store_path(sv);
// exceptions will be thrown into Rust
this->store->narFromPath(store_path, sink);
sink.eof();
}
std::unique_ptr<CNixStore> open_nix_store() {
return std::make_unique<CNixStore>();
}

View file

@ -79,6 +79,7 @@ public:
bool flip_direction,
bool include_outputs,
bool include_derivers);
void nar_from_path(RVec<unsigned char> base_name, RBox<AsyncWriteSender> sender);
};
std::unique_ptr<CNixStore> open_nix_store();