mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
install: handle when both -t and -D used together
This commit is contained in:
parent
81c45cde47
commit
2b70ccd61b
1 changed files with 35 additions and 22 deletions
|
@ -471,13 +471,19 @@ fn standard(mut paths: Vec<String>, b: &Behavior) -> UResult<()> {
|
||||||
if sources.len() > 1 || (target.exists() && target.is_dir()) {
|
if sources.len() > 1 || (target.exists() && target.is_dir()) {
|
||||||
copy_files_into_dir(sources, &target, b)
|
copy_files_into_dir(sources, &target, b)
|
||||||
} else {
|
} else {
|
||||||
if let Some(parent) = target.parent() {
|
// if -t is used in combination with -D, create whole target because it does not include filename
|
||||||
if !parent.exists() && b.create_leading {
|
let to_create: Option<&Path> = if b.target_dir.is_some() && b.create_leading {
|
||||||
|
Some(target.as_path())
|
||||||
|
} else {
|
||||||
|
target.parent()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(to_create) = to_create {
|
||||||
|
if !to_create.exists() && b.create_leading {
|
||||||
if b.verbose {
|
if b.verbose {
|
||||||
let mut result = PathBuf::new();
|
let mut result = PathBuf::new();
|
||||||
// When creating directories with -Dv, show directory creations step
|
// When creating directories with -Dv, show directory creations step by step
|
||||||
// by step
|
for part in to_create.components() {
|
||||||
for part in parent.components() {
|
|
||||||
result.push(part.as_os_str());
|
result.push(part.as_os_str());
|
||||||
if !Path::new(part.as_os_str()).is_dir() {
|
if !Path::new(part.as_os_str()).is_dir() {
|
||||||
// Don't display when the directory already exists
|
// Don't display when the directory already exists
|
||||||
|
@ -486,32 +492,39 @@ fn standard(mut paths: Vec<String>, b: &Behavior) -> UResult<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = fs::create_dir_all(parent) {
|
if let Err(e) = fs::create_dir_all(to_create) {
|
||||||
return Err(InstallError::CreateDirFailed(parent.to_path_buf(), e).into());
|
return Err(InstallError::CreateDirFailed(to_create.to_path_buf(), e).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Silent the warning as we want to the error message
|
// Silent the warning as we want to the error message
|
||||||
#[allow(clippy::question_mark)]
|
#[allow(clippy::question_mark)]
|
||||||
if mode::chmod(parent, b.mode()).is_err() {
|
if mode::chmod(to_create, b.mode()).is_err() {
|
||||||
return Err(InstallError::ChmodFailed(parent.to_path_buf()).into());
|
return Err(InstallError::ChmodFailed(to_create.to_path_buf()).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if target.is_file() || is_new_file_path(&target) {
|
let source = sources.first().ok_or_else(|| {
|
||||||
copy(
|
UUsageError::new(
|
||||||
sources.get(0).ok_or_else(|| {
|
1,
|
||||||
UUsageError::new(
|
format!(
|
||||||
1,
|
"missing destination file operand after '{}'",
|
||||||
format!(
|
target.to_str().unwrap()
|
||||||
"missing destination file operand after '{}'",
|
),
|
||||||
target.to_str().unwrap()
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})?,
|
|
||||||
&target,
|
|
||||||
b,
|
|
||||||
)
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// If the -D flag was passed (target does not include filename),
|
||||||
|
// we need to add the source name to the target_dir
|
||||||
|
// because `copy` expects `to` to be a file, not a directory
|
||||||
|
let target = if target.is_dir() && b.create_leading {
|
||||||
|
target.join(source)
|
||||||
|
} else {
|
||||||
|
target // already includes dest filename
|
||||||
|
};
|
||||||
|
|
||||||
|
if target.is_file() || is_new_file_path(&target) {
|
||||||
|
copy(source, &target, b)
|
||||||
} else {
|
} else {
|
||||||
Err(InstallError::InvalidTarget(target).into())
|
Err(InstallError::InvalidTarget(target).into())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue