From 43cac3e4be1951d9dc6f0f29d43c048a915bf8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 21 Jul 2024 18:45:05 +0200 Subject: [PATCH 1/2] pyc-zero-mtime: suppress per-file logging This tool would generally modify thousands of files, and logging one line per file seems excessive. It also might slow down the processing, as those logs are saved somewhere. --- src/handlers/ar.rs | 2 +- src/handlers/jar.rs | 2 +- src/handlers/javadoc.rs | 2 +- src/handlers/mod.rs | 16 ++++++++++++---- src/handlers/pyc.rs | 4 ++-- tests/test_handlers/mod.rs | 2 +- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/handlers/ar.rs b/src/handlers/ar.rs index cc0bfed..17f3dc5 100644 --- a/src/handlers/ar.rs +++ b/src/handlers/ar.rs @@ -59,7 +59,7 @@ impl super::Processor for Ar { fn process(&self, input_path: &Path) -> Result { let mut have_mod = false; - let (mut io, mut input) = InputOutputHelper::open(input_path, self.config.check)?; + let (mut io, mut input) = InputOutputHelper::open(input_path, self.config.check, true)?; let mut buf = [0; MAGIC.len()]; input.read_exact(&mut buf)?; diff --git a/src/handlers/jar.rs b/src/handlers/jar.rs index f69af42..3b0f86a 100644 --- a/src/handlers/jar.rs +++ b/src/handlers/jar.rs @@ -34,7 +34,7 @@ impl super::Processor for Jar { fn process(&self, input_path: &Path) -> Result { let mut have_mod = false; - let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?; + let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, true)?; let mut input = zip::ZipArchive::new(input)?; io.open_output()?; diff --git a/src/handlers/javadoc.rs b/src/handlers/javadoc.rs index 31d9e7c..59b5718 100644 --- a/src/handlers/javadoc.rs +++ b/src/handlers/javadoc.rs @@ -85,7 +85,7 @@ impl super::Processor for Javadoc { let mut have_mod = false; let mut after_header = false; - let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?; + let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, true)?; io.open_output()?; let mut output = BufWriter::new(io.output.as_mut().unwrap()); diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 6bff690..52a43c0 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -6,7 +6,7 @@ pub mod javadoc; pub mod pyc; use anyhow::{bail, Context, Result}; -use log::{debug, info, warn}; +use log::{log, debug, info, warn, Level}; use serde::{Serialize, Deserialize}; use std::ascii::escape_default; use std::collections::HashMap; @@ -371,6 +371,7 @@ pub struct InputOutputHelper<'a> { pub output: Option, pub check: bool, + pub verbose: bool, // include logging about each modified file } impl<'a> Drop for InputOutputHelper<'a> { @@ -396,7 +397,11 @@ fn unwrap_os_string(filename: &OsStr) -> Result<&str> { } impl<'a> InputOutputHelper<'a> { - pub fn open(input_path: &'a Path, check: bool) -> Result<(Self, BufReader)> { + pub fn open( + input_path: &'a Path, + check: bool, + verbose: bool, + ) -> Result<(Self, BufReader)> { let input = File::open(input_path) .with_context(|| format!("Cannot open {:?}", input_path))?; @@ -410,6 +415,7 @@ impl<'a> InputOutputHelper<'a> { output_path: None, output: None, check, + verbose, }; Ok((io, input)) @@ -474,7 +480,8 @@ impl<'a> InputOutputHelper<'a> { // If it has multiple links, we reopen the orignal file and rewrite it. // This way the inode number is retained and hard links are not broken. if meta.nlink() == 1 { - info!("{}: replacing with normalized version", self.input_path.display()); + log!(if self.verbose { Level::Info } else { Level::Debug }, + "{}: replacing with normalized version", self.input_path.display()); if !self.check { output.set_permissions(meta.permissions())?; @@ -495,7 +502,8 @@ impl<'a> InputOutputHelper<'a> { Ok(ProcessResult::Replaced) } else { - info!("{}: rewriting with normalized contents", self.input_path.display()); + log!(if self.verbose { Level::Info } else { Level::Debug }, + "{}: rewriting with normalized contents", self.input_path.display()); if !self.check { output.seek(io::SeekFrom::Start(0))?; diff --git a/src/handlers/pyc.rs b/src/handlers/pyc.rs index a6d6a87..fb70535 100644 --- a/src/handlers/pyc.rs +++ b/src/handlers/pyc.rs @@ -779,7 +779,7 @@ impl super::Processor for Pyc { } fn process(&self, input_path: &Path) -> Result { - let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?; + let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, true)?; let mut parser = PycParser::from_file(input_path, input)?; if parser.version < (3, 0) { @@ -852,7 +852,7 @@ impl super::Processor for PycZeroMtime { } fn process(&self, input_path: &Path) -> Result { - let (mut io, input) = InputOutputHelper::open(input_path, self.config.check)?; + let (mut io, input) = InputOutputHelper::open(input_path, self.config.check, false)?; let mut parser = PycParser::from_file(input_path, input)?; let have_mod = parser.set_zero_mtime()?; diff --git a/tests/test_handlers/mod.rs b/tests/test_handlers/mod.rs index c21b538..4f9ea6a 100644 --- a/tests/test_handlers/mod.rs +++ b/tests/test_handlers/mod.rs @@ -52,7 +52,7 @@ impl handlers::Processor for Trivial { fn test_input_output_helper_drop() { let (_dir, input) = prepare_dir("tests/cases/libempty.a").unwrap(); - let (mut helper, _) = handlers::InputOutputHelper::open(&*input, false).unwrap(); + let (mut helper, _) = handlers::InputOutputHelper::open(&*input, false, false).unwrap(); helper.open_output().unwrap(); let output_path = helper.output_path.as_ref().unwrap().clone(); From 003c8cc4bf4ab28d30ab5ef6d740623c663780ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 21 Jul 2024 18:45:54 +0200 Subject: [PATCH 2/2] Increase log level for stale temporary files This isn't really expected to happen, so make it show up at the default logging level. --- src/handlers/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 52a43c0..2875d1b 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -441,7 +441,7 @@ impl<'a> InputOutputHelper<'a> { bail!("{}: cannot open temporary file: {}", output_path.display(), e); } - debug!("{}: stale temporary file found, removing", output_path.display()); + info!("{}: stale temporary file found, removing", output_path.display()); fs::remove_file(&output_path)?; openopts.open(&output_path)? }