From 75371071445bc456292e1c91624b6f3c83699b8f Mon Sep 17 00:00:00 2001 From: Cutieguwu Date: Wed, 31 Dec 2025 20:48:51 -0500 Subject: [PATCH] Yet more stuff. Direct IO is not working at all. --- Cargo.lock | 5 +++-- Cargo.toml | 3 ++- src/io.rs | 19 ++++++++++++------- src/recovery.rs | 21 ++++++++++++++------- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 68751e0..bfee729 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,15 +125,16 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "libc", "ron", "serde", ] [[package]] name = "libc" -version = "0.2.177" +version = "0.2.178" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" +checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" [[package]] name = "memchr" diff --git a/Cargo.toml b/Cargo.toml index c70301d..e9f44d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,10 @@ [package] name = "kramer" version = "0.1.0" -edition = "2021" +edition = "2024" [dependencies] +libc = "0.2.178" # NOTE: # = X.X.X is the version used in testing. # Use this version for greatest compatibility. diff --git a/src/io.rs b/src/io.rs index 56e765a..74e7ef4 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,5 +1,6 @@ use std::fs::{File, OpenOptions}; use std::io::{self, Seek, SeekFrom}; +use std::os::unix::fs::{FileExt, OpenOptionsExt}; use crate::cli::CONFIG; @@ -22,8 +23,18 @@ pub fn get_stream_length(stream: &mut S) -> io::Result { 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 { @@ -40,16 +51,10 @@ pub fn load_output() -> anyhow::Result { }) } -pub fn load_map_read() -> anyhow::Result { +pub fn load_map_read() -> std::io::Result { OpenOptions::new() .read(true) .open(crate::path::MAP_PATH.clone()) - .with_context(|| { - format!( - "Failed to open/create mapping file at: {}", - crate::path::MAP_PATH.display() - ) - }) } pub fn load_map_write() -> anyhow::Result { diff --git a/src/recovery.rs b/src/recovery.rs index fadee86..f641002 100644 --- a/src/recovery.rs +++ b/src/recovery.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::{BufWriter, Read, Seek, SeekFrom, Write}; use std::usize; -use anyhow::Context; +use anyhow::{Context, anyhow}; use crate::cli::CONFIG; use crate::mapping::prelude::*; @@ -18,13 +18,16 @@ pub struct Recover { impl Recover { pub fn new() -> anyhow::Result { let input: File = crate::io::load_input()?; - let output: File = crate::io::load_output()?; let map: MapFile = { - crate::io::load_map_read()? - .try_into() - .unwrap_or(MapFile::new(CONFIG.sector_size)) + if let Ok(f) = crate::io::load_map_read() + && let Ok(map_file) = MapFile::try_from(f) + { + map_file + } else { + MapFile::new(CONFIG.sector_size) + } }; let mut r = Recover { @@ -120,12 +123,15 @@ impl Recover { println!("Hit error: {:?}", err); if CONFIG.reopen_on_error { self.reload_input() - .context("Failed to reload input file after previous error.")?; + .context("Failed to reload input file after previous error")?; } self.input - .seek_relative((read_position + buf_capacity) as i64) + .seek_relative(buf_capacity as i64) .context("Failed to seek input by buf_capacity to skip previous error")?; + self.output + .seek_relative(buf_capacity as i64) + .context("Failed to seek output by buf_capacity to skip previous error")?; // I don't remember what level was for. cluster.stage = Stage::ForIsolation { level: 1 }; @@ -138,6 +144,7 @@ impl Recover { } self.map.update(cluster); + self.map.write_to(&mut crate::io::load_map_write()?)?; read_position += buf_capacity; } }