11
11
//! Blocking Windows-based file I/O
12
12
13
13
use alloc:: arc:: Arc ;
14
- use libc:: { c_int, c_void} ;
15
- use libc;
14
+ use libc:: { mod, c_int} ;
16
15
use std:: c_str:: CString ;
17
16
use std:: mem;
18
17
use std:: os:: windows:: fill_utf16_buf_and_decode;
19
18
use std:: ptr;
20
19
use std:: rt:: rtio;
21
20
use std:: rt:: rtio:: { IoResult , IoError } ;
22
21
use std:: str;
23
- use std:: vec;
24
22
25
23
pub type fd_t = libc:: c_int ;
26
24
@@ -344,8 +342,6 @@ pub fn mkdir(p: &CString, _mode: uint) -> IoResult<()> {
344
342
}
345
343
346
344
pub fn readdir ( p : & CString ) -> IoResult < Vec < CString > > {
347
- use std:: rt:: libc_heap:: malloc_raw;
348
-
349
345
fn prune ( root : & CString , dirs : Vec < Path > ) -> Vec < CString > {
350
346
let root = unsafe { CString :: new ( root. as_ptr ( ) , false ) } ;
351
347
let root = Path :: new ( root) ;
@@ -355,38 +351,35 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
355
351
} ) . map ( |path| root. join ( path) . to_c_str ( ) ) . collect ( )
356
352
}
357
353
358
- extern {
359
- fn rust_list_dir_wfd_size ( ) -> libc:: size_t ;
360
- fn rust_list_dir_wfd_fp_buf ( wfd : * mut libc:: c_void ) -> * const u16 ;
361
- }
362
354
let star = Path :: new ( unsafe {
363
355
CString :: new ( p. as_ptr ( ) , false )
364
356
} ) . join ( "*" ) ;
365
357
let path = try!( to_utf16 ( & star. to_c_str ( ) ) ) ;
366
358
367
359
unsafe {
368
- let wfd_ptr = malloc_raw ( rust_list_dir_wfd_size ( ) as uint ) ;
369
- let find_handle = libc:: FindFirstFileW ( path. as_ptr ( ) ,
370
- wfd_ptr as libc:: HANDLE ) ;
360
+ let mut wfd = mem:: zeroed ( ) ;
361
+ let find_handle = libc:: FindFirstFileW ( path. as_ptr ( ) , & mut wfd) ;
371
362
if find_handle != libc:: INVALID_HANDLE_VALUE {
372
- let mut paths = vec ! ( ) ;
373
- let mut more_files = 1 as libc:: c_int ;
363
+ let mut paths = vec ! [ ] ;
364
+ let mut more_files = 1 as libc:: BOOL ;
374
365
while more_files != 0 {
375
- let fp_buf = rust_list_dir_wfd_fp_buf ( wfd_ptr as * mut c_void ) ;
376
- if fp_buf as uint == 0 {
377
- fail ! ( "os::list_dir() failure: got null ptr from wfd" ) ;
378
- } else {
379
- let fp_vec = vec:: raw:: from_buf ( fp_buf, libc:: wcslen ( fp_buf) as uint ) ;
380
- let fp_trimmed = str:: truncate_utf16_at_nul ( fp_vec. as_slice ( ) ) ;
381
- let fp_str = String :: from_utf16 ( fp_trimmed)
382
- . expect ( "rust_list_dir_wfd_fp_buf returned invalid UTF-16" ) ;
383
- paths. push ( Path :: new ( fp_str) ) ;
366
+ {
367
+ let filename = str:: truncate_utf16_at_nul ( wfd. cFileName ) ;
368
+ match String :: from_utf16 ( filename) {
369
+ Some ( filename) => paths. push ( Path :: new ( filename) ) ,
370
+ None => {
371
+ assert ! ( libc:: FindClose ( find_handle) != 0 ) ;
372
+ return Err ( IoError {
373
+ code : super :: c:: ERROR_ILLEGAL_CHARACTER as uint ,
374
+ extra : 0 ,
375
+ detail : Some ( format ! ( "path was not valid UTF-16: {}" , filename) ) ,
376
+ } )
377
+ } , // FIXME #12056: Convert the UCS-2 to invalid utf-8 instead of erroring
378
+ }
384
379
}
385
- more_files = libc:: FindNextFileW ( find_handle,
386
- wfd_ptr as libc:: HANDLE ) ;
380
+ more_files = libc:: FindNextFileW ( find_handle, & mut wfd) ;
387
381
}
388
382
assert ! ( libc:: FindClose ( find_handle) != 0 ) ;
389
- libc:: free ( wfd_ptr as * mut c_void ) ;
390
383
Ok ( prune ( p, paths) )
391
384
} else {
392
385
Err ( super :: last_error ( ) )
0 commit comments