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