mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
truncate: don't error in --no-create with reference case
This commit is contained in:
parent
4fcf912c85
commit
a3ab064f35
2 changed files with 38 additions and 12 deletions
|
@ -180,8 +180,11 @@ pub fn uu_app() -> Command {
|
||||||
/// size of the file.
|
/// size of the file.
|
||||||
fn file_truncate(filename: &str, create: bool, size: u64) -> std::io::Result<()> {
|
fn file_truncate(filename: &str, create: bool, size: u64) -> std::io::Result<()> {
|
||||||
let path = Path::new(filename);
|
let path = Path::new(filename);
|
||||||
let f = OpenOptions::new().write(true).create(create).open(path)?;
|
match OpenOptions::new().write(true).create(create).open(path) {
|
||||||
f.set_len(size)
|
Ok(file) => file.set_len(size),
|
||||||
|
Err(e) if e.kind() == ErrorKind::NotFound && !create => Ok(()),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Truncate files to a size relative to a given file.
|
/// Truncate files to a size relative to a given file.
|
||||||
|
@ -337,15 +340,8 @@ fn truncate_size_only(size_string: &str, filenames: &[String], create: bool) ->
|
||||||
Err(_) => 0,
|
Err(_) => 0,
|
||||||
};
|
};
|
||||||
let tsize = mode.to_size(fsize);
|
let tsize = mode.to_size(fsize);
|
||||||
match file_truncate(filename, create, tsize) {
|
file_truncate(filename, create, tsize)
|
||||||
Ok(_) => continue,
|
.map_err_context(|| format!("cannot open {} for writing", filename.quote()))?;
|
||||||
Err(e) if e.kind() == ErrorKind::NotFound && !create => continue,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(
|
|
||||||
e.map_err_context(|| format!("cannot open {} for writing", filename.quote()))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -268,7 +268,7 @@ fn test_new_file() {
|
||||||
|
|
||||||
/// Test for not creating a non-existent file.
|
/// Test for not creating a non-existent file.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_new_file_no_create() {
|
fn test_new_file_no_create_size_only() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
let filename = "new_file_that_does_not_exist_yet";
|
let filename = "new_file_that_does_not_exist_yet";
|
||||||
ucmd.args(&["-s", "8", "-c", filename])
|
ucmd.args(&["-s", "8", "-c", filename])
|
||||||
|
@ -278,6 +278,36 @@ fn test_new_file_no_create() {
|
||||||
assert!(!at.file_exists(filename));
|
assert!(!at.file_exists(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test for not creating a non-existent file.
|
||||||
|
#[test]
|
||||||
|
#[ignore = "other bug"]
|
||||||
|
fn test_new_file_no_create_reference_only() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let mut old_file = at.make_file(FILE1);
|
||||||
|
old_file.write_all(b"1234567890").unwrap();
|
||||||
|
let filename = "new_file_that_does_not_exist_yet";
|
||||||
|
ucmd.args(&["-r", FILE1, "-c", filename])
|
||||||
|
.succeeds()
|
||||||
|
.no_stdout()
|
||||||
|
.no_stderr();
|
||||||
|
assert!(!at.file_exists(filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test for not creating a non-existent file.
|
||||||
|
#[test]
|
||||||
|
#[ignore = "other bug"]
|
||||||
|
fn test_new_file_no_create_size_and_reference() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let mut old_file = at.make_file(FILE1);
|
||||||
|
old_file.write_all(b"1234567890").unwrap();
|
||||||
|
let filename = "new_file_that_does_not_exist_yet";
|
||||||
|
ucmd.args(&["-r", FILE1, "-s", "+8", "-c", filename])
|
||||||
|
.succeeds()
|
||||||
|
.no_stdout()
|
||||||
|
.no_stderr();
|
||||||
|
assert!(!at.file_exists(filename));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_division_by_zero_size_only() {
|
fn test_division_by_zero_size_only() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue