From c4cce9f8a4f4fa1f24b8ada2bffc4116c4f7944f 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 | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/uu/shred/src/shred.rs b/src/uu/shred/src/shred.rs index 55a36bfe2bf..c329340c5b6 100644 --- a/src/uu/shred/src/shred.rs +++ b/src/uu/shred/src/shred.rs @@ -6,7 +6,7 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore (words) writeback wipesync +// spell-checker:ignore (words) writeback wipesync IWRITE use clap::{crate_version, Arg, ArgAction, Command}; use rand::prelude::SliceRandom; @@ -16,9 +16,13 @@ use std::fs; use std::fs::{File, OpenOptions}; use std::io; use std::io::prelude::*; +#[cfg(target_family = "unix")] +use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; +#[cfg(target_family = "unix")] +use uucore::libc::S_IWRITE; use uucore::{format_usage, show, show_if_err, util_name}; const BLOCK_SIZE: usize = 512; @@ -462,6 +466,14 @@ fn wipe_file( if force { let metadata = fs::metadata(path).map_err_context(String::new)?; let mut perms = metadata.permissions(); + #[cfg(target_family = "unix")] + // NOTE: set_readonly(false) makes the file world-writable on Unix. + #[allow(clippy::unnecessary_cast)] + // NOTE: S_IWRITE type is u16 on macOS. + perms.set_mode(perms.mode() | (S_IWRITE as u32)); + #[cfg(not(target_family = "unix"))] + #[allow(clippy::permissions_set_readonly_false)] + // FIXME: Clippy still throws error on non-UNIX platforms. perms.set_readonly(false); fs::set_permissions(path, perms).map_err_context(String::new)?; }