mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
Merge pull request #3978 from jfinkels/cp-copy-contents-fifo
cp: implement --copy-contents option for fifos
This commit is contained in:
commit
47e3149e85
4 changed files with 87 additions and 22 deletions
|
@ -677,7 +677,6 @@ fn add_all_attributes() -> Vec<Attribute> {
|
||||||
impl Options {
|
impl Options {
|
||||||
fn from_matches(matches: &ArgMatches) -> CopyResult<Self> {
|
fn from_matches(matches: &ArgMatches) -> CopyResult<Self> {
|
||||||
let not_implemented_opts = vec![
|
let not_implemented_opts = vec![
|
||||||
options::COPY_CONTENTS,
|
|
||||||
#[cfg(not(any(windows, unix)))]
|
#[cfg(not(any(windows, unix)))]
|
||||||
options::ONE_FILE_SYSTEM,
|
options::ONE_FILE_SYSTEM,
|
||||||
options::CONTEXT,
|
options::CONTEXT,
|
||||||
|
@ -1457,7 +1456,7 @@ fn copy_helper(
|
||||||
* https://github.com/rust-lang/rust/issues/79390
|
* https://github.com/rust-lang/rust/issues/79390
|
||||||
*/
|
*/
|
||||||
File::create(dest).context(dest.display().to_string())?;
|
File::create(dest).context(dest.display().to_string())?;
|
||||||
} else if source_is_fifo && options.recursive {
|
} else if source_is_fifo && options.recursive && !options.copy_contents {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
copy_fifo(dest, options.overwrite)?;
|
copy_fifo(dest, options.overwrite)?;
|
||||||
} else if source_is_symlink {
|
} else if source_is_symlink {
|
||||||
|
@ -1469,6 +1468,8 @@ fn copy_helper(
|
||||||
options.reflink_mode,
|
options.reflink_mode,
|
||||||
options.sparse_mode,
|
options.sparse_mode,
|
||||||
context,
|
context,
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
|
||||||
|
source_is_fifo,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,29 +21,39 @@ macro_rules! FICLONE {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The fallback behavior for [`clone`] on failed system call.
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
enum CloneFallback {
|
||||||
|
/// Raise an error.
|
||||||
|
Error,
|
||||||
|
|
||||||
|
/// Use [`std::io::copy`].
|
||||||
|
IOCopy,
|
||||||
|
|
||||||
|
/// Use [`std::fs::copy`].
|
||||||
|
FSCopy,
|
||||||
|
}
|
||||||
|
|
||||||
/// Use the Linux `ioctl_ficlone` API to do a copy-on-write clone.
|
/// Use the Linux `ioctl_ficlone` API to do a copy-on-write clone.
|
||||||
///
|
///
|
||||||
/// If `fallback` is true and there is a failure performing the clone,
|
/// `fallback` controls what to do if the system call fails.
|
||||||
/// then this function performs a standard [`std::fs::copy`]. Otherwise,
|
|
||||||
/// this function returns an error.
|
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
fn clone<P>(source: P, dest: P, fallback: bool) -> std::io::Result<()>
|
fn clone<P>(source: P, dest: P, fallback: CloneFallback) -> std::io::Result<()>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
{
|
{
|
||||||
let src_file = File::open(&source)?;
|
let mut src_file = File::open(&source)?;
|
||||||
let dst_file = File::create(&dest)?;
|
let mut dst_file = File::create(&dest)?;
|
||||||
let src_fd = src_file.as_raw_fd();
|
let src_fd = src_file.as_raw_fd();
|
||||||
let dst_fd = dst_file.as_raw_fd();
|
let dst_fd = dst_file.as_raw_fd();
|
||||||
let result = unsafe { libc::ioctl(dst_fd, FICLONE!(), src_fd) };
|
let result = unsafe { libc::ioctl(dst_fd, FICLONE!(), src_fd) };
|
||||||
if result != 0 {
|
if result == 0 {
|
||||||
if fallback {
|
return Ok(());
|
||||||
std::fs::copy(source, dest).map(|_| ())
|
}
|
||||||
} else {
|
match fallback {
|
||||||
Err(std::io::Error::last_os_error())
|
CloneFallback::Error => Err(std::io::Error::last_os_error()),
|
||||||
}
|
CloneFallback::IOCopy => std::io::copy(&mut src_file, &mut dst_file).map(|_| ()),
|
||||||
} else {
|
CloneFallback::FSCopy => std::fs::copy(source, dest).map(|_| ()),
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,18 +99,31 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copies `source` to `dest` using copy-on-write if possible.
|
/// 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`].
|
||||||
pub(crate) fn copy_on_write(
|
pub(crate) fn copy_on_write(
|
||||||
source: &Path,
|
source: &Path,
|
||||||
dest: &Path,
|
dest: &Path,
|
||||||
reflink_mode: ReflinkMode,
|
reflink_mode: ReflinkMode,
|
||||||
sparse_mode: SparseMode,
|
sparse_mode: SparseMode,
|
||||||
context: &str,
|
context: &str,
|
||||||
|
source_is_fifo: bool,
|
||||||
) -> CopyResult<()> {
|
) -> CopyResult<()> {
|
||||||
let result = match (reflink_mode, sparse_mode) {
|
let result = match (reflink_mode, sparse_mode) {
|
||||||
(ReflinkMode::Never, _) => std::fs::copy(source, dest).map(|_| ()),
|
(ReflinkMode::Never, _) => std::fs::copy(source, dest).map(|_| ()),
|
||||||
(ReflinkMode::Auto, SparseMode::Always) => sparse_copy(source, dest),
|
(ReflinkMode::Auto, SparseMode::Always) => sparse_copy(source, dest),
|
||||||
(ReflinkMode::Auto, _) => clone(source, dest, true),
|
|
||||||
(ReflinkMode::Always, SparseMode::Auto) => clone(source, dest, false),
|
(ReflinkMode::Auto, _) => {
|
||||||
|
if source_is_fifo {
|
||||||
|
clone(source, dest, CloneFallback::IOCopy)
|
||||||
|
} else {
|
||||||
|
clone(source, dest, CloneFallback::FSCopy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(ReflinkMode::Always, SparseMode::Auto) => clone(source, dest, CloneFallback::Error),
|
||||||
(ReflinkMode::Always, _) => {
|
(ReflinkMode::Always, _) => {
|
||||||
return Err("`--reflink=always` can be used only with --sparse=auto".into())
|
return Err("`--reflink=always` can be used only with --sparse=auto".into())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
// * file that was distributed with this source code.
|
// * file that was distributed with this source code.
|
||||||
// spell-checker:ignore reflink
|
// spell-checker:ignore reflink
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::fs;
|
use std::fs::{self, File};
|
||||||
|
use std::io;
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
@ -13,12 +14,16 @@ use quick_error::ResultExt;
|
||||||
use crate::{CopyResult, ReflinkMode, SparseMode};
|
use crate::{CopyResult, ReflinkMode, SparseMode};
|
||||||
|
|
||||||
/// Copies `source` to `dest` using copy-on-write if possible.
|
/// 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).
|
||||||
pub(crate) fn copy_on_write(
|
pub(crate) fn copy_on_write(
|
||||||
source: &Path,
|
source: &Path,
|
||||||
dest: &Path,
|
dest: &Path,
|
||||||
reflink_mode: ReflinkMode,
|
reflink_mode: ReflinkMode,
|
||||||
sparse_mode: SparseMode,
|
sparse_mode: SparseMode,
|
||||||
context: &str,
|
context: &str,
|
||||||
|
source_is_fifo: bool,
|
||||||
) -> CopyResult<()> {
|
) -> CopyResult<()> {
|
||||||
if sparse_mode != SparseMode::Auto {
|
if sparse_mode != SparseMode::Auto {
|
||||||
return Err("--sparse is only supported on linux".to_string().into());
|
return Err("--sparse is only supported on linux".to_string().into());
|
||||||
|
@ -65,8 +70,15 @@ pub(crate) fn copy_on_write(
|
||||||
format!("failed to clone {:?} from {:?}: {}", source, dest, error).into(),
|
format!("failed to clone {:?} from {:?}: {}", source, dest, error).into(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ReflinkMode::Auto => fs::copy(source, dest).context(context)?,
|
_ => {
|
||||||
ReflinkMode::Never => fs::copy(source, dest).context(context)?,
|
if source_is_fifo {
|
||||||
|
let mut src_file = File::open(source)?;
|
||||||
|
let mut dst_file = File::create(dest)?;
|
||||||
|
io::copy(&mut src_file, &mut dst_file).context(context)?
|
||||||
|
} else {
|
||||||
|
fs::copy(source, dest).context(context)?
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE clob btrfs ROOTDIR USERDIR procfs
|
// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE clob btrfs ROOTDIR USERDIR procfs outfile
|
||||||
|
|
||||||
use crate::common::util::*;
|
use crate::common::util::*;
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
|
@ -2251,3 +2251,32 @@ fn test_copy_dir_preserve_permissions_inaccessible_file() {
|
||||||
let metadata2 = at.metadata("d2");
|
let metadata2 = at.metadata("d2");
|
||||||
assert_metadata_eq!(metadata1, metadata2);
|
assert_metadata_eq!(metadata1, metadata2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test for copying the contents of a FIFO as opposed to the FIFO object itself.
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[test]
|
||||||
|
fn test_copy_contents_fifo() {
|
||||||
|
let scenario = TestScenario::new(util_name!());
|
||||||
|
let at = &scenario.fixtures;
|
||||||
|
|
||||||
|
// Start the `cp` process, reading the contents of `fifo` and
|
||||||
|
// writing to regular file `outfile`.
|
||||||
|
at.mkfifo("fifo");
|
||||||
|
let mut ucmd = scenario.ucmd();
|
||||||
|
let child = ucmd
|
||||||
|
.args(&["--copy-contents", "fifo", "outfile"])
|
||||||
|
.run_no_wait();
|
||||||
|
|
||||||
|
// Write some bytes to the `fifo`. We expect these bytes to get
|
||||||
|
// copied through to `outfile`.
|
||||||
|
std::fs::write(at.plus("fifo"), "foo").unwrap();
|
||||||
|
|
||||||
|
// At this point the child process should have terminated
|
||||||
|
// successfully with no output. The `outfile` should have the
|
||||||
|
// contents of `fifo` copied into it.
|
||||||
|
let output = child.wait_with_output().unwrap();
|
||||||
|
assert!(output.status.success());
|
||||||
|
assert!(output.stdout.is_empty());
|
||||||
|
assert!(output.stderr.is_empty());
|
||||||
|
assert_eq!(at.read("outfile"), "foo");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue