Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0d858f4
install: implement copying from streams
DaringCuteSeal Nov 28, 2024
6354202
uucore: add common splice-write functionality
DaringCuteSeal Dec 17, 2024
19d3094
cat: use Linux splice functionality from uucore
DaringCuteSeal Dec 17, 2024
7bc1309
install: use Linux splice functionality from uucore
DaringCuteSeal Dec 17, 2024
82887c5
cp: implement copying special files for unix
DaringCuteSeal Dec 17, 2024
f58f5b5
tests/cp: add test for copying from anonymous pipe stream
DaringCuteSeal Dec 17, 2024
2ebcadf
cp: fix Cargo.toml formatting
DaringCuteSeal Dec 17, 2024
eb4506d
install: fix Cargo.toml formatting
DaringCuteSeal Dec 17, 2024
d800960
cp: remove file mode functionality import for windows
DaringCuteSeal Dec 17, 2024
f1e077d
cp: correctly exit for unix under copy function at platform/other
DaringCuteSeal Dec 17, 2024
fc3fb38
cat: correct i/o handle trait for non-unix systems
DaringCuteSeal Dec 17, 2024
077030e
cp: fix style by collapsing if statement
DaringCuteSeal Dec 17, 2024
0871eca
cp: allow too many arguments for clippy on copy_helper
DaringCuteSeal Dec 17, 2024
7f400f1
cp: only check if file is fifo on unix
DaringCuteSeal Dec 17, 2024
fd066f1
cp: remove unnecessary borrow on copy_on_write under platform/other
DaringCuteSeal Dec 17, 2024
c7cfd14
cp: fix platform-dependent code
DaringCuteSeal Dec 17, 2024
011362c
uucore: correct conditions for splice module exporting
DaringCuteSeal Dec 17, 2024
c3e7c4e
cp: removed unused imports for windows
DaringCuteSeal Dec 17, 2024
223f7fc
uucore/splice: fix import name
DaringCuteSeal Dec 17, 2024
9076413
uucore/splice: conditional importing for pipe functionalities
DaringCuteSeal Dec 17, 2024
7509998
uucore: only export splice module in linux and android
DaringCuteSeal Dec 17, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/uu/cat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ uucore = { workspace = true, features = ["fs", "pipes"] }
[target.'cfg(unix)'.dependencies]
nix = { workspace = true }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
uucore = { workspace = true, features = ["splice"] }

[[bin]]
name = "cat"
path = "src/main.rs"
11 changes: 6 additions & 5 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use uucore::display::Quotable;
use uucore::error::UResult;
use uucore::fs::FileInformation;

#[cfg(unix)]
use std::os::fd::{AsFd, AsRawFd};

/// Linux splice support
#[cfg(any(target_os = "linux", target_os = "android"))]
mod splice;
use uucore::splice::write_fast_using_splice;

#[cfg(unix)]
use std::os::fd::{AsFd, AsRawFd};

/// Unix domain socket support
#[cfg(unix)]
Expand All @@ -26,6 +26,7 @@ use std::net::Shutdown;
use std::os::unix::fs::FileTypeExt;
#[cfg(unix)]
use std::os::unix::net::UnixStream;

use uucore::{format_usage, help_about, help_usage};

const USAGE: &str = help_usage!("cat.md");
Expand Down Expand Up @@ -454,7 +455,7 @@ fn write_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
{
// If we're on Linux or Android, try to use the splice() system call
// for faster writing. If it works, we're done.
if !splice::write_fast_using_splice(handle, &stdout_lock)? {
if !write_fast_using_splice(&handle.reader, &stdout_lock)?.1 {
return Ok(());
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/uu/cp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ indicatif = { workspace = true }
xattr = { workspace = true }
exacl = { workspace = true, optional = true }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
uucore = { workspace = true, features = ["splice"] }

[[bin]]
name = "cp"
path = "src/main.rs"
Expand Down
53 changes: 41 additions & 12 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::{HashMap, HashSet};
#[cfg(not(windows))]
use std::ffi::CString;
use std::ffi::OsString;
use std::fs::{self, File, Metadata, OpenOptions, Permissions};
use std::fs::{self, Metadata, OpenOptions, Permissions};
use std::io;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
Expand Down Expand Up @@ -1933,10 +1933,12 @@ fn handle_copy_mode(

let source_is_symlink = source_file_type.is_symlink();

#[cfg(unix)]
let source_is_stream = source_file_type.is_fifo()
|| source_file_type.is_block_device()
|| source_file_type.is_char_device();
#[cfg(unix)]
let source_is_fifo = source_file_type.is_fifo();
#[cfg(not(unix))]
let source_is_fifo = false;

match options.copy_mode {
CopyMode::Link => {
Expand Down Expand Up @@ -1971,7 +1973,10 @@ fn handle_copy_mode(
options,
context,
source_is_symlink,
#[cfg(unix)]
source_is_fifo,
#[cfg(unix)]
source_is_stream,
symlinked_files,
)?;
}
Expand All @@ -1991,7 +1996,10 @@ fn handle_copy_mode(
options,
context,
source_is_symlink,
#[cfg(unix)]
source_is_fifo,
#[cfg(unix)]
source_is_stream,
symlinked_files,
)?;
}
Expand Down Expand Up @@ -2021,7 +2029,10 @@ fn handle_copy_mode(
options,
context,
source_is_symlink,
#[cfg(unix)]
source_is_fifo,
#[cfg(unix)]
source_is_stream,
symlinked_files,
)?;
}
Expand All @@ -2034,7 +2045,10 @@ fn handle_copy_mode(
options,
context,
source_is_symlink,
#[cfg(unix)]
source_is_fifo,
#[cfg(unix)]
source_is_stream,
symlinked_files,
)?;
}
Expand Down Expand Up @@ -2284,6 +2298,19 @@ fn copy_file(
}

if options.dereference(source_in_command_line) {
// HACK: when `cp` reads from inter-process pipe, its original pipe would've been
// inaccessible once we reach this part of the code as it has been closed. That means we
// can't dereference it to get its attribute.
// find a more reliable way to distinguish between named pipes and anonymous pipes.
#[cfg(unix)]
if source_metadata.file_type().is_fifo() {
copy_attributes(source, dest, &options.attributes)?;
} else if let Ok(src) = canonicalize(source, MissingHandling::Normal, ResolveMode::Physical)
{
copy_attributes(&src, dest, &options.attributes)?;
}

#[cfg(not(unix))]
if let Ok(src) = canonicalize(source, MissingHandling::Normal, ResolveMode::Physical) {
copy_attributes(&src, dest, &options.attributes)?;
}
Expand Down Expand Up @@ -2350,13 +2377,15 @@ fn handle_no_preserve_mode(options: &Options, org_mode: u32) -> u32 {

/// 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.
#[allow(clippy::too_many_arguments)]
fn copy_helper(
source: &Path,
dest: &Path,
options: &Options,
context: &str,
source_is_symlink: bool,
source_is_fifo: bool,
#[cfg(unix)] source_is_fifo: bool,
#[cfg(unix)] source_is_stream: bool,
symlinked_files: &mut HashSet<FileInformation>,
) -> CopyResult<()> {
if options.parents {
Expand All @@ -2368,15 +2397,13 @@ fn copy_helper(
return Err(Error::NotADirectory(dest.to_path_buf()));
}

if source.as_os_str() == "/dev/null" {
/* workaround a limitation of fs::copy
* https://github.com/rust-lang/rust/issues/79390
*/
File::create(dest).context(dest.display().to_string())?;
} else if source_is_fifo && options.recursive && !options.copy_contents {
#[cfg(unix)]
if source_is_fifo && options.recursive && !options.copy_contents {
#[cfg(unix)]
copy_fifo(dest, options.overwrite, options.debug)?;
} else if source_is_symlink {
return Ok(());
}
if source_is_symlink {
copy_link(source, dest, symlinked_files)?;
} else {
let copy_debug = copy_on_write(
Expand All @@ -2385,7 +2412,9 @@ fn copy_helper(
options.reflink_mode,
options.sparse_mode,
context,
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
#[cfg(unix)]
source_is_stream,
#[cfg(unix)]
source_is_fifo,
)?;

Expand Down
74 changes: 52 additions & 22 deletions src/uu/cp/src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use libc::{SEEK_DATA, SEEK_HOLE};
use std::fs::{File, OpenOptions};
use std::io::Read;
use std::io::{Read, Write};
use std::os::fd::AsFd;
use std::os::unix::fs::FileExt;
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::{FileTypeExt, OpenOptionsExt};
use std::os::unix::io::AsRawFd;
use std::path::Path;

use uucore::splice::write_fast_using_splice;

use quick_error::ResultExt;

use uucore::mode::get_umask;
Expand Down Expand Up @@ -220,8 +223,8 @@ fn check_dest_is_fifo(dest: &Path) -> bool {
}
}

/// Copy the contents of the given source FIFO to the given file.
fn copy_fifo_contents<P>(source: P, dest: P) -> std::io::Result<u64>
/// Copy the contents of the given source stream to a given file.
fn copy_stream_contents<P>(is_pipe: bool, source: P, dest: P) -> std::io::Result<u64>
where
P: AsRef<Path>,
{
Expand Down Expand Up @@ -250,23 +253,50 @@ where
.write(true)
.mode(mode)
.open(&dest)?;
let num_bytes_copied = std::io::copy(&mut src_file, &mut dst_file)?;
dst_file.set_permissions(src_file.metadata()?.permissions())?;
Ok(num_bytes_copied)

let num_bytes_copied;
#[cfg(any(target_os = "linux", target_os = "android"))]
{
// If we're on Linux or Android, try to use the splice() system call
// for faster writing. If it works, we're done.
let res = write_fast_using_splice(&src_file, &dst_file.as_fd())?;
if !res.1 {
num_bytes_copied = res.0;
} else {
num_bytes_copied = std::io::copy(&mut src_file, &mut dst_file)?
.try_into()
.unwrap();
dst_file.flush()?;
}
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
{
num_bytes_copied = std::io::copy(&mut src_file, &mut dst_file)?
.try_into()
.unwrap();
}

// If stream is a character or block device, we don't need to take care of its permissions for
// now. See: https://github.com/uutils/coreutils/pull/4211
if is_pipe {
dst_file.set_permissions(src_file.metadata()?.permissions())?;
}

Ok(num_bytes_copied.try_into().unwrap())
}

/// Copies `source` to `dest` using copy-on-write if possible.
///
/// The `source_is_fifo` flag must be set to `true` if and only if
/// `source` is a FIFO (also known as a named pipe). In this case,
/// copy-on-write is not possible, so we copy the contents using
/// [`std::io::copy`].
/// The `source_is_stream` flag must be set to `true` if and only if
/// `source` is a stream (i.e FIFOs, block/character devices).
pub(crate) fn copy_on_write(
source: &Path,
dest: &Path,
reflink_mode: ReflinkMode,
sparse_mode: SparseMode,
context: &str,
source_is_stream: bool,
source_is_fifo: bool,
) -> CopyResult<CopyDebug> {
let mut copy_debug = CopyDebug {
Expand All @@ -279,10 +309,10 @@ pub(crate) fn copy_on_write(
copy_debug.sparse_detection = SparseDebug::Zeros;
// Default SparseDebug val for SparseMode::Always
copy_debug.reflink = OffloadReflinkDebug::No;
if source_is_fifo {
if source_is_stream {
copy_debug.offload = OffloadReflinkDebug::Avoided;

copy_fifo_contents(source, dest).map(|_| ())
copy_stream_contents(source_is_fifo, source, dest).map(|_| ())
} else {
let mut copy_method = CopyMethod::Default;
let result = handle_reflink_never_sparse_always(source, dest);
Expand All @@ -300,10 +330,10 @@ pub(crate) fn copy_on_write(
(ReflinkMode::Never, SparseMode::Never) => {
copy_debug.reflink = OffloadReflinkDebug::No;

if source_is_fifo {
if source_is_stream {
copy_debug.offload = OffloadReflinkDebug::Avoided;

copy_fifo_contents(source, dest).map(|_| ())
copy_stream_contents(source_is_fifo, source, dest).map(|_| ())
} else {
let result = handle_reflink_never_sparse_never(source);
if let Ok(debug) = result {
Expand All @@ -315,9 +345,9 @@ pub(crate) fn copy_on_write(
(ReflinkMode::Never, SparseMode::Auto) => {
copy_debug.reflink = OffloadReflinkDebug::No;

if source_is_fifo {
if source_is_stream {
copy_debug.offload = OffloadReflinkDebug::Avoided;
copy_fifo_contents(source, dest).map(|_| ())
copy_stream_contents(source_is_fifo, source, dest).map(|_| ())
} else {
let mut copy_method = CopyMethod::Default;
let result = handle_reflink_never_sparse_auto(source, dest);
Expand All @@ -335,10 +365,10 @@ pub(crate) fn copy_on_write(
(ReflinkMode::Auto, SparseMode::Always) => {
copy_debug.sparse_detection = SparseDebug::Zeros; // Default SparseDebug val for
// SparseMode::Always
if source_is_fifo {
if source_is_stream {
copy_debug.offload = OffloadReflinkDebug::Avoided;

copy_fifo_contents(source, dest).map(|_| ())
copy_stream_contents(source_is_fifo, source, dest).map(|_| ())
} else {
let mut copy_method = CopyMethod::Default;
let result = handle_reflink_auto_sparse_always(source, dest);
Expand All @@ -356,9 +386,9 @@ pub(crate) fn copy_on_write(

(ReflinkMode::Auto, SparseMode::Never) => {
copy_debug.reflink = OffloadReflinkDebug::No;
if source_is_fifo {
if source_is_stream {
copy_debug.offload = OffloadReflinkDebug::Avoided;
copy_fifo_contents(source, dest).map(|_| ())
copy_stream_contents(source_is_fifo, source, dest).map(|_| ())
} else {
let result = handle_reflink_auto_sparse_never(source);
if let Ok(debug) = result {
Expand All @@ -369,9 +399,9 @@ pub(crate) fn copy_on_write(
}
}
(ReflinkMode::Auto, SparseMode::Auto) => {
if source_is_fifo {
if source_is_stream {
copy_debug.offload = OffloadReflinkDebug::Unsupported;
copy_fifo_contents(source, dest).map(|_| ())
copy_stream_contents(source_is_fifo, source, dest).map(|_| ())
} else {
let mut copy_method = CopyMethod::Default;
let result = handle_reflink_auto_sparse_auto(source, dest);
Expand Down
11 changes: 7 additions & 4 deletions src/uu/cp/src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ use crate::{CopyDebug, CopyResult, OffloadReflinkDebug, ReflinkMode, SparseDebug

/// Copies `source` to `dest` using copy-on-write if possible.
///
/// The `source_is_fifo` flag must be set to `true` if and only if
/// `source` is a FIFO (also known as a named pipe).
/// The `source_is_stream` flag must be set to `true` if and only if
/// `source` is a stream (i.e FIFOs, block/character devices)
pub(crate) fn copy_on_write(
source: &Path,
dest: &Path,
reflink_mode: ReflinkMode,
sparse_mode: SparseMode,
context: &str,
source_is_fifo: bool,
source_is_stream: bool,
#[allow(unused_variables)] source_is_fifo: bool,
) -> CopyResult<CopyDebug> {
if sparse_mode != SparseMode::Auto {
return Err("--sparse is only supported on linux".to_string().into());
Expand Down Expand Up @@ -85,7 +86,9 @@ pub(crate) fn copy_on_write(
}
_ => {
copy_debug.reflink = OffloadReflinkDebug::Yes;
if source_is_fifo {
if source_is_stream {
// `fs::copy` does not support copying special files (including streams).
// Instead, `cp` would have to make the files then use io::copy.
let mut src_file = File::open(source)?;
let mut dst_file = File::create(dest)?;
io::copy(&mut src_file, &mut dst_file).context(context)?
Expand Down
Loading
Loading