@@ -10,13 +10,13 @@ use std::env;
1010use std:: ffi:: OsString ;
1111use std:: fmt:: Write as _;
1212use std:: fs:: File ;
13- use std:: io:: { BufRead , BufReader , Write , stdout} ;
13+ use std:: io:: { BufRead , BufReader , ErrorKind , Write , stdout} ;
1414use std:: path:: Path ;
1515
1616use clap:: { Arg , ArgAction , Command } ;
1717use uucore:: colors:: { FILE_ATTRIBUTE_CODES , FILE_COLORS , FILE_TYPES , TERMS } ;
1818use uucore:: display:: Quotable ;
19- use uucore:: error:: { UResult , USimpleError , UUsageError } ;
19+ use uucore:: error:: { UResult , USimpleError , UUsageError , strip_errno } ;
2020use uucore:: translate;
2121
2222use uucore:: { format_usage, parser:: parse_glob} ;
@@ -202,20 +202,32 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
202202 parse ( fin. lines ( ) . map_while ( Result :: ok) , out_format, "-" )
203203 } else {
204204 let path = Path :: new ( & file_arg) ;
205- if path. is_dir ( ) {
206- return Err ( USimpleError :: new (
207- 2 ,
208- translate ! ( "dircolors-error-expected-file-got-directory" , "path" => path. quote( ) ) ,
209- ) ) ;
205+ let read_error = |e : & std:: io:: Error | {
206+ USimpleError :: new (
207+ 1 ,
208+ translate ! ( "dircolors-error-read" , "path" => path. maybe_quote( ) , "error" => strip_errno( e) ) ,
209+ )
210+ } ;
211+ // Windows detects a directory as error of open instead of read
212+ let file = File :: open ( path) . map_err ( |e| {
213+ if cfg ! ( windows) && path. is_dir ( ) {
214+ USimpleError :: new (
215+ 1 ,
216+ translate ! ( "dircolors-error-read-directory" , "path" => path. maybe_quote( ) ) ,
217+ )
218+ } else {
219+ USimpleError :: new ( 1 , format ! ( "{}: {e}" , path. maybe_quote( ) ) )
220+ }
221+ } ) ?;
222+ let mut lines = Vec :: new ( ) ;
223+ for line in BufReader :: new ( file) . lines ( ) {
224+ match line {
225+ Ok ( line) => lines. push ( line) ,
226+ Err ( e) if e. kind ( ) == ErrorKind :: InvalidData => break ,
227+ Err ( e) => return Err ( read_error ( & e) ) ,
228+ }
210229 }
211- let file = File :: open ( path)
212- . map_err ( |e| USimpleError :: new ( 1 , format ! ( "{}: {e}" , path. maybe_quote( ) ) ) ) ?;
213- let fin = BufReader :: new ( file) ;
214- parse (
215- fin. lines ( ) . map_while ( Result :: ok) ,
216- out_format,
217- & path. to_string_lossy ( ) ,
218- )
230+ parse ( lines. into_iter ( ) , out_format, & path. to_string_lossy ( ) )
219231 } ;
220232
221233 let string = result. map_err ( |s| USimpleError :: new ( 1 , s) ) ?;
0 commit comments