@@ -18,7 +18,7 @@ pub mod options {
1818
1919static ARG_FILES : & str = "files" ;
2020
21- #[ cfg( unix ) ]
21+ #[ cfg( not ( windows ) ) ]
2222mod platform {
2323 #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
2424 use std:: fs:: { File , OpenOptions } ;
@@ -38,6 +38,8 @@ mod platform {
3838 reason = "fn sig must match on all platforms"
3939 ) ]
4040 pub fn do_sync ( ) -> UResult < ( ) > {
41+ // maximize compatibility of scripts by noop on targets global sync is impossible
42+ #[ cfg( unix) ]
4143 rustix:: fs:: sync ( ) ;
4244 Ok ( ( ) )
4345 }
@@ -76,16 +78,6 @@ mod platform {
7678 }
7779 Ok ( ( ) )
7880 }
79-
80- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
81- pub fn do_syncfs ( files : & [ String ] ) -> UResult < ( ) > {
82- do_sync_with ( files, rustix:: fs:: syncfs)
83- }
84-
85- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
86- pub fn do_fdatasync ( files : & [ String ] ) -> UResult < ( ) > {
87- do_sync_with ( files, rustix:: fs:: fdatasync)
88- }
8981}
9082
9183#[ cfg( windows) ]
@@ -248,11 +240,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
248240 if files. is_empty ( ) {
249241 sync ( ) ?;
250242 } else {
251- #[ cfg( any( target_os = "linux" , target_os = "android" , windows) ) ]
252243 syncfs ( & files) ?;
253244 }
254245 } else if matches. get_flag ( options:: DATA ) {
255- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
256246 fdatasync ( & files) ?;
257247 } else {
258248 sync ( ) ?;
@@ -261,6 +251,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
261251}
262252
263253pub fn uu_app ( ) -> Command {
254+ #[ cfg( any( target_os = "linux" , target_os = "android" , windows) ) ]
255+ let syncfs = translate ! ( "sync-help-file-system" ) ;
256+ #[ cfg( not( any( target_os = "linux" , target_os = "android" , windows) ) ) ]
257+ let syncfs = translate ! ( "sync-help-files" ) ;
264258 Command :: new ( "sync" )
265259 . version ( uucore:: crate_version!( ) )
266260 . help_template ( uucore:: localized_help_template ( "sync" ) )
@@ -272,7 +266,7 @@ pub fn uu_app() -> Command {
272266 . short ( 'f' )
273267 . long ( options:: FILE_SYSTEM )
274268 . conflicts_with ( options:: DATA )
275- . help ( translate ! ( "sync-help-file-system" ) )
269+ . help ( syncfs )
276270 . action ( ArgAction :: SetTrue ) ,
277271 )
278272 . arg (
@@ -294,12 +288,36 @@ fn sync() -> UResult<()> {
294288 platform:: do_sync ( )
295289}
296290
297- #[ cfg( any( target_os = "linux" , target_os = "android" , windows ) ) ]
291+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
298292fn syncfs ( files : & [ String ] ) -> UResult < ( ) > {
299- platform:: do_syncfs ( files)
293+ platform:: do_sync_with ( files, rustix :: fs :: syncfs )
300294}
301295
302- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
296+ // compromise: sync files only, not FS containing the files
297+ #[ cfg( not( any( target_os = "linux" , target_os = "android" , windows) ) ) ]
298+ fn syncfs ( files : & [ String ] ) -> UResult < ( ) > {
299+ do_sync_with_regular_file ( files, |f| f. sync_all ( ) )
300+ }
301+
302+ #[ cfg( any( target_os = "linux" , target_os = "android" , windows) ) ]
303+ fn fdatasync ( files : & [ String ] ) -> UResult < ( ) > {
304+ platform:: do_sync_with ( files, rustix:: fs:: fdatasync)
305+ }
306+
307+ #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
303308fn fdatasync ( files : & [ String ] ) -> UResult < ( ) > {
304- platform:: do_fdatasync ( files)
309+ do_sync_with_regular_file ( files, |f| f. sync_data ( ) )
310+ }
311+
312+ #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
313+ fn do_sync_with_regular_file < F > ( files : & [ String ] , op : F ) -> UResult < ( ) >
314+ where
315+ F : Fn ( std:: fs:: File ) -> std:: io:: Result < ( ) > ,
316+ {
317+ use uucore:: error:: FromIo ;
318+ for path in files {
319+ let f = std:: fs:: OpenOptions :: new ( ) . read ( true ) . open ( path) ?;
320+ op ( f) . map_err_context ( || translate ! ( "sync-error-syncing-file" , "file" => path. quote( ) ) ) ?;
321+ }
322+ Ok ( ( ) )
305323}
0 commit comments