1- use { Errno , Result , NixPath } ;
2- use libc:: { self , c_int, c_uint} ;
1+ use { Error , Errno , Result , NixPath } ;
2+ use libc:: { self , c_int, c_uint, c_char , size_t , ssize_t } ;
33use sys:: stat:: Mode ;
44use std:: os:: unix:: io:: RawFd ;
5+ use std:: ffi:: OsStr ;
6+ use std:: os:: unix:: ffi:: OsStrExt ;
57
68#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
79use sys:: uio:: IoVec ; // For vmsplice
@@ -18,6 +20,25 @@ mod ffi {
1820 pub const F_GET_SEALS : c_int = 1034 ;
1921}
2022
23+ #[ cfg( not( any( target_os = "ios" , target_os = "macos" ) ) ) ]
24+ libc_bitflags ! {
25+ pub flags AtFlags : c_int {
26+ AT_SYMLINK_NOFOLLOW ,
27+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
28+ AT_NO_AUTOMOUNT ,
29+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
30+ AT_EMPTY_PATH
31+ }
32+ }
33+
34+ #[ cfg( any( target_os = "ios" , target_os = "macos" ) ) ]
35+ bitflags ! (
36+ pub flags AtFlags : c_int {
37+ // hack because bitflags require one entry
38+ const EMPTY = 0x0
39+ }
40+ ) ;
41+
2142pub fn open < P : ?Sized + NixPath > ( path : & P , oflag : OFlag , mode : Mode ) -> Result < RawFd > {
2243 let fd = try!( path. with_nix_path ( |cstr| {
2344 unsafe { libc:: open ( cstr. as_ptr ( ) , oflag. bits ( ) , mode. bits ( ) as c_uint ) }
@@ -26,6 +47,44 @@ pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<R
2647 Errno :: result ( fd)
2748}
2849
50+ pub fn openat < P : ?Sized + NixPath > ( dirfd : RawFd , path : & P , oflag : OFlag , mode : Mode ) -> Result < RawFd > {
51+ let fd = try!( path. with_nix_path ( |cstr| {
52+ unsafe { libc:: openat ( dirfd, cstr. as_ptr ( ) , oflag. bits ( ) , mode. bits ( ) as c_uint ) }
53+ } ) ) ;
54+ Errno :: result ( fd)
55+ }
56+
57+ fn wrap_readlink_result < ' a > ( buffer : & ' a mut [ u8 ] , res : ssize_t )
58+ -> Result < & ' a OsStr > {
59+ match Errno :: result ( res) {
60+ Err ( err) => Err ( err) ,
61+ Ok ( len) => {
62+ if ( len as usize ) >= buffer. len ( ) {
63+ Err ( Error :: Sys ( Errno :: ENAMETOOLONG ) )
64+ } else {
65+ Ok ( OsStr :: from_bytes ( & buffer[ ..( len as usize ) ] ) )
66+ }
67+ }
68+ }
69+ }
70+
71+ pub fn readlink < ' a , P : ?Sized + NixPath > ( path : & P , buffer : & ' a mut [ u8 ] ) -> Result < & ' a OsStr > {
72+ let res = try!( path. with_nix_path ( |cstr| {
73+ unsafe { libc:: readlink ( cstr. as_ptr ( ) , buffer. as_mut_ptr ( ) as * mut c_char , buffer. len ( ) as size_t ) }
74+ } ) ) ;
75+
76+ wrap_readlink_result ( buffer, res)
77+ }
78+
79+
80+ pub fn readlinkat < ' a , P : ?Sized + NixPath > ( dirfd : RawFd , path : & P , buffer : & ' a mut [ u8 ] ) -> Result < & ' a OsStr > {
81+ let res = try!( path. with_nix_path ( |cstr| {
82+ unsafe { libc:: readlinkat ( dirfd, cstr. as_ptr ( ) , buffer. as_mut_ptr ( ) as * mut c_char , buffer. len ( ) as size_t ) }
83+ } ) ) ;
84+
85+ wrap_readlink_result ( buffer, res)
86+ }
87+
2988pub enum FcntlArg < ' a > {
3089 F_DUPFD ( RawFd ) ,
3190 F_DUPFD_CLOEXEC ( RawFd ) ,
0 commit comments