blob: f2ac23f07965ef17f90998552b3a4d9e4a81dc91 [file] [log] [blame]
Victor Hsiehf01f3232020-12-11 13:31:31 -08001/*
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 Hsiehd0bb5d32021-03-19 12:48:03 -070017use std::cmp::min;
Victor Hsiehf01f3232020-12-11 13:31:31 -080018use std::convert::TryFrom;
19use std::io;
Victor Hsiehf01f3232020-12-11 13:31:31 -080020use std::sync::{Arc, Mutex};
21
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070022use super::{ChunkBuffer, RandomWrite, ReadByChunk};
Victor Hsiehda3fbc42021-02-23 16:12:49 -080023use crate::common::CHUNK_SIZE;
Victor Hsiehf01f3232020-12-11 13:31:31 -080024
25use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService;
26use authfs_aidl_interface::binder::Strong;
27
28type VirtFdService = Strong<dyn IVirtFdService::IVirtFdService>;
29
Victor Hsieh60acfd32021-02-23 13:08:13 -080030fn remote_read_chunk(
31 service: &Arc<Mutex<VirtFdService>>,
32 remote_fd: i32,
33 chunk_index: u64,
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070034 buf: &mut ChunkBuffer,
Victor Hsieh60acfd32021-02-23 13:08:13 -080035) -> 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 Hsiehd0bb5d32021-03-19 12:48:03 -070044 buf.copy_from_slice(&chunk);
45 Ok(min(buf.len(), chunk.len()))
Victor Hsieh60acfd32021-02-23 13:08:13 -080046}
47
Victor Hsieh09e26262021-03-03 16:00:55 -080048pub struct RemoteFileReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080049 // 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 Hsieh09e26262021-03-03 16:00:55 -080054impl RemoteFileReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080055 pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self {
Victor Hsieh09e26262021-03-03 16:00:55 -080056 RemoteFileReader { service, file_fd }
Victor Hsiehf01f3232020-12-11 13:31:31 -080057 }
58}
59
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070060impl ReadByChunk for RemoteFileReader {
61 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsieh60acfd32021-02-23 13:08:13 -080062 remote_read_chunk(&self.service, self.file_fd, chunk_index, buf)
Victor Hsiehf01f3232020-12-11 13:31:31 -080063 }
64}
65
Victor Hsieh09e26262021-03-03 16:00:55 -080066pub struct RemoteMerkleTreeReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080067 // 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 Hsieh09e26262021-03-03 16:00:55 -080073impl RemoteMerkleTreeReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080074 pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self {
Victor Hsieh09e26262021-03-03 16:00:55 -080075 RemoteMerkleTreeReader { service, file_fd }
Victor Hsiehf01f3232020-12-11 13:31:31 -080076 }
77}
78
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070079impl ReadByChunk for RemoteMerkleTreeReader {
80 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsiehda3fbc42021-02-23 16:12:49 -080081 let offset = i64::try_from(chunk_index * CHUNK_SIZE)
Victor Hsiehf01f3232020-12-11 13:31:31 -080082 .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
83
Victor Hsieh60acfd32021-02-23 13:08:13 -080084 let chunk = self
85 .service
Victor Hsiehf01f3232020-12-11 13:31:31 -080086 .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 Hsiehd0bb5d32021-03-19 12:48:03 -070090 buf.copy_from_slice(&chunk);
91 Ok(min(buf.len(), chunk.len()))
Victor Hsiehf01f3232020-12-11 13:31:31 -080092 }
93}
Victor Hsieh60acfd32021-02-23 13:08:13 -080094
95pub 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
101impl RemoteFileEditor {
Victor Hsieh60acfd32021-02-23 13:08:13 -0800102 pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self {
103 RemoteFileEditor { service, file_fd }
104 }
105}
106
107impl 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 Hsiehd0bb5d32021-03-19 12:48:03 -0700121impl ReadByChunk for RemoteFileEditor {
122 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsieh60acfd32021-02-23 13:08:13 -0800123 remote_read_chunk(&self.service, self.file_fd, chunk_index, buf)
124 }
125}