split: Added error when attempting to create file that already exists as directory (#9945)

* split: Added error when attempting to create file that already exists as dir

* split: Added integration test test_split::test_split_directory_already_exists

* Fixed dependency error in windows.rs

* Modified test to work on systems without /dev/zero

* Attempt to fix windows error handling

* Removed test for windows and made it more rigorous

* Err made to look more like gnu

* Updated test to reflect change in err message
This commit is contained in:
Max Ambaum
2026-01-02 13:03:01 +01:00
committed by GitHub
parent 84e6f03ccb
commit 6ac9543169
4 changed files with 29 additions and 7 deletions
+1
View File
@@ -43,6 +43,7 @@ split-error-unable-to-reopen-file = unable to re-open { $file }; aborting
split-error-file-descriptor-limit = at file descriptor limit, but no file descriptor left to close. Closed { $count } writers before.
split-error-shell-process-returned = Shell process returned { $code }
split-error-shell-process-terminated = Shell process terminated by signal
split-error-is-a-directory = { $dir }: Is a directory
# Help messages for command-line options
split-help-bytes = put SIZE bytes per output file
+7 -4
View File
@@ -4,8 +4,8 @@
// file that was distributed with this source code.
use std::env;
use std::ffi::OsStr;
use std::io::Write;
use std::io::{BufWriter, Error, Result};
use std::io::{ErrorKind, Write};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use uucore::error::USimpleError;
@@ -139,10 +139,13 @@ pub fn instantiate_current_writer(
.create(true)
.truncate(true)
.open(Path::new(&filename))
.map_err(|_| {
Error::other(
.map_err(|e| match e.kind() {
ErrorKind::IsADirectory => Error::other(
translate!("split-error-is-a-directory", "dir" => filename),
),
_ => Error::other(
translate!("split-error-unable-to-open-file", "file" => filename),
)
),
})?
} else {
// re-open file that we previously created to append to it
+8 -3
View File
@@ -3,8 +3,8 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::ffi::OsStr;
use std::io::Write;
use std::io::{BufWriter, Error, Result};
use std::io::{ErrorKind, Write};
use std::path::Path;
use uucore::fs;
use uucore::translate;
@@ -25,8 +25,13 @@ pub fn instantiate_current_writer(
.create(true)
.truncate(true)
.open(Path::new(&filename))
.map_err(|_| {
Error::other(translate!("split-error-unable-to-open-file", "file" => filename))
.map_err(|e| match e.kind() {
ErrorKind::IsADirectory => {
Error::other(translate!("split-error-is-a-directory", "dir" => filename))
}
_ => {
Error::other(translate!("split-error-unable-to-open-file", "file" => filename))
}
})?
} else {
// re-open file that we previously created to append to it
+13
View File
@@ -2078,3 +2078,16 @@ fn test_split_non_utf8_additional_suffix() {
"Expected at least one split file to be created"
);
}
#[test]
#[cfg(target_os = "linux")] // To re-enable on Windows once I work out what goes wrong with it.
fn test_split_directory_already_exists() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("xaa"); // For collision with.
at.touch("file");
ucmd.args(&["file"])
.fails_with_code(1)
.no_stdout()
.stderr_is("split: xaa: Is a directory\n");
}