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 } /* * IO Error Poisoning: * * * Attempt 1: * * Wrap C calls w/ `ffi`. Most importantly, execute the `close()` through C. */ 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())) /* use std::ffi::CString; use std::os::fd::FromRawFd; let path = CString::new(CONFIG.input.to_str().unwrap().to_owned()).unwrap(); let flags = libc::O_DIRECT | libc::O_RDONLY; let f = unsafe { File::from_raw_fd(libc::open(path.as_ptr(), flags)) }; */ } 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() ) }) }