kitab

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | LICENSE

commit 9214f86501ef9d9f5ab834bbdb6d18fe45b2fb8e
parent 59071ec880c4a1f434efdb803f2e88d5ea1848d8
Author: lash <dev@holbrook.no>
Date:   Sat, 23 Jul 2022 07:40:07 +0000

WIP add biblatex import support

Diffstat:
MCargo.toml | 2++
Msrc/lib.rs | 2++
Msrc/main.rs | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Msrc/meta.rs | 33++++++++++++++++-----------------
4 files changed, 82 insertions(+), 20 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -3,6 +3,7 @@ name = "kitab" version = "0.0.1" authors = ["nolash <dev@holbrook.no>"] edition = "2021" +rust-version = "1.60" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -27,6 +28,7 @@ urn = "^0.4.0" #tempfile = "^3.3.0" clap = "^2.34.0" directories = "^4.0.1" +walkdir = "^2.3.2" [dev-dependencies] tempfile = "^3.3.0" diff --git a/src/lib.rs b/src/lib.rs @@ -9,6 +9,8 @@ pub mod store; #[cfg(feature = "rdf")] pub mod rdf; +pub mod biblatex; + #[cfg(test)] mod tests { use env_logger; diff --git a/src/main.rs b/src/main.rs @@ -18,6 +18,8 @@ use clap::{ use directories::{ BaseDirs, }; +use walkdir::WalkDir; +use hex; use log::{ debug, info, @@ -28,6 +30,13 @@ use kitab::rdf::{ read as rdf_read, write as rdf_write, }; +use kitab::biblatex::{ + read_all as biblatex_read_all, +}; +use kitab::meta::{ + MetaData, + digest_from_path, +}; fn args_setup() -> ArgMatches<'static> { @@ -52,9 +61,21 @@ fn args_setup() -> ArgMatches<'static> { .required(true) .index(1) ); - o = o.subcommand(o_import); + let mut o_scan = ( + SubCommand::with_name("scan") + .about("import information from file") + .version("0.0.1") + ); + o_scan = o_scan.arg( + Arg::with_name("PATH") + .help("Path to operate on") + .required(true) + .index(1) + ); + o = o.subcommand(o_scan); + o.get_matches() } @@ -99,7 +120,7 @@ fn str_to_path(args: &ArgMatches) -> PathBuf { p_canon } -fn exec_import(f: &Path, index_path: &Path) { +fn exec_import_rdf(f: &Path, index_path: &Path) { #[cfg(feature = "rdf")] { let f = File::open(f).unwrap(); @@ -114,6 +135,34 @@ fn exec_import(f: &Path, index_path: &Path) { } } +fn exec_import_biblatex(f: &Path, index_path: &Path) { + let f = File::open(f).unwrap(); + biblatex_read_all(&f); +} + +fn exec_scan(p: &Path, index_path: &Path) { + for entry in WalkDir::new(&p) + .into_iter() + .filter_map(Result::ok) + .filter(|e| !e.file_type().is_dir()) { + let ep = entry.path(); + let z = digest_from_path(ep); + let z_hex = hex::encode(z); + + let fp = index_path.join(&z_hex); + match fp.canonicalize() { + Ok(v) => { + info!("apply {:?} for {:?}", entry, z_hex); + let m = MetaData::from_path(ep).unwrap(); + m.to_xattr(&p); + }, + Err(e) => { + debug!("metadata not found for {:?} -> {:?}", entry, z_hex); + }, + } + } +} + fn main() { env_logger::init(); @@ -126,7 +175,17 @@ fn main() { Some(v) => { let p = str_to_path(v); info!("have path {:?}", &p); - return exec_import(p.as_path(), index_dir.as_path()); + //return exec_import(p.as_path(), index_dir.as_path()); + return exec_import_biblatex(p.as_path(), index_dir.as_path()); + }, + _ => {}, + } + + match args.subcommand_matches("scan") { + Some(v) => { + let p = str_to_path(v); + info!("have path {:?}", &p); + return exec_scan(p.as_path(), index_dir.as_path()); }, _ => {}, } diff --git a/src/meta.rs b/src/meta.rs @@ -61,6 +61,21 @@ pub fn check_xattr() { } +pub fn digest_from_path(filepath: &path::Path) -> Vec<u8> { + let mut h = Sha512::new(); + let st = metadata(filepath).unwrap(); + let bs: u64 = st.st_blksize(); + let sz: u64 = st.st_size(); + let mut b: Vec<u8> = vec!(0; bs as usize); + let mut f = File::open(filepath).unwrap(); + let mut i: usize = 0; + while i < sz as usize { + let c = f.read(&mut b).unwrap(); + h.update(&b[..c]); + i += c; + } + h.finalize().to_vec() + } impl MetaData { pub fn new(title: &str, author: &str, typ: EntryType, digest: Vec<u8>, filename: Option<FileName>) -> MetaData { let dc = DCMetaData::new(title, author, typ); @@ -163,22 +178,6 @@ impl MetaData { hex::encode(&self.digest) } - fn digest_from_path(filepath: &path::Path) -> Vec<u8> { - let mut h = Sha512::new(); - let st = metadata(filepath).unwrap(); - let bs: u64 = st.st_blksize(); - let sz: u64 = st.st_size(); - let mut b: Vec<u8> = vec!(0; bs as usize); - let mut f = File::open(filepath).unwrap(); - let mut i: usize = 0; - while i < sz as usize { - let c = f.read(&mut b).unwrap(); - h.update(&b[..c]); - i += c; - } - h.finalize().to_vec() - } - pub fn from_xattr(filepath: &path::Path) -> MetaData { let mut title: String = String::new(); @@ -186,7 +185,7 @@ impl MetaData { let mut typ: EntryType = EntryType::Unknown(String::new()); let filename: FileName; - let digest = MetaData::digest_from_path(filepath); + let digest = digest_from_path(filepath); filename = filepath.file_name() .unwrap()