kitab

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

commit 120b3b63a71b3a0ce0d54f4e8bc6e91a60d3d671
parent a10b02ce31440618574a3ca4b9049ad3b03d9476
Author: lash <dev@holbrook.no>
Date:   Tue, 27 Sep 2022 07:56:26 +0000

Connect digest args for apply to apply code digest list

Diffstat:
Msrc/digest.rs | 18++++++++++++++++++
Msrc/main.rs | 36++++++++++++++++++++++++++++++++----
Msrc/meta.rs | 20+++++++++++++++++---
Msrc/rdf.rs | 3++-
4 files changed, 69 insertions(+), 8 deletions(-)

diff --git a/src/digest.rs b/src/digest.rs @@ -1,5 +1,6 @@ use std::marker::Copy; use std::fmt; +use std::str::FromStr; use crate::error::ParseError; @@ -17,6 +18,23 @@ pub enum DigestType { MD5, } +impl FromStr for DigestType { + type Err = ParseError; + fn from_str(s: &str) -> Result<DigestType, Self::Err> { + match s { + "md5" => { + return Ok(DigestType::MD5); + }, + "sha512" => { + return Ok(DigestType::Sha512); + }, + _ => { + return Err(ParseError::new("Unknown digest string")); + }, + }; + } +} + /// Encapsulations of supported digests for digest data. pub enum RecordDigest { Sha512(Vec<u8>), diff --git a/src/main.rs b/src/main.rs @@ -9,6 +9,7 @@ use std::path::{ Path, PathBuf, }; +use std::str::FromStr; use env_logger; use clap::{ App, @@ -89,6 +90,15 @@ fn args_setup() -> ArgMatches<'static> { .required(true) .index(1) ); + o_apply = o_apply.arg( + Arg::with_name("adddigest") + .short("d") + .long("digest") + .help("Additional digest to store") + .multiple(true) + .takes_value(true) + .number_of_values(1) + ); o = o.subcommand(o_apply); // let mut o_entry = ( @@ -252,7 +262,7 @@ fn exec_apply(p: &Path, index_path: &Path, mut extra_digest_types: Vec<DigestTyp Ok(v) => { let f = File::open(&v).unwrap(); let m = rdf_read(f); - info!("apply {:?} -> {:?} for {:?}", entry, &m, &digest); + info!("apply {:?} -> {:?}", entry, &m); m.to_xattr(&ep); }, Err(e) => { @@ -340,10 +350,28 @@ fn main() { let mut r = true; match args.subcommand_matches("apply") { - Some(v) => { - let p = str_to_path(v); + Some(arg) => { + let p = str_to_path(&arg); + let mut digests: Vec<DigestType> = Vec::new(); + match arg.values_of("adddigest") { + Some(r) => { + for digest_str in r { + match DigestType::from_str(&digest_str) { + Ok(digest) => { + info!("using digest type {}", digest_str); + digests.push(digest); + }, + Err(e) => { + panic!("invalid digest URN: {:?}", e); + }, + } + } + }, + None => {}, + }; + info!("apply from path {:?}", &p); - if !exec_apply(p.as_path(), index_dir.as_path(), vec!()) { + if !exec_apply(p.as_path(), index_dir.as_path(), digests) { r = false; } }, diff --git a/src/meta.rs b/src/meta.rs @@ -88,8 +88,22 @@ pub fn digests_from_path(filepath: &path::Path, digest_types: &Vec<digest::Diges #[cfg(feature = "md5")] pub fn digest_md5_from_path(filepath: &path::Path) -> digest::RecordDigest { - let ctx = md5::Context::new(); - digest::RecordDigest::MD5(vec!()) + let mut ctx = md5::Context::new(); + let mut f = File::open(filepath).unwrap(); + let mut buf = [0; 512]; + + let mut run = true; + while run { + let c = f.read(&mut buf[..]).unwrap(); + if c < 512 { + run = false; + } + if c > 0 { + ctx.consume(&buf[..c]); + } + } + let d = ctx.compute(); + digest::RecordDigest::MD5(d.to_vec()) } /// Generates the native `sha512` digest of a file. @@ -503,7 +517,7 @@ impl MetaData { impl fmt::Debug for MetaData { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", format_args!("title \"{}\" author \"{}\" digest {}", self.title(), self.author(), self.fingerprint())) + write!(f, "{}", format_args!("title \"{}\" author \"{}\" digest {}", self.title(), self.author(), self.urn())) } } diff --git a/src/rdf.rs b/src/rdf.rs @@ -65,7 +65,8 @@ pub enum RdfError { pub fn write(entry: &MetaData, w: impl Write) -> Result<usize, std::io::Error> { let mut tfmt = TurtleFormatter::new(w); - let urn_str = format!("URN:sha512:{}", entry.fingerprint()); + //let urn_str = format!("URN:sha512:{}", entry.fingerprint()); + let urn_str = format!("URN:{}", entry.urn()); let urn = Subject::NamedNode( NamedNode{ iri: urn_str.as_str(),