blob: 903c143b3a179d70ccfbf03cad0714021bfdc876 [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 -080020
Victor Hsieh2445e332021-06-04 16:44:53 -070021use super::{ChunkBuffer, RandomWrite, ReadByChunk, VirtFdService};
Victor Hsiehda3fbc42021-02-23 16:12:49 -080022use crate::common::CHUNK_SIZE;
Victor Hsiehf01f3232020-12-11 13:31:31 -080023
Victor Hsieh60acfd32021-02-23 13:08:13 -080024fn remote_read_chunk(
Victor Hsiehc3d45b12021-06-30 09:16:41 -070025 service: &VirtFdService,
Victor Hsieh60acfd32021-02-23 13:08:13 -080026 remote_fd: i32,
27 chunk_index: u64,
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070028 buf: &mut ChunkBuffer,
Victor Hsieh60acfd32021-02-23 13:08:13 -080029) -> io::Result<usize> {
30 let offset = i64::try_from(chunk_index * CHUNK_SIZE)
31 .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
32
33 let chunk = service
Victor Hsieh60acfd32021-02-23 13:08:13 -080034 .readFile(remote_fd, offset, buf.len() as i32)
35 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
Victor Hsiehc81738d2021-03-25 09:52:00 -070036 let size = min(buf.len(), chunk.len());
37 buf[..size].copy_from_slice(&chunk[..size]);
38 Ok(size)
Victor Hsieh60acfd32021-02-23 13:08:13 -080039}
40
Victor Hsieh09e26262021-03-03 16:00:55 -080041pub struct RemoteFileReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080042 // This needs to have Sync trait to be used in fuse::worker::start_message_loop.
Victor Hsiehc3d45b12021-06-30 09:16:41 -070043 service: VirtFdService,
Victor Hsiehf01f3232020-12-11 13:31:31 -080044 file_fd: i32,
45}
46
Victor Hsieh09e26262021-03-03 16:00:55 -080047impl RemoteFileReader {
Victor Hsiehc3d45b12021-06-30 09:16:41 -070048 pub fn new(service: VirtFdService, file_fd: i32) -> Self {
Victor Hsieh09e26262021-03-03 16:00:55 -080049 RemoteFileReader { service, file_fd }
Victor Hsiehf01f3232020-12-11 13:31:31 -080050 }
51}
52
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070053impl ReadByChunk for RemoteFileReader {
54 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsieh60acfd32021-02-23 13:08:13 -080055 remote_read_chunk(&self.service, self.file_fd, chunk_index, buf)
Victor Hsiehf01f3232020-12-11 13:31:31 -080056 }
57}
58
Victor Hsieh09e26262021-03-03 16:00:55 -080059pub struct RemoteMerkleTreeReader {
Victor Hsiehf01f3232020-12-11 13:31:31 -080060 // This needs to be a Sync to be used in fuse::worker::start_message_loop.
Victor Hsiehc3d45b12021-06-30 09:16:41 -070061 service: VirtFdService,
Victor Hsiehf01f3232020-12-11 13:31:31 -080062 file_fd: i32,
63}
64
Victor Hsieh09e26262021-03-03 16:00:55 -080065impl RemoteMerkleTreeReader {
Victor Hsiehc3d45b12021-06-30 09:16:41 -070066 pub fn new(service: VirtFdService, file_fd: i32) -> Self {
Victor Hsieh09e26262021-03-03 16:00:55 -080067 RemoteMerkleTreeReader { service, file_fd }
Victor Hsiehf01f3232020-12-11 13:31:31 -080068 }
69}
70
Victor Hsiehd0bb5d32021-03-19 12:48:03 -070071impl ReadByChunk for RemoteMerkleTreeReader {
72 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsiehda3fbc42021-02-23 16:12:49 -080073 let offset = i64::try_from(chunk_index * CHUNK_SIZE)
Victor Hsiehf01f3232020-12-11 13:31:31 -080074 .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
75
Victor Hsieh60acfd32021-02-23 13:08:13 -080076 let chunk = self
77 .service
Victor Hsiehf01f3232020-12-11 13:31:31 -080078 .readFsverityMerkleTree(self.file_fd, offset, buf.len() as i32)
79 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
Victor Hsiehc81738d2021-03-25 09:52:00 -070080 let size = min(buf.len(), chunk.len());
81 buf[..size].copy_from_slice(&chunk[..size]);
82 Ok(size)
Victor Hsiehf01f3232020-12-11 13:31:31 -080083 }
84}
Victor Hsieh60acfd32021-02-23 13:08:13 -080085
86pub struct RemoteFileEditor {
87 // This needs to have Sync trait to be used in fuse::worker::start_message_loop.
Victor Hsiehc3d45b12021-06-30 09:16:41 -070088 service: VirtFdService,
Victor Hsieh60acfd32021-02-23 13:08:13 -080089 file_fd: i32,
90}
91
92impl RemoteFileEditor {
Victor Hsiehc3d45b12021-06-30 09:16:41 -070093 pub fn new(service: VirtFdService, file_fd: i32) -> Self {
Victor Hsieh60acfd32021-02-23 13:08:13 -080094 RemoteFileEditor { service, file_fd }
95 }
96}
97
98impl RandomWrite for RemoteFileEditor {
99 fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
100 let offset =
101 i64::try_from(offset).map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
102 let size = self
103 .service
Chris Wailes68c39f82021-07-27 16:03:44 -0700104 .writeFile(self.file_fd, buf, offset)
Victor Hsieh60acfd32021-02-23 13:08:13 -0800105 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
106 Ok(size as usize) // within range because size is supposed to <= buf.len(), which is a usize
107 }
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700108
109 fn resize(&self, size: u64) -> io::Result<()> {
110 let size =
111 i64::try_from(size).map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?;
112 self.service
Victor Hsieh9d0ab622021-04-26 17:07:02 -0700113 .resize(self.file_fd, size)
114 .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?;
115 Ok(())
116 }
Victor Hsieh60acfd32021-02-23 13:08:13 -0800117}
118
Victor Hsiehd0bb5d32021-03-19 12:48:03 -0700119impl ReadByChunk for RemoteFileEditor {
120 fn read_chunk(&self, chunk_index: u64, buf: &mut ChunkBuffer) -> io::Result<usize> {
Victor Hsieh60acfd32021-02-23 13:08:13 -0800121 remote_read_chunk(&self.service, self.file_fd, chunk_index, buf)
122 }
123}