use crate::error::{Error, Result};
use crate::verify::{Digest, read_region};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
#[derive(Debug)]
pub struct SourceFile {
path: PathBuf,
file: File,
len: u64,
modified: Option<SystemTime>,
}
impl SourceFile {
pub fn open(path: &Path) -> Result<SourceFile> {
let file = File::open(path)?;
let meta = file.metadata()?;
if !meta.is_file() {
return Err(Error::InvalidArgument(format!(
"{} is not a regular file",
path.display()
)));
}
bypass_cache(&file);
Ok(SourceFile {
path: path.to_path_buf(),
file,
len: meta.len(),
modified: meta.modified().ok(),
})
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn len(&self) -> u64 {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn check_unchanged(&self) -> Result<()> {
let meta = std::fs::metadata(&self.path)?;
if meta.len() != self.len || meta.modified().ok() != self.modified {
return Err(Error::Integrity(format!(
"{} changed since it was registered (size or modification time)",
self.path.display()
)));
}
Ok(())
}
pub fn read_into(
&self,
offset: u64,
dst: &mut [u8],
expected: Option<&Digest>,
) -> Result<Digest> {
self.check_unchanged()?;
let end = offset
.checked_add(dst.len() as u64)
.ok_or_else(|| Error::InvalidArgument("region end overflows".into()))?;
if end > self.len {
return Err(Error::InvalidArgument(format!(
"region {offset}..{end} is outside {} ({} bytes)",
self.path.display(),
self.len
)));
}
let digest = read_region(&self.file, offset, dst, expected)?;
forget_cached(&self.file, offset, dst.len());
Ok(digest)
}
}
#[cfg(target_os = "macos")]
fn bypass_cache(file: &File) {
use std::os::fd::AsRawFd;
unsafe {
libc::fcntl(file.as_raw_fd(), libc::F_NOCACHE, 1);
}
}
#[cfg(not(target_os = "macos"))]
fn bypass_cache(_file: &File) {}
#[cfg(target_os = "linux")]
fn forget_cached(file: &File, offset: u64, len: usize) {
use std::os::fd::AsRawFd;
unsafe {
libc::posix_fadvise(
file.as_raw_fd(),
offset as libc::off_t,
len as libc::off_t,
libc::POSIX_FADV_DONTNEED,
);
}
}
#[cfg(not(target_os = "linux"))]
fn forget_cached(_file: &File, _offset: u64, _len: usize) {}
#[cfg(test)]
mod tests {
use super::*;
use crate::verify::digest;
use std::io::Write;
fn temp_file(name: &str, data: &[u8]) -> PathBuf {
let dir = std::env::temp_dir().join(format!("memopro-rt-src-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join(name);
let mut f = File::create(&path).unwrap();
f.write_all(data).unwrap();
path
}
#[test]
fn reads_regions_and_checks_digests() {
let data: Vec<u8> = (0..(9 << 20)).map(|i: u32| (i * 7 % 256) as u8).collect();
let path = temp_file("a.bin", &data);
let src = SourceFile::open(&path).unwrap();
let mut dst = vec![0u8; 5 << 20];
let d = src.read_into(1234, &mut dst, None).unwrap();
assert_eq!(&dst[..], &data[1234..1234 + (5 << 20)]);
assert_eq!(d, digest(&dst));
assert!(src.read_into(1234, &mut dst, Some(&d)).is_ok());
let wrong = digest(b"other");
assert!(matches!(
src.read_into(1234, &mut dst, Some(&wrong)),
Err(Error::Integrity(_))
));
let mut too_long = vec![0u8; 10 << 20];
assert!(matches!(
src.read_into(0, &mut too_long, None),
Err(Error::InvalidArgument(_))
));
std::fs::remove_file(&path).ok();
}
#[test]
fn unaligned_reads_up_to_the_end_of_the_file_are_exact() {
let data: Vec<u8> = (0..(9 << 20) + 12345)
.map(|i: u32| (i * 13 % 251) as u8)
.collect();
let path = temp_file("c.bin", &data);
let src = SourceFile::open(&path).unwrap();
for (offset, len) in [
(19, 64),
(data.len() - (5 << 20) - 7, (5 << 20) + 7),
(16384, 4096),
] {
let mut dst = vec![0u8; len];
let d = src.read_into(offset as u64, &mut dst, None).unwrap();
assert_eq!(&dst[..], &data[offset..offset + len]);
assert_eq!(d, digest(&dst));
}
std::fs::remove_file(&path).ok();
}
#[test]
fn a_changed_file_is_refused() {
let path = temp_file("b.bin", &[1u8; 4096]);
let src = SourceFile::open(&path).unwrap();
std::fs::write(&path, [2u8; 5000]).unwrap();
let mut dst = vec![0u8; 100];
assert!(matches!(
src.read_into(0, &mut dst, None),
Err(Error::Integrity(_))
));
std::fs::remove_file(&path).ok();
}
}