Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame^] | 1 | mod dir; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 2 | mod remote_file; |
| 3 | |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame^] | 4 | pub use dir::{InMemoryDir, RemoteDirEditor}; |
Victor Hsieh | 6a47e7f | 2021-03-03 15:53:49 -0800 | [diff] [blame] | 5 | pub use remote_file::{RemoteFileEditor, RemoteFileReader, RemoteMerkleTreeReader}; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 6 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 7 | use binder::unstable_api::{new_spibinder, AIBinder}; |
| 8 | use binder::FromIBinder; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 9 | use std::io; |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame^] | 10 | use std::path::{Path, MAIN_SEPARATOR}; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 11 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 12 | use crate::common::CHUNK_SIZE; |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 13 | use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService::IVirtFdService; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 14 | use authfs_aidl_interface::binder::{Status, Strong}; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 15 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 16 | pub type VirtFdService = Strong<dyn IVirtFdService>; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 17 | pub type VirtFdServiceStatus = Status; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 18 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 19 | pub type ChunkBuffer = [u8; CHUNK_SIZE as usize]; |
| 20 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 21 | pub const RPC_SERVICE_PORT: u32 = 3264; |
| 22 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 23 | pub fn get_rpc_binder_service(cid: u32) -> io::Result<VirtFdService> { |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 24 | // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be |
| 25 | // safely taken by new_spibinder. |
| 26 | let ibinder = unsafe { |
| 27 | new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, RPC_SERVICE_PORT) as *mut AIBinder) |
| 28 | }; |
| 29 | if let Some(ibinder) = ibinder { |
Chris Wailes | 2acfb0a | 2021-07-21 11:51:22 -0700 | [diff] [blame] | 30 | Ok(<dyn IVirtFdService>::try_from(ibinder).map_err(|e| { |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 31 | io::Error::new( |
| 32 | io::ErrorKind::AddrNotAvailable, |
| 33 | format!("Cannot connect to RPC service: {}", e), |
| 34 | ) |
| 35 | })?) |
| 36 | } else { |
| 37 | Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid raw AIBinder")) |
| 38 | } |
| 39 | } |
| 40 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 41 | /// A trait for reading data by chunks. Chunks can be read by specifying the chunk index. Only the |
| 42 | /// last chunk may have incomplete chunk size. |
| 43 | pub trait ReadByChunk { |
| 44 | /// Reads the `chunk_index`-th chunk to a `ChunkBuffer`. Returns the size read, which has to be |
| 45 | /// `CHUNK_SIZE` except for the last incomplete chunk. Reading beyond the file size (including |
| 46 | /// empty file) should return 0. |
| 47 | fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize>; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /// A trait to write a buffer to the destination at a given offset. The implementation does not |
| 51 | /// necessarily own or maintain the destination state. |
| 52 | /// |
| 53 | /// NB: The trait is required in a member of `fusefs::AuthFs`, which is required to be Sync and |
| 54 | /// immutable (this the member). |
| 55 | pub trait RandomWrite { |
| 56 | /// Writes `buf` to the destination at `offset`. Returns the written size, which may not be the |
| 57 | /// full buffer. |
| 58 | fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>; |
| 59 | |
| 60 | /// Writes the full `buf` to the destination at `offset`. |
| 61 | fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> { |
| 62 | let mut input_offset = 0; |
| 63 | let mut output_offset = offset; |
| 64 | while input_offset < buf.len() { |
| 65 | let size = self.write_at(&buf[input_offset..], output_offset)?; |
| 66 | input_offset += size; |
| 67 | output_offset += size as u64; |
| 68 | } |
| 69 | Ok(()) |
| 70 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 71 | |
| 72 | /// Resizes the file to the new size. |
| 73 | fn resize(&self, size: u64) -> io::Result<()>; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 74 | } |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame^] | 75 | |
| 76 | /// Checks whether the path is a simple file name without any directory separator. |
| 77 | pub fn validate_basename(path: &Path) -> io::Result<()> { |
| 78 | if matches!(path.to_str(), Some(path_str) if !path_str.contains(MAIN_SEPARATOR)) { |
| 79 | Ok(()) |
| 80 | } else { |
| 81 | Err(io::Error::from_raw_os_error(libc::EINVAL)) |
| 82 | } |
| 83 | } |