File tree 2 files changed +13
-31
lines changed
2 files changed +13
-31
lines changed Original file line number Diff line number Diff line change @@ -75,28 +75,6 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
75
75
}
76
76
}
77
77
78
- // Some system functions expect the user to pass a appropiately-sized buffer
79
- // without specifying its size. They will only report back whether the buffer
80
- // was large enough or not.
81
- //
82
- // The callback is yielded an uninitialized vector which can be passed to a
83
- // syscall. The closure is expected to return `Err(v)` with the passed vector
84
- // if the space was insufficient and `Ok(r)` if the syscall did not fail due to
85
- // insufficient space.
86
- fn fill_bytes_buf < F , T > ( mut f : F ) -> io:: Result < T >
87
- where F : FnMut ( Vec < u8 > ) -> Result < io:: Result < T > , Vec < u8 > > ,
88
- {
89
- let mut buf = Vec :: new ( ) ;
90
- let mut n = os:: BUF_BYTES ;
91
- loop {
92
- buf. reserve ( n) ;
93
- match f ( buf) {
94
- Err ( b) => { buf = b; n *= 2 ; }
95
- Ok ( r) => return r,
96
- }
97
- }
98
- }
99
-
100
78
pub fn cvt < T : One + PartialEq + Neg < Output =T > > ( t : T ) -> io:: Result < T > {
101
79
let one: T = T :: one ( ) ;
102
80
if t == -one {
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ use sys::c;
30
30
use sys:: fd;
31
31
use vec;
32
32
33
- pub const BUF_BYTES : usize = 2048 ;
33
+ const GETCWD_BUF_BYTES : usize = 2048 ;
34
34
const TMPBUF_SZ : usize = 128 ;
35
35
36
36
/// Returns the platform-specific value of errno
@@ -94,22 +94,26 @@ pub fn error_string(errno: i32) -> String {
94
94
}
95
95
96
96
pub fn getcwd ( ) -> io:: Result < PathBuf > {
97
- super :: fill_bytes_buf ( |mut buf| {
97
+ let mut buf = Vec :: new ( ) ;
98
+ let mut n = GETCWD_BUF_BYTES ;
99
+ loop {
98
100
unsafe {
101
+ buf. reserve ( n) ;
99
102
let ptr = buf. as_mut_ptr ( ) as * mut libc:: c_char ;
100
- Ok ( if !libc:: getcwd ( ptr, buf. capacity ( ) as libc:: size_t ) . is_null ( ) {
103
+ if !libc:: getcwd ( ptr, buf. capacity ( ) as libc:: size_t ) . is_null ( ) {
101
104
let len = CStr :: from_ptr ( buf. as_ptr ( ) as * const libc:: c_char ) . to_bytes ( ) . len ( ) ;
102
105
buf. set_len ( len) ;
103
- Ok ( PathBuf :: from ( OsString :: from_bytes ( buf) . unwrap ( ) ) )
106
+ buf. shrink_to_fit ( ) ;
107
+ return Ok ( PathBuf :: from ( OsString :: from_vec ( buf) ) ) ;
104
108
} else {
105
109
let error = io:: Error :: last_os_error ( ) ;
106
- if error. raw_os_error ( ) . unwrap ( ) == libc:: ERANGE {
107
- return Err ( buf ) ;
110
+ if error. raw_os_error ( ) != Some ( libc:: ERANGE ) {
111
+ return Err ( error ) ;
108
112
}
109
- Err ( error )
110
- } )
113
+ }
114
+ n *= 2 ;
111
115
}
112
- } )
116
+ }
113
117
}
114
118
115
119
pub fn chdir ( p : & path:: Path ) -> io:: Result < ( ) > {
You can’t perform that action at this time.
0 commit comments