• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 mod local_file;
2 mod remote_file;
3 
4 pub use local_file::LocalFileReader;
5 pub use remote_file::{RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader};
6 
7 use std::io;
8 
9 use crate::common::CHUNK_SIZE;
10 
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.
get_local_binder() -> Strong<dyn IVirtFdService::IVirtFdService>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 
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.
read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize>28     fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize>;
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.
write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>39     fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
40 
41     /// Writes the full `buf` to the destination at `offset`.
write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()>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 
53     /// Resizes the file to the new size.
resize(&self, size: u64) -> io::Result<()>54     fn resize(&self, size: u64) -> io::Result<()>;
55 }
56