@@ -20,36 +20,42 @@ pub(crate) fn fd_path_for_cxx(fd: RawFd, buf: &mut [u8]) -> isize {
2020 . map_or ( -1_isize , |_| buf. len ( ) as isize )
2121}
2222
23- #[ no_mangle]
23+ #[ unsafe ( no_mangle) ]
2424unsafe extern "C" fn canonical_path ( path : * const c_char , buf : * mut u8 , bufsz : usize ) -> isize {
25- match Utf8CStr :: from_ptr ( path) {
26- Ok ( p) => {
27- let mut buf = cstr_buf:: wrap_ptr ( buf, bufsz) ;
28- FsPath :: from ( p)
29- . realpath ( & mut buf)
30- . map_or ( -1 , |_| buf. len ( ) as isize )
25+ unsafe {
26+ match Utf8CStr :: from_ptr ( path) {
27+ Ok ( p) => {
28+ let mut buf = cstr_buf:: wrap_ptr ( buf, bufsz) ;
29+ FsPath :: from ( p)
30+ . realpath ( & mut buf)
31+ . map_or ( -1 , |_| buf. len ( ) as isize )
32+ }
33+ Err ( _) => -1 ,
3134 }
32- Err ( _) => -1 ,
3335 }
3436}
3537
36- #[ export_name = "mkdirs" ]
38+ #[ unsafe ( export_name = "mkdirs" ) ]
3739unsafe extern "C" fn mkdirs_for_cxx ( path : * const c_char , mode : mode_t ) -> i32 {
38- match Utf8CStr :: from_ptr ( path) {
39- Ok ( p) => FsPath :: from ( p) . mkdirs ( mode) . map_or ( -1 , |_| 0 ) ,
40- Err ( _) => -1 ,
40+ unsafe {
41+ match Utf8CStr :: from_ptr ( path) {
42+ Ok ( p) => FsPath :: from ( p) . mkdirs ( mode) . map_or ( -1 , |_| 0 ) ,
43+ Err ( _) => -1 ,
44+ }
4145 }
4246}
4347
44- #[ export_name = "rm_rf" ]
48+ #[ unsafe ( export_name = "rm_rf" ) ]
4549unsafe extern "C" fn rm_rf_for_cxx ( path : * const c_char ) -> bool {
46- match Utf8CStr :: from_ptr ( path) {
47- Ok ( p) => FsPath :: from ( p) . remove_all ( ) . is_ok ( ) ,
48- Err ( _) => false ,
50+ unsafe {
51+ match Utf8CStr :: from_ptr ( path) {
52+ Ok ( p) => FsPath :: from ( p) . remove_all ( ) . is_ok ( ) ,
53+ Err ( _) => false ,
54+ }
4955 }
5056}
5157
52- #[ no_mangle]
58+ #[ unsafe ( no_mangle) ]
5359unsafe extern "C" fn frm_rf ( fd : OwnedFd ) -> bool {
5460 fn inner ( fd : OwnedFd ) -> io:: Result < ( ) > {
5561 Directory :: try_from ( fd) ?. remove_all ( )
@@ -83,110 +89,122 @@ pub(crate) unsafe fn readlinkat_for_cxx(
8389 buf : * mut u8 ,
8490 bufsz : usize ,
8591) -> isize {
86- // readlinkat() may fail on x86 platform, returning random value
87- // instead of number of bytes placed in buf (length of link)
88- cfg_if ! {
89- if #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ] {
90- libc:: memset( buf. cast( ) , 0 , bufsz) ;
91- let mut r = libc:: readlinkat( dirfd, path, buf. cast( ) , bufsz - 1 ) ;
92- if r > 0 {
93- r = libc:: strlen( buf. cast( ) ) as isize ;
94- }
95- } else {
96- let r = libc:: readlinkat( dirfd, path, buf. cast( ) , bufsz - 1 ) ;
97- if r >= 0 {
98- * buf. offset( r) = b'\0' ;
92+ unsafe {
93+ // readlinkat() may fail on x86 platform, returning random value
94+ // instead of number of bytes placed in buf (length of link)
95+ cfg_if ! {
96+ if #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ] {
97+ libc:: memset( buf. cast( ) , 0 , bufsz) ;
98+ let mut r = libc:: readlinkat( dirfd, path, buf. cast( ) , bufsz - 1 ) ;
99+ if r > 0 {
100+ r = libc:: strlen( buf. cast( ) ) as isize ;
101+ }
102+ } else {
103+ let r = libc:: readlinkat( dirfd, path, buf. cast( ) , bufsz - 1 ) ;
104+ if r >= 0 {
105+ * buf. offset( r) = b'\0' ;
106+ }
99107 }
100108 }
109+ r
101110 }
102- r
103111}
104112
105- #[ export_name = "cp_afc" ]
113+ #[ unsafe ( export_name = "cp_afc" ) ]
106114unsafe extern "C" fn cp_afc_for_cxx ( src : * const c_char , dest : * const c_char ) -> bool {
107- if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
108- if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
109- let src = FsPath :: from ( src) ;
110- let dest = FsPath :: from ( dest) ;
111- return src
112- . copy_to ( dest)
113- . log_cxx_with_msg ( |w| {
114- w. write_fmt ( format_args ! ( "cp_afc {} -> {} failed" , src, dest) )
115- } )
116- . is_ok ( ) ;
115+ unsafe {
116+ if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
117+ if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
118+ let src = FsPath :: from ( src) ;
119+ let dest = FsPath :: from ( dest) ;
120+ return src
121+ . copy_to ( dest)
122+ . log_cxx_with_msg ( |w| {
123+ w. write_fmt ( format_args ! ( "cp_afc {} -> {} failed" , src, dest) )
124+ } )
125+ . is_ok ( ) ;
126+ }
117127 }
128+ false
118129 }
119- false
120130}
121131
122- #[ export_name = "mv_path" ]
132+ #[ unsafe ( export_name = "mv_path" ) ]
123133unsafe extern "C" fn mv_path_for_cxx ( src : * const c_char , dest : * const c_char ) -> bool {
124- if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
125- if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
126- let src = FsPath :: from ( src) ;
127- let dest = FsPath :: from ( dest) ;
128- return src
129- . move_to ( dest)
130- . log_cxx_with_msg ( |w| {
131- w. write_fmt ( format_args ! ( "mv_path {} -> {} failed" , src, dest) )
132- } )
133- . is_ok ( ) ;
134+ unsafe {
135+ if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
136+ if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
137+ let src = FsPath :: from ( src) ;
138+ let dest = FsPath :: from ( dest) ;
139+ return src
140+ . move_to ( dest)
141+ . log_cxx_with_msg ( |w| {
142+ w. write_fmt ( format_args ! ( "mv_path {} -> {} failed" , src, dest) )
143+ } )
144+ . is_ok ( ) ;
145+ }
134146 }
147+ false
135148 }
136- false
137149}
138150
139- #[ export_name = "link_path" ]
151+ #[ unsafe ( export_name = "link_path" ) ]
140152unsafe extern "C" fn link_path_for_cxx ( src : * const c_char , dest : * const c_char ) -> bool {
141- if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
142- if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
143- let src = FsPath :: from ( src) ;
144- let dest = FsPath :: from ( dest) ;
145- return src
146- . link_to ( dest)
147- . log_cxx_with_msg ( |w| {
148- w. write_fmt ( format_args ! ( "link_path {} -> {} failed" , src, dest) )
149- } )
150- . is_ok ( ) ;
153+ unsafe {
154+ if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
155+ if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
156+ let src = FsPath :: from ( src) ;
157+ let dest = FsPath :: from ( dest) ;
158+ return src
159+ . link_to ( dest)
160+ . log_cxx_with_msg ( |w| {
161+ w. write_fmt ( format_args ! ( "link_path {} -> {} failed" , src, dest) )
162+ } )
163+ . is_ok ( ) ;
164+ }
151165 }
166+ false
152167 }
153- false
154168}
155169
156- #[ export_name = "clone_attr" ]
170+ #[ unsafe ( export_name = "clone_attr" ) ]
157171unsafe extern "C" fn clone_attr_for_cxx ( src : * const c_char , dest : * const c_char ) -> bool {
158- if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
159- if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
160- let src = FsPath :: from ( src) ;
161- let dest = FsPath :: from ( dest) ;
162- return clone_attr ( src, dest)
163- . log_cxx_with_msg ( |w| {
164- w. write_fmt ( format_args ! ( "clone_attr {} -> {} failed" , src, dest) )
165- } )
166- . is_ok ( ) ;
172+ unsafe {
173+ if let Ok ( src) = Utf8CStr :: from_ptr ( src) {
174+ if let Ok ( dest) = Utf8CStr :: from_ptr ( dest) {
175+ let src = FsPath :: from ( src) ;
176+ let dest = FsPath :: from ( dest) ;
177+ return clone_attr ( src, dest)
178+ . log_cxx_with_msg ( |w| {
179+ w. write_fmt ( format_args ! ( "clone_attr {} -> {} failed" , src, dest) )
180+ } )
181+ . is_ok ( ) ;
182+ }
167183 }
184+ false
168185 }
169- false
170186}
171187
172- #[ export_name = "fclone_attr" ]
188+ #[ unsafe ( export_name = "fclone_attr" ) ]
173189unsafe extern "C" fn fclone_attr_for_cxx ( a : RawFd , b : RawFd ) -> bool {
174190 fclone_attr ( a, b)
175191 . log_cxx_with_msg ( |w| w. write_str ( "fclone_attr failed" ) )
176192 . is_ok ( )
177193}
178194
179- #[ export_name = "cxx$utf8str$new" ]
195+ #[ unsafe ( export_name = "cxx$utf8str$new" ) ]
180196unsafe extern "C" fn str_new ( this : & mut & Utf8CStr , s : * const u8 , len : usize ) {
181- * this = Utf8CStr :: from_bytes ( slice_from_ptr ( s, len) ) . unwrap_or ( cstr ! ( "" ) ) ;
197+ unsafe {
198+ * this = Utf8CStr :: from_bytes ( slice_from_ptr ( s, len) ) . unwrap_or ( cstr ! ( "" ) ) ;
199+ }
182200}
183201
184- #[ export_name = "cxx$utf8str$ptr" ]
202+ #[ unsafe ( export_name = "cxx$utf8str$ptr" ) ]
185203unsafe extern "C" fn str_ptr ( this : & & Utf8CStr ) -> * const u8 {
186204 this. as_ptr ( ) . cast ( )
187205}
188206
189- #[ export_name = "cxx$utf8str$len" ]
207+ #[ unsafe ( export_name = "cxx$utf8str$len" ) ]
190208unsafe extern "C" fn str_len ( this : & & Utf8CStr ) -> usize {
191209 this. len ( )
192210}
0 commit comments