blob: 703eddb43d73448e7aa451667b68f34456e3ade1 [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
Victor Hsieh2445e332021-06-04 16:44:53 -07007use binder::unstable_api::{new_spibinder, AIBinder};
8use binder::FromIBinder;
Victor Hsieh09e26262021-03-03 16:00:55 -08009use std::io;
10
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070011use crate::common::CHUNK_SIZE;
Victor Hsieh2445e332021-06-04 16:44:53 -070012use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService::IVirtFdService;
Victor Hsieh09e26262021-03-03 16:00:55 -080013use authfs_aidl_interface::binder::{get_interface, Strong};
14
Victor Hsieh2445e332021-06-04 16:44:53 -070015pub type VirtFdService = Strong<dyn IVirtFdService>;
Victor Hsieh09e26262021-03-03 16:00:55 -080016
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070017pub type ChunkBuffer = [u8; CHUNK_SIZE as usize];
18
Victor Hsieh2445e332021-06-04 16:44:53 -070019pub const RPC_SERVICE_PORT: u32 = 3264;
20
21fn get_local_binder() -> io::Result<VirtFdService> {
22 let service_name = "authfs_fd_server";
Chris Wailes68c39f82021-07-27 16:03:44 -070023 get_interface(service_name).map_err(|e| {
Victor Hsieh2445e332021-06-04 16:44:53 -070024 io::Error::new(
25 io::ErrorKind::AddrNotAvailable,
26 format!("Cannot reach authfs_fd_server binder service: {}", e),
27 )
28 })
29}
30
31fn get_rpc_binder(cid: u32) -> io::Result<VirtFdService> {
32 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
33 // safely taken by new_spibinder.
34 let ibinder = unsafe {
35 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, RPC_SERVICE_PORT) as *mut AIBinder)
36 };
37 if let Some(ibinder) = ibinder {
Chris Wailes2acfb0a2021-07-21 11:51:22 -070038 Ok(<dyn IVirtFdService>::try_from(ibinder).map_err(|e| {
Victor Hsieh2445e332021-06-04 16:44:53 -070039 io::Error::new(
40 io::ErrorKind::AddrNotAvailable,
41 format!("Cannot connect to RPC service: {}", e),
42 )
43 })?)
44 } else {
45 Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid raw AIBinder"))
46 }
47}
48
49pub fn get_binder_service(cid: Option<u32>) -> io::Result<VirtFdService> {
50 if let Some(cid) = cid {
51 get_rpc_binder(cid)
52 } else {
53 get_local_binder()
54 }
55}
56
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070057/// A trait for reading data by chunks. Chunks can be read by specifying the chunk index. Only the
58/// last chunk may have incomplete chunk size.
59pub trait ReadByChunk {
60 /// Reads the `chunk_index`-th chunk to a `ChunkBuffer`. Returns the size read, which has to be
61 /// `CHUNK_SIZE` except for the last incomplete chunk. Reading beyond the file size (including
62 /// empty file) should return 0.
63 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize>;
Victor Hsieh09e26262021-03-03 16:00:55 -080064}
65
66/// A trait to write a buffer to the destination at a given offset. The implementation does not
67/// necessarily own or maintain the destination state.
68///
69/// NB: The trait is required in a member of `fusefs::AuthFs`, which is required to be Sync and
70/// immutable (this the member).
71pub trait RandomWrite {
72 /// Writes `buf` to the destination at `offset`. Returns the written size, which may not be the
73 /// full buffer.
74 fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
75
76 /// Writes the full `buf` to the destination at `offset`.
77 fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
78 let mut input_offset = 0;
79 let mut output_offset = offset;
80 while input_offset < buf.len() {
81 let size = self.write_at(&buf[input_offset..], output_offset)?;
82 input_offset += size;
83 output_offset += size as u64;
84 }
85 Ok(())
86 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -070087
88 /// Resizes the file to the new size.
89 fn resize(&self, size: u64) -> io::Result<()>;
Victor Hsieh09e26262021-03-03 16:00:55 -080090}