Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame^] | 1 | mod local_file; |
| 2 | mod remote_file; |
| 3 | |
| 4 | pub use local_file::LocalFileReader; |
| 5 | pub use remote_file::{RemoteFileReader, RemoteMerkleTreeReader}; |
| 6 | |
| 7 | use std::io; |
| 8 | |
| 9 | use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService; |
| 10 | use authfs_aidl_interface::binder::{get_interface, Strong}; |
| 11 | |
| 12 | // TODO(victorhsieh): use remote binder. |
| 13 | pub fn get_local_binder() -> Strong<dyn IVirtFdService::IVirtFdService> { |
| 14 | let service_name = "authfs_fd_server"; |
| 15 | get_interface(&service_name).expect("Cannot reach authfs_fd_server binder service") |
| 16 | } |
| 17 | |
| 18 | /// A trait for reading data by chunks. The data is assumed readonly and has fixed length. Chunks |
| 19 | /// can be read by specifying the chunk index. Only the last chunk may have incomplete chunk size. |
| 20 | pub trait ReadOnlyDataByChunk { |
| 21 | /// Read the `chunk_index`-th chunk to `buf`. Each slice/chunk has size `CHUNK_SIZE` except for |
| 22 | /// the last one, which can be an incomplete chunk. `buf` is currently required to be large |
| 23 | /// enough to hold a full chunk of data. Reading beyond the file size (including empty file) |
| 24 | /// will crash. |
| 25 | fn read_chunk(&self, chunk_index: u64, buf: &mut [u8]) -> io::Result<usize>; |
| 26 | } |
| 27 | |
| 28 | /// A trait to write a buffer to the destination at a given offset. The implementation does not |
| 29 | /// necessarily own or maintain the destination state. |
| 30 | /// |
| 31 | /// NB: The trait is required in a member of `fusefs::AuthFs`, which is required to be Sync and |
| 32 | /// immutable (this the member). |
| 33 | pub trait RandomWrite { |
| 34 | /// Writes `buf` to the destination at `offset`. Returns the written size, which may not be the |
| 35 | /// full buffer. |
| 36 | fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>; |
| 37 | |
| 38 | /// Writes the full `buf` to the destination at `offset`. |
| 39 | fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> { |
| 40 | let mut input_offset = 0; |
| 41 | let mut output_offset = offset; |
| 42 | while input_offset < buf.len() { |
| 43 | let size = self.write_at(&buf[input_offset..], output_offset)?; |
| 44 | input_offset += size; |
| 45 | output_offset += size as u64; |
| 46 | } |
| 47 | Ok(()) |
| 48 | } |
| 49 | } |