get and create a FileDescriptor from the fd (#43)

This commit is contained in:
Abdullah Al-Banna
2025-11-30 08:43:28 -08:00
committed by GitHub
parent c1b7009a7b
commit 5f1e03911f
3 changed files with 23 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
// Jackson Coxson
use std::{io::SeekFrom, pin::Pin};
use std::{io::SeekFrom, marker::PhantomPinned, pin::Pin};
use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
@@ -13,8 +13,25 @@ pub struct FileDescriptor<'a> {
}
impl<'a> FileDescriptor<'a> {
pub(crate) fn new(inner: Pin<Box<InnerFileDescriptor<'a>>>) -> Self {
Self { inner }
/// create a new FileDescriptor from a raw fd
///
/// # Safety
/// make sure the fd is an opened file, and that you got it from a previous
/// FileDescriptor via `as_raw_fd()` method
pub unsafe fn new(client: &'a mut super::AfcClient, fd: u64, path: String) -> Self {
Self {
inner: Box::pin(InnerFileDescriptor {
client,
fd,
path,
pending_fut: None,
_m: PhantomPinned,
}),
}
}
pub fn as_raw_fd(&self) -> u64 {
self.inner.fd
}
}
impl FileDescriptor<'_> {

View File

@@ -44,17 +44,6 @@ pub(crate) struct InnerFileDescriptor<'a> {
pub(crate) _m: std::marker::PhantomPinned,
}
impl<'a> InnerFileDescriptor<'a> {
pub(crate) fn new(client: &'a mut super::AfcClient, fd: u64, path: String) -> Pin<Box<Self>> {
Box::pin(Self {
client,
fd,
path,
pending_fut: None,
_m: std::marker::PhantomPinned,
})
}
}
impl InnerFileDescriptor<'_> {
/// Generic helper to send an AFC packet and read the response
pub async fn send_packet(

View File

@@ -410,9 +410,9 @@ impl AfcClient {
return Err(IdeviceError::UnexpectedResponse);
}
let fd = u64::from_le_bytes(res.header_payload[..8].try_into().unwrap());
Ok(FileDescriptor::new(inner_file::InnerFileDescriptor::new(
self, fd, path,
)))
// we know it's a valid fd
Ok(unsafe { FileDescriptor::new(self, fd, path) })
}
/// Creates a hard or symbolic link