mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #2912 from jfinkels/truncate-create-non-existent-file
truncate: create non-existent file by default
This commit is contained in:
commit
b816e80e2f
2 changed files with 35 additions and 3 deletions
|
@ -278,9 +278,16 @@ fn truncate_size_only(
|
||||||
Err(e) => crash!(1, "Invalid number: {}", e.to_string()),
|
Err(e) => crash!(1, "Invalid number: {}", e.to_string()),
|
||||||
};
|
};
|
||||||
for filename in &filenames {
|
for filename in &filenames {
|
||||||
let fsize = usize::try_from(metadata(filename)?.len()).unwrap();
|
let fsize = match metadata(filename) {
|
||||||
let tsize = mode.to_size(fsize);
|
Ok(m) => m.len(),
|
||||||
file_truncate(filename, create, tsize)?;
|
Err(_) => 0,
|
||||||
|
};
|
||||||
|
let tsize = mode.to_size(fsize as usize);
|
||||||
|
match file_truncate(filename, create, tsize) {
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(e) if e.kind() == ErrorKind::NotFound && !create => continue,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -321,3 +321,28 @@ fn test_truncate_bytes_size() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test that truncating a non-existent file creates that file.
|
||||||
|
#[test]
|
||||||
|
fn test_new_file() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let filename = "new_file_that_does_not_exist_yet";
|
||||||
|
ucmd.args(&["-s", "8", filename])
|
||||||
|
.succeeds()
|
||||||
|
.no_stdout()
|
||||||
|
.no_stderr();
|
||||||
|
assert!(at.file_exists(filename));
|
||||||
|
assert_eq!(at.read_bytes(filename), vec![b'\0'; 8]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test for not creating a non-existent file.
|
||||||
|
#[test]
|
||||||
|
fn test_new_file_no_create() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let filename = "new_file_that_does_not_exist_yet";
|
||||||
|
ucmd.args(&["-s", "8", "-c", filename])
|
||||||
|
.succeeds()
|
||||||
|
.no_stdout()
|
||||||
|
.no_stderr();
|
||||||
|
assert!(!at.file_exists(filename));
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue