[thin_dump] fix overrides

This commit is contained in:
Joe Thornber 2021-10-20 13:06:15 +01:00
parent 56ea130650
commit 959b04ecb5
3 changed files with 15 additions and 16 deletions

View File

@ -265,33 +265,40 @@ fn emit_entries(
Ok(())
}
// FIXME: surely this is a standard fun?
fn override_<'a, T>(opt: &'a Option<T>, default: &'a T) -> &'a T {
match opt {
Some(v) => v,
None => default,
}
}
pub fn dump_metadata(
engine: Arc<dyn IoEngine>,
out: &mut dyn MetadataVisitor,
sb: &Superblock,
md: &Metadata,
overrides: &SuperblockOverrides,
) -> Result<()> {
let data_root = unpack::<SMRoot>(&sb.data_sm_root[0..])?;
let out_sb = ir::Superblock {
uuid: "".to_string(),
time: sb.time,
transaction: sb.transaction_id,
transaction: *override_(&overrides.transaction_id, &sb.transaction_id),
flags: if sb.flags.needs_check { Some(1) } else { None },
version: Some(2),
data_block_size: sb.data_block_size,
nr_data_blocks: data_root.nr_blocks,
data_block_size: *override_(&overrides.data_block_size, &sb.data_block_size),
nr_data_blocks: *override_(&overrides.nr_data_blocks, &data_root.nr_blocks),
metadata_snap: None,
};
out.superblock_b(&out_sb)?;
// ctx.report.set_title("Dumping shared regions");
for d in &md.defs {
out.def_shared_b(&format!("{}", d.def_id))?;
emit_entries(engine.clone(), out, &d.map.entries)?;
out.def_shared_e()?;
}
// ctx.report.set_title("Dumping devices");
for dev in &md.devs {
let device = ir::Device {
dev_id: dev.thin_id,
@ -336,7 +343,7 @@ pub fn dump(opts: ThinDumpOptions) -> Result<()> {
}
let mut out = xml::XmlWriter::new(writer);
dump_metadata(ctx.engine, &mut out, &sb, &md)
dump_metadata(ctx.engine, &mut out, &sb, &md, &opts.overrides)
}
//------------------------------------------

View File

@ -72,7 +72,7 @@ pub fn repair(opts: ThinRepairOptions) -> Result<()> {
);
let mut restorer = Restorer::new(&mut w, ctx.report);
dump_metadata(ctx.engine_in, &mut restorer, &sb, &md)
dump_metadata(ctx.engine_in, &mut restorer, &sb, &md, &opts.overrides)
}
//------------------------------------------

View File

@ -145,33 +145,27 @@ fn no_stderr() -> Result<()> {
// test superblock overriding & repair
// TODO: share with thin_repair
#[cfg(not(feature = "rust_tests"))]
fn override_something(flag: &str, value: &str, pattern: &str) -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?;
let output = run_ok_raw(thin_dump_cmd(args![&md, flag, value]))?;
if !cfg!(feature = "rust_tests") {
assert_eq!(output.stderr.len(), 0);
}
assert_eq!(output.stderr.len(), 0);
assert!(std::str::from_utf8(&output.stdout[0..])?.contains(pattern));
Ok(())
}
#[test]
#[cfg(not(feature = "rust_tests"))]
fn override_transaction_id() -> Result<()> {
override_something("--transaction-id", "2345", "transaction=\"2345\"")
}
#[test]
#[cfg(not(feature = "rust_tests"))]
fn override_data_block_size() -> Result<()> {
override_something("--data-block-size", "8192", "data_block_size=\"8192\"")
}
#[test]
#[cfg(not(feature = "rust_tests"))]
fn override_nr_data_blocks() -> Result<()> {
override_something("--nr-data-blocks", "234500", "nr_data_blocks=\"234500\"")
}
@ -206,7 +200,6 @@ fn repair_superblock() -> Result<()> {
// TODO: share with thin_repair
#[test]
#[cfg(not(feature = "rust_tests"))]
fn missing_transaction_id() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?;
@ -243,7 +236,6 @@ fn missing_data_block_size() -> Result<()> {
}
#[test]
#[cfg(not(feature = "rust_tests"))]
fn missing_nr_data_blocks() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?;