blob: 89fbd9d5c74934ee5d03b44bf8c0b41fd5ec16ae [file] [log] [blame]
Victor Hsieh09e26262021-03-03 16:00:55 -08001mod local_file;
2mod remote_file;
3
4pub use local_file::LocalFileReader;
Victor Hsieh6a47e7f2021-03-03 15:53:49 -08005pub use remote_file::{RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader};
Victor Hsieh09e26262021-03-03 16:00:55 -08006
7use std::io;
8
9use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService;
10use authfs_aidl_interface::binder::{get_interface, Strong};
11
12// TODO(victorhsieh): use remote binder.
13pub 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.
20pub 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).
33pub 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}