blob: 404e3a5a437a3f210d9dff9aee7a31e1fd8fb4c1 [file] [log] [blame]
Victor Hsieh09e26262021-03-03 16:00:55 -08001mod remote_file;
2
Victor Hsieh6a47e7f2021-03-03 15:53:49 -08003pub use remote_file::{RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader};
Victor Hsieh09e26262021-03-03 16:00:55 -08004
Victor Hsieh2445e332021-06-04 16:44:53 -07005use binder::unstable_api::{new_spibinder, AIBinder};
6use binder::FromIBinder;
Victor Hsieh09e26262021-03-03 16:00:55 -08007use std::io;
8
Victor Hsiehd0bb5d32021-03-19 12:48:03 -07009use crate::common::CHUNK_SIZE;
Victor Hsieh2445e332021-06-04 16:44:53 -070010use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService::IVirtFdService;
Victor Hsieh1a8cd042021-09-03 16:29:45 -070011use authfs_aidl_interface::binder::Strong;
Victor Hsieh09e26262021-03-03 16:00:55 -080012
Victor Hsieh2445e332021-06-04 16:44:53 -070013pub type VirtFdService = Strong<dyn IVirtFdService>;
Victor Hsieh09e26262021-03-03 16:00:55 -080014
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070015pub type ChunkBuffer = [u8; CHUNK_SIZE as usize];
16
Victor Hsieh2445e332021-06-04 16:44:53 -070017pub const RPC_SERVICE_PORT: u32 = 3264;
18
Victor Hsieh1a8cd042021-09-03 16:29:45 -070019pub fn get_rpc_binder_service(cid: u32) -> io::Result<VirtFdService> {
Victor Hsieh2445e332021-06-04 16:44:53 -070020 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
21 // safely taken by new_spibinder.
22 let ibinder = unsafe {
23 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, RPC_SERVICE_PORT) as *mut AIBinder)
24 };
25 if let Some(ibinder) = ibinder {
Chris Wailes2acfb0a2021-07-21 11:51:22 -070026 Ok(<dyn IVirtFdService>::try_from(ibinder).map_err(|e| {
Victor Hsieh2445e332021-06-04 16:44:53 -070027 io::Error::new(
28 io::ErrorKind::AddrNotAvailable,
29 format!("Cannot connect to RPC service: {}", e),
30 )
31 })?)
32 } else {
33 Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid raw AIBinder"))
34 }
35}
36
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070037/// A trait for reading data by chunks. Chunks can be read by specifying the chunk index. Only the
38/// last chunk may have incomplete chunk size.
39pub trait ReadByChunk {
40 /// Reads the `chunk_index`-th chunk to a `ChunkBuffer`. Returns the size read, which has to be
41 /// `CHUNK_SIZE` except for the last incomplete chunk. Reading beyond the file size (including
42 /// empty file) should return 0.
43 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize>;
Victor Hsieh09e26262021-03-03 16:00:55 -080044}
45
46/// A trait to write a buffer to the destination at a given offset. The implementation does not
47/// necessarily own or maintain the destination state.
48///
49/// NB: The trait is required in a member of `fusefs::AuthFs`, which is required to be Sync and
50/// immutable (this the member).
51pub trait RandomWrite {
52 /// Writes `buf` to the destination at `offset`. Returns the written size, which may not be the
53 /// full buffer.
54 fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
55
56 /// Writes the full `buf` to the destination at `offset`.
57 fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
58 let mut input_offset = 0;
59 let mut output_offset = offset;
60 while input_offset < buf.len() {
61 let size = self.write_at(&buf[input_offset..], output_offset)?;
62 input_offset += size;
63 output_offset += size as u64;
64 }
65 Ok(())
66 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -070067
68 /// Resizes the file to the new size.
69 fn resize(&self, size: u64) -> io::Result<()>;
Victor Hsieh09e26262021-03-03 16:00:55 -080070}