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
24 changes: 15 additions & 9 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,9 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
copy_files_into_dir(sources, &target, b)
} else {
let source = sources.first().unwrap();
let source_metadata = metadata_for_source(source)?;

if source.is_dir() {
if source_metadata.is_dir() {
return Err(InstallError::OmittingDirectory(source.clone()).into());
}

Expand Down Expand Up @@ -827,6 +828,11 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
}
}

fn metadata_for_source(path: &Path) -> UResult<fs::Metadata> {
path.metadata()
.map_err_context(|| format!("cannot stat {}", path.quote()))
}

/// Copy some files into a directory.
///
/// Prints verbose information and error messages.
Expand All @@ -842,15 +848,15 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UR
return Err(InstallError::TargetDirIsntDir(target_dir.to_path_buf()).into());
}
for sourcepath in files {
if let Err(err) = sourcepath
.metadata()
.map_err_context(|| format!("cannot stat {}", sourcepath.quote()))
{
show!(err);
continue;
}
let source_metadata = match metadata_for_source(sourcepath) {
Ok(metadata) => metadata,
Err(err) => {
show!(err);
continue;
}
};

if sourcepath.is_dir() {
if source_metadata.is_dir() {
let err = InstallError::OmittingDirectory(sourcepath.clone());
show!(err);
continue;
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ fn test_install_target_file() {
assert!(at.file_exists(file2));
}

#[test]
fn test_install_missing_source_reports_cannot_stat_with_path() {
new_ucmd!()
.arg("missing_source")
.arg("target_file")
.fails_with_code(1)
.stderr_contains("cannot stat 'missing_source': No such file or directory");
}

#[test]
fn test_install_target_new_file() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading