Skip to content

Commit 6ac9543

Browse files
authored
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
1 parent 84e6f03 commit 6ac9543

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

src/uu/split/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ split-error-unable-to-reopen-file = unable to re-open { $file }; aborting
4343
split-error-file-descriptor-limit = at file descriptor limit, but no file descriptor left to close. Closed { $count } writers before.
4444
split-error-shell-process-returned = Shell process returned { $code }
4545
split-error-shell-process-terminated = Shell process terminated by signal
46+
split-error-is-a-directory = { $dir }: Is a directory
4647
4748
# Help messages for command-line options
4849
split-help-bytes = put SIZE bytes per output file

src/uu/split/src/platform/unix.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// file that was distributed with this source code.
55
use std::env;
66
use std::ffi::OsStr;
7-
use std::io::Write;
87
use std::io::{BufWriter, Error, Result};
8+
use std::io::{ErrorKind, Write};
99
use std::path::Path;
1010
use std::process::{Child, Command, Stdio};
1111
use uucore::error::USimpleError;
@@ -139,10 +139,13 @@ pub fn instantiate_current_writer(
139139
.create(true)
140140
.truncate(true)
141141
.open(Path::new(&filename))
142-
.map_err(|_| {
143-
Error::other(
142+
.map_err(|e| match e.kind() {
143+
ErrorKind::IsADirectory => Error::other(
144+
translate!("split-error-is-a-directory", "dir" => filename),
145+
),
146+
_ => Error::other(
144147
translate!("split-error-unable-to-open-file", "file" => filename),
145-
)
148+
),
146149
})?
147150
} else {
148151
// re-open file that we previously created to append to it

src/uu/split/src/platform/windows.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55
use std::ffi::OsStr;
6-
use std::io::Write;
76
use std::io::{BufWriter, Error, Result};
7+
use std::io::{ErrorKind, Write};
88
use std::path::Path;
99
use uucore::fs;
1010
use uucore::translate;
@@ -25,8 +25,13 @@ pub fn instantiate_current_writer(
2525
.create(true)
2626
.truncate(true)
2727
.open(Path::new(&filename))
28-
.map_err(|_| {
29-
Error::other(translate!("split-error-unable-to-open-file", "file" => filename))
28+
.map_err(|e| match e.kind() {
29+
ErrorKind::IsADirectory => {
30+
Error::other(translate!("split-error-is-a-directory", "dir" => filename))
31+
}
32+
_ => {
33+
Error::other(translate!("split-error-unable-to-open-file", "file" => filename))
34+
}
3035
})?
3136
} else {
3237
// re-open file that we previously created to append to it

tests/by-util/test_split.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,3 +2078,16 @@ fn test_split_non_utf8_additional_suffix() {
20782078
"Expected at least one split file to be created"
20792079
);
20802080
}
2081+
2082+
#[test]
2083+
#[cfg(target_os = "linux")] // To re-enable on Windows once I work out what goes wrong with it.
2084+
fn test_split_directory_already_exists() {
2085+
let (at, mut ucmd) = at_and_ucmd!();
2086+
2087+
at.mkdir("xaa"); // For collision with.
2088+
at.touch("file");
2089+
ucmd.args(&["file"])
2090+
.fails_with_code(1)
2091+
.no_stdout()
2092+
.stderr_is("split: xaa: Is a directory\n");
2093+
}

0 commit comments

Comments
 (0)