-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Make std::env::current_dir
work for path names longer than 2048 bytes on non-Windows
#26896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,28 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind { | |
} | ||
} | ||
|
||
// Some system functions expect the user to pass a appropiately-sized buffer | ||
// without specifying its size. They will only report back whether the buffer | ||
// was large enough or not. | ||
// | ||
// The callback is yielded an uninitialized vector which can be passed to a | ||
// syscall. The closure is expected to return `Err(v)` with the passed vector | ||
// if the space was insufficient and `Ok(r)` if the syscall did not fail due to | ||
// insufficient space. | ||
fn fill_bytes_buf<F, T>(mut f: F) -> io::Result<T> | ||
where F: FnMut(Vec<u8>) -> Result<io::Result<T>,Vec<u8>>, | ||
{ | ||
let mut buf = Vec::new(); | ||
let mut n = os::BUF_BYTES; | ||
loop { | ||
buf.reserve(n); | ||
match f(buf) { | ||
Err(b) => { buf = b; n *= 2; } | ||
Ok(r) => return r, | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There aren't any other functions using this on the unix side, so I think it'd be best to just inline the logic into |
||
|
||
pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> { | ||
let one: T = T::one(); | ||
if t == -one { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,25 +22,17 @@ use io; | |
use iter; | ||
use libc::{self, c_int, c_char, c_void}; | ||
use mem; | ||
use ptr; | ||
use path::{self, PathBuf}; | ||
use ptr; | ||
use slice; | ||
use str; | ||
use sys::c; | ||
use sys::fd; | ||
use vec; | ||
|
||
const BUF_BYTES: usize = 2048; | ||
pub const BUF_BYTES: usize = 2048; | ||
const TMPBUF_SZ: usize = 128; | ||
|
||
fn bytes2path(b: &[u8]) -> PathBuf { | ||
PathBuf::from(<OsStr as OsStrExt>::from_bytes(b)) | ||
} | ||
|
||
fn os2path(os: OsString) -> PathBuf { | ||
bytes2path(os.as_bytes()) | ||
} | ||
|
||
/// Returns the platform-specific value of errno | ||
pub fn errno() -> i32 { | ||
#[cfg(any(target_os = "macos", | ||
|
@@ -102,14 +94,22 @@ pub fn error_string(errno: i32) -> String { | |
} | ||
|
||
pub fn getcwd() -> io::Result<PathBuf> { | ||
let mut buf = [0 as c_char; BUF_BYTES]; | ||
unsafe { | ||
if libc::getcwd(buf.as_mut_ptr(), buf.len() as libc::size_t).is_null() { | ||
Err(io::Error::last_os_error()) | ||
} else { | ||
Ok(bytes2path(CStr::from_ptr(buf.as_ptr()).to_bytes())) | ||
super::fill_bytes_buf(|mut buf| { | ||
unsafe { | ||
let ptr = buf.as_mut_ptr() as *mut libc::c_char; | ||
Ok(if !libc::getcwd(ptr, buf.capacity() as libc::size_t).is_null() { | ||
let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len(); | ||
buf.set_len(len); | ||
Ok(PathBuf::from(OsString::from_bytes(buf).unwrap())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the |
||
} else { | ||
let error = io::Error::last_os_error(); | ||
if error.raw_os_error().unwrap() == libc::ERANGE { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you can also compare against There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a program error if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abandoning sanity-checking for idomatic code is not something I'd like to do, but since you insist, I'll change it. |
||
return Err(buf); | ||
} | ||
Err(error) | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
pub fn chdir(p: &path::Path) -> io::Result<()> { | ||
|
@@ -129,11 +129,14 @@ pub struct SplitPaths<'a> { | |
} | ||
|
||
pub fn split_paths<'a>(unparsed: &'a OsStr) -> SplitPaths<'a> { | ||
fn bytes_to_path(b: &[u8]) -> PathBuf { | ||
PathBuf::from(<OsStr as OsStrExt>::from_bytes(b)) | ||
} | ||
fn is_colon(b: &u8) -> bool { *b == b':' } | ||
let unparsed = unparsed.as_bytes(); | ||
SplitPaths { | ||
iter: unparsed.split(is_colon as fn(&u8) -> bool) | ||
.map(bytes2path as fn(&'a [u8]) -> PathBuf) | ||
.map(bytes_to_path as fn(&'a [u8]) -> PathBuf) | ||
} | ||
} | ||
|
||
|
@@ -444,7 +447,7 @@ pub fn page_size() -> usize { | |
} | ||
|
||
pub fn temp_dir() -> PathBuf { | ||
getenv("TMPDIR".as_ref()).map(os2path).unwrap_or_else(|| { | ||
getenv("TMPDIR".as_ref()).map(PathBuf::from).unwrap_or_else(|| { | ||
if cfg!(target_os = "android") { | ||
PathBuf::from("/data/local/tmp") | ||
} else { | ||
|
@@ -456,7 +459,7 @@ pub fn temp_dir() -> PathBuf { | |
pub fn home_dir() -> Option<PathBuf> { | ||
return getenv("HOME".as_ref()).or_else(|| unsafe { | ||
fallback() | ||
}).map(os2path); | ||
}).map(PathBuf::from); | ||
|
||
#[cfg(any(target_os = "android", | ||
target_os = "ios"))] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function actually primarily exists on Windows due to the utf-16 to wtf-8 conversion because it allows us to use a u16 stack buffer first and avoid the extra heap allocation unless necessary. On Unix, however, we shouldn't need this function because the same byte buffer filled in by the OS can be converted directly into an
OsString
.