blob: bbe5e6c19f1037245495e038dc8c51468d57d0e6 [file] [log] [blame]
Victor Hsieh45636232021-10-15 17:52:51 -07001mod remote_dir;
Victor Hsieh09e26262021-03-03 16:00:55 -08002mod remote_file;
3
Victor Hsieh45636232021-10-15 17:52:51 -07004pub use remote_dir::RemoteDirEditor;
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 Hsieh45636232021-10-15 17:52:51 -070013use authfs_aidl_interface::binder::{Status, Strong};
Victor Hsieh09e26262021-03-03 16:00:55 -080014
Victor Hsieh2445e332021-06-04 16:44:53 -070015pub type VirtFdService = Strong<dyn IVirtFdService>;
Victor Hsieh45636232021-10-15 17:52:51 -070016pub type VirtFdServiceStatus = Status;
Victor Hsieh09e26262021-03-03 16:00:55 -080017
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070018pub type ChunkBuffer = [u8; CHUNK_SIZE as usize];
19
Victor Hsieh2445e332021-06-04 16:44:53 -070020pub const RPC_SERVICE_PORT: u32 = 3264;
21
Victor Hsieh1a8cd042021-09-03 16:29:45 -070022pub fn get_rpc_binder_service(cid: u32) -> io::Result<VirtFdService> {
Victor Hsieh2445e332021-06-04 16:44:53 -070023 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
24 // safely taken by new_spibinder.
25 let ibinder = unsafe {
26 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, RPC_SERVICE_PORT) as *mut AIBinder)
27 };
28 if let Some(ibinder) = ibinder {
Chris Wailes2acfb0a2021-07-21 11:51:22 -070029 Ok(<dyn IVirtFdService>::try_from(ibinder).map_err(|e| {
Victor Hsieh2445e332021-06-04 16:44:53 -070030 io::Error::new(
31 io::ErrorKind::AddrNotAvailable,
32 format!("Cannot connect to RPC service: {}", e),
33 )
34 })?)
35 } else {
36 Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid raw AIBinder"))
37 }
38}
39
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070040/// A trait for reading data by chunks. Chunks can be read by specifying the chunk index. Only the
41/// last chunk may have incomplete chunk size.
42pub trait ReadByChunk {
43 /// Reads the `chunk_index`-th chunk to a `ChunkBuffer`. Returns the size read, which has to be
44 /// `CHUNK_SIZE` except for the last incomplete chunk. Reading beyond the file size (including
45 /// empty file) should return 0.
46 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize>;
Victor Hsieh09e26262021-03-03 16:00:55 -080047}
48
49/// A trait to write a buffer to the destination at a given offset. The implementation does not
50/// necessarily own or maintain the destination state.
51///
52/// NB: The trait is required in a member of `fusefs::AuthFs`, which is required to be Sync and
53/// immutable (this the member).
54pub trait RandomWrite {
55 /// Writes `buf` to the destination at `offset`. Returns the written size, which may not be the
56 /// full buffer.
57 fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
58
59 /// Writes the full `buf` to the destination at `offset`.
60 fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
61 let mut input_offset = 0;
62 let mut output_offset = offset;
63 while input_offset < buf.len() {
64 let size = self.write_at(&buf[input_offset..], output_offset)?;
65 input_offset += size;
66 output_offset += size as u64;
67 }
68 Ok(())
69 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -070070
71 /// Resizes the file to the new size.
72 fn resize(&self, size: u64) -> io::Result<()>;
Victor Hsieh09e26262021-03-03 16:00:55 -080073}