Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::fmt::Display;
use std::fs::{self, Metadata, OpenOptions, Permissions};
#[cfg(unix)]
use std::os::unix::fs::{FileTypeExt, PermissionsExt};
#[cfg(unix)]
use std::os::unix::net::UnixListener;
use std::path::{Path, PathBuf, StripPrefixError};
use std::{fmt, io};
use uucore::LocalizedCommand;
Expand Down Expand Up @@ -2073,6 +2075,7 @@ fn handle_copy_mode(
symlinked_files: &mut HashSet<FileInformation>,
source_in_command_line: bool,
source_is_fifo: bool,
source_is_socket: bool,
#[cfg(unix)] source_is_stream: bool,
) -> CopyResult<PerformedAction> {
let source_is_symlink = source_metadata.is_symlink();
Expand Down Expand Up @@ -2112,6 +2115,7 @@ fn handle_copy_mode(
context,
source_is_symlink,
source_is_fifo,
source_is_socket,
symlinked_files,
#[cfg(unix)]
source_is_stream,
Expand All @@ -2134,6 +2138,7 @@ fn handle_copy_mode(
context,
source_is_symlink,
source_is_fifo,
source_is_socket,
symlinked_files,
#[cfg(unix)]
source_is_stream,
Expand Down Expand Up @@ -2169,6 +2174,7 @@ fn handle_copy_mode(
context,
source_is_symlink,
source_is_fifo,
source_is_socket,
symlinked_files,
#[cfg(unix)]
source_is_stream,
Expand All @@ -2183,6 +2189,7 @@ fn handle_copy_mode(
context,
source_is_symlink,
source_is_fifo,
source_is_socket,
symlinked_files,
#[cfg(unix)]
source_is_stream,
Expand Down Expand Up @@ -2409,8 +2416,12 @@ fn copy_file(

#[cfg(unix)]
let source_is_fifo = source_metadata.file_type().is_fifo();
#[cfg(unix)]
let source_is_socket = source_metadata.file_type().is_socket();
#[cfg(not(unix))]
let source_is_fifo = false;
#[cfg(not(unix))]
let source_is_socket = false;

let source_is_stream = is_stream(&source_metadata);

Expand All @@ -2423,6 +2434,7 @@ fn copy_file(
symlinked_files,
source_in_command_line,
source_is_fifo,
source_is_socket,
#[cfg(unix)]
source_is_stream,
)?;
Expand Down Expand Up @@ -2549,6 +2561,7 @@ fn copy_helper(
context: &str,
source_is_symlink: bool,
source_is_fifo: bool,
source_is_socket: bool,
symlinked_files: &mut HashSet<FileInformation>,
#[cfg(unix)] source_is_stream: bool,
) -> CopyResult<()> {
Expand All @@ -2561,7 +2574,10 @@ fn copy_helper(
return Err(CpError::NotADirectory(dest.to_path_buf()));
}

if source_is_fifo && options.recursive && !options.copy_contents {
if source_is_socket && options.recursive && !options.copy_contents {
#[cfg(unix)]
copy_socket(dest, options.overwrite, options.debug)?;
} else if source_is_fifo && options.recursive && !options.copy_contents {
#[cfg(unix)]
copy_fifo(dest, options.overwrite, options.debug)?;
} else if source_is_symlink {
Expand Down Expand Up @@ -2598,6 +2614,17 @@ fn copy_fifo(dest: &Path, overwrite: OverwriteMode, debug: bool) -> CopyResult<(
.map_err(|_| translate!("cp-error-cannot-create-fifo", "path" => dest.quote()).into())
}

#[cfg(unix)]
fn copy_socket(dest: &Path, overwrite: OverwriteMode, debug: bool) -> CopyResult<()> {
if dest.exists() {
overwrite.verify(dest, debug)?;
fs::remove_file(dest)?;
}

UnixListener::bind(dest)?;
Ok(())
}

fn copy_link(
source: &Path,
dest: &Path,
Expand Down
27 changes: 24 additions & 3 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// file that was distributed with this source code.

// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE clob btrfs neve ROOTDIR USERDIR outfile uufs xattrs
// spell-checker:ignore bdfl hlsl IRWXO IRWXG nconfined matchpathcon libselinux-devel prwx doesnotexist reftests subdirs
// spell-checker:ignore bdfl hlsl IRWXO IRWXG nconfined matchpathcon libselinux-devel prwx doesnotexist reftests subdirs mksocket srwx
use uucore::display::Quotable;
#[cfg(feature = "feat_selinux")]
use uucore::selinux::get_getfattr_output;
Expand All @@ -18,10 +18,10 @@ use std::io::Write;
#[cfg(not(windows))]
use std::os::unix::fs;

#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
#[cfg(unix)]
use std::os::unix::fs::{FileTypeExt, MetadataExt};
#[cfg(windows)]
use std::os::windows::fs::symlink_file;
#[cfg(not(windows))]
Expand Down Expand Up @@ -3105,6 +3105,27 @@ fn test_cp_fifo() {
assert_eq!(permission, "prwx-wx--x".to_string());
}

#[test]
#[cfg(unix)]
fn test_cp_socket() {
let (at, mut ucmd) = at_and_ucmd!();
at.mksocket("socket");
// Also test that permissions are preserved
at.set_mode("socket", 0o731);
ucmd.arg("--preserve=mode")
.arg("-r")
.arg("socket")
.arg("socket2")
.succeeds()
.no_stderr()
.no_stdout();

let metadata = std::fs::metadata(at.subdir.join("socket2")).unwrap();
let permission = uucore::fs::display_permissions(&metadata, true);
assert!(metadata.file_type().is_socket());
assert_eq!(permission, "srwx-wx--x".to_string());
}

#[cfg(all(unix, not(target_vendor = "apple")))]
fn find_other_group(current: u32) -> Option<u32> {
// Get the first group that doesn't match current
Expand Down
11 changes: 10 additions & 1 deletion tests/uutests/src/lib/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//spell-checker: ignore (linux) rlimit prlimit coreutil ggroups uchild uncaptured scmd SHLVL canonicalized openpty
//spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus tmpfs
//spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus tmpfs mksocket

#![allow(dead_code)]
#![allow(
Expand Down Expand Up @@ -34,6 +34,8 @@ use std::os::fd::OwnedFd;
#[cfg(unix)]
use std::os::unix::fs::{PermissionsExt, symlink as symlink_dir, symlink as symlink_file};
#[cfg(unix)]
use std::os::unix::net::UnixListener;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
Expand Down Expand Up @@ -1132,6 +1134,13 @@ impl AtPath {
}
}

#[cfg(unix)]
pub fn mksocket(&self, socket: &str) {
let full_path = self.plus_as_string(socket);
log_info("mksocket", &full_path);
UnixListener::bind(full_path).expect("Socket file creation failed.");
}

#[cfg(not(windows))]
pub fn is_fifo(&self, fifo: &str) -> bool {
unsafe {
Expand Down
Loading