Compare commits

..

No commits in common. "bf2b2715d49c7b303b601318a779733a08838913" and "3f7ae7bd8bab693f7eb4f4760c51fb2d6d509880" have entirely different histories.

4 changed files with 24 additions and 66 deletions

View File

@ -1,6 +0,0 @@
* Implement a "read from mlocate" feature.
* Implement a "write to mlocate" feature".
* Implement a "write to rslocate01" feature.
* implement a "write to rzlocate01" feature, stealing wildly from
dictd.

View File

@ -1,3 +0,0 @@
pub trait LocateDb {
fn is(magic: &[u8]) -> bool;
}

View File

@ -1,33 +1,15 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! MLocate
//!
//! The readme has the full explanation, but the `locate` suite of
//! tools present in all Linux distributions is used to locate files
//! on your storage device. Rather than search the device directly,
//! `locate` scans a catalog file created during downtime.
//!
//! `MLocate` is the most popular implementation of the locate system,
//! but it has three annoying flaws:
//!
//! 1. The archive file isn't very compressed.
//! 2. The archive file is always an average of 12 hours out of date.
//! 3. The archive is accessible only through a command line program.
//!
//! This program intends to read one of two different formats, the
//! classic mlocate format, or a new format that exploits a few nifty
//! tricks to try and make the database file smaller and access
//! faster.
extern crate structview; extern crate structview;
use structview::{u32_be, View};
pub mod database; #[derive(Clone, Copy, View)]
pub mod mlocate_db; #[repr(C)]
pub struct MlHeader {
use crate::database::LocateDb; magic: [u8; 8], /* '\0', 'm', 'l', 'o', 'c', 'a', 't', 'e' */
use crate::mlocate_db::MlHeader; conf_size: u32_be,
version: u8,
check_visibility: u8,
pad: [u8; 2],
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -40,10 +22,20 @@ mod tests {
let db = File::open("/var/lib/mlocate/mlocate.db").expect("Unable to open file"); let db = File::open("/var/lib/mlocate/mlocate.db").expect("Unable to open file");
let mut reader = BufReader::new(db); let mut reader = BufReader::new(db);
match reader.fill_buf() { match reader.fill_buf() {
Ok(buffer) => { Ok(buffer) => match MlHeader::view(&buffer[0..32]) {
assert!(MlHeader::is(buffer), "Could not read DB"); 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(()) Ok(())
} }
Err(_) => Err("The header did not unpack.".to_owned()),
},
Err(_) => Err("The header could not be read".to_owned()), Err(_) => Err("The header could not be read".to_owned()),
} }
} }

View File

@ -1,25 +0,0 @@
use crate::database::LocateDb;
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],
}
impl LocateDb for MlHeader {
fn is(magic: &[u8]) -> bool {
if magic.len() < 32 {
false
} else {
match MlHeader::view(&magic[0..32]) {
Ok(header) => header.magic[0..8] == *b"\0mlocate" && header.version == 0,
Err(_) => false,
}
}
}
}