1515//! ```
1616
1717use anyhow:: { anyhow, Result } ;
18+ use std:: ffi:: OsString ;
1819use std:: path:: { Path , PathBuf } ;
1920use std:: process:: { Child , Command as ProcessCommand } ;
2021
@@ -154,30 +155,24 @@ fn start_command_with_output_redirect(
154155 // For FIFOs, we use shell redirection (sh -c "cmd > fifo") to avoid blocking
155156 // The shell handles the FIFO opening in the background properly
156157
157- let cmd_string = match cmd {
158+ // The arguments stay OsString values and reach the shell as positional
159+ // parameters, forwarded with "$@". Quoting them into the script text would
160+ // need UTF-8 and would lose the bytes of a name that is not UTF-8.
161+ let ( cmd_string, args) : ( String , Vec < OsString > ) = match cmd {
158162 Command :: External { program, args, .. } => {
159163 let expanded_program = crate :: parser:: expand_with_command_sub ( program, state) ?;
160- let expanded_args =
161- crate :: word:: expand_words ( args, state, crate :: word:: ProcessSubs :: Refuse ) ?
162- . lossy_fields ( ) ;
163-
164- // Build command string: prog arg1 arg2 > fifo
165- let mut cmd = expanded_program;
166- for arg in expanded_args {
167- cmd. push ( ' ' ) ;
168- // Simple shell escaping (single quotes)
169- cmd. push_str ( & format ! ( "'{}'" , arg. replace( '\'' , "'\\ ''" ) ) ) ;
170- }
171- cmd
164+ let expansion =
165+ crate :: word:: expand_words ( args, state, crate :: word:: ProcessSubs :: Refuse ) ?;
166+ ( format ! ( "{expanded_program} \" $@\" " ) , expansion. fields )
172167 }
173- Command :: Pwd { .. } => "pwd" . to_string ( ) ,
168+ Command :: Pwd { .. } => ( "pwd" . to_string ( ) , Vec :: new ( ) ) ,
174169 Command :: Ls { path, .. } => {
175170 let mut cmd = "ls" . to_string ( ) ;
176171 if let Some ( p) = path {
177172 let expanded = crate :: parser:: expand_with_command_sub ( p, state) ?;
178173 cmd. push_str ( & format ! ( " '{}'" , expanded. replace( '\'' , "'\\ ''" ) ) ) ;
179174 }
180- cmd
175+ ( cmd, Vec :: new ( ) )
181176 }
182177 _ => {
183178 return Err ( anyhow ! (
@@ -188,13 +183,7 @@ fn start_command_with_output_redirect(
188183
189184 // Use sh -c to run: cmd > fifo_path
190185 let shell_cmd = format ! ( "{} > {}" , cmd_string, output_path. display( ) ) ;
191-
192- let child = ProcessCommand :: new ( "sh" )
193- . arg ( "-c" )
194- . arg ( & shell_cmd)
195- . spawn ( ) ?;
196-
197- Ok ( child)
186+ spawn_sh ( & shell_cmd, & args)
198187}
199188
200189/// Start command with stdin redirected from file (for >(cmd))
@@ -205,20 +194,13 @@ fn start_command_with_input_redirect(
205194) -> Result < Child > {
206195 // For FIFOs, use shell redirection: sh -c "cmd < fifo"
207196
208- let cmd_string = match cmd {
197+ // As above: OsString positional parameters forwarded with "$@".
198+ let ( cmd_string, args) : ( String , Vec < OsString > ) = match cmd {
209199 Command :: External { program, args, .. } => {
210200 let expanded_program = crate :: parser:: expand_with_command_sub ( program, state) ?;
211- let expanded_args =
212- crate :: word:: expand_words ( args, state, crate :: word:: ProcessSubs :: Refuse ) ?
213- . lossy_fields ( ) ;
214-
215- // Build command string
216- let mut cmd = expanded_program;
217- for arg in expanded_args {
218- cmd. push ( ' ' ) ;
219- cmd. push_str ( & format ! ( "'{}'" , arg. replace( '\'' , "'\\ ''" ) ) ) ;
220- }
221- cmd
201+ let expansion =
202+ crate :: word:: expand_words ( args, state, crate :: word:: ProcessSubs :: Refuse ) ?;
203+ ( format ! ( "{expanded_program} \" $@\" " ) , expansion. fields )
222204 }
223205 _ => {
224206 return Err ( anyhow ! (
@@ -229,13 +211,18 @@ fn start_command_with_input_redirect(
229211
230212 // Use sh -c to run: cmd < fifo_path
231213 let shell_cmd = format ! ( "{} < {}" , cmd_string, input_path. display( ) ) ;
214+ spawn_sh ( & shell_cmd, & args)
215+ }
232216
233- let child = ProcessCommand :: new ( "sh" )
217+ /// Run `sh -c script sh args...`. The arguments become `$1`, `$2`, ... and are
218+ /// passed as raw OS strings, so their bytes and their boundaries survive.
219+ fn spawn_sh ( script : & str , args : & [ OsString ] ) -> Result < Child > {
220+ Ok ( ProcessCommand :: new ( "sh" )
234221 . arg ( "-c" )
235- . arg ( & shell_cmd )
236- . spawn ( ) ? ;
237-
238- Ok ( child )
222+ . arg ( script )
223+ . arg ( "sh" )
224+ . args ( args )
225+ . spawn ( ) ? )
239226}
240227
241228#[ cfg( test) ]
@@ -289,4 +276,44 @@ mod tests {
289276 proc_sub1. cleanup ( ) . unwrap ( ) ;
290277 proc_sub2. cleanup ( ) . unwrap ( ) ;
291278 }
279+
280+ /// A file named `bad\xff.bin` (not UTF-8) and a pattern that matches it.
281+ #[ cfg( unix) ]
282+ fn non_utf8_fixture ( dir : & Path ) -> String {
283+ use std:: ffi:: OsStr ;
284+ use std:: os:: unix:: ffi:: OsStrExt ;
285+ let name = OsStr :: from_bytes ( b"bad\xff .bin" ) ;
286+ std:: fs:: write ( dir. join ( name) , "12345" ) . unwrap ( ) ;
287+ format ! ( "{}/bad*.bin" , crate :: glob:: escape( & dir. to_string_lossy( ) ) )
288+ }
289+
290+ #[ cfg( unix) ]
291+ #[ test]
292+ fn output_redirect_keeps_non_utf8_filename_bytes ( ) {
293+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
294+ let mut state = ShellState :: new ( temp_dir. path ( ) . to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
295+ let pattern = non_utf8_fixture ( temp_dir. path ( ) ) ;
296+ let cmd = parse_command ( & format ! ( "cat {pattern}" ) ) . unwrap ( ) ;
297+ let out = temp_dir. path ( ) . join ( "out.txt" ) ;
298+
299+ let mut child = start_command_with_output_redirect ( & cmd, & mut state, & out) . unwrap ( ) ;
300+ assert ! ( child. wait( ) . unwrap( ) . success( ) ) ;
301+ assert_eq ! ( std:: fs:: read_to_string( & out) . unwrap( ) , "12345" ) ;
302+ }
303+
304+ #[ cfg( unix) ]
305+ #[ test]
306+ fn input_redirect_keeps_non_utf8_filename_bytes ( ) {
307+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
308+ let mut state = ShellState :: new ( temp_dir. path ( ) . to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
309+ let pattern = non_utf8_fixture ( temp_dir. path ( ) ) ;
310+ let copy = temp_dir. path ( ) . join ( "copy.txt" ) ;
311+ let cmd = parse_command ( & format ! ( "sort -o {} {pattern}" , copy. display( ) ) ) . unwrap ( ) ;
312+ let input = temp_dir. path ( ) . join ( "in.txt" ) ;
313+ std:: fs:: write ( & input, "" ) . unwrap ( ) ;
314+
315+ let mut child = start_command_with_input_redirect ( & cmd, & mut state, & input) . unwrap ( ) ;
316+ assert ! ( child. wait( ) . unwrap( ) . success( ) ) ;
317+ assert_eq ! ( std:: fs:: read_to_string( & copy) . unwrap( ) , "12345\n " ) ;
318+ }
292319}
0 commit comments