Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 17 | use std::cmp::min; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 18 | use std::convert::TryFrom; |
| 19 | use std::io; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 20 | use std::sync::{Arc, Mutex}; |
| 21 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 22 | use super::{ChunkBuffer, RandomWrite, ReadByChunk}; |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 23 | use crate::common::CHUNK_SIZE; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 24 | |
| 25 | use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService; |
| 26 | use authfs_aidl_interface::binder::Strong; |
| 27 | |
| 28 | type VirtFdService = Strong<dyn IVirtFdService::IVirtFdService>; |
| 29 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 30 | fn remote_read_chunk( |
| 31 | service: &Arc<Mutex<VirtFdService>>, |
| 32 | remote_fd: i32, |
| 33 | chunk_index: u64, |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 34 | buf: &mut ChunkBuffer, |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 35 | ) -> io::Result<usize> { |
| 36 | let offset = i64::try_from(chunk_index * CHUNK_SIZE) |
| 37 | .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?; |
| 38 | |
| 39 | let chunk = service |
| 40 | .lock() |
| 41 | .unwrap() |
| 42 | .readFile(remote_fd, offset, buf.len() as i32) |
| 43 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?; |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 44 | buf.copy_from_slice(&chunk); |
| 45 | Ok(min(buf.len(), chunk.len())) |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 48 | pub struct RemoteFileReader { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 49 | // This needs to have Sync trait to be used in fuse::worker::start_message_loop. |
| 50 | service: Arc<Mutex<VirtFdService>>, |
| 51 | file_fd: i32, |
| 52 | } |
| 53 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 54 | impl RemoteFileReader { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 55 | pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self { |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 56 | RemoteFileReader { service, file_fd } |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 60 | impl ReadByChunk for RemoteFileReader { |
| 61 | fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 62 | remote_read_chunk(&self.service, self.file_fd, chunk_index, buf) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 66 | pub struct RemoteMerkleTreeReader { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 67 | // This needs to be a Sync to be used in fuse::worker::start_message_loop. |
| 68 | // TODO(victorhsieh): change to Strong<> once binder supports it. |
| 69 | service: Arc<Mutex<VirtFdService>>, |
| 70 | file_fd: i32, |
| 71 | } |
| 72 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 73 | impl RemoteMerkleTreeReader { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 74 | pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self { |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 75 | RemoteMerkleTreeReader { service, file_fd } |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 79 | impl ReadByChunk for RemoteMerkleTreeReader { |
| 80 | fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> { |
Victor Hsieh | da3fbc4 | 2021-02-23 16:12:49 -0800 | [diff] [blame] | 81 | let offset = i64::try_from(chunk_index * CHUNK_SIZE) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 82 | .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?; |
| 83 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 84 | let chunk = self |
| 85 | .service |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 86 | .lock() |
| 87 | .unwrap() |
| 88 | .readFsverityMerkleTree(self.file_fd, offset, buf.len() as i32) |
| 89 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?; |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 90 | buf.copy_from_slice(&chunk); |
| 91 | Ok(min(buf.len(), chunk.len())) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 94 | |
| 95 | pub struct RemoteFileEditor { |
| 96 | // This needs to have Sync trait to be used in fuse::worker::start_message_loop. |
| 97 | service: Arc<Mutex<VirtFdService>>, |
| 98 | file_fd: i32, |
| 99 | } |
| 100 | |
| 101 | impl RemoteFileEditor { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 102 | pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self { |
| 103 | RemoteFileEditor { service, file_fd } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl RandomWrite for RemoteFileEditor { |
| 108 | fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> { |
| 109 | let offset = |
| 110 | i64::try_from(offset).map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?; |
| 111 | let size = self |
| 112 | .service |
| 113 | .lock() |
| 114 | .unwrap() |
| 115 | .writeFile(self.file_fd, &buf, offset) |
| 116 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?; |
| 117 | Ok(size as usize) // within range because size is supposed to <= buf.len(), which is a usize |
| 118 | } |
| 119 | } |
| 120 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 121 | impl ReadByChunk for RemoteFileEditor { |
| 122 | fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 123 | remote_read_chunk(&self.service, self.file_fd, chunk_index, buf) |
| 124 | } |
| 125 | } |