@@ -1574,6 +1574,41 @@ fn test_sparse_notrunc_extends_file() {
15741574 assert_eq ! ( at. metadata( "outfile" ) . len( ) , 1024 ) ;
15751575}
15761576
1577+ /// With `conv=sparse,fsync` (or `fdatasync`), the trailing zero block extends
1578+ /// the file before the sync, so the sync covers the final length.
1579+ #[ cfg( target_os = "linux" ) ]
1580+ #[ uutests:: wasi_ignore( NoSubprocessSpawning ) ]
1581+ #[ rstest]
1582+ #[ case:: fsync( "fsync" ) ]
1583+ #[ case:: fdatasync( "fdatasync" ) ]
1584+ fn test_sparse_extends_before_sync ( #[ case] sync : & str ) {
1585+ let ( at, _ucmd) = at_and_ucmd ! ( ) ;
1586+ at. write_bytes ( "infile" , & [ 0 ; 4096 ] ) ;
1587+ let trace = at. plus ( "trace" ) ;
1588+ let Ok ( out) = Command :: new ( "strace" )
1589+ . arg ( "-o" )
1590+ . arg ( & trace)
1591+ . args ( [ "-e" , "trace=fsync,fdatasync,ftruncate" ] )
1592+ . arg ( get_tests_binary ( ) )
1593+ . arg ( "dd" )
1594+ . args ( [ "if=infile" , "of=outfile" , "bs=4096" , "status=none" ] )
1595+ . arg ( format ! ( "conv=sparse,{sync}" ) )
1596+ . current_dir ( at. as_string ( ) )
1597+ . output ( )
1598+ else {
1599+ return ; // missing strace
1600+ } ;
1601+ if String :: from_utf8_lossy ( & out. stderr ) . contains ( "ptrace" ) {
1602+ return ; // tracing not permitted here
1603+ }
1604+ assert ! ( out. status. success( ) ) ;
1605+ assert_eq ! ( at. metadata( "outfile" ) . len( ) , 4096 ) ;
1606+ let trace = at. read ( "trace" ) ;
1607+ let extend = trace. find ( ", 4096)" ) . expect ( "final ftruncate" ) ;
1608+ let synced = trace. find ( & format ! ( "{sync}(" ) ) . expect ( "sync call" ) ;
1609+ assert ! ( extend < synced, "{trace}" ) ;
1610+ }
1611+
15771612/// `conv=notrunc,sparse` must not shrink an output file longer than the copy.
15781613#[ test]
15791614fn test_sparse_notrunc_does_not_shrink ( ) {
0 commit comments