@@ -12,12 +12,14 @@ use cap_primitives::fs::FileType;
12
12
use cap_std:: fs:: { Dir , File , Metadata } ;
13
13
use cap_tempfile:: cap_std;
14
14
use cap_tempfile:: cap_std:: fs:: DirEntry ;
15
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
15
16
use rustix:: path:: Arg ;
16
17
use std:: cmp:: Ordering ;
17
18
use std:: ffi:: OsStr ;
18
19
use std:: io:: Result ;
19
20
use std:: io:: { self , Write } ;
20
21
use std:: ops:: Deref ;
22
+ #[ cfg( unix) ]
21
23
use std:: os:: fd:: OwnedFd ;
22
24
use std:: path:: { Path , PathBuf } ;
23
25
@@ -197,6 +199,7 @@ pub trait CapStdExtDirExt {
197
199
/// ```
198
200
///
199
201
/// Any existing file will be replaced.
202
+ #[ cfg( not( windows) ) ]
200
203
fn atomic_replace_with < F , T , E > (
201
204
& self ,
202
205
destname : impl AsRef < Path > ,
@@ -207,9 +210,11 @@ pub trait CapStdExtDirExt {
207
210
E : From < std:: io:: Error > ;
208
211
209
212
/// Atomically write the provided contents to a file.
213
+ #[ cfg( not( windows) ) ]
210
214
fn atomic_write ( & self , destname : impl AsRef < Path > , contents : impl AsRef < [ u8 ] > ) -> Result < ( ) > ;
211
215
212
216
/// Atomically write the provided contents to a file, using specified permissions.
217
+ #[ cfg( not( windows) ) ]
213
218
fn atomic_write_with_perms (
214
219
& self ,
215
220
destname : impl AsRef < Path > ,
@@ -447,6 +452,7 @@ fn subdir_of<'d, 'p>(d: &'d Dir, p: &'p Path) -> io::Result<(DirOwnedOrBorrowed<
447
452
Ok ( ( r, name) )
448
453
}
449
454
455
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
450
456
/// A thin wrapper for [`openat2`] but that retries on interruption.
451
457
fn openat2_with_retry (
452
458
dirfd : impl std:: os:: fd:: AsFd ,
@@ -474,6 +480,7 @@ fn openat2_with_retry(
474
480
} )
475
481
}
476
482
483
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
477
484
fn is_mountpoint_impl_statx ( root : & Dir , path : & Path ) -> Result < Option < bool > > {
478
485
// https://github.com/systemd/systemd/blob/8fbf0a214e2fe474655b17a4b663122943b55db0/src/basic/mountpoint-util.c#L176
479
486
use rustix:: fs:: StatxAttributes ;
@@ -577,6 +584,7 @@ where
577
584
debug_assert ! ( matches!( flow, std:: ops:: ControlFlow :: Continue ( ( ) ) ) ) ;
578
585
// Open the child directory, using the noxdev API if
579
586
// we're configured not to cross devices,
587
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
580
588
let d = {
581
589
if !config. noxdev {
582
590
entry. open_dir ( ) ?
@@ -587,6 +595,10 @@ where
587
595
continue ;
588
596
}
589
597
} ;
598
+
599
+ #[ cfg( not( any( target_os = "android" , target_os = "linux" ) ) ) ]
600
+ let d = entry. open_dir ( ) ?;
601
+
590
602
// Recurse into the target directory
591
603
walk_inner ( & d, path, callback, config) ?;
592
604
path. pop ( ) ;
@@ -707,6 +719,7 @@ impl CapStdExtDirExt for Dir {
707
719
Ok ( ( ) )
708
720
}
709
721
722
+ #[ cfg( not( windows) ) ]
710
723
fn atomic_replace_with < F , T , E > (
711
724
& self ,
712
725
destname : impl AsRef < Path > ,
@@ -744,10 +757,12 @@ impl CapStdExtDirExt for Dir {
744
757
Ok ( r)
745
758
}
746
759
760
+ #[ cfg( not( windows) ) ]
747
761
fn atomic_write ( & self , destname : impl AsRef < Path > , contents : impl AsRef < [ u8 ] > ) -> Result < ( ) > {
748
762
self . atomic_replace_with ( destname, |f| f. write_all ( contents. as_ref ( ) ) )
749
763
}
750
764
765
+ #[ cfg( not( windows) ) ]
751
766
fn atomic_write_with_perms (
752
767
& self ,
753
768
destname : impl AsRef < Path > ,
@@ -784,6 +799,7 @@ impl CapStdExtDirExt for Dir {
784
799
. map_err ( Into :: into)
785
800
}
786
801
802
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
787
803
fn is_mountpoint ( & self , path : impl AsRef < Path > ) -> Result < Option < bool > > {
788
804
is_mountpoint_impl_statx ( self , path. as_ref ( ) ) . map_err ( Into :: into)
789
805
}
@@ -931,7 +947,10 @@ mod tests {
931
947
#[ test]
932
948
fn test_validate_relpath_no_uplinks ( ) {
933
949
let ok_cases = [ "foo" , "foo/bar" , "foo/bar/" ] ;
950
+ #[ cfg( unix) ]
934
951
let err_cases = [ "/foo" , "/" , "../foo" , "foo/../bar" ] ;
952
+ #[ cfg( windows) ]
953
+ let err_cases = [ "C:\\ foo" , "../foo" , "foo/../bar" ] ;
935
954
936
955
for case in ok_cases {
937
956
assert ! ( validate_relpath_no_uplinks( Path :: new( case) ) . is_ok( ) ) ;
0 commit comments