From af2207433dafe981c16f9b43e352eea4184a032e Mon Sep 17 00:00:00 2001 From: LoveSy Date: Fri, 7 Jul 2023 01:01:11 +0800 Subject: [PATCH] Fix error logging ok_or will unconditionally creates a LoggedResult, which prints an error even it successes. Use ok_or_else which lazily creates a LoggedResult only if it fails. --- native/src/boot/cpio.rs | 9 ++++++--- native/src/boot/payload.rs | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/native/src/boot/cpio.rs b/native/src/boot/cpio.rs index 03008eb2c..268ee4c7f 100644 --- a/native/src/boot/cpio.rs +++ b/native/src/boot/cpio.rs @@ -319,7 +319,10 @@ impl Cpio { } fn extract_entry(&self, path: &str, out: &Path) -> LoggedResult<()> { - let entry = self.entries.get(path).ok_or(log_err!("No such file"))?; + let entry = self + .entries + .get(path) + .ok_or_else(|| log_err!("No such file"))?; eprintln!("Extracting entry [{}] to [{}]", path, out.to_string_lossy()); if let Some(parent) = out.parent() { DirBuilder::new() @@ -449,7 +452,7 @@ impl Cpio { let entry = self .entries .remove(&norm_path(from)) - .ok_or(log_err!("no such entry {}", from))?; + .ok_or_else(|| log_err!("no such entry {}", from))?; self.entries.insert(norm_path(to), entry); eprintln!("Move [{}] -> [{}]", from, to); Ok(()) @@ -598,7 +601,7 @@ fn x8u>(x: &[u8; 8]) -> LoggedResult { let mut ret = 0u32; for i in x { let c = *i as char; - let v = c.to_digit(16).ok_or(log_err!("bad cpio header"))?; + let v = c.to_digit(16).ok_or_else(|| log_err!("bad cpio header"))?; ret = ret * 16 + v; } ret.try_into().map_err(|_| log_err!("bad cpio header")) diff --git a/native/src/boot/payload.rs b/native/src/boot/payload.rs index f4b48af3e..dab86d848 100644 --- a/native/src/boot/payload.rs +++ b/native/src/boot/payload.rs @@ -157,9 +157,11 @@ fn do_extract_boot_from_payload( for ext in operation.dst_extents.iter() { let out_seek = ext .start_block - .ok_or(bad_payload!("start block not found"))? + .ok_or_else(|| bad_payload!("start block not found"))? * block_size; - let num_blocks = ext.num_blocks.ok_or(bad_payload!("num blocks not found"))?; + let num_blocks = ext + .num_blocks + .ok_or_else(|| bad_payload!("num blocks not found"))?; out_file.seek(SeekFrom::Start(out_seek))?; out_file.write_zeros(num_blocks as usize)?; }