commit 76a905da4eea9fc3a8384f83bc5359754a3f13dc Author: Elf M. Sternberg Date: Wed Jun 23 19:00:56 2021 -0700 Successfully read the mlocatedb header. This commit shows how to read the mlocatedb header, with a test to assert that the read is correct. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2a5b8c3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "mlocate-rs" +version = "0.1.0" +authors = ["Elf M. Sternberg "] +edition = "2018" +license = "MPL-2.0+" +description = "Rust implementation of the Linux mlocate client, with library." +repository = "https://github.com/elfsternberg/mlocate-rs" +readme = "./README.md" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +globset = "0.4.8" +regex = "1.5.4" +clap = "2.33.3" +structview = "1.1.0" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..75ada27 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,42 @@ +extern crate structview; +use structview::{u32_be, View}; + +#[derive(Clone, Copy, View)] +#[repr(C)] +pub struct MlHeader { + magic: [u8; 8], /* '\0', 'm', 'l', 'o', 'c', 'a', 't', 'e' */ + conf_size: u32_be, + version: u8, + check_visibility: u8, + pad: [u8; 2], +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + use std::io::{BufRead, BufReader}; + + #[test] + fn can_read_header() -> Result<(), String> { + let db = File::open("/var/lib/mlocate/mlocate.db").expect("Unable to open file"); + let mut reader = BufReader::new(db); + match reader.fill_buf() { + Ok(buffer) => match MlHeader::view(&buffer[0..32]) { + Ok(header) => { + let text = &header.magic[1..8]; + assert_eq!(text, *b"mlocate"); + assert_eq!(header.version, 0); + let magic = std::str::from_utf8(text).unwrap_or("ERROR"); + println!( + "magic: {}\nversion: {}\nconf_size: {}\nvisibility: {}", + magic, header.version, header.conf_size, header.check_visibility + ); + Ok(()) + } + Err(_) => Err("The header did not unpack.".to_owned()), + }, + Err(_) => Err("The header could not be read".to_owned()), + } + } +}