blob: 0b6c0077eb553b37599bd60b91e95ceabe16ea1b [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 Hsieh2445e332021-06-04 16:44:53 -070022use super::{ChunkBuffer, RandomWrite, ReadByChunk, VirtFdService};
Victor Hsiehda3fbc42021-02-23 16:12:49 -080023use crate::common::CHUNK_SIZE;
Victor Hsiehf01f3232020-12-11 13:31:31 -080024
Victor Hsieh60acfd32021-02-23 13:08:13 -080025fn remote_read_chunk(
26 service: &Arc<Mutex<VirtFdService>>,
27 remote_fd: i32,
28 chunk_index: u64,
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070029 buf: &mut ChunkBuffer,
Victor Hsieh60acfd32021-02-23 13:08:13 -080030) -> io::Result<usize> {
31 let offset = i64::try_from(chunk_index * CHUNK_SIZE)
32 .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
33
34 let chunk = service
35 .lock()
36 .unwrap()
37 .readFile(remote_fd, offset, buf.len() as i32)
38 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
Victor Hsiehc81738d2021-03-25 09:52:00 -070039 let size = min(buf.len(), chunk.len());
40 buf[..size].copy_from_slice(&chunk[..size]);
41 Ok(size)
Victor Hsieh60acfd32021-02-23 13:08:13 -080042}
43
Victor Hsieh09e26262021-03-03 16:00:55 -080044pub struct RemoteFileReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080045 // This needs to have Sync trait to be used in fuse::worker::start_message_loop.
46 service: Arc<Mutex<VirtFdService>>,
47 file_fd: i32,
48}
49
Victor Hsieh09e26262021-03-03 16:00:55 -080050impl RemoteFileReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080051 pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self {
Victor Hsieh09e26262021-03-03 16:00:55 -080052 RemoteFileReader { service, file_fd }
Victor Hsiehf01f3232020-12-11 13:31:31 -080053 }
54}
55
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070056impl ReadByChunk for RemoteFileReader {
57 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsieh60acfd32021-02-23 13:08:13 -080058 remote_read_chunk(&self.service, self.file_fd, chunk_index, buf)
Victor Hsiehf01f3232020-12-11 13:31:31 -080059 }
60}
61
Victor Hsieh09e26262021-03-03 16:00:55 -080062pub struct RemoteMerkleTreeReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080063 // This needs to be a Sync to be used in fuse::worker::start_message_loop.
64 // TODO(victorhsieh): change to Strong<> once binder supports it.
65 service: Arc<Mutex<VirtFdService>>,
66 file_fd: i32,
67}
68
Victor Hsieh09e26262021-03-03 16:00:55 -080069impl RemoteMerkleTreeReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080070 pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self {
Victor Hsieh09e26262021-03-03 16:00:55 -080071 RemoteMerkleTreeReader { service, file_fd }
Victor Hsiehf01f3232020-12-11 13:31:31 -080072 }
73}
74
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070075impl ReadByChunk for RemoteMerkleTreeReader {
76 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsiehda3fbc42021-02-23 16:12:49 -080077 let offset = i64::try_from(chunk_index * CHUNK_SIZE)
Victor Hsiehf01f3232020-12-11 13:31:31 -080078 .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
79
Victor Hsieh60acfd32021-02-23 13:08:13 -080080 let chunk = self
81 .service
Victor Hsiehf01f3232020-12-11 13:31:31 -080082 .lock()
83 .unwrap()
84 .readFsverityMerkleTree(self.file_fd, offset, buf.len() as i32)
85 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
Victor Hsiehc81738d2021-03-25 09:52:00 -070086 let size = min(buf.len(), chunk.len());
87 buf[..size].copy_from_slice(&chunk[..size]);
88 Ok(size)
Victor Hsiehf01f3232020-12-11 13:31:31 -080089 }
90}
Victor Hsieh60acfd32021-02-23 13:08:13 -080091
92pub struct RemoteFileEditor {
93 // This needs to have Sync trait to be used in fuse::worker::start_message_loop.
94 service: Arc<Mutex<VirtFdService>>,
95 file_fd: i32,
96}
97
98impl RemoteFileEditor {
Victor Hsieh60acfd32021-02-23 13:08:13 -080099 pub fn new(service: Arc<Mutex<VirtFdService>>, file_fd: i32) -> Self {
100 RemoteFileEditor { service, file_fd }
101 }
102}
103
104impl RandomWrite for RemoteFileEditor {
105 fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
106 let offset =
107 i64::try_from(offset).map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
108 let size = self
109 .service
110 .lock()
111 .unwrap()
112 .writeFile(self.file_fd, &buf, offset)
113 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
114 Ok(size as usize) // within range because size is supposed to <= buf.len(), which is a usize
115 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700116
117 fn resize(&self, size: u64) -> io::Result<()> {
118 let size =
119 i64::try_from(size).map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
120 self.service
121 .lock()
122 .unwrap()
123 .resize(self.file_fd, size)
124 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
125 Ok(())
126 }
Victor Hsieh60acfd32021-02-23 13:08:13 -0800127}
128
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700129impl ReadByChunk for RemoteFileEditor {
130 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsieh60acfd32021-02-23 13:08:13 -0800131 remote_read_chunk(&self.service, self.file_fd, chunk_index, buf)
132 }
133}