Skip to content

Commit d99d4fb

Browse files
committed
Address some comments on the pull request
1 parent c8a5b13 commit d99d4fb

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

src/libstd/sys/unix/mod.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,20 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
7979
// without specifying its size. They will only report back whether the buffer
8080
// was large enough or not.
8181
//
82-
// The callback is yielded a (pointer, len) pair which can be
83-
// passed to a syscall. The `ptr` is valid for `len` items (i8 in this case).
84-
// The closure is expected to return `None` if the space was insufficient and
85-
// `Some(r)` if the syscall did not fail due to insufficient space.
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.
8686
fn fill_bytes_buf<F, T>(mut f: F) -> io::Result<T>
87-
where F: FnMut(*mut i8, libc::size_t) -> Option<io::Result<T>>,
87+
where F: FnMut(Vec<u8>) -> Result<io::Result<T>,Vec<u8>>,
8888
{
89-
// Start off with a stack buf but then spill over to the heap if we end up
90-
// needing more space.
91-
let mut stack_buf = [0i8; os::BUF_BYTES];
92-
let mut heap_buf = Vec::new();
93-
unsafe {
94-
let mut n = stack_buf.len();
95-
loop {
96-
let buf = if n <= stack_buf.len() {
97-
&mut stack_buf[..]
98-
} else {
99-
heap_buf.set_len(0);
100-
heap_buf.reserve(n);
101-
heap_buf.set_len(n);
102-
&mut heap_buf[..]
103-
};
104-
105-
match f(buf.as_mut_ptr(), n as libc::size_t) {
106-
None => n *= 2,
107-
Some(r) => return r,
108-
}
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,
10996
}
11097
}
11198
}

src/libstd/sys/unix/os.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ use vec;
3333
pub const BUF_BYTES: usize = 2048;
3434
const TMPBUF_SZ: usize = 128;
3535

36-
fn bytes2path(b: &[u8]) -> PathBuf {
37-
PathBuf::from(<OsStr as OsStrExt>::from_bytes(b))
38-
}
39-
40-
fn os2path(os: OsString) -> PathBuf {
41-
bytes2path(os.as_bytes())
42-
}
43-
4436
/// Returns the platform-specific value of errno
4537
pub fn errno() -> i32 {
4638
#[cfg(any(target_os = "macos",
@@ -102,14 +94,17 @@ pub fn error_string(errno: i32) -> String {
10294
}
10395

10496
pub fn getcwd() -> io::Result<PathBuf> {
105-
super::fill_bytes_buf(|buf, len| {
97+
super::fill_bytes_buf(|mut buf| {
10698
unsafe {
107-
Some(if !libc::getcwd(buf, len).is_null() {
108-
Ok(bytes2path(CStr::from_ptr(buf).to_bytes()))
99+
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() {
101+
let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len();
102+
buf.set_len(len);
103+
Ok(PathBuf::from(OsString::from_bytes(buf).unwrap()))
109104
} else {
110105
let error = io::Error::last_os_error();
111106
if error.raw_os_error().unwrap() == libc::ERANGE {
112-
return None;
107+
return Err(buf);
113108
}
114109
Err(error)
115110
})
@@ -134,11 +129,14 @@ pub struct SplitPaths<'a> {
134129
}
135130

136131
pub fn split_paths<'a>(unparsed: &'a OsStr) -> SplitPaths<'a> {
132+
fn bytes_to_path(b: &[u8]) -> PathBuf {
133+
PathBuf::from(<OsStr as OsStrExt>::from_bytes(b))
134+
}
137135
fn is_colon(b: &u8) -> bool { *b == b':' }
138136
let unparsed = unparsed.as_bytes();
139137
SplitPaths {
140138
iter: unparsed.split(is_colon as fn(&u8) -> bool)
141-
.map(bytes2path as fn(&'a [u8]) -> PathBuf)
139+
.map(bytes_to_path as fn(&'a [u8]) -> PathBuf)
142140
}
143141
}
144142

@@ -449,7 +447,7 @@ pub fn page_size() -> usize {
449447
}
450448

451449
pub fn temp_dir() -> PathBuf {
452-
getenv("TMPDIR".as_ref()).map(os2path).unwrap_or_else(|| {
450+
getenv("TMPDIR".as_ref()).map(PathBuf::from).unwrap_or_else(|| {
453451
if cfg!(target_os = "android") {
454452
PathBuf::from("/data/local/tmp")
455453
} else {
@@ -461,7 +459,7 @@ pub fn temp_dir() -> PathBuf {
461459
pub fn home_dir() -> Option<PathBuf> {
462460
return getenv("HOME".as_ref()).or_else(|| unsafe {
463461
fallback()
464-
}).map(os2path);
462+
}).map(PathBuf::from);
465463

466464
#[cfg(any(target_os = "android",
467465
target_os = "ios"))]

0 commit comments

Comments
 (0)