@@ -20,7 +20,7 @@ use std::{
2020} ;
2121
2222use itertools:: Itertools ;
23- use uucore:: error:: UResult ;
23+ use uucore:: error:: { UResult , strip_errno } ;
2424
2525use crate :: Output ;
2626use crate :: chunks:: RecycledChunk ;
@@ -52,10 +52,37 @@ pub fn ext_sort(
5252 let settings = settings. clone ( ) ;
5353 move || sorter ( & recycled_receiver, & sorted_sender, & settings)
5454 } ) ;
55- if settings. compress_prog . is_some ( ) {
55+
56+ // Test if compression program exists and works, disable if not
57+ let mut effective_settings = settings. clone ( ) ;
58+ if let Some ( ref prog) = settings. compress_prog {
59+ // Test the compression program by trying to spawn it
60+ match std:: process:: Command :: new ( prog)
61+ . stdin ( std:: process:: Stdio :: null ( ) )
62+ . stdout ( std:: process:: Stdio :: null ( ) )
63+ . stderr ( std:: process:: Stdio :: null ( ) )
64+ . spawn ( )
65+ {
66+ Ok ( mut child) => {
67+ // Kill the test process immediately
68+ let _ = child. kill ( ) ;
69+ }
70+ Err ( err) => {
71+ // Print the error and disable compression
72+ eprintln ! (
73+ "sort: could not run compress program '{}': {}" ,
74+ prog,
75+ strip_errno( & err)
76+ ) ;
77+ effective_settings. compress_prog = None ;
78+ }
79+ }
80+ }
81+
82+ if effective_settings. compress_prog . is_some ( ) {
5683 reader_writer :: < _ , WriteableCompressedTmpFile > (
5784 files,
58- settings ,
85+ & effective_settings ,
5986 & sorted_receiver,
6087 recycled_sender,
6188 output,
@@ -64,7 +91,7 @@ pub fn ext_sort(
6491 } else {
6592 reader_writer :: < _ , WriteablePlainTmpFile > (
6693 files,
67- settings ,
94+ & effective_settings ,
6895 & sorted_receiver,
6996 recycled_sender,
7097 output,
0 commit comments