wala-rust

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

commit f8e0328ec33d9c32d54a9e1d181d2c1d7abe0d8a
parent e88c7f59ca3842e43730170520c2c89da764c333
Author: lash <dev@holbrook.no>
Date:   Thu, 22 Sep 2022 16:03:07 +0000

Refactor requestresult, add aliased value, binary contents in trace

Diffstat:
Msrc/record.rs | 71+++++++++++++++++++++++++++++++++++++++++++++--------------------------
Msrc/request.rs | 108++++++++++++++++---------------------------------------------------------------
Msrc/trace.rs | 36+++++++++++++++++++++++++++++-------
3 files changed, 96 insertions(+), 119 deletions(-)

diff --git a/src/record.rs b/src/record.rs @@ -58,8 +58,44 @@ pub struct RequestResult { pub m: Option<Mime>, /// Contains the file name to use for download request requesting a filename. pub n: Option<String>, - /// Contains the authentication result + /// Contains the authentication result. pub a: Option<AuthResult>, + /// Aliase content, in case of mutable reference. + pub s: Option<String>, +} + +impl RequestResult { + pub fn new(typ: RequestResultType) -> RequestResult { + RequestResult { + typ: typ, + v: None, + f: None, + m: None, + n: None, + a: None, + s: None, + } + } + + pub fn with_content(mut self, s: String) -> RequestResult { + self.v = Some(s); + self + } + + pub fn with_auth(mut self, a: AuthResult) -> RequestResult { + self.a = Some(a); + self + } + + pub fn with_file(mut self, f: File) -> RequestResult { + self.f = Some(f); + self + } + + pub fn with_aliased(mut self, s: String) -> RequestResult { + self.s = Some(s); + self + } } impl fmt::Display for RequestResult { @@ -94,6 +130,8 @@ pub struct Record { pub digest: Vec<u8>, /// Server side path to content. pub path: PathBuf, + /// Alias + pub alias: Option<Vec<u8>>, } /// Identifier part of the for mutable content reference. @@ -162,14 +200,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res }, Err(e) => { error!("cannot read from request body: {}", e); - let err = RequestResult{ - typ: RequestResultType::ReadError, - v: None, - f: None, - m: None, - n: None, - a: None, - }; + let err = RequestResult::new(RequestResultType::ReadError); return Err(err); }, } @@ -177,14 +208,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res if expected_size > 0 { if expected_size != total_size { - let err = RequestResult{ - typ: RequestResultType::ReadError, - v: None, - f: None, - m: None, - n: None, - a: None, - }; + let err = RequestResult::new(RequestResultType::ReadError); return Err(err); } } @@ -195,14 +219,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res of }, Err(e) => { - let err = RequestResult{ - typ: RequestResultType::WriteError, - v: None, - f: None, - m: None, - n: None, - a: None, - }; + let err = RequestResult::new(RequestResultType::WriteError); return Err(err); } }; @@ -214,6 +231,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res let r = Record{ digest: z, path: final_path_buf, + alias: None, }; Ok(r) } @@ -241,7 +259,7 @@ pub fn put_mutable(path: &Path, mut f: impl Read, expected_size: usize, key: &Re Ok(v) => { match remove_file(&link_path_buf) { Ok(r) => { - info!("unlinked mutable ref on {:?}", &e); + info!("unlinked mutable ref on {:?}", &link_path_buf); }, Err(e) => { debug!("clear symlink failed {:?}", &e); @@ -251,6 +269,7 @@ pub fn put_mutable(path: &Path, mut f: impl Read, expected_size: usize, key: &Re let r = Record{ digest: pointer, path: link_path_buf.clone(), + alias: Some(v.digest), }; return Ok(r); }, diff --git a/src/request.rs b/src/request.rs @@ -44,71 +44,43 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s match method { Method::Put => { if !auth_result.valid() { - return RequestResult{ - typ: RequestResultType::AuthError, - v: None, - f: None, - m: None, - n: None, - a: None, - }; + return RequestResult::new(RequestResultType::AuthError); } if auth_result.active() { - let res: RequestResult; + let mut res: RequestResult; let rk = ResourceKey::from_str(url.as_str()).unwrap(); debug!("mutable put, authenticated as {:?} using mutable key {} -> {}", auth_result, &url, &rk); //let ptr = rk.pointer_for(&auth_result); match put_mutable(path, f, expected_size, &rk, &auth_result) { Ok(v) => { let digest_hex = hex::encode(v.digest); - res = RequestResult{ - typ: RequestResultType::Changed, - v: Some(digest_hex), - f: None, - m: None, - n: None, - a: Some(auth_result), - }; + let alias_hex = hex::encode(v.alias.unwrap()); + res = RequestResult::new(RequestResultType::Changed); + res = res.with_content(digest_hex); + res = res.with_auth(auth_result); + res = res.with_aliased(alias_hex); }, Err(e) => { let err_str = format!("{:?}", e); error!("{}", err_str); - res = RequestResult { - typ: RequestResultType::RecordError, - v: Some(String::from(err_str)), - f: None, - m: None, - n: None, - a: None, - }; + res = RequestResult::new(RequestResultType::RecordError); + res = res.with_content(String::from(err_str)); }, }; return res; } else { debug!("immutable put"); - let res: RequestResult; + let mut res: RequestResult; match put_immutable(path, f, expected_size) { Ok(v) => { let digest_hex = hex::encode(v.digest); - res = RequestResult{ - typ: RequestResultType::Changed, - v: Some(digest_hex), - f: None, - m: None, - n: None, - a: None, - }; + res = RequestResult::new(RequestResultType::Changed); + res = res.with_content(digest_hex); }, Err(e) => { let err_str = format!("{}", e); - res = RequestResult { - typ: RequestResultType::RecordError, - v: Some(String::from(err_str)), - f: None, - m: None, - n: None, - a: None, - }; + res = RequestResult::new(RequestResultType::RecordError); + res = res.with_content(String::from(err_str)); }, }; return res; @@ -118,14 +90,9 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s let digest = match hex::decode(&url) { Err(e) => { let err_str = format!("{}", e); - return RequestResult { - typ: RequestResultType::InputError, - v: Some(String::from(err_str)), - f: None, - m: None, - n: None, - a: None, - }; + let mut res = RequestResult::new(RequestResultType::InputError); + res = res.with_content(String::from(err_str)); + return res; }, Ok(v) => { v @@ -137,26 +104,9 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s match get_record(digest.clone(), full_path_buf.as_path()) { Some(v) => { - let mut res = RequestResult { - typ: RequestResultType::Found, - v: None, //Some(String::new()), - f: Some(v), - m: None, - n: None, - a: None, - }; -// match get_meta_type(path, &digest) { -// Some(v) => { -// res.m = Some(v); -// }, -// _ => {}, -// }; -// match get_meta_filename(path, &digest) { -// Some(v) => { -// res.n = Some(v); -// }, -// _ => {}, -// }; + let mut res = RequestResult::new(RequestResultType::Found); + res = res.with_file(v); + #[cfg(feature = "meta")] { res.m = get_meta_type(path, &digest); @@ -165,27 +115,13 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s return res; }, None => { - return RequestResult { - typ: RequestResultType::RecordError, - v: Some(String::new()), - f: None, - m: None, - n: None, - a: None, - }; + return RequestResult::new(RequestResultType::RecordError); }, }; }, _ => {}, }; - RequestResult { - typ: RequestResultType::InputError, - v: Some(String::new()), - f: None, - m: None, - n: None, - a: None, - } + RequestResult::new(RequestResultType::InputError) } #[cfg(test)] diff --git a/src/trace.rs b/src/trace.rs @@ -1,5 +1,8 @@ use std::path::Path; -use std::fs::File; +use std::fs::{ + File, + OpenOptions, +}; use std::io::Write; use hex; @@ -27,14 +30,14 @@ pub fn trace_request(p: &Path, res: &RequestResult) { if rf.len() == 0 { return; } - let mut content = String::new(); + //let mut content = String::new(); + let mut content: Vec<u8> = vec!(); + let mut identity = String::new(); match &res.a { Some(auth) => { if auth.active() { - let identity = hex::encode(&auth.identity); - content.push_str(&identity); - //content.push('\t'); - //content.push_str("foo"); + content = auth.identity.clone(); + identity = hex::encode(&content); } else { rf = String::new(); } @@ -45,9 +48,28 @@ pub fn trace_request(p: &Path, res: &RequestResult) { if rf.len() == 0 { return; } - let fp = p.join(rf); + let fp = p.join(&rf); let mut f = File::create(fp).unwrap(); f.write(content.as_ref()); + + // useless update because we can always resolve mutable to immutable from data dir +// if content.len() != 0 { +// let rf_content_hex = res.s.as_ref().unwrap(); +// let rf_content = hex::decode(rf_content_hex).unwrap(); +// //let rf_content = s.clone(); +// let fp = p.join(&identity); +// +// let mut f = OpenOptions::new() +// .write(true) +// .append(true) +// .create(true) +// .open(fp) +// .unwrap(); +// +// let rf_bin = hex::decode(rf).unwrap(); +// f.write(rf_bin.as_ref()); +// f.write(rf_content.as_ref()); +// } } #[cfg(test)]