@@ -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,34 @@ 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
209
let dir = unsafe {
210
- & * ( self . inner . dir . as_ptr ( ) . offset ( offset. try_into ( ) . unwrap ( ) ) as * const dirent64 )
210
+ & * ( self . inner . dir . as_ptr ( ) . add ( offset) as * const dirent64 )
211
211
} ;
212
212
213
- if counter == self . pos . try_into ( ) . unwrap ( ) {
213
+ if counter == self . pos {
214
214
self . pos += 1 ;
215
215
216
216
// After dirent64, the file name is stored. d_reclen represents the length of the dirent64
217
217
// plus the length of the file name. Consequently, file name has a size of d_reclen minus
218
218
// the size of dirent64. The file name is always a C string and terminated by `\0`.
219
219
// Consequently, we are able to ignore the last byte.
220
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 ( )
221
+ CStr :: from_ptr ( & dir. d_name as * const _ as * const i8 ) . to_bytes ( )
226
222
} ;
227
223
let entry = DirEntry {
228
224
root : self . inner . root . clone ( ) ,
229
225
ino : dir. d_ino ,
230
226
type_ : dir. d_type as u32 ,
231
- name : OsString :: from_vec ( name_bytes) ,
227
+ name : OsString :: from_vec ( name_bytes. to_vec ( ) ) ,
232
228
} ;
233
229
234
230
return Some ( Ok ( entry) ) ;
@@ -237,7 +233,7 @@ impl Iterator for ReadDir {
237
233
counter += 1 ;
238
234
239
235
// move to the next dirent64, which is directly stored after the previous one
240
- offset = offset + dir. d_off ;
236
+ offset = offset + usize :: from ( dir. d_reclen ) ;
241
237
}
242
238
}
243
239
}
0 commit comments