1- use super :: abi:: {
1+ use super :: fd:: FileDesc ;
2+ use super :: hermit_abi:: {
23 self , dirent64, stat as stat_struct, DT_DIR , DT_LNK , DT_REG , DT_UNKNOWN , O_APPEND , O_CREAT ,
34 O_EXCL , O_RDONLY , O_RDWR , O_TRUNC , O_WRONLY , S_IFDIR , S_IFLNK , S_IFMT , S_IFREG ,
45} ;
5- use super :: fd:: FileDesc ;
66use crate :: ffi:: { CStr , OsStr , OsString } ;
77use crate :: fmt;
88use crate :: io:: { self , Error , ErrorKind } ;
@@ -47,7 +47,7 @@ impl InnerReadDir {
4747
4848pub struct ReadDir {
4949 inner : Arc < InnerReadDir > ,
50- pos : i64 ,
50+ pos : usize ,
5151}
5252
5353impl ReadDir {
@@ -197,38 +197,31 @@ impl Iterator for ReadDir {
197197
198198 fn next ( & mut self ) -> Option < io:: Result < DirEntry > > {
199199 let mut counter: usize = 0 ;
200- let mut offset: i64 = 0 ;
200+ let mut offset: usize = 0 ;
201201
202202 // loop over all directory entries and search the entry for the current position
203203 loop {
204204 // leave function, if the loop reaches the of the buffer (with all entries)
205- if offset >= self . inner . dir . len ( ) . try_into ( ) . unwrap ( ) {
205+ if offset >= self . inner . dir . len ( ) {
206206 return None ;
207207 }
208208
209- let dir = unsafe {
210- & * ( self . inner . dir . as_ptr ( ) . offset ( offset. try_into ( ) . unwrap ( ) ) as * const dirent64 )
211- } ;
209+ let dir = unsafe { & * ( self . inner . dir . as_ptr ( ) . add ( offset) as * const dirent64 ) } ;
212210
213- if counter == self . pos . try_into ( ) . unwrap ( ) {
211+ if counter == self . pos {
214212 self . pos += 1 ;
215213
216214 // After dirent64, the file name is stored. d_reclen represents the length of the dirent64
217215 // plus the length of the file name. Consequently, file name has a size of d_reclen minus
218216 // the size of dirent64. The file name is always a C string and terminated by `\0`.
219217 // Consequently, we are able to ignore the last byte.
220- let name_bytes = unsafe {
221- core:: slice:: from_raw_parts (
222- & dir. d_name as * const _ as * const u8 ,
223- dir. d_reclen as usize - core:: mem:: size_of :: < dirent64 > ( ) - 1 ,
224- )
225- . to_vec ( )
226- } ;
218+ let name_bytes =
219+ unsafe { CStr :: from_ptr ( & dir. d_name as * const _ as * const i8 ) . to_bytes ( ) } ;
227220 let entry = DirEntry {
228221 root : self . inner . root . clone ( ) ,
229222 ino : dir. d_ino ,
230223 type_ : dir. d_type as u32 ,
231- name : OsString :: from_vec ( name_bytes) ,
224+ name : OsString :: from_vec ( name_bytes. to_vec ( ) ) ,
232225 } ;
233226
234227 return Some ( Ok ( entry) ) ;
@@ -237,7 +230,7 @@ impl Iterator for ReadDir {
237230 counter += 1 ;
238231
239232 // move to the next dirent64, which is directly stored after the previous one
240- offset = offset + dir. d_off ;
233+ offset = offset + usize :: from ( dir. d_reclen ) ;
241234 }
242235 }
243236}
@@ -365,7 +358,7 @@ impl File {
365358 mode = 0 ;
366359 }
367360
368- let fd = unsafe { cvt ( abi :: open ( path. as_ptr ( ) , flags, mode) ) ? } ;
361+ let fd = unsafe { cvt ( hermit_abi :: open ( path. as_ptr ( ) , flags, mode) ) ? } ;
369362 Ok ( File ( unsafe { FileDesc :: from_raw_fd ( fd as i32 ) } ) )
370363 }
371364
@@ -446,7 +439,7 @@ impl DirBuilder {
446439
447440 pub fn mkdir ( & self , path : & Path ) -> io:: Result < ( ) > {
448441 run_path_with_cstr ( path, & |path| {
449- cvt ( unsafe { abi :: mkdir ( path. as_ptr ( ) , self . mode ) } ) . map ( |_| ( ) )
442+ cvt ( unsafe { hermit_abi :: mkdir ( path. as_ptr ( ) , self . mode ) } ) . map ( |_| ( ) )
450443 } )
451444 }
452445
@@ -508,7 +501,8 @@ impl FromRawFd for File {
508501}
509502
510503pub fn readdir ( path : & Path ) -> io:: Result < ReadDir > {
511- let fd_raw = run_path_with_cstr ( path, & |path| cvt ( unsafe { abi:: opendir ( path. as_ptr ( ) ) } ) ) ?;
504+ let fd_raw =
505+ run_path_with_cstr ( path, & |path| cvt ( unsafe { hermit_abi:: opendir ( path. as_ptr ( ) ) } ) ) ?;
512506 let fd = unsafe { FileDesc :: from_raw_fd ( fd_raw as i32 ) } ;
513507 let root = path. to_path_buf ( ) ;
514508
@@ -519,8 +513,9 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
519513 // reserve memory to receive all directory entries
520514 vec. resize ( sz, 0 ) ;
521515
522- let readlen =
523- unsafe { abi:: getdents64 ( fd. as_raw_fd ( ) , vec. as_mut_ptr ( ) as * mut dirent64 , sz) } ;
516+ let readlen = unsafe {
517+ hermit_abi:: getdents64 ( fd. as_raw_fd ( ) , vec. as_mut_ptr ( ) as * mut dirent64 , sz)
518+ } ;
524519 if readlen > 0 {
525520 // shrink down to the minimal size
526521 vec. resize ( readlen. try_into ( ) . unwrap ( ) , 0 ) ;
@@ -529,7 +524,7 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
529524
530525 // if the buffer is too small, getdents64 returns EINVAL
531526 // otherwise, getdents64 returns an error number
532- if readlen != ( -abi :: errno:: EINVAL ) . into ( ) {
527+ if readlen != ( -hermit_abi :: errno:: EINVAL ) . into ( ) {
533528 return Err ( Error :: from_raw_os_error ( readlen. try_into ( ) . unwrap ( ) ) ) ;
534529 }
535530
@@ -547,7 +542,7 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
547542}
548543
549544pub fn unlink ( path : & Path ) -> io:: Result < ( ) > {
550- run_path_with_cstr ( path, & |path| cvt ( unsafe { abi :: unlink ( path. as_ptr ( ) ) } ) . map ( |_| ( ) ) )
545+ run_path_with_cstr ( path, & |path| cvt ( unsafe { hermit_abi :: unlink ( path. as_ptr ( ) ) } ) . map ( |_| ( ) ) )
551546}
552547
553548pub fn rename ( _old : & Path , _new : & Path ) -> io:: Result < ( ) > {
@@ -559,7 +554,7 @@ pub fn set_perm(_p: &Path, _perm: FilePermissions) -> io::Result<()> {
559554}
560555
561556pub fn rmdir ( path : & Path ) -> io:: Result < ( ) > {
562- run_path_with_cstr ( path, & |path| cvt ( unsafe { abi :: rmdir ( path. as_ptr ( ) ) } ) . map ( |_| ( ) ) )
557+ run_path_with_cstr ( path, & |path| cvt ( unsafe { hermit_abi :: rmdir ( path. as_ptr ( ) ) } ) . map ( |_| ( ) ) )
563558}
564559
565560pub fn remove_dir_all ( _path : & Path ) -> io:: Result < ( ) > {
@@ -581,15 +576,15 @@ pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
581576pub fn stat ( path : & Path ) -> io:: Result < FileAttr > {
582577 run_path_with_cstr ( path, & |path| {
583578 let mut stat_val: stat_struct = unsafe { mem:: zeroed ( ) } ;
584- cvt ( unsafe { abi :: stat ( path. as_ptr ( ) , & mut stat_val) } ) ?;
579+ cvt ( unsafe { hermit_abi :: stat ( path. as_ptr ( ) , & mut stat_val) } ) ?;
585580 Ok ( FileAttr :: from_stat ( stat_val) )
586581 } )
587582}
588583
589584pub fn lstat ( path : & Path ) -> io:: Result < FileAttr > {
590585 run_path_with_cstr ( path, & |path| {
591586 let mut stat_val: stat_struct = unsafe { mem:: zeroed ( ) } ;
592- cvt ( unsafe { abi :: lstat ( path. as_ptr ( ) , & mut stat_val) } ) ?;
587+ cvt ( unsafe { hermit_abi :: lstat ( path. as_ptr ( ) , & mut stat_val) } ) ?;
593588 Ok ( FileAttr :: from_stat ( stat_val) )
594589 } )
595590}
0 commit comments