1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Fix: install /dev/null target (#1646)

* fix(install): workaround the /dev/null bug

Caused by a limitation of fs::copy in rust. see:
https://github.com/rust-lang/rust/issues/79390
This commit is contained in:
Sylvestre Ledru 2021-02-11 15:59:58 +01:00 committed by GitHub
parent 6c8af26e7f
commit 51383e10e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 14 deletions

View file

@ -17,6 +17,7 @@ use uucore::entries::{grp2gid, usr2uid};
use uucore::perms::{wrap_chgrp, wrap_chown, Verbosity}; use uucore::perms::{wrap_chgrp, wrap_chown, Verbosity};
use std::fs; use std::fs;
use std::fs::File;
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::result::Result; use std::result::Result;
@ -419,7 +420,7 @@ fn standard(paths: Vec<String>, b: Behavior) -> i32 {
/// ///
fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> i32 { fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> i32 {
if !target_dir.is_dir() { if !target_dir.is_dir() {
show_error!("target {} is not a directory", target_dir.display()); show_error!("target '{}' is not a directory", target_dir.display());
return 1; return 1;
} }
@ -429,7 +430,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) ->
Some(name) => target_dir.join(name), Some(name) => target_dir.join(name),
None => { None => {
show_error!( show_error!(
"cannot stat {}: No such file or directory", "cannot stat '{}': No such file or directory",
sourcepath.display() sourcepath.display()
); );
@ -479,11 +480,22 @@ fn copy_file_to_file(file: &PathBuf, target: &PathBuf, b: &Behavior) -> i32 {
/// If the copy system call fails, we print a verbose error and return an empty error value. /// If the copy system call fails, we print a verbose error and return an empty error value.
/// ///
fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
let io_result = fs::copy(from, to); if from.to_string_lossy() == "/dev/null" {
/* workaround a limitation of fs::copy
if let Err(err) = io_result { * https://github.com/rust-lang/rust/issues/79390
*/
if let Err(err) = File::create(to) {
show_error!(
"install: cannot install '{}' to '{}': {}",
from.display(),
to.display(),
err
);
return Err(());
}
} else if let Err(err) = fs::copy(from, to) {
show_error!( show_error!(
"cannot install {} to {}: {}", "cannot install '{}' to '{}': {}",
from.display(), from.display(),
to.display(), to.display(),
err err

View file

@ -330,14 +330,6 @@ fn test_install_target_file_dev_null() {
let file1 = "/dev/null"; let file1 = "/dev/null";
let file2 = "test_install_target_file_file_i2"; let file2 = "test_install_target_file_file_i2";
at.touch(file1);
at.touch(file2);
ucmd.arg(file1).arg(file2).fails();
/* Uncomment when fixed
ucmd.arg(file1).arg(file2).succeeds().no_stderr(); ucmd.arg(file1).arg(file2).succeeds().no_stderr();
assert!(at.file_exists(file1));
assert!(at.file_exists(file2)); assert!(at.file_exists(file2));
*/
} }