File tree Expand file tree Collapse file tree 2 files changed +13
-31
lines changed Expand file tree Collapse file tree 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 {
7575 }
7676}
7777
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-
10078pub fn cvt < T : One + PartialEq + Neg < Output =T > > ( t : T ) -> io:: Result < T > {
10179 let one: T = T :: one ( ) ;
10280 if t == -one {
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ use sys::c;
3030use sys:: fd;
3131use vec;
3232
33- pub const BUF_BYTES : usize = 2048 ;
33+ const GETCWD_BUF_BYTES : usize = 2048 ;
3434const TMPBUF_SZ : usize = 128 ;
3535
3636/// Returns the platform-specific value of errno
@@ -94,22 +94,26 @@ pub fn error_string(errno: i32) -> String {
9494}
9595
9696pub 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 {
98100 unsafe {
101+ buf. reserve ( n) ;
99102 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 ( ) {
101104 let len = CStr :: from_ptr ( buf. as_ptr ( ) as * const libc:: c_char ) . to_bytes ( ) . len ( ) ;
102105 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) ) ) ;
104108 } else {
105109 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 ) ;
108112 }
109- Err ( error )
110- } )
113+ }
114+ n *= 2 ;
111115 }
112- } )
116+ }
113117}
114118
115119pub fn chdir ( p : & path:: Path ) -> io:: Result < ( ) > {
You can’t perform that action at this time.
0 commit comments