From b8d9552ef58efb9586fd252437c557b22f51d33f Mon Sep 17 00:00:00 2001 From: Miles Liu Date: Fri, 10 Mar 2023 16:35:20 +0800 Subject: [PATCH] shred: fix `permissions_set_readonly_false` clippy error --- src/uu/shred/src/shred.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/uu/shred/src/shred.rs b/src/uu/shred/src/shred.rs index 55a36bfe2..75967a3dd 100644 --- a/src/uu/shred/src/shred.rs +++ b/src/uu/shred/src/shred.rs @@ -16,9 +16,13 @@ use std::fs; use std::fs::{File, OpenOptions}; use std::io; use std::io::prelude::*; +#[cfg(unix)] +use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; +#[cfg(unix)] +use uucore::libc::S_IWUSR; use uucore::{format_usage, show, show_if_err, util_name}; const BLOCK_SIZE: usize = 512; @@ -462,6 +466,18 @@ fn wipe_file( if force { let metadata = fs::metadata(path).map_err_context(String::new)?; let mut perms = metadata.permissions(); + #[cfg(unix)] + #[allow(clippy::useless_conversion)] + { + // NOTE: set_readonly(false) makes the file world-writable on Unix. + // NOTE: S_IWUSR type is u16 on macOS. + if (perms.mode() & u32::from(S_IWUSR)) == 0 { + perms.set_mode(u32::from(S_IWUSR)); + } + } + #[cfg(not(unix))] + // TODO: Remove the following once https://github.com/rust-lang/rust-clippy/issues/10477 is resolved. + #[allow(clippy::permissions_set_readonly_false)] perms.set_readonly(false); fs::set_permissions(path, perms).map_err_context(String::new)?; }