From 409a660082f8a08e95d0e1447e5021ed81b9472d Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Tue, 9 Jun 2020 13:03:39 +0100 Subject: [PATCH] [pack/unpack] Get the functional tests working again. There's some hard coded version numbers in the tests, but I'm leaving for now since I'll rewrite in Rust to avoid too much of a proliferation of languages. --- .../scenario-string-constants.scm | 38 +++++++++++++------ functional-tests/thin-functional-tests.scm | 34 ++++++++--------- src/bin/thin_metadata_pack.rs | 10 ++++- src/bin/thin_metadata_unpack.rs | 9 ++++- src/lib.rs | 1 + src/pack/pack.rs | 20 ++++++++-- 6 files changed, 77 insertions(+), 35 deletions(-) diff --git a/functional-tests/scenario-string-constants.scm b/functional-tests/scenario-string-constants.scm index e416ed0..88c9ab9 100644 --- a/functional-tests/scenario-string-constants.scm +++ b/functional-tests/scenario-string-constants.scm @@ -75,20 +75,34 @@ Options: {-V|--version}") (define thin-metadata-pack-help - "Usage: thin_metadata_pack [options] -Options: - {-i|--input} - {-o|--output} - {-h|--help} - {-V|--version}") + "thin_metadata_pack 0.8.5 +Produces a compressed file of thin metadata. Only packs metadata blocks that are actually used. + +USAGE: + thin_metadata_pack -i -o + +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + -i Specify thinp metadata binary device/file + -o Specify packed output file") (define thin-metadata-unpack-help - "Usage: thin_metadata_unpack [options] -Options: - {-i|--input} - {-o|--output} - {-h|--help} - {-V|--version}") + "thin_metadata_unpack 0.8.5 +Unpack a compressed file of thin metadata. + +USAGE: + thin_metadata_unpack -i -o + +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + -i Specify thinp metadata binary device/file + -o Specify packed output file") (define cache-check-help "Usage: cache_check [options] {device|file} diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm index 0537642..b6e5a77 100644 --- a/functional-tests/thin-functional-tests.scm +++ b/functional-tests/thin-functional-tests.scm @@ -509,17 +509,17 @@ ;;;----------------------------------------------------------- (define-scenario (thin-metadata-pack version) - "thin_metadata_pack accepts --version" + "accepts --version" (run-ok-rcv (stdout _) (thin-metadata-pack "--version") - (assert-equal tools-version stdout))) + (assert-equal "thin_metadata_pack 0.8.5" stdout))) (define-scenario (thin-metadata-pack h) - "thin_metadata_pack accepts -h" + "accepts -h" (run-ok-rcv (stdout _) (thin-metadata-pack "-h") (assert-equal thin-metadata-pack-help stdout))) (define-scenario (thin-metadata-pack help) - "thin_metadata_pack accepts --help" + "accepts --help" (run-ok-rcv (stdout _) (thin-metadata-pack "--help") (assert-equal thin-metadata-pack-help stdout))) @@ -527,41 +527,41 @@ "Unrecognised option should cause failure" (with-valid-metadata (md) (run-fail-rcv (stdout stderr) (thin-metadata-pack "--unleash-the-hedgehogs") - (assert-matches ".*thin_metadata_pack: unrecognized option '--unleash-the-hedgehogs" stderr)))) + (assert-starts-with "error: Found argument '--unleash-the-hedgehogs'" stderr)))) (define-scenario (thin-metadata-pack missing-input-file) "the input file wasn't specified" (with-empty-metadata (md) (run-fail-rcv (_ stderr) (thin-metadata-pack "-o " md) - (assert-starts-with "No input file provided." stderr)))) + (assert-starts-with "error: The following required arguments were not provided:\n -i " stderr)))) (define-scenario (thin-metadata-pack no-such-input-file) "the input file can't be found" (with-empty-metadata (md) (run-fail-rcv (_ stderr) (thin-metadata-pack "-i no-such-file -o" md) - (assert-starts-with "Couldn't stat path" stderr)))) + (assert-starts-with "Couldn't find input file" stderr)))) (define-scenario (thin-metadata-pack missing-output-file) "the output file wasn't specified" (with-empty-metadata (md) (run-fail-rcv (_ stderr) (thin-metadata-pack "-i" md) - (assert-starts-with "No output file provided." stderr)))) + (assert-starts-with "error: The following required arguments were not provided:\n -o " stderr)))) ;;;----------------------------------------------------------- - ;;; thin_metadata_pack scenarios + ;;; thin_metadata_unpack scenarios ;;;----------------------------------------------------------- (define-scenario (thin-metadata-unpack version) - "thin_metadata_pack accepts --version" + "accepts --version" (run-ok-rcv (stdout _) (thin-metadata-unpack "--version") - (assert-equal tools-version stdout))) + (assert-equal "thin_metadata_unpack 0.8.5" stdout))) (define-scenario (thin-metadata-unpack h) - "thin_metadata_pack accepts -h" + "accepts -h" (run-ok-rcv (stdout _) (thin-metadata-unpack "-h") (assert-equal thin-metadata-unpack-help stdout))) (define-scenario (thin-metadata-unpack help) - "thin_metadata_pack accepts --help" + "accepts --help" (run-ok-rcv (stdout _) (thin-metadata-unpack "--help") (assert-equal thin-metadata-unpack-help stdout))) @@ -569,25 +569,25 @@ "Unrecognised option should cause failure" (with-valid-metadata (md) (run-fail-rcv (stdout stderr) (thin-metadata-unpack "--unleash-the-hedgehogs") - (assert-matches ".*thin_metadata_unpack: unrecognized option '--unleash-the-hedgehogs" stderr)))) + (assert-starts-with "error: Found argument '--unleash-the-hedgehogs'" stderr)))) (define-scenario (thin-metadata-unpack missing-input-file) "the input file wasn't specified" (with-empty-metadata (md) (run-fail-rcv (_ stderr) (thin-metadata-unpack "-o " md) - (assert-starts-with "No input file provided." stderr)))) + (assert-starts-with "error: The following required arguments were not provided:\n -i " stderr)))) (define-scenario (thin-metadata-unpack no-such-input-file) "the input file can't be found" (with-empty-metadata (md) (run-fail-rcv (_ stderr) (thin-metadata-unpack "-i no-such-file -o" md) - (assert-starts-with "Couldn't open pack file" stderr)))) + (assert-starts-with "Couldn't find input file" stderr)))) (define-scenario (thin-metadata-unpack missing-output-file) "the output file wasn't specified" (with-empty-metadata (md) (run-fail-rcv (_ stderr) (thin-metadata-unpack "-i" md) - (assert-starts-with "No output file provided." stderr)))) + (assert-starts-with "error: The following required arguments were not provided:\n -o " stderr)))) (define-scenario (thin-metadata-unpack garbage-input-file) "the input file is just zeroes" diff --git a/src/bin/thin_metadata_pack.rs b/src/bin/thin_metadata_pack.rs index 27d0435..6019147 100644 --- a/src/bin/thin_metadata_pack.rs +++ b/src/bin/thin_metadata_pack.rs @@ -2,7 +2,8 @@ extern crate clap; extern crate thinp; use clap::{App, Arg}; -use std::process; +use std::process::exit; +use thinp::file_utils; fn main() { let parser = App::new("thin_metadata_pack") @@ -25,8 +26,13 @@ fn main() { let input_file = matches.value_of("INPUT").unwrap(); let output_file = matches.value_of("OUTPUT").unwrap(); + if !file_utils::file_exists(input_file) { + eprintln!("Couldn't find input file '{}'.", &input_file); + exit(1); + } + if let Err(reason) = thinp::pack::pack::pack(&input_file, &output_file) { println!("Application error: {}\n", reason); - process::exit(1); + exit(1); } } diff --git a/src/bin/thin_metadata_unpack.rs b/src/bin/thin_metadata_unpack.rs index 1a7436d..79cdd87 100644 --- a/src/bin/thin_metadata_unpack.rs +++ b/src/bin/thin_metadata_unpack.rs @@ -3,6 +3,9 @@ extern crate thinp; use clap::{App, Arg}; use std::process; +use thinp::file_utils; + +use std::process::exit; fn main() { let parser = App::new("thin_metadata_unpack") @@ -21,11 +24,15 @@ fn main() { .value_name("FILE") .takes_value(true)); - let matches = parser.get_matches(); let input_file = matches.value_of("INPUT").unwrap(); let output_file = matches.value_of("OUTPUT").unwrap(); + if !file_utils::file_exists(input_file) { + eprintln!("Couldn't find input file '{}'.", &input_file); + exit(1); + } + if let Err(reason) = thinp::pack::pack::unpack(&input_file, &output_file) { println!("Application error: {}", reason); process::exit(1); diff --git a/src/lib.rs b/src/lib.rs index b51595b..9a193e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,5 +13,6 @@ extern crate quickcheck_macros; pub mod block_manager; pub mod check; +pub mod file_utils; pub mod pack; pub mod version; diff --git a/src/pack/pack.rs b/src/pack/pack.rs index 3406372..b4afa15 100644 --- a/src/pack/pack.rs +++ b/src/pack/pack.rs @@ -168,12 +168,26 @@ fn read_header(mut r: R) -> io::Result where R: byteorder::ReadBytesExt, { + use std::process::exit; + let magic = r.read_u64::()?; - assert_eq!(magic, MAGIC); + if magic != MAGIC { + eprintln!("Not a pack file."); + exit(1); + } + let version = r.read_u64::()?; - assert_eq!(version, PACK_VERSION); + if version != PACK_VERSION { + eprintln!("unsupported pack file version ({}).", PACK_VERSION); + exit(1); + } + let block_size = r.read_u64::()?; - assert_eq!(block_size, 4096); + if block_size != BLOCK_SIZE { + eprintln!("block size is not {}", BLOCK_SIZE); + exit(1); + } + r.read_u64::() }