diff --git a/src/uu/split/locales/en-US.ftl b/src/uu/split/locales/en-US.ftl index 4247eb5b9d9..629b8956d75 100644 --- a/src/uu/split/locales/en-US.ftl +++ b/src/uu/split/locales/en-US.ftl @@ -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 diff --git a/src/uu/split/src/platform/unix.rs b/src/uu/split/src/platform/unix.rs index d1257954d3e..d530ee25966 100644 --- a/src/uu/split/src/platform/unix.rs +++ b/src/uu/split/src/platform/unix.rs @@ -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 diff --git a/src/uu/split/src/platform/windows.rs b/src/uu/split/src/platform/windows.rs index e443a9cfb3b..6693e4fe909 100644 --- a/src/uu/split/src/platform/windows.rs +++ b/src/uu/split/src/platform/windows.rs @@ -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 diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index f710e14425b..497559aca3e 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -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"); +}