|
1 |
| -use {Error, Result, NixPath}; |
| 1 | +use {Result, NixPath}; |
2 | 2 | use errno::Errno;
|
3 | 3 | use libc::{self, c_int, c_uint, c_char, size_t, ssize_t};
|
4 | 4 | use sys::stat::Mode;
|
5 | 5 | use std::os::raw;
|
6 | 6 | use std::os::unix::io::RawFd;
|
7 |
| -use std::ffi::OsStr; |
8 |
| -use std::os::unix::ffi::OsStrExt; |
| 7 | +use std::ffi::OsString; |
| 8 | +use std::os::unix::ffi::OsStringExt; |
9 | 9 |
|
10 | 10 | #[cfg(any(target_os = "android", target_os = "linux"))]
|
11 | 11 | use std::ptr; // For splice and copy_file_range
|
@@ -177,36 +177,33 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(old_dirfd: Option<Ra
|
177 | 177 | Errno::result(res).map(drop)
|
178 | 178 | }
|
179 | 179 |
|
180 |
| -fn wrap_readlink_result(buffer: &mut[u8], res: ssize_t) -> Result<&OsStr> { |
| 180 | +fn wrap_readlink_result(v: &mut Vec<u8>, res: ssize_t) -> Result<OsString> { |
181 | 181 | match Errno::result(res) {
|
182 | 182 | Err(err) => Err(err),
|
183 | 183 | Ok(len) => {
|
184 |
| - if len < 0 { |
185 |
| - Err(Error::Sys(Errno::EINVAL)) |
186 |
| - } else if (len as usize) > buffer.len() { |
187 |
| - Err(Error::Sys(Errno::ENAMETOOLONG)) |
188 |
| - } else { |
189 |
| - Ok(OsStr::from_bytes(&buffer[..(len as usize)])) |
190 |
| - } |
| 184 | + unsafe { v.set_len(len as usize) } |
| 185 | + Ok(OsString::from_vec(v.to_vec())) |
191 | 186 | }
|
192 | 187 | }
|
193 | 188 | }
|
194 | 189 |
|
195 |
| -pub fn readlink<'a, P: ?Sized + NixPath>(path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> { |
| 190 | +pub fn readlink<'a, P: ?Sized + NixPath>(path: &P) -> Result<OsString> { |
| 191 | + let mut v = vec![0u8; libc::PATH_MAX as usize]; |
196 | 192 | let res = path.with_nix_path(|cstr| {
|
197 |
| - unsafe { libc::readlink(cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) } |
| 193 | + unsafe { libc::readlink(cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.len() as size_t) } |
198 | 194 | })?;
|
199 |
| - |
200 |
| - wrap_readlink_result(buffer, res) |
| 195 | + |
| 196 | + wrap_readlink_result(&mut v, res) |
201 | 197 | }
|
202 | 198 |
|
203 | 199 |
|
204 |
| -pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> { |
| 200 | +pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> { |
| 201 | + let mut v = vec![0u8; libc::PATH_MAX as usize]; |
205 | 202 | let res = path.with_nix_path(|cstr| {
|
206 |
| - unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) } |
| 203 | + unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.len() as size_t) } |
207 | 204 | })?;
|
208 | 205 |
|
209 |
| - wrap_readlink_result(buffer, res) |
| 206 | + wrap_readlink_result(&mut v, res) |
210 | 207 | }
|
211 | 208 |
|
212 | 209 | /// Computes the raw fd consumed by a function of the form `*at`.
|
|
0 commit comments