use std::fs::{File, OpenOptions}; use std::io::{self, Seek, SeekFrom}; use std::os::unix::fs::OpenOptionsExt; use crate::cli::CONFIG; use anyhow::Context; /// Get length of data stream. /// Physical length of data stream in bytes /// (multiple of sector_size, rather than actual). /// /// This will attempt to return the stream to its current read position. pub fn get_stream_length(stream: &mut S) -> io::Result { let pos = stream.stream_position()?; let len = stream.seek(SeekFrom::End(0)); stream.seek(SeekFrom::Start(pos))?; len } pub fn load_input() -> anyhow::Result { OpenOptions::new() .read(true) .custom_flags(libc::O_DIRECT) .open(&CONFIG.input) .with_context(|| format!("Failed to open input file: {}", &CONFIG.input.display())) } pub fn load_output() -> anyhow::Result { OpenOptions::new() .read(true) .write(true) .create(true) .open(crate::path::OUTPUT_PATH.clone()) .with_context(|| { format!( "Failed to open/create output file at: {}", crate::path::OUTPUT_PATH.display() ) }) } pub fn load_map_read() -> std::io::Result { OpenOptions::new() .read(true) .open(crate::path::MAP_PATH.clone()) } pub fn load_map_write() -> anyhow::Result { OpenOptions::new() .write(true) .create(true) .truncate(true) // Wipe old map. Should really make a backup first. .open(crate::path::MAP_PATH.clone()) .with_context(|| { format!( "Failed to open map file at: {}", crate::path::MAP_PATH.display() ) }) } #[repr(C, align(512))] pub struct DirectIOBuffer(pub [u8; crate::MAX_BUFFER_SIZE]); impl Default for DirectIOBuffer { fn default() -> Self { Self([crate::FB_NULL_VALUE; _]) } } impl From<[u8; crate::MAX_BUFFER_SIZE]> for DirectIOBuffer { fn from(value: [u8; crate::MAX_BUFFER_SIZE]) -> Self { Self(value) } }