blob: 9ff8ea59fbb927cfc8f722565eb86ca9d24dad64 [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
Victor Hsiehd0bb5d32021-03-19 12:48:03 -07009use crate::common::CHUNK_SIZE;
10
Victor Hsieh09e26262021-03-03 16:00:55 -080011use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService;
12use authfs_aidl_interface::binder::{get_interface, Strong};
13
14// TODO(victorhsieh): use remote binder.
15pub 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 Hsiehd0bb5d32021-03-19 12:48:03 -070020pub 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.
24pub 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 Hsieh09e26262021-03-03 16:00:55 -080029}
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).
36pub 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 }
52}