1
- use super :: abi:: {
1
+ use super :: fd:: FileDesc ;
2
+ use super :: hermit_abi:: {
2
3
self , dirent64, stat as stat_struct, DT_DIR , DT_LNK , DT_REG , DT_UNKNOWN , O_APPEND , O_CREAT ,
3
4
O_EXCL , O_RDONLY , O_RDWR , O_TRUNC , O_WRONLY , S_IFDIR , S_IFLNK , S_IFMT , S_IFREG ,
4
5
} ;
5
- use super :: fd:: FileDesc ;
6
6
use crate :: ffi:: { CStr , OsStr , OsString } ;
7
7
use crate :: fmt;
8
8
use crate :: io:: { self , Error , ErrorKind } ;
@@ -47,7 +47,7 @@ impl InnerReadDir {
47
47
48
48
pub struct ReadDir {
49
49
inner : Arc < InnerReadDir > ,
50
- pos : i64 ,
50
+ pos : usize ,
51
51
}
52
52
53
53
impl ReadDir {
@@ -197,38 +197,31 @@ impl Iterator for ReadDir {
197
197
198
198
fn next ( & mut self ) -> Option < io:: Result < DirEntry > > {
199
199
let mut counter: usize = 0 ;
200
- let mut offset: i64 = 0 ;
200
+ let mut offset: usize = 0 ;
201
201
202
202
// loop over all directory entries and search the entry for the current position
203
203
loop {
204
204
// 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 ( ) {
206
206
return None ;
207
207
}
208
208
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 ) } ;
212
210
213
- if counter == self . pos . try_into ( ) . unwrap ( ) {
211
+ if counter == self . pos {
214
212
self . pos += 1 ;
215
213
216
214
// After dirent64, the file name is stored. d_reclen represents the length of the dirent64
217
215
// plus the length of the file name. Consequently, file name has a size of d_reclen minus
218
216
// the size of dirent64. The file name is always a C string and terminated by `\0`.
219
217
// 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 ( ) } ;
227
220
let entry = DirEntry {
228
221
root : self . inner . root . clone ( ) ,
229
222
ino : dir. d_ino ,
230
223
type_ : dir. d_type as u32 ,
231
- name : OsString :: from_vec ( name_bytes) ,
224
+ name : OsString :: from_vec ( name_bytes. to_vec ( ) ) ,
232
225
} ;
233
226
234
227
return Some ( Ok ( entry) ) ;
@@ -237,7 +230,7 @@ impl Iterator for ReadDir {
237
230
counter += 1 ;
238
231
239
232
// 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 ) ;
241
234
}
242
235
}
243
236
}
@@ -365,7 +358,7 @@ impl File {
365
358
mode = 0 ;
366
359
}
367
360
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) ) ? } ;
369
362
Ok ( File ( unsafe { FileDesc :: from_raw_fd ( fd as i32 ) } ) )
370
363
}
371
364
@@ -446,7 +439,7 @@ impl DirBuilder {
446
439
447
440
pub fn mkdir ( & self , path : & Path ) -> io:: Result < ( ) > {
448
441
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 ( |_| ( ) )
450
443
} )
451
444
}
452
445
@@ -508,7 +501,8 @@ impl FromRawFd for File {
508
501
}
509
502
510
503
pub 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 ( ) ) } ) ) ?;
512
506
let fd = unsafe { FileDesc :: from_raw_fd ( fd_raw as i32 ) } ;
513
507
let root = path. to_path_buf ( ) ;
514
508
@@ -519,8 +513,9 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
519
513
// reserve memory to receive all directory entries
520
514
vec. resize ( sz, 0 ) ;
521
515
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
+ } ;
524
519
if readlen > 0 {
525
520
// shrink down to the minimal size
526
521
vec. resize ( readlen. try_into ( ) . unwrap ( ) , 0 ) ;
@@ -529,7 +524,7 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
529
524
530
525
// if the buffer is too small, getdents64 returns EINVAL
531
526
// otherwise, getdents64 returns an error number
532
- if readlen != ( -abi :: errno:: EINVAL ) . into ( ) {
527
+ if readlen != ( -hermit_abi :: errno:: EINVAL ) . into ( ) {
533
528
return Err ( Error :: from_raw_os_error ( readlen. try_into ( ) . unwrap ( ) ) ) ;
534
529
}
535
530
@@ -547,7 +542,7 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
547
542
}
548
543
549
544
pub 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 ( |_| ( ) ) )
551
546
}
552
547
553
548
pub fn rename ( _old : & Path , _new : & Path ) -> io:: Result < ( ) > {
@@ -559,7 +554,7 @@ pub fn set_perm(_p: &Path, _perm: FilePermissions) -> io::Result<()> {
559
554
}
560
555
561
556
pub 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 ( |_| ( ) ) )
563
558
}
564
559
565
560
pub fn remove_dir_all ( _path : & Path ) -> io:: Result < ( ) > {
@@ -581,15 +576,15 @@ pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
581
576
pub fn stat ( path : & Path ) -> io:: Result < FileAttr > {
582
577
run_path_with_cstr ( path, & |path| {
583
578
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) } ) ?;
585
580
Ok ( FileAttr :: from_stat ( stat_val) )
586
581
} )
587
582
}
588
583
589
584
pub fn lstat ( path : & Path ) -> io:: Result < FileAttr > {
590
585
run_path_with_cstr ( path, & |path| {
591
586
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) } ) ?;
593
588
Ok ( FileAttr :: from_stat ( stat_val) )
594
589
} )
595
590
}
0 commit comments