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 
17 use log::error;
18 use std::convert::TryInto;
19 use std::io;
20 
21 use crate::file::VirtFdService;
22 use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService::FsStat::FsStat;
23 
24 /// Relevant/interesting stats of a remote filesystem.
25 pub struct RemoteFsStats {
26     /// Block size of the filesystem
27     pub block_size: u64,
28     /// Fragment size of the filesystem
29     pub fragment_size: u64,
30     /// Number of blocks in the filesystem
31     pub block_numbers: u64,
32     /// Number of free blocks
33     pub block_available: u64,
34     /// Number of free inodes
35     pub inodes_available: u64,
36     /// Maximum filename length
37     pub max_filename: u64,
38 }
39 
40 pub struct RemoteFsStatsReader {
41     service: VirtFdService,
42 }
43 
44 impl RemoteFsStatsReader {
new(service: VirtFdService) -> Self45     pub fn new(service: VirtFdService) -> Self {
46         Self { service }
47     }
48 
statfs(&self) -> io::Result<RemoteFsStats>49     pub fn statfs(&self) -> io::Result<RemoteFsStats> {
50         let st = self.service.statfs().map_err(|e| {
51             error!("Failed to call statfs on fd_server: {:?}", e);
52             io::Error::from_raw_os_error(libc::EIO)
53         })?;
54         try_into_remote_fs_stats(st).map_err(|_| {
55             error!("Received invalid stats from fd_server");
56             io::Error::from_raw_os_error(libc::EIO)
57         })
58     }
59 }
60 
try_into_remote_fs_stats(st: FsStat) -> Result<RemoteFsStats, std::num::TryFromIntError>61 fn try_into_remote_fs_stats(st: FsStat) -> Result<RemoteFsStats, std::num::TryFromIntError> {
62     Ok(RemoteFsStats {
63         block_size: st.blockSize.try_into()?,
64         fragment_size: st.fragmentSize.try_into()?,
65         block_numbers: st.blockNumbers.try_into()?,
66         block_available: st.blockAvailable.try_into()?,
67         inodes_available: st.inodesAvailable.try_into()?,
68         max_filename: st.maxFilename.try_into()?,
69     })
70 }
71