From 13be1ae377f56cf489340544ff10b654032247e9 Mon Sep 17 00:00:00 2001 From: Jackson Coxson Date: Mon, 5 Jan 2026 06:55:11 -0700 Subject: [PATCH] Add read_entire to FFI a --- ffi/examples/afc.c | 2 +- ffi/src/afc.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/ffi/examples/afc.c b/ffi/examples/afc.c index c2ac39e..a6edfb4 100644 --- a/ffi/examples/afc.c +++ b/ffi/examples/afc.c @@ -254,7 +254,7 @@ int main(int argc, char **argv) { } else { uint8_t *data = NULL; size_t length = 0; - err = afc_file_read(file, &data, &length); + err = afc_file_read_entire(file, &data, &length); if (err == NULL) { if (write_file(dest_path, data, length)) { printf("File downloaded successfully\n"); diff --git a/ffi/src/afc.rs b/ffi/src/afc.rs index 549ab7a..8a3d692 100644 --- a/ffi/src/afc.rs +++ b/ffi/src/afc.rs @@ -604,6 +604,45 @@ pub unsafe extern "C" fn afc_file_read( } } +/// Reads all data from an open file. +/// +/// # Arguments +/// * [`handle`] - File handle to read from +/// * [`data`] - Will be set to point to the read data +/// * [`length`] - The number of bytes read from the file +/// +/// # Returns +/// An IdeviceFfiError on error, null on success +/// +/// # Safety +/// All pointers must be valid and non-null +#[unsafe(no_mangle)] +pub unsafe extern "C" fn afc_file_read_entire( + handle: *mut AfcFileHandle, + data: *mut *mut u8, + length: *mut libc::size_t, +) -> *mut IdeviceFfiError { + if handle.is_null() || data.is_null() || length.is_null() { + return ffi_err!(IdeviceError::FfiInvalidArg); + } + + let fd = unsafe { &mut *(handle as *mut idevice::afc::file::FileDescriptor) }; + let res: Result, IdeviceError> = run_sync(async move { fd.read_entire().await }); + + match res { + Ok(bytes) => { + let mut boxed = bytes.into_boxed_slice(); + unsafe { + *data = boxed.as_mut_ptr(); + *length = boxed.len(); + } + std::mem::forget(boxed); + null_mut() + } + Err(e) => ffi_err!(e), + } +} + /// Moves the read/write cursor in an open file. /// /// # Arguments