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-07-20 12:37:12 +05:30
|
|
|
fn path() -> &'a std::ffi::OsStr {
|
|
|
|
THIN_REPAIR.as_ref()
|
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 {
|
|
|
|
cpp_msg::bad_option_hint(option)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
cpp_msg::FILE_NOT_FOUND
|
|
|
|
}
|
|
|
|
|
|
|
|
fn missing_input_arg() -> &'a str {
|
|
|
|
cpp_msg::MISSING_INPUT_ARG
|
|
|
|
}
|
|
|
|
|
|
|
|
fn corrupted_input() -> &'a str {
|
|
|
|
"The following field needs to be provided on the command line due to corruption in the superblock"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> OutputProgram<'a> for ThinRepair {
|
|
|
|
fn file_not_found() -> &'a str {
|
|
|
|
cpp_msg::FILE_NOT_FOUND
|
|
|
|
}
|
|
|
|
|
|
|
|
fn missing_output_arg() -> &'a str {
|
|
|
|
cpp_msg::MISSING_OUTPUT_ARG
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
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-07-20 23:41:46 +05:30
|
|
|
run_fail(THIN_REPAIR, 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
|
|
|
|
|
|
|
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-07-20 23:41:46 +05:30
|
|
|
let output = run_ok_raw(THIN_REPAIR, args![flag, val, "-i", &md1, "-o", &md2])?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert_eq!(output.stderr.len(), 0);
|
2021-07-20 23:41:46 +05:30
|
|
|
let output = run_ok(THIN_DUMP, args![&md2])?;
|
2021-07-01 19:57:37 +05:30
|
|
|
assert!(output.contains(pattern));
|
2020-08-04 16:00:46 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_transaction_id() -> Result<()> {
|
|
|
|
override_thing("--transaction-id", "2345", "transaction=\"2345\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_data_block_size() -> Result<()> {
|
|
|
|
override_thing("--data-block-size", "8192", "data_block_size=\"8192\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
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-07-01 19:57:37 +05:30
|
|
|
let original = run_ok_raw(
|
|
|
|
THIN_DUMP,
|
2021-07-20 23:41:46 +05:30
|
|
|
args![
|
2021-07-01 19:57:37 +05:30
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
2021-07-20 23:41:46 +05:30
|
|
|
&md1
|
2021-07-01 19:57:37 +05:30
|
|
|
],
|
|
|
|
)?;
|
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(
|
|
|
|
THIN_REPAIR,
|
2021-07-20 23:41:46 +05:30
|
|
|
args![
|
2021-07-01 19:57:37 +05:30
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
"-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-07-20 23:41:46 +05:30
|
|
|
let repaired = run_ok_raw(THIN_DUMP, 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-07-20 23:41:46 +05:30
|
|
|
let stderr = run_fail(THIN_REPAIR, args![flag1, flag2, "-i", &md1, "-o", &md2])?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert!(stderr.contains(pattern));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_transaction_id() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
"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(
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
"data block size",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_nr_data_blocks() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"nr data blocks",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//-----------------------------------------
|