2020-08-04 16:00:46 +05:30
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
mod common;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
use common::common_args::*;
|
2021-07-20 15:07:55 +05:30
|
|
|
use common::fixture::*;
|
2021-07-01 19:57:37 +05:30
|
|
|
use common::input_arg::*;
|
|
|
|
use common::output_option::*;
|
2021-07-20 15:07:55 +05:30
|
|
|
use common::process::*;
|
|
|
|
use common::program::*;
|
|
|
|
use common::target::*;
|
2020-08-07 19:00:00 +05:30
|
|
|
use common::test_dir::*;
|
2021-07-20 15:07:55 +05:30
|
|
|
use common::thin::*;
|
2020-08-04 16:00:46 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
const USAGE: &str = "Usage: thin_repair [options] {device|file}\n\
|
|
|
|
Options:\n \
|
|
|
|
{-h|--help}\n \
|
|
|
|
{-i|--input} <input metadata (binary format)>\n \
|
|
|
|
{-o|--output} <output metadata (binary format)>\n \
|
|
|
|
{--transaction-id} <natural>\n \
|
|
|
|
{--data-block-size} <natural>\n \
|
|
|
|
{--nr-data-blocks} <natural>\n \
|
|
|
|
{-V|--version}";
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//-----------------------------------------
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
struct ThinRepair;
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
impl<'a> Program<'a> for ThinRepair {
|
|
|
|
fn name() -> &'a str {
|
|
|
|
"thin_repair"
|
|
|
|
}
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-10-18 21:29:17 +05:30
|
|
|
fn cmd<I>(args: I) -> Command
|
2021-10-11 16:37:26 +05:30
|
|
|
where
|
|
|
|
I: IntoIterator,
|
|
|
|
I::Item: Into<std::ffi::OsString>,
|
|
|
|
{
|
|
|
|
thin_repair_cmd(args)
|
2021-07-06 07:21:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fn usage() -> &'a str {
|
|
|
|
USAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
fn arg_type() -> ArgType {
|
|
|
|
ArgType::IoOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bad_option_hint(option: &str) -> String {
|
2021-08-31 07:34:48 +05:30
|
|
|
msg::bad_option_hint(option)
|
2021-07-06 07:21:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> InputProgram<'a> for ThinRepair {
|
|
|
|
fn mk_valid_input(td: &mut TestDir) -> Result<std::path::PathBuf> {
|
|
|
|
mk_valid_md(td)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_not_found() -> &'a str {
|
2021-08-31 07:34:48 +05:30
|
|
|
msg::FILE_NOT_FOUND
|
2021-07-06 07:21:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fn missing_input_arg() -> &'a str {
|
2021-08-31 07:34:48 +05:30
|
|
|
msg::MISSING_INPUT_ARG
|
2021-07-06 07:21:27 +05:30
|
|
|
}
|
|
|
|
|
2021-08-31 07:34:48 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2021-07-06 07:21:27 +05:30
|
|
|
fn corrupted_input() -> &'a str {
|
|
|
|
"The following field needs to be provided on the command line due to corruption in the superblock"
|
|
|
|
}
|
2021-08-31 07:34:48 +05:30
|
|
|
|
|
|
|
#[cfg(feature = "rust_tests")]
|
|
|
|
fn corrupted_input() -> &'a str {
|
|
|
|
"data block size needs to be provided due to corruption in the superblock"
|
|
|
|
}
|
2021-07-06 07:21:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> OutputProgram<'a> for ThinRepair {
|
|
|
|
fn missing_output_arg() -> &'a str {
|
2021-08-31 07:34:48 +05:30
|
|
|
msg::MISSING_OUTPUT_ARG
|
2021-07-06 07:21:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 21:49:44 +05:30
|
|
|
impl<'a> MetadataWriter<'a> for ThinRepair {
|
|
|
|
fn file_not_found() -> &'a str {
|
2021-08-31 07:34:48 +05:30
|
|
|
msg::FILE_NOT_FOUND
|
2021-08-30 21:49:44 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
test_accepts_help!(ThinRepair);
|
|
|
|
test_accepts_version!(ThinRepair);
|
|
|
|
test_rejects_bad_option!(ThinRepair);
|
|
|
|
|
|
|
|
test_input_file_not_found!(ThinRepair);
|
2021-07-07 22:45:28 +05:30
|
|
|
test_input_cannot_be_a_directory!(ThinRepair);
|
2021-07-06 07:21:27 +05:30
|
|
|
test_corrupted_input_data!(ThinRepair);
|
|
|
|
|
|
|
|
test_missing_output_option!(ThinRepair);
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//-----------------------------------------
|
|
|
|
// test output to a small file
|
|
|
|
|
|
|
|
// TODO: share with thin_restore
|
2020-08-04 16:00:46 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_repair_xml() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_zeroed_md(&mut td)?;
|
|
|
|
let xml = mk_valid_xml(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_repair_cmd(args!["-i", &xml, "-o", &md]))?;
|
2020-08-04 16:00:46 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//-----------------------------------------
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// TODO: share with thin_dump
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-09-10 08:03:53 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2020-08-04 16:00:46 +05:30
|
|
|
fn override_thing(flag: &str, val: &str, pattern: &str) -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md1 = mk_valid_md(&mut td)?;
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let output = run_ok_raw(thin_repair_cmd(args![flag, val, "-i", &md1, "-o", &md2]))?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert_eq!(output.stderr.len(), 0);
|
2021-10-11 16:37:26 +05:30
|
|
|
let output = run_ok(thin_dump_cmd(args![&md2]))?;
|
2021-07-01 19:57:37 +05:30
|
|
|
assert!(output.contains(pattern));
|
2020-08-04 16:00:46 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-10 08:03:53 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2020-08-04 16:00:46 +05:30
|
|
|
fn override_transaction_id() -> Result<()> {
|
|
|
|
override_thing("--transaction-id", "2345", "transaction=\"2345\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-10 08:03:53 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2020-08-04 16:00:46 +05:30
|
|
|
fn override_data_block_size() -> Result<()> {
|
|
|
|
override_thing("--data-block-size", "8192", "data_block_size=\"8192\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-10 08:03:53 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2020-08-04 16:00:46 +05:30
|
|
|
fn override_nr_data_blocks() -> Result<()> {
|
|
|
|
override_thing("--nr-data-blocks", "234500", "nr_data_blocks=\"234500\"")
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// FIXME: that's repair_superblock in thin_dump.rs
|
2020-08-04 16:00:46 +05:30
|
|
|
#[test]
|
|
|
|
fn superblock_succeeds() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md1 = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let original = run_ok_raw(thin_dump_cmd(args![&md1]))?;
|
2021-08-30 13:49:54 +05:30
|
|
|
if !cfg!(feature = "rust_tests") {
|
|
|
|
assert_eq!(original.stderr.len(), 0);
|
|
|
|
}
|
2020-08-04 16:00:46 +05:30
|
|
|
damage_superblock(&md1)?;
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
run_ok(
|
2021-10-11 16:37:26 +05:30
|
|
|
thin_repair_cmd(
|
2021-07-20 23:41:46 +05:30
|
|
|
args![
|
2021-09-10 08:03:53 +05:30
|
|
|
"--transaction-id=1",
|
2021-07-01 19:57:37 +05:30
|
|
|
"--data-block-size=128",
|
2021-09-10 08:03:53 +05:30
|
|
|
"--nr-data-blocks=20480",
|
2021-07-01 19:57:37 +05:30
|
|
|
"-i",
|
2021-07-20 23:41:46 +05:30
|
|
|
&md1,
|
2021-07-01 19:57:37 +05:30
|
|
|
"-o",
|
2021-07-20 23:41:46 +05:30
|
|
|
&md2
|
2021-07-01 19:57:37 +05:30
|
|
|
],
|
2021-10-11 16:37:26 +05:30
|
|
|
))?;
|
|
|
|
let repaired = run_ok_raw(thin_dump_cmd(args![&md2]))?;
|
2021-08-30 13:49:54 +05:30
|
|
|
if !cfg!(feature = "rust_tests") {
|
|
|
|
assert_eq!(repaired.stderr.len(), 0);
|
|
|
|
}
|
2020-08-04 16:00:46 +05:30
|
|
|
assert_eq!(original.stdout, repaired.stdout);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
// TODO: share with thin_dump
|
|
|
|
|
2021-05-04 13:40:20 +05:30
|
|
|
fn missing_thing(flag1: &str, flag2: &str, pattern: &str) -> Result<()> {
|
2020-08-04 16:00:46 +05:30
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md1 = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md1)?;
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let stderr = run_fail(thin_repair_cmd(args![flag1, flag2, "-i", &md1, "-o", &md2]))?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert!(stderr.contains(pattern));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-10 08:03:53 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2020-08-04 16:00:46 +05:30
|
|
|
fn missing_transaction_id() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
|
|
|
"--data-block-size=128",
|
2021-09-10 08:03:53 +05:30
|
|
|
"--nr-data-blocks=20480",
|
2021-05-04 13:40:20 +05:30
|
|
|
"transaction id",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_data_block_size() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
2021-09-10 08:03:53 +05:30
|
|
|
"--transaction-id=1",
|
|
|
|
"--nr-data-blocks=20480",
|
2021-05-04 13:40:20 +05:30
|
|
|
"data block size",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-10 08:03:53 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2020-08-04 16:00:46 +05:30
|
|
|
fn missing_nr_data_blocks() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
2021-09-10 08:03:53 +05:30
|
|
|
"--transaction-id=1",
|
2021-05-04 13:40:20 +05:30
|
|
|
"--data-block-size=128",
|
|
|
|
"nr data blocks",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//-----------------------------------------
|