@@ -11,21 +11,85 @@ use crate::{
1111 compare_by, open,
1212} ;
1313use itertools:: Itertools ;
14+ use std:: { cmp:: Ordering , ffi:: OsStr } ;
15+ #[ cfg( not( target_os = "wasi" ) ) ]
1416use std:: {
15- cmp:: Ordering ,
16- ffi:: OsStr ,
1717 io:: Read ,
1818 iter,
1919 sync:: mpsc:: { Receiver , SyncSender , sync_channel} ,
2020 thread,
2121} ;
2222use uucore:: error:: UResult ;
2323
24+ fn buffer_size ( settings : & GlobalSettings ) -> usize {
25+ if settings. buffer_size < 100 * 1024 {
26+ // when the buffer size is smaller than 100KiB we choose it instead of the default.
27+ // this improves testability.
28+ settings. buffer_size
29+ } else {
30+ 100 * 1024
31+ }
32+ }
33+
34+ /// Checks `chunk` against the end of `prev_chunk` (if any) and within itself.
35+ ///
36+ /// Returns [`SortError::Disorder`] for the first line that violates the requested
37+ /// ordering. `line_idx` is the running line count and is advanced as lines are checked.
38+ fn check_chunk (
39+ path : & OsStr ,
40+ settings : & GlobalSettings ,
41+ max_allowed_cmp : Ordering ,
42+ prev_chunk : Option < & Chunk > ,
43+ chunk : & Chunk ,
44+ line_idx : & mut usize ,
45+ ) -> UResult < ( ) > {
46+ * line_idx += 1 ;
47+ if let Some ( prev_chunk) = prev_chunk {
48+ // Check if the first element of the new chunk is greater than the last
49+ // element from the previous chunk
50+ let prev_last = prev_chunk. lines ( ) . last ( ) . unwrap ( ) ;
51+ let new_first = chunk. lines ( ) . first ( ) . unwrap ( ) ;
52+
53+ if compare_by (
54+ prev_last,
55+ new_first,
56+ settings,
57+ prev_chunk. line_data ( ) ,
58+ chunk. line_data ( ) ,
59+ ) > max_allowed_cmp
60+ {
61+ return Err ( SortError :: Disorder {
62+ file : path. to_owned ( ) ,
63+ line_number : * line_idx,
64+ line : String :: from_utf8_lossy ( new_first. line ) . into_owned ( ) ,
65+ silent : settings. check_silent ,
66+ }
67+ . into ( ) ) ;
68+ }
69+ }
70+
71+ for ( a, b) in chunk. lines ( ) . iter ( ) . tuple_windows ( ) {
72+ * line_idx += 1 ;
73+ if compare_by ( a, b, settings, chunk. line_data ( ) , chunk. line_data ( ) ) > max_allowed_cmp {
74+ return Err ( SortError :: Disorder {
75+ file : path. to_owned ( ) ,
76+ line_number : * line_idx,
77+ line : String :: from_utf8_lossy ( b. line ) . into_owned ( ) ,
78+ silent : settings. check_silent ,
79+ }
80+ . into ( ) ) ;
81+ }
82+ }
83+
84+ Ok ( ( ) )
85+ }
86+
2487/// Check if the file at `path` is ordered.
2588///
2689/// # Returns
2790///
2891/// The code we should exit with.
92+ #[ cfg( not( target_os = "wasi" ) ) ]
2993pub fn check ( path : & OsStr , settings : & GlobalSettings ) -> UResult < ( ) > {
3094 let max_allowed_cmp = if settings. unique {
3195 // If `unique` is enabled, the previous line must compare _less_ to the next one.
@@ -42,13 +106,7 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
42106 move || reader ( file, & recycled_receiver, & loaded_sender, & settings)
43107 } ) ;
44108 for _ in 0 ..2 {
45- let _ = recycled_sender. send ( RecycledChunk :: new ( if settings. buffer_size < 100 * 1024 {
46- // when the buffer size is smaller than 100KiB we choose it instead of the default.
47- // this improves testability.
48- settings. buffer_size
49- } else {
50- 100 * 1024
51- } ) ) ;
109+ let _ = recycled_sender. send ( RecycledChunk :: new ( buffer_size ( settings) ) ) ;
52110 }
53111
54112 let mut prev_chunk: Option < Chunk > = None ;
@@ -60,47 +118,21 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
60118 // disorder, the reader is usually still working at that point, so we shut it down
61119 // in an orderly fashion below instead of just dropping our end.
62120 ' outer: for chunk in & loaded_receiver {
63- line_idx += 1 ;
121+ if let Err ( e) = check_chunk (
122+ path,
123+ settings,
124+ max_allowed_cmp,
125+ prev_chunk. as_ref ( ) ,
126+ & chunk,
127+ & mut line_idx,
128+ ) {
129+ result = Err ( e) ;
130+ break ' outer;
131+ }
64132 if let Some ( prev_chunk) = prev_chunk. take ( ) {
65- // Check if the first element of the new chunk is greater than the last
66- // element from the previous chunk
67- let prev_last = prev_chunk. lines ( ) . last ( ) . unwrap ( ) ;
68- let new_first = chunk. lines ( ) . first ( ) . unwrap ( ) ;
69-
70- if compare_by (
71- prev_last,
72- new_first,
73- settings,
74- prev_chunk. line_data ( ) ,
75- chunk. line_data ( ) ,
76- ) > max_allowed_cmp
77- {
78- result = Err ( SortError :: Disorder {
79- file : path. to_owned ( ) ,
80- line_number : line_idx,
81- line : String :: from_utf8_lossy ( new_first. line ) . into_owned ( ) ,
82- silent : settings. check_silent ,
83- }
84- . into ( ) ) ;
85- break ' outer;
86- }
87133 let _ = recycled_sender. send ( prev_chunk. recycle ( ) ) ;
88134 }
89135
90- for ( a, b) in chunk. lines ( ) . iter ( ) . tuple_windows ( ) {
91- line_idx += 1 ;
92- if compare_by ( a, b, settings, chunk. line_data ( ) , chunk. line_data ( ) ) > max_allowed_cmp {
93- result = Err ( SortError :: Disorder {
94- file : path. to_owned ( ) ,
95- line_number : line_idx,
96- line : String :: from_utf8_lossy ( b. line ) . into_owned ( ) ,
97- silent : settings. check_silent ,
98- }
99- . into ( ) ) ;
100- break ' outer;
101- }
102- }
103-
104136 prev_chunk = Some ( chunk) ;
105137 }
106138
@@ -117,7 +149,55 @@ pub fn check(path: &OsStr, settings: &GlobalSettings) -> UResult<()> {
117149 result. and ( reader_result)
118150}
119151
152+ /// Check if the file at `path` is ordered.
153+ ///
154+ /// WASI has no threads, so chunks are read and checked on the current thread,
155+ /// one chunk at a time, stopping as soon as a disorder is found.
156+ #[ cfg( target_os = "wasi" ) ]
157+ pub fn check ( path : & OsStr , settings : & GlobalSettings ) -> UResult < ( ) > {
158+ let max_allowed_cmp = if settings. unique {
159+ Ordering :: Less
160+ } else {
161+ Ordering :: Equal
162+ } ;
163+
164+ let mut file = open ( path) ?;
165+ let mut carry_over = vec ! [ ] ;
166+ let ( sender, receiver) = std:: sync:: mpsc:: sync_channel ( 1 ) ;
167+ let mut prev_chunk: Option < Chunk > = None ;
168+ let mut line_idx = 0 ;
169+ loop {
170+ let recycled = RecycledChunk :: new ( buffer_size ( settings) ) ;
171+ let should_continue = chunks:: read (
172+ & sender,
173+ recycled,
174+ None ,
175+ & mut carry_over,
176+ & mut file,
177+ & mut std:: iter:: empty ( ) ,
178+ settings. line_ending . into ( ) ,
179+ settings,
180+ ) ?;
181+ while let Ok ( chunk) = receiver. try_recv ( ) {
182+ check_chunk (
183+ path,
184+ settings,
185+ max_allowed_cmp,
186+ prev_chunk. as_ref ( ) ,
187+ & chunk,
188+ & mut line_idx,
189+ ) ?;
190+ prev_chunk = Some ( chunk) ;
191+ }
192+ if !should_continue {
193+ break ;
194+ }
195+ }
196+ Ok ( ( ) )
197+ }
198+
120199/// The function running on the reader thread.
200+ #[ cfg( not( target_os = "wasi" ) ) ]
121201fn reader (
122202 mut file : Box < dyn Read + Send > ,
123203 receiver : & Receiver < RecycledChunk > ,
0 commit comments