diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 58c56138e..034cd4b12 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1743,50 +1743,7 @@ fn copy_file( let mut permissions = source_metadata.permissions(); #[cfg(unix)] { - let mut mode = permissions.mode(); - - let (is_preserve_mode, is_explicit_no_preserve_mode) = options.preserve_mode(); - if !is_preserve_mode { - use libc::{ - S_IRGRP, S_IROTH, S_IRUSR, S_IRWXG, S_IRWXO, S_IRWXU, S_IWGRP, S_IWOTH, S_IWUSR, - }; - - #[cfg(not(any( - target_os = "android", - target_os = "macos", - target_os = "macos-12", - target_os = "freebsd", - )))] - const MODE_RW_UGO: u32 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; - #[cfg(not(any( - target_os = "android", - target_os = "macos", - target_os = "macos-12", - target_os = "freebsd", - )))] - const S_IRWXUGO: u32 = S_IRWXU | S_IRWXG | S_IRWXO; - - #[cfg(any( - target_os = "android", - target_os = "macos", - target_os = "macos-12", - target_os = "freebsd", - ))] - const MODE_RW_UGO: u32 = - (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32; - #[cfg(any( - target_os = "android", - target_os = "macos", - target_os = "macos-12", - target_os = "freebsd", - ))] - const S_IRWXUGO: u32 = (S_IRWXU | S_IRWXG | S_IRWXO) as u32; - - match is_explicit_no_preserve_mode { - true => mode = MODE_RW_UGO, - false => mode &= S_IRWXUGO, - } - } + let mut mode = handling_no_preserve_mode(options, permissions.mode()); // apply umask use uucore::mode::get_umask; @@ -1918,6 +1875,52 @@ fn copy_file( Ok(()) } +fn handling_no_preserve_mode(options: &Options, org_mode: u32) -> u32 { + let (is_preserve_mode, is_explicit_no_preserve_mode) = options.preserve_mode(); + if !is_preserve_mode { + use libc::{ + S_IRGRP, S_IROTH, S_IRUSR, S_IRWXG, S_IRWXO, S_IRWXU, S_IWGRP, S_IWOTH, S_IWUSR, + }; + + #[cfg(not(any( + target_os = "android", + target_os = "macos", + target_os = "macos-12", + target_os = "freebsd", + )))] + const MODE_RW_UGO: u32 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + #[cfg(not(any( + target_os = "android", + target_os = "macos", + target_os = "macos-12", + target_os = "freebsd", + )))] + const S_IRWXUGO: u32 = S_IRWXU | S_IRWXG | S_IRWXO; + + #[cfg(any( + target_os = "android", + target_os = "macos", + target_os = "macos-12", + target_os = "freebsd", + ))] + const MODE_RW_UGO: u32 = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32; + #[cfg(any( + target_os = "android", + target_os = "macos", + target_os = "macos-12", + target_os = "freebsd", + ))] + const S_IRWXUGO: u32 = (S_IRWXU | S_IRWXG | S_IRWXO) as u32; + + match is_explicit_no_preserve_mode { + true => return MODE_RW_UGO, + false => return org_mode & S_IRWXUGO, + }; + } + + org_mode +} + /// Copy the file from `source` to `dest` either using the normal `fs::copy` or a /// copy-on-write scheme if --reflink is specified and the filesystem supports it. fn copy_helper(