Skip to content

Commit 94a42b2

Browse files
committed
[thin_dump] Prompt for repair only on input errors
thin_dump should display the reparing hint only if there's any error in the input metadata rather than the output process. A context therefore is added to the returned error for callers to determine the error type. Note that a broken pipe error (EPIPE) is treated as an output error since the Rust std library ignores SIGPIPE by default [1][2]. [1] rust-lang/rust#13158 [2] rust-lang/rust#62569
1 parent fa3b3c6 commit 94a42b2

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

src/commands/thin_dump.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
use crate::commands::utils::*;
1010
use crate::commands::Command;
1111
use crate::report::*;
12-
use crate::thin::dump::{dump, ThinDumpOptions};
12+
use crate::thin::dump::{dump, OutputError, ThinDumpOptions};
1313
use crate::thin::metadata_repair::SuperblockOverrides;
1414

1515
pub struct ThinDumpCommand;
@@ -128,13 +128,17 @@ impl<'a> Command<'a> for ThinDumpCommand {
128128
},
129129
};
130130

131-
dump(opts).map_err(|reason| {
132-
report.fatal(&format!("{}", reason));
133-
report.fatal(
134-
"metadata contains errors (run thin_check for details).\n\
135-
perhaps you wanted to run with --repair ?",
136-
);
137-
std::io::Error::from_raw_os_error(libc::EPERM)
138-
})
131+
if let Err(e) = dump(opts) {
132+
if !e.is::<OutputError>() {
133+
report.fatal(&format!("{:?}", e));
134+
report.fatal(
135+
"metadata contains errors (run thin_check for details).\n\
136+
perhaps you wanted to run with --repair ?",
137+
);
138+
}
139+
return Err(std::io::Error::from_raw_os_error(libc::EPERM));
140+
}
141+
142+
Ok(())
139143
}
140144
}

src/thin/dump.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{anyhow, Result};
1+
use anyhow::{anyhow, Context, Result};
22
use std::fs::File;
33
use std::io::BufWriter;
44
use std::io::Write;
@@ -153,56 +153,59 @@ pub struct ThinDumpOptions<'a> {
153153
pub overrides: SuperblockOverrides,
154154
}
155155

156-
struct Context {
156+
struct ThinDumpContext {
157157
report: Arc<Report>,
158158
engine: Arc<dyn IoEngine + Send + Sync>,
159159
}
160160

161-
fn mk_context(opts: &ThinDumpOptions) -> Result<Context> {
161+
fn mk_context(opts: &ThinDumpOptions) -> Result<ThinDumpContext> {
162162
let engine: Arc<dyn IoEngine + Send + Sync> = if opts.async_io {
163163
Arc::new(AsyncIoEngine::new(opts.input, MAX_CONCURRENT_IO, false)?)
164164
} else {
165165
let nr_threads = std::cmp::max(8, num_cpus::get() * 2);
166166
Arc::new(SyncIoEngine::new(opts.input, nr_threads, false)?)
167167
};
168168

169-
Ok(Context {
169+
Ok(ThinDumpContext {
170170
report: opts.report.clone(),
171171
engine,
172172
})
173173
}
174174

175175
//------------------------------------------
176176

177-
fn emit_leaf(v: &mut MappingVisitor, b: &Block) -> Result<()> {
177+
// a wrapper for callers to indicate the error type
178+
#[derive(Debug)]
179+
pub struct OutputError;
180+
181+
impl std::fmt::Display for OutputError {
182+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183+
write!(f, "output error")
184+
}
185+
}
186+
187+
fn emit_leaf(v: &MappingVisitor, b: &Block) -> Result<()> {
178188
use Node::*;
179189
let path = Vec::new();
180190
let kr = KeyRange::new();
181191

182192
let bt = checksum::metadata_block_type(b.get_data());
183193
if bt != checksum::BT::NODE {
184-
return Err(anyhow!(format!(
185-
"checksum failed for node {}, {:?}",
186-
b.loc, bt
187-
)));
194+
return Err(anyhow!("checksum failed for node {}, {:?}", b.loc, bt));
188195
}
189196

190197
let node = unpack_node::<BlockTime>(&path, b.get_data(), true, true)?;
191198

192199
match node {
193-
Internal { .. } => {
194-
return Err(anyhow!("not a leaf"));
195-
}
200+
Internal { .. } => Err(anyhow!("block {} is not a leaf", b.loc)),
196201
Leaf {
197202
header,
198203
keys,
199204
values,
200-
} => {
201-
v.visit(&path, &kr, &header, &keys, &values)?;
202-
}
205+
} => v
206+
.visit(&path, &kr, &header, &keys, &values)
207+
.context(OutputError),
203208
}
204-
205-
Ok(())
206209
}
207210

208211
fn read_for<T>(engine: Arc<dyn IoEngine>, blocks: &[u64], mut t: T) -> Result<()>
@@ -214,7 +217,8 @@ where
214217
.read_many(cs)
215218
.map_err(|_e| anyhow!("read_many failed"))?
216219
{
217-
t(b.map_err(|_e| anyhow!("read of individual block failed"))?)?;
220+
let blk = b.map_err(|_e| anyhow!("read of individual block failed"))?;
221+
t(blk)?;
218222
}
219223
}
220224

@@ -226,15 +230,14 @@ fn emit_leaves(
226230
out: &mut dyn MetadataVisitor,
227231
leaves: &[u64],
228232
) -> Result<()> {
229-
let mut v = MappingVisitor::new(out);
233+
let v = MappingVisitor::new(out);
230234
let proc = |b| {
231-
emit_leaf(&mut v, &b)?;
235+
emit_leaf(&v, &b)?;
232236
Ok(())
233237
};
234238

235239
read_for(engine, leaves, proc)?;
236-
v.end_walk()
237-
.map_err(|e| anyhow!("failed to emit leaves: {}", e))
240+
v.end_walk().context(OutputError)
238241
}
239242

240243
fn emit_entries(
@@ -255,7 +258,7 @@ fn emit_entries(
255258
leaves.clear();
256259
}
257260
let str = format!("{}", id);
258-
out.ref_shared(&str)?;
261+
out.ref_shared(&str).context(OutputError)?;
259262
}
260263
}
261264
}
@@ -284,12 +287,13 @@ pub fn dump_metadata(
284287
nr_data_blocks: data_root.nr_blocks,
285288
metadata_snap: None,
286289
};
287-
out.superblock_b(&out_sb)?;
290+
out.superblock_b(&out_sb).context(OutputError)?;
288291

289292
for d in &md.defs {
290-
out.def_shared_b(&format!("{}", d.def_id))?;
293+
out.def_shared_b(&format!("{}", d.def_id))
294+
.context(OutputError)?;
291295
emit_entries(engine.clone(), out, &d.map.entries)?;
292-
out.def_shared_e()?;
296+
out.def_shared_e().context(OutputError)?;
293297
}
294298

295299
for dev in &md.devs {
@@ -300,12 +304,12 @@ pub fn dump_metadata(
300304
creation_time: dev.detail.creation_time,
301305
snap_time: dev.detail.snapshotted_time,
302306
};
303-
out.device_b(&device)?;
307+
out.device_b(&device).context(OutputError)?;
304308
emit_entries(engine.clone(), out, &dev.map.entries)?;
305-
out.device_e()?;
309+
out.device_e().context(OutputError)?;
306310
}
307-
out.superblock_e()?;
308-
out.eof()?;
311+
out.superblock_e().context(OutputError)?;
312+
out.eof().context(OutputError)?;
309313

310314
Ok(())
311315
}
@@ -327,11 +331,13 @@ pub fn dump(opts: ThinDumpOptions) -> Result<()> {
327331
read_superblock(ctx.engine.as_ref(), SUPERBLOCK_LOCATION)
328332
.and_then(|sb| sb.overrides(&opts.overrides))?
329333
};
334+
330335
let md = build_metadata(ctx.engine.clone(), &sb)?;
331336
let md = optimise_metadata(md)?;
332337

333338
let writer: Box<dyn Write> = if opts.output.is_some() {
334-
Box::new(BufWriter::new(File::create(opts.output.unwrap())?))
339+
let f = File::create(opts.output.unwrap()).context(OutputError)?;
340+
Box::new(BufWriter::new(f))
335341
} else {
336342
Box::new(BufWriter::new(std::io::stdout()))
337343
};

0 commit comments

Comments
 (0)