Successfully read the mlocatedb header.
This commit shows how to read the mlocatedb header, with a test to assert that the read is correct.
This commit is contained in:
commit
76a905da4e
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
Cargo.lock
|
|
@ -0,0 +1,17 @@
|
||||||
|
[package]
|
||||||
|
name = "mlocate-rs"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Elf M. Sternberg <elf.sternberg@gmail.com>"]
|
||||||
|
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"
|
|
@ -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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue