Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [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 | |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 17 | use anyhow::{anyhow, bail, Result}; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 18 | use nix::sys::stat::FileStat; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 19 | use std::fs::File; |
| 20 | use std::os::unix::fs::FileTypeExt; |
| 21 | use std::os::unix::io::AsRawFd; |
| 22 | use std::path::Path; |
| 23 | use std::thread; |
| 24 | use std::time::{Duration, Instant}; |
| 25 | |
| 26 | /// Returns when the file exists on the given `path` or timeout (1s) occurs. |
| 27 | pub fn wait_for_path<P: AsRef<Path>>(path: P) -> Result<()> { |
| 28 | const TIMEOUT: Duration = Duration::from_secs(1); |
| 29 | const INTERVAL: Duration = Duration::from_millis(10); |
| 30 | let begin = Instant::now(); |
| 31 | while !path.as_ref().exists() { |
| 32 | if begin.elapsed() > TIMEOUT { |
| 33 | bail!("{:?} not found. TIMEOUT.", path.as_ref()); |
| 34 | } |
| 35 | thread::sleep(INTERVAL); |
| 36 | } |
| 37 | Ok(()) |
| 38 | } |
| 39 | |
| 40 | /// Returns hexadecimal reprentation of a given byte array. |
| 41 | pub fn hexstring_from(s: &[u8]) -> String { |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 42 | s.iter().map(|byte| format!("{:02x}", byte)).reduce(|i, j| i + &j).unwrap_or_default() |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 43 | } |
| 44 | |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 45 | /// Parses a hexadecimal string into a byte array |
| 46 | pub fn parse_hexstring(s: &str) -> Result<Vec<u8>> { |
| 47 | let len = s.len(); |
| 48 | if len % 2 != 0 { |
| 49 | bail!("length {} is not even", len) |
| 50 | } else { |
| 51 | (0..len) |
| 52 | .step_by(2) |
| 53 | .map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(|e| anyhow!(e))) |
| 54 | .collect() |
| 55 | } |
| 56 | } |
| 57 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 58 | /// fstat that accepts a path rather than FD |
| 59 | pub fn fstat(p: &Path) -> Result<FileStat> { |
| 60 | let f = File::open(p)?; |
| 61 | Ok(nix::sys::stat::fstat(f.as_raw_fd())?) |
| 62 | } |
| 63 | |
| 64 | // From include/uapi/linux/fs.h |
| 65 | const BLK: u8 = 0x12; |
| 66 | const BLKGETSIZE64: u8 = 114; |
| 67 | nix::ioctl_read!(_blkgetsize64, BLK, BLKGETSIZE64, libc::size_t); |
| 68 | |
| 69 | /// Gets the size of a block device |
| 70 | pub fn blkgetsize64(p: &Path) -> Result<u64> { |
| 71 | let f = File::open(p)?; |
| 72 | if !f.metadata()?.file_type().is_block_device() { |
| 73 | bail!("{:?} is not a block device", p); |
| 74 | } |
| 75 | let mut size: usize = 0; |
| 76 | // SAFETY: kernel copies the return value out to `size`. The file is kept open until the end of |
| 77 | // this function. |
| 78 | unsafe { _blkgetsize64(f.as_raw_fd(), &mut size) }?; |
| 79 | Ok(size as u64) |
| 80 | } |