Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! IO utilities |
| 16 | |
Jiyong Park | a999a32 | 2022-02-07 15:10:32 +0900 | [diff] [blame] | 17 | use anyhow::{anyhow, bail, Result}; |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 18 | use log::debug; |
| 19 | use std::fmt::Debug; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 20 | use std::fs::File; |
| 21 | use std::io; |
Jiyong Park | a999a32 | 2022-02-07 15:10:32 +0900 | [diff] [blame] | 22 | use std::os::unix::fs::FileTypeExt; |
| 23 | use std::os::unix::io::AsRawFd; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 24 | use std::path::Path; |
| 25 | use std::thread; |
| 26 | use std::time::{Duration, Instant}; |
| 27 | |
| 28 | const SLEEP_DURATION: Duration = Duration::from_millis(5); |
| 29 | |
| 30 | /// waits for a file with a timeout and returns it |
Jooyung Han | 697c860 | 2023-07-24 18:02:36 +0900 | [diff] [blame] | 31 | /// |
| 32 | /// WARNING: This only guarantees file creation. When there's another thread |
| 33 | /// writing a file and you're waiting for the file, reading the file should be |
| 34 | /// synchronized with other mechanism than just waiting for the creation. |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 35 | pub fn wait_for_file<P: AsRef<Path> + Debug>(path: P, timeout: Duration) -> Result<File> { |
| 36 | debug!("waiting for {:?}...", path); |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 37 | let begin = Instant::now(); |
| 38 | loop { |
| 39 | match File::open(&path) { |
| 40 | Ok(file) => return Ok(file), |
| 41 | Err(error) => { |
| 42 | if error.kind() != io::ErrorKind::NotFound { |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 43 | return Err(anyhow!(error)); |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 44 | } |
| 45 | if begin.elapsed() > timeout { |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 46 | return Err(anyhow!(io::Error::from(io::ErrorKind::NotFound))); |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 47 | } |
| 48 | thread::sleep(SLEEP_DURATION); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Jiyong Park | a999a32 | 2022-02-07 15:10:32 +0900 | [diff] [blame] | 54 | // From include/uapi/linux/fs.h |
| 55 | const BLK: u8 = 0x12; |
| 56 | const BLKFLSBUF: u8 = 97; |
| 57 | nix::ioctl_none!(_blkflsbuf, BLK, BLKFLSBUF); |
| 58 | |
| 59 | pub fn blkflsbuf(f: &mut File) -> Result<()> { |
| 60 | if !f.metadata()?.file_type().is_block_device() { |
| 61 | bail!("{:?} is not a block device", f.as_raw_fd()); |
| 62 | } |
| 63 | // SAFETY: The file is kept open until the end of this function. |
| 64 | unsafe { _blkflsbuf(f.as_raw_fd()) }?; |
| 65 | Ok(()) |
| 66 | } |
| 67 | |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 68 | #[cfg(test)] |
| 69 | mod tests { |
| 70 | use super::*; |
Jooyung Han | 697c860 | 2023-07-24 18:02:36 +0900 | [diff] [blame] | 71 | use std::fs::rename; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 72 | use std::io::{Read, Write}; |
| 73 | |
| 74 | #[test] |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 75 | fn test_wait_for_file() -> Result<()> { |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 76 | let test_dir = tempfile::TempDir::new().unwrap(); |
| 77 | let test_file = test_dir.path().join("test.txt"); |
Jooyung Han | 697c860 | 2023-07-24 18:02:36 +0900 | [diff] [blame] | 78 | let temp_file = test_dir.path().join("test.txt~"); |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 79 | thread::spawn(move || -> io::Result<()> { |
Jooyung Han | 697c860 | 2023-07-24 18:02:36 +0900 | [diff] [blame] | 80 | // Sleep to ensure that `wait_for_file` actually waits |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 81 | thread::sleep(Duration::from_secs(1)); |
Jooyung Han | 697c860 | 2023-07-24 18:02:36 +0900 | [diff] [blame] | 82 | // Write to a temp file and then rename it to avoid the race between |
| 83 | // write and read. |
| 84 | File::create(&temp_file)?.write_all(b"test")?; |
| 85 | rename(temp_file, test_file) |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 86 | }); |
| 87 | |
| 88 | let test_file = test_dir.path().join("test.txt"); |
Charisee | 96113f3 | 2023-01-26 09:00:42 +0000 | [diff] [blame] | 89 | let mut file = wait_for_file(test_file, Duration::from_secs(5))?; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 90 | let mut buffer = String::new(); |
| 91 | file.read_to_string(&mut buffer)?; |
| 92 | assert_eq!("test", buffer); |
| 93 | Ok(()) |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn test_wait_for_file_fails() { |
| 98 | let test_dir = tempfile::TempDir::new().unwrap(); |
| 99 | let test_file = test_dir.path().join("test.txt"); |
Charisee | 96113f3 | 2023-01-26 09:00:42 +0000 | [diff] [blame] | 100 | let file = wait_for_file(test_file, Duration::from_secs(1)); |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 101 | assert!(file.is_err()); |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 102 | assert_eq!( |
| 103 | io::ErrorKind::NotFound, |
| 104 | file.unwrap_err().root_cause().downcast_ref::<io::Error>().unwrap().kind() |
| 105 | ); |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 106 | } |
| 107 | } |