fix(freshclam): run with sudo when running without sudo fails (#1118)

This commit is contained in:
Gideon
2025-10-27 16:44:53 +01:00
committed by GitHub
parent e5b3ed1461
commit 48aa6b5ac5

View File

@@ -1251,7 +1251,34 @@ pub fn run_certbot(ctx: &ExecutionContext) -> Result<()> {
pub fn run_freshclam(ctx: &ExecutionContext) -> Result<()> {
let freshclam = require("freshclam")?;
print_separator(t!("Update ClamAV Database(FreshClam)"));
ctx.execute(freshclam).status_checked()
let output = ctx.execute(&freshclam).output()?;
let output = match output {
ExecutorOutput::Wet(output) => output,
ExecutorOutput::Dry => return Ok(()), // In a dry run, just exit after running without sudo
};
// Check if running without sudo was successful
if output.status.success() {
// Success, so write the output and exit
std::io::stdout().lock().write_all(&output.stdout).unwrap();
std::io::stderr().lock().write_all(&output.stderr).unwrap();
return Ok(());
}
// Since running without sudo failed (hopefully due to permission errors), try running with sudo.
debug!("`freshclam` (without sudo) resulted in error: {:?}", output);
let sudo = ctx.require_sudo()?;
match sudo.execute(ctx, freshclam)?.status_checked() {
Ok(()) => Ok(()), // Success! The output of only the sudo'ed process is written.
Err(err) => {
// Error! We add onto the error the output of running without sudo for more information.
Err(err.wrap_err(format!(
"Running `freshclam` with sudo failed as well as running without sudo. Output without sudo: {output:?}"
)))
}
}
}
/// Involve `pio upgrade` to update PlatformIO core.