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; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 5 | pub use remote_file::{RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader}; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 6 | |
| 7 | use std::io; |
| 8 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 9 | use crate::common::CHUNK_SIZE; |
| 10 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 11 | use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService; |
| 12 | use authfs_aidl_interface::binder::{get_interface, Strong}; |
| 13 | |
| 14 | // TODO(victorhsieh): use remote binder. |
| 15 | pub fn get_local_binder() -> Strong<dyn IVirtFdService::IVirtFdService> { |
| 16 | let service_name = "authfs_fd_server"; |
| 17 | get_interface(&service_name).expect("Cannot reach authfs_fd_server binder service") |
| 18 | } |
| 19 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 20 | pub type ChunkBuffer = [u8; CHUNK_SIZE as usize]; |
| 21 | |
| 22 | /// A trait for reading data by chunks. Chunks can be read by specifying the chunk index. Only the |
| 23 | /// last chunk may have incomplete chunk size. |
| 24 | pub trait ReadByChunk { |
| 25 | /// Reads the `chunk_index`-th chunk to a `ChunkBuffer`. Returns the size read, which has to be |
| 26 | /// `CHUNK_SIZE` except for the last incomplete chunk. Reading beyond the file size (including |
| 27 | /// empty file) should return 0. |
| 28 | fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize>; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /// A trait to write a buffer to the destination at a given offset. The implementation does not |
| 32 | /// necessarily own or maintain the destination state. |
| 33 | /// |
| 34 | /// NB: The trait is required in a member of `fusefs::AuthFs`, which is required to be Sync and |
| 35 | /// immutable (this the member). |
| 36 | pub trait RandomWrite { |
| 37 | /// Writes `buf` to the destination at `offset`. Returns the written size, which may not be the |
| 38 | /// full buffer. |
| 39 | fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>; |
| 40 | |
| 41 | /// Writes the full `buf` to the destination at `offset`. |
| 42 | fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> { |
| 43 | let mut input_offset = 0; |
| 44 | let mut output_offset = offset; |
| 45 | while input_offset < buf.len() { |
| 46 | let size = self.write_at(&buf[input_offset..], output_offset)?; |
| 47 | input_offset += size; |
| 48 | output_offset += size as u64; |
| 49 | } |
| 50 | Ok(()) |
| 51 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame^] | 52 | |
| 53 | /// Resizes the file to the new size. |
| 54 | fn resize(&self, size: u64) -> io::Result<()>; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 55 | } |