|
6 | 6 |
|
7 | 7 | use std::ffi::{CStr, CString}; |
8 | 8 | use std::io; |
| 9 | +// TODO: Import from std::os::fd::{} since Rust 1.66 |
| 10 | +use std::os::unix::io::{FromRawFd, OwnedFd}; |
9 | 11 | use std::ptr::NonNull; |
10 | 12 |
|
11 | 13 | /// A native [`AAssetManager *`] |
@@ -224,7 +226,34 @@ impl Asset { |
224 | 226 | } |
225 | 227 | } |
226 | 228 |
|
227 | | - //pub fn open_file_descriptor(&self) -> TODO |
| 229 | + /// Returns whether this asset's internal buffer is allocated in ordinary RAM (i.e. not `mmap`ped). |
| 230 | + #[doc(alias = "AAsset_isAllocated")] |
| 231 | + pub fn is_allocated(&self) -> bool { |
| 232 | + unsafe { ffi::AAsset_isAllocated(self.ptr.as_ptr()) != 0 } |
| 233 | + } |
| 234 | + |
| 235 | + /// Open a new file descriptor that can be used to read the asset data. |
| 236 | + /// |
| 237 | + /// Returns an error if direct fd access is not possible (for example, if the asset is compressed). |
| 238 | + #[doc(alias = "AAsset_openFileDescriptor64")] |
| 239 | + pub fn open_file_descriptor(&self) -> io::Result<OpenedFileDescriptor> { |
| 240 | + let mut offset = 0; |
| 241 | + let mut size = 0; |
| 242 | + let res = |
| 243 | + unsafe { ffi::AAsset_openFileDescriptor64(self.ptr.as_ptr(), &mut offset, &mut size) }; |
| 244 | + if res >= 0 { |
| 245 | + Ok(OpenedFileDescriptor { |
| 246 | + fd: unsafe { OwnedFd::from_raw_fd(res) }, |
| 247 | + offset: offset as usize, |
| 248 | + size: size as usize, |
| 249 | + }) |
| 250 | + } else { |
| 251 | + Err(io::Error::new( |
| 252 | + io::ErrorKind::Other, |
| 253 | + "Android Asset openFileDescriptor error", |
| 254 | + )) |
| 255 | + } |
| 256 | + } |
228 | 257 | } |
229 | 258 |
|
230 | 259 | impl io::Read for Asset { |
@@ -268,3 +297,12 @@ impl io::Seek for Asset { |
268 | 297 | } |
269 | 298 | } |
270 | 299 | } |
| 300 | + |
| 301 | +/// Contains the opened file descriptor returned by [`Asset::open_file_descriptor()`], together |
| 302 | +/// with the offset and size of the given asset within that file descriptor. |
| 303 | +#[derive(Debug)] |
| 304 | +pub struct OpenedFileDescriptor { |
| 305 | + pub fd: OwnedFd, |
| 306 | + pub offset: usize, |
| 307 | + pub size: usize, |
| 308 | +} |
0 commit comments