Skip to content

Commit

Permalink
Improve block size parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostassoid committed Sep 16, 2019
1 parent 9385dce commit 319386a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Change Log

## [$Unreleased] - $ReleaseDate

This release is mostly laying the groundwork for a more serious improvements.

### Added

* It is now possible to provide block size with a scale unit. E.g. `128k` (128 kilobytes) instead of `131072`. Additionally, the number is checked to be a power of two.

### Changed

* Usuccessful validation now retries at the last successful position.
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
[package]
name = "lethe"
version = "0.1.6-dev"
authors = ["kostassoid <[email protected]>"]
authors = ["Konstantin Alexandroff <[email protected]>"]
description = "A command-line tool to generate cryptocurrency wallets"
homepage = "https://github.com/Kostassoid/lethe"
repository = "https://github.com/Kostassoid/lethe"
categories = ["command-line-utilities"]
keywords = ["security", "command-line", "disk", "erase", "wipe", "storage"]
readme = "README.md"
license = "Apache-2.0"
edition = "2018"

[badges]
travis-ci = { repository = "Kostassoid/lethe", branch = "master" }

[dependencies]
nix = "0.14.1"
clap = "~2.33"
Expand Down
4 changes: 3 additions & 1 deletion release.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
disable-publish = true
dev-version-ext = "dev"
pre-release-replacements = [
{file="README.md", search="v[0-9]+\\.[0-9]+\\.[0-9]+", replace="v{{version}}"}
{file="README.md", search="v[0-9]+\\.[0-9]+\\.[0-9]+", replace="v{{version}}"},
{file="CHANGELOG.md", search="$Unreleased", replace="{{version}}"},
{file="CHANGELOG.md", search="$ReleaseDate", replace="{{date}}"}
]
7 changes: 1 addition & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn main() {
_ => Verify::Last
};
let block_size_override = cmd.value_of("blocksize")
.map(|bs| parse_block_size(bs)
.map(|bs| ui::args::parse_block_size(bs)
.unwrap_or_else(|err| {
eprintln!("Invalid blocksize value. {}", err);
std::process::exit(1);
Expand Down Expand Up @@ -154,8 +154,3 @@ fn main() {
}
}
}

fn parse_block_size(s: &str) -> Result<usize, std::num::ParseIntError> {

s.parse::<usize>()
}
54 changes: 54 additions & 0 deletions src/ui/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use regex::Regex;

pub fn parse_block_size(s: &str) -> Result<usize, &'static str> {
let block_size_regex = Regex::new(r"^(?i)(\d+) *((k|m)b?)?$").unwrap();
let captures = block_size_regex.captures(s);

match captures {
Some(groups) => {
let units = groups[1].parse::<usize>().map_err(|_x| "Not a number.")?;
let unit_size = match groups.get(3).map(|m| m.as_str().to_uppercase()) {
Some(ref u) if u == "K" => 1024,
Some(ref u) if u == "M" => 1024 * 1024,
_ => 1
};

let bytes_length = (units * unit_size) as usize;
if bytes_length & (bytes_length - 1) == 0 {
Ok((units * unit_size) as usize)
} else {
Err("Should be a power of two.")
}
},
_ => {
Err("Use a number of bytes with optional scale (e.g. 4096, 128k or 2M).")
}
}
}

#[cfg(test)]
mod test {
use super::*;
use assert_matches::*;

#[test]
fn test_block_size_parser_good() {
let k128 = 128 * 1024;
let m2 = 2 * 1024 * 1024;

assert_eq!(parse_block_size("4096"), Ok(4096));
assert_eq!(parse_block_size("128k"), Ok(k128));
assert_eq!(parse_block_size("128K"), Ok(k128));
assert_eq!(parse_block_size("2m"), Ok(m2));
assert_eq!(parse_block_size("2M"), Ok(m2));
}

#[test]
fn test_block_size_parser_bad() {
assert_matches!(parse_block_size(""), Err(_));
assert_matches!(parse_block_size("xxx"), Err(_));
assert_matches!(parse_block_size("-128k"), Err(_));
assert_matches!(parse_block_size("4096.000"), Err(_));
assert_matches!(parse_block_size("4095"), Err(_));
}
}
1 change: 1 addition & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod cli;
pub mod args;

0 comments on commit 319386a

Please sign in to comment.