1
- use std:: convert:: TryFrom ;
2
1
use std:: ffi;
3
2
use std:: fs:: File ;
4
3
use std:: fs:: OpenOptions ;
5
4
use std:: io:: { self , ErrorKind , Read , Write } ;
6
- use std:: mem:: MaybeUninit ;
7
5
#[ cfg( unix) ]
8
6
use std:: os:: unix:: fs:: OpenOptionsExt ;
9
7
#[ cfg( windows) ]
@@ -46,6 +44,8 @@ use crate::vm::VirtualMachine;
46
44
// just to avoid unused import warnings
47
45
#[ cfg( unix) ]
48
46
use crate :: pyobject:: PyIterable ;
47
+ #[ cfg( unix) ]
48
+ use std:: convert:: TryFrom ;
49
49
50
50
#[ cfg( windows) ]
51
51
pub const MODULE_NAME : & str = "nt" ;
@@ -135,6 +135,11 @@ impl PyPathLike {
135
135
mode : OutputMode :: String ,
136
136
}
137
137
}
138
+ #[ cfg( windows) ]
139
+ pub fn wide ( & self ) -> Vec < u16 > {
140
+ use std:: os:: windows:: ffi:: OsStrExt ;
141
+ self . path . encode_wide ( ) . chain ( std:: iter:: once ( 0 ) ) . collect ( )
142
+ }
138
143
}
139
144
140
145
impl TryFromObject for PyPathLike {
@@ -409,19 +414,17 @@ fn getgroups() -> nix::Result<Vec<Gid>> {
409
414
}
410
415
411
416
#[ cfg( unix) ]
412
- fn os_access ( path : PyStringRef , mode : u8 , vm : & VirtualMachine ) -> PyResult < bool > {
417
+ fn os_access ( path : PyPathLike , mode : u8 , vm : & VirtualMachine ) -> PyResult < bool > {
413
418
use std:: os:: unix:: fs:: MetadataExt ;
414
419
415
- let path = path. as_str ( ) ;
416
-
417
420
let flags = AccessFlags :: from_bits ( mode) . ok_or_else ( || {
418
421
vm. new_value_error (
419
422
"One of the flags is wrong, there are only 4 possibilities F_OK, R_OK, W_OK and X_OK"
420
423
. to_owned ( ) ,
421
424
)
422
425
} ) ?;
423
426
424
- let metadata = fs:: metadata ( path) ;
427
+ let metadata = fs:: metadata ( & path . path ) ;
425
428
426
429
// if it's only checking for F_OK
427
430
if flags == AccessFlags :: F_OK {
@@ -443,6 +446,19 @@ fn os_access(path: PyStringRef, mode: u8, vm: &VirtualMachine) -> PyResult<bool>
443
446
444
447
Ok ( r_ok && w_ok && x_ok)
445
448
}
449
+ #[ cfg( windows) ]
450
+ fn os_access ( path : PyPathLike , mode : u8 ) -> bool {
451
+ use winapi:: um:: { fileapi, winnt} ;
452
+ let attr = unsafe { fileapi:: GetFileAttributesW ( path. wide ( ) . as_ptr ( ) ) } ;
453
+ attr != fileapi:: INVALID_FILE_ATTRIBUTES
454
+ && ( mode & 2 == 0
455
+ || attr & winnt:: FILE_ATTRIBUTE_READONLY == 0
456
+ || attr & winnt:: FILE_ATTRIBUTE_DIRECTORY != 0 )
457
+ }
458
+ #[ cfg( not( any( unix, windows) ) ) ]
459
+ fn os_access ( path : PyStringRef , mode : u8 , vm : & VirtualMachine ) -> PyResult < bool > {
460
+ unimplemented ! ( )
461
+ }
446
462
447
463
fn os_error ( message : OptionalArg < PyStringRef > , vm : & VirtualMachine ) -> PyResult {
448
464
let msg = message. map_or ( "" . to_owned ( ) , |msg| msg. as_str ( ) . to_owned ( ) ) ;
@@ -1608,6 +1624,7 @@ fn envp_from_dict(dict: PyDictRef, vm: &VirtualMachine) -> PyResult<Vec<ffi::CSt
1608
1624
. collect ( )
1609
1625
}
1610
1626
1627
+ #[ cfg( any( target_os = "linux" , target_os = "freebsd" , target_os = "macos" ) ) ]
1611
1628
#[ derive( FromArgs ) ]
1612
1629
struct PosixSpawnArgs {
1613
1630
#[ pyarg( positional_only) ]
@@ -1639,7 +1656,7 @@ impl PosixSpawnArgs {
1639
1656
. map_err ( |_| vm. new_value_error ( "path should not have nul bytes" . to_owned ( ) ) ) ?;
1640
1657
1641
1658
let mut file_actions = unsafe {
1642
- let mut fa = MaybeUninit :: uninit ( ) ;
1659
+ let mut fa = std :: mem :: MaybeUninit :: uninit ( ) ;
1643
1660
assert ! ( libc:: posix_spawn_file_actions_init( fa. as_mut_ptr( ) ) == 0 ) ;
1644
1661
fa. assume_init ( )
1645
1662
} ;
@@ -1691,7 +1708,7 @@ impl PosixSpawnArgs {
1691
1708
}
1692
1709
1693
1710
let mut attrp = unsafe {
1694
- let mut sa = MaybeUninit :: uninit ( ) ;
1711
+ let mut sa = std :: mem :: MaybeUninit :: uninit ( ) ;
1695
1712
assert ! ( libc:: posix_spawnattr_init( sa. as_mut_ptr( ) ) == 0 ) ;
1696
1713
sa. assume_init ( )
1697
1714
} ;
@@ -1857,7 +1874,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1857
1874
#[ allow( unused_mut) ]
1858
1875
let mut support_funcs = vec ! [
1859
1876
SupportFunc :: new( vm, "open" , os_open, None , Some ( false ) , None ) ,
1860
- // access Some Some None
1877
+ SupportFunc :: new ( vm , " access" , os_access , Some ( false ) , Some ( false ) , None ) ,
1861
1878
SupportFunc :: new( vm, "chdir" , os_chdir, Some ( false ) , None , None ) ,
1862
1879
// chflags Some, None Some
1863
1880
// chown Some Some Some
@@ -1973,7 +1990,6 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
1973
1990
let uname_result = UnameResult :: make_class ( ctx) ;
1974
1991
1975
1992
extend_module ! ( vm, module, {
1976
- "access" => ctx. new_function( os_access) ,
1977
1993
"chmod" => ctx. new_function( os_chmod) ,
1978
1994
"get_inheritable" => ctx. new_function( os_get_inheritable) , // TODO: windows
1979
1995
"get_blocking" => ctx. new_function( os_get_blocking) ,
0 commit comments