Skip to content
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

fix readlink/readlinkat to return too long only when it is long #1109

Merged
merged 8 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] - ReleaseDate
### Added
### Changed
- Changed `readlink` and `readlinkat` to return `osString`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first letter of OsString should be capitalized. Also, since this change is backwards incompatible it would be courteous to our users to explain how to convert their existing code.

([#1109](https://github.com/nix-rust/nix/pull/1109))
### Fixed
### Removed

Expand Down
31 changes: 15 additions & 16 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use {Error, Result, NixPath};
use {Result, NixPath};
use errno::Errno;
use libc::{self, c_int, c_uint, c_char, size_t, ssize_t};
use sys::stat::Mode;
use std::os::raw;
use std::os::unix::io::RawFd;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;

#[cfg(any(target_os = "android", target_os = "linux"))]
use std::ptr; // For splice and copy_file_range
Expand Down Expand Up @@ -177,34 +177,33 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(old_dirfd: Option<Ra
Errno::result(res).map(drop)
}

fn wrap_readlink_result(buffer: &mut[u8], res: ssize_t) -> Result<&OsStr> {
fn wrap_readlink_result(v: &mut Vec<u8>, res: ssize_t) -> Result<OsString> {
match Errno::result(res) {
Err(err) => Err(err),
Ok(len) => {
if (len as usize) >= buffer.len() {
Err(Error::Sys(Errno::ENAMETOOLONG))
} else {
Ok(OsStr::from_bytes(&buffer[..(len as usize)]))
}
unsafe { v.set_len(len as usize) }
Ok(OsString::from_vec(v.to_vec()))
}
}
}

pub fn readlink<'a, P: ?Sized + NixPath>(path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> {
pub fn readlink<'a, P: ?Sized + NixPath>(path: &P) -> Result<OsString> {
let mut v = vec![0u8; libc::PATH_MAX as usize];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to zero initialize. You can use an uninitialized vec instead.

let res = path.with_nix_path(|cstr| {
unsafe { libc::readlink(cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) }
unsafe { libc::readlink(cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.len() as size_t) }
})?;

wrap_readlink_result(buffer, res)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space here.

wrap_readlink_result(&mut v, res)
}


pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> {
pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> {
let mut v = vec![0u8; libc::PATH_MAX as usize];
let res = path.with_nix_path(|cstr| {
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) }
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.len() as size_t) }
})?;

wrap_readlink_result(buffer, res)
wrap_readlink_result(&mut v, res)
}

/// Computes the raw fd consumed by a function of the form `*at`.
Expand Down
9 changes: 4 additions & 5 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ fn test_readlink() {
let dirfd = open(tempdir.path(),
OFlag::empty(),
Mode::empty()).unwrap();
let expected_dir = src.to_str().unwrap();

assert_eq!(readlink(&dst).unwrap().to_str().unwrap(), expected_dir);
assert_eq!(readlinkat(dirfd, "b").unwrap().to_str().unwrap(), expected_dir);

let mut buf = vec![0; src.to_str().unwrap().len() + 1];
assert_eq!(readlink(&dst, &mut buf).unwrap().to_str().unwrap(),
src.to_str().unwrap());
assert_eq!(readlinkat(dirfd, "b", &mut buf).unwrap().to_str().unwrap(),
src.to_str().unwrap());
}

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down
5 changes: 2 additions & 3 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,13 @@ fn test_canceling_alarm() {

#[test]
fn test_symlinkat() {
let mut buf = [0; 1024];
let tempdir = tempfile::tempdir().unwrap();

let target = tempdir.path().join("a");
let linkpath = tempdir.path().join("b");
symlinkat(&target, None, &linkpath).unwrap();
assert_eq!(
readlink(&linkpath, &mut buf).unwrap().to_str().unwrap(),
readlink(&linkpath).unwrap().to_str().unwrap(),
target.to_str().unwrap()
);

Expand All @@ -592,7 +591,7 @@ fn test_symlinkat() {
let linkpath = "d";
symlinkat(target, Some(dirfd), linkpath).unwrap();
assert_eq!(
readlink(&tempdir.path().join(linkpath), &mut buf)
readlink(&tempdir.path().join(linkpath))
.unwrap()
.to_str()
.unwrap(),
Expand Down