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 | 35dfa1e | 2022-01-12 17:03:35 -0800 | [diff] [blame] | 9 | use crate::common::{divide_roundup, CHUNK_SIZE}; |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 10 | use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService::IVirtFdService; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 11 | use authfs_aidl_interface::binder::{Status, Strong}; |
Andrew Walbran | c4ce787 | 2022-07-29 11:26:41 +0000 | [diff] [blame^] | 12 | use binder::StatusCode; |
| 13 | use binder_common::rpc_client::connect_rpc_binder; |
| 14 | use std::convert::TryFrom; |
| 15 | use std::io; |
| 16 | use std::path::{Path, MAIN_SEPARATOR}; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 17 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 18 | pub type VirtFdService = Strong<dyn IVirtFdService>; |
Victor Hsieh | 4563623 | 2021-10-15 17:52:51 -0700 | [diff] [blame] | 19 | pub type VirtFdServiceStatus = Status; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 20 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 21 | pub type ChunkBuffer = [u8; CHUNK_SIZE as usize]; |
| 22 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 23 | pub const RPC_SERVICE_PORT: u32 = 3264; |
| 24 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 25 | pub fn get_rpc_binder_service(cid: u32) -> io::Result<VirtFdService> { |
Andrew Walbran | c4ce787 | 2022-07-29 11:26:41 +0000 | [diff] [blame^] | 26 | connect_rpc_binder(cid, RPC_SERVICE_PORT).map_err(|e| match e { |
| 27 | StatusCode::BAD_VALUE => { |
| 28 | io::Error::new(io::ErrorKind::InvalidInput, "Invalid raw AIBinder") |
| 29 | } |
| 30 | _ => io::Error::new( |
| 31 | io::ErrorKind::AddrNotAvailable, |
| 32 | format!("Cannot connect to RPC service: {}", e), |
| 33 | ), |
| 34 | }) |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 37 | /// 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. |
| 39 | pub 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 Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 44 | } |
| 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). |
| 51 | pub 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 Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 67 | |
| 68 | /// Resizes the file to the new size. |
| 69 | fn resize(&self, size: u64) -> io::Result<()>; |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 70 | } |
Victor Hsieh | 4d6b9d4 | 2021-11-08 15:53:49 -0800 | [diff] [blame] | 71 | |
| 72 | /// Checks whether the path is a simple file name without any directory separator. |
| 73 | pub fn validate_basename(path: &Path) -> io::Result<()> { |
| 74 | if matches!(path.to_str(), Some(path_str) if !path_str.contains(MAIN_SEPARATOR)) { |
| 75 | Ok(()) |
| 76 | } else { |
| 77 | Err(io::Error::from_raw_os_error(libc::EINVAL)) |
| 78 | } |
| 79 | } |
Victor Hsieh | 35dfa1e | 2022-01-12 17:03:35 -0800 | [diff] [blame] | 80 | |
| 81 | pub struct EagerChunkReader { |
| 82 | buffer: Vec<u8>, |
| 83 | } |
| 84 | |
| 85 | impl EagerChunkReader { |
| 86 | pub fn new<F: ReadByChunk>(chunked_file: F, file_size: u64) -> io::Result<EagerChunkReader> { |
| 87 | let last_index = divide_roundup(file_size, CHUNK_SIZE); |
| 88 | let file_size = usize::try_from(file_size).unwrap(); |
| 89 | let mut buffer = Vec::with_capacity(file_size); |
| 90 | let mut chunk_buffer = [0; CHUNK_SIZE as usize]; |
| 91 | for index in 0..last_index { |
| 92 | let size = chunked_file.read_chunk(index, &mut chunk_buffer)?; |
| 93 | buffer.extend_from_slice(&chunk_buffer[..size]); |
| 94 | } |
| 95 | if buffer.len() < file_size { |
| 96 | Err(io::Error::new( |
| 97 | io::ErrorKind::InvalidData, |
| 98 | format!("Insufficient data size ({} < {})", buffer.len(), file_size), |
| 99 | )) |
| 100 | } else { |
| 101 | Ok(EagerChunkReader { buffer }) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl ReadByChunk for EagerChunkReader { |
| 107 | fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> { |
| 108 | if let Some(chunk) = &self.buffer.chunks(CHUNK_SIZE as usize).nth(chunk_index as usize) { |
| 109 | buf[..chunk.len()].copy_from_slice(chunk); |
| 110 | Ok(chunk.len()) |
| 111 | } else { |
| 112 | Ok(0) // Read beyond EOF is normal |
| 113 | } |
| 114 | } |
| 115 | } |