harmos_runtime/filesystem/
input.rs1use std::{
20 io::{self, Read},
21 path::Path,
22};
23
24pub fn read(path: &Path, limit: usize) -> io::Result<Vec<u8>> {
45 let maximum = u64::try_from(limit)
46 .ok()
47 .and_then(|value| value.checked_add(1))
48 .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "input file limit is too large"))?;
49 if !std::fs::metadata(path)?.is_file() {
50 return Err(io::Error::new(io::ErrorKind::InvalidInput, "input must be a regular file"));
51 }
52 let file = std::fs::File::open(path)?;
53 if !file.metadata()?.is_file() {
54 return Err(io::Error::new(io::ErrorKind::InvalidInput, "input must be a regular file"));
55 }
56 let mut bytes = Vec::new();
57 file.take(maximum).read_to_end(&mut bytes)?;
58 if bytes.len() > limit {
59 return Err(io::Error::new(io::ErrorKind::InvalidInput, format!("input file exceeds {limit} bytes")));
60 }
61 Ok(bytes)
62}
63
64