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 | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame^] | 20 | use std::path::Path; |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 21 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 22 | use super::{ChunkBuffer, RandomWrite, ReadByChunk, VirtFdService}; |
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 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 25 | fn remote_read_chunk( |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 26 | service: &VirtFdService, |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 27 | remote_fd: i32, |
| 28 | chunk_index: u64, |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 29 | buf: &mut ChunkBuffer, |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 30 | ) -> 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 |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 35 | .readFile(remote_fd, offset, buf.len() as i32) |
| 36 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?; |
Victor Hsieh | c81738d | 2021-03-25 09:52:00 -0700 | [diff] [blame] | 37 | let size = min(buf.len(), chunk.len()); |
| 38 | buf[..size].copy_from_slice(&chunk[..size]); |
| 39 | Ok(size) |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 42 | pub struct RemoteFileReader { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 43 | // This needs to have Sync trait to be used in fuse::worker::start_message_loop. |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 44 | service: VirtFdService, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 45 | file_fd: i32, |
| 46 | } |
| 47 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 48 | impl RemoteFileReader { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 49 | pub fn new(service: VirtFdService, file_fd: i32) -> Self { |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 50 | RemoteFileReader { service, file_fd } |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 51 | } |
Victor Hsieh | d18b975 | 2021-11-09 16:03:34 -0800 | [diff] [blame^] | 52 | |
| 53 | pub fn new_by_path( |
| 54 | service: VirtFdService, |
| 55 | dir_fd: i32, |
| 56 | related_path: &Path, |
| 57 | ) -> io::Result<Self> { |
| 58 | let file_fd = |
| 59 | service.openFileInDirectory(dir_fd, related_path.to_str().unwrap()).map_err(|e| { |
| 60 | io::Error::new( |
| 61 | io::ErrorKind::Other, |
| 62 | format!( |
| 63 | "Failed to create a remote file reader by path {}: {}", |
| 64 | related_path.display(), |
| 65 | e.get_description() |
| 66 | ), |
| 67 | ) |
| 68 | })?; |
| 69 | Ok(RemoteFileReader { service, file_fd }) |
| 70 | } |
| 71 | |
| 72 | pub fn get_remote_fd(&self) -> i32 { |
| 73 | self.file_fd |
| 74 | } |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 77 | impl ReadByChunk for RemoteFileReader { |
| 78 | 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] | 79 | remote_read_chunk(&self.service, self.file_fd, chunk_index, buf) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 83 | pub struct RemoteMerkleTreeReader { |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 84 | // This needs to be a Sync to be used in fuse::worker::start_message_loop. |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 85 | service: VirtFdService, |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 86 | file_fd: i32, |
| 87 | } |
| 88 | |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 89 | impl RemoteMerkleTreeReader { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 90 | pub fn new(service: VirtFdService, file_fd: i32) -> Self { |
Victor Hsieh | 09e2626 | 2021-03-03 16:00:55 -0800 | [diff] [blame] | 91 | RemoteMerkleTreeReader { service, file_fd } |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 95 | impl ReadByChunk for RemoteMerkleTreeReader { |
| 96 | 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] | 97 | let offset = i64::try_from(chunk_index * CHUNK_SIZE) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 98 | .map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?; |
| 99 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 100 | let chunk = self |
| 101 | .service |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 102 | .readFsverityMerkleTree(self.file_fd, offset, buf.len() as i32) |
| 103 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?; |
Victor Hsieh | c81738d | 2021-03-25 09:52:00 -0700 | [diff] [blame] | 104 | let size = min(buf.len(), chunk.len()); |
| 105 | buf[..size].copy_from_slice(&chunk[..size]); |
| 106 | Ok(size) |
Victor Hsieh | f01f323 | 2020-12-11 13:31:31 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 109 | |
| 110 | pub struct RemoteFileEditor { |
| 111 | // This needs to have Sync trait to be used in fuse::worker::start_message_loop. |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 112 | service: VirtFdService, |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 113 | file_fd: i32, |
| 114 | } |
| 115 | |
| 116 | impl RemoteFileEditor { |
Victor Hsieh | c3d45b1 | 2021-06-30 09:16:41 -0700 | [diff] [blame] | 117 | pub fn new(service: VirtFdService, file_fd: i32) -> Self { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 118 | RemoteFileEditor { service, file_fd } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl RandomWrite for RemoteFileEditor { |
| 123 | fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> { |
| 124 | let offset = |
| 125 | i64::try_from(offset).map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?; |
| 126 | let size = self |
| 127 | .service |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 128 | .writeFile(self.file_fd, buf, offset) |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 129 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?; |
| 130 | Ok(size as usize) // within range because size is supposed to <= buf.len(), which is a usize |
| 131 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 132 | |
| 133 | fn resize(&self, size: u64) -> io::Result<()> { |
| 134 | let size = |
| 135 | i64::try_from(size).map_err(|_| io::Error::from_raw_os_error(libc::EOVERFLOW))?; |
| 136 | self.service |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 137 | .resize(self.file_fd, size) |
| 138 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e.get_description()))?; |
| 139 | Ok(()) |
| 140 | } |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Victor Hsieh | d0bb5d3 | 2021-03-19 12:48:03 -0700 | [diff] [blame] | 143 | impl ReadByChunk for RemoteFileEditor { |
| 144 | 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] | 145 | remote_read_chunk(&self.service, self.file_fd, chunk_index, buf) |
| 146 | } |
| 147 | } |