Skip to content

Commit

Permalink
metadata string allows '=' symbol; more nice date strings
Browse files Browse the repository at this point in the history
  • Loading branch information
garikello3d committed Jan 8, 2024
1 parent 822349a commit 3be8dd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn time_str() -> String {
let now = OffsetDateTime::now_local().unwrap_or(OffsetDateTime::now_utc());
let dt = now.date();
let tm = now.time();
format!("{}-{}-{} {:02}:{:02}:{:02} Z{:02}", dt.year(), dt.month() as u8, dt.day(), tm.hour(), tm.minute(), tm.second(), now.offset().whole_hours())
format!("{}-{:02}-{:02} {:02}:{:02}:{:02} Z{:02}", dt.year(), dt.month() as u8, dt.day(), tm.hour(), tm.minute(), tm.second(), now.offset().whole_hours())
}

pub fn backup<R: Read>(
Expand Down Expand Up @@ -94,7 +94,7 @@ pub fn backup<R: Read>(
let end_timestamp = timestamp();
let end_time_str = time_str();
let throughput_mbps = if end_timestamp - hash_seed != 0 { stats.in_data_len as u64 / 1024 / 1024 / (end_timestamp - hash_seed) } else { 0 };
stats.misc_info = Some(format!("built from {}/{}, started at {}, ended at {}, took {} seconds, througput {} MB/s",
stats.misc_info = Some(format!("revision={}/{}, started={}, ended={}, took={}s, througput={}MB/s",
option_env!("GIT_BRANCH").unwrap_or("?"),
option_env!("GIT_REV").unwrap_or("?"),
start_time_str, end_time_str, end_timestamp - hash_seed, throughput_mbps));
Expand Down
19 changes: 11 additions & 8 deletions src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ impl Stats {
.map_err(|e|format!("cannot read metadata: {}", e))?;

for line in s.split("\n").map(|ln| ln.trim()).filter(|ln| ln.len() > 0) {
let param_val = line.split("=").collect::<Vec<&str>>();
if param_val.len() != 2 {
return Err(format!("invalid metadata line: '{}'", line));
let delim_pos = line.find('=').ok_or(format!("invalid metadata line: '{}'", line))?;
if delim_pos == 0 {
return Err(format!("empty param name in metadata: '{}'", line));
}
let param = param_val[0].trim();
let val = param_val[1].trim();
if delim_pos == line.len() - 1 {
return Err(format!("empty name value in metadata: '{}'", line));
}
let param = &line[.. delim_pos];
let val = &line[delim_pos + 1 ..];
if !map.insert(param, val).is_none() {
return Err(format!("duplicate key '{}'", param));
return Err(format!("duplicate key: '{}'", param));
}
}

Expand Down Expand Up @@ -107,7 +110,7 @@ mod tests {
chunk_len=2\n\
auth=Author Name\n\
auth_len=3\n
misc_info=XXX".as_bytes().to_vec().as_slice()).unwrap(),
misc_info=ABC=1, XYZ=2".as_bytes().to_vec().as_slice()).unwrap(),
Stats {
in_data_len: 12345,
in_data_hash: 0xabcde,
Expand All @@ -117,7 +120,7 @@ mod tests {
out_chunk_size: 2,
auth_string: "Author Name".to_owned(),
auth_chunk_size: 3,
misc_info: Some("XXX".to_owned())
misc_info: Some("ABC=1, XYZ=2".to_owned())
}
);
}
Expand Down

0 comments on commit 3be8dd5

Please sign in to comment.