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 6 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] - ReleaseDate
### Added
### Changed
- Changed `readlink` and `readlinkat` to return `OsString`
([#1109](https://github.com/nix-rust/nix/pull/1109))

Copy link
Member

Choose a reason for hiding this comment

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

This example isn't going to mean much to a reader. I'm thinking something in English, like "the buffer argument of readlink and readlinkat has been removed, and the return value is now an owned type. Existing code can be updated by removing the buffer argument and removing any clone or similar operation on the output."

```rust
use nix::fcntl::{readlink, readlinkat};
Copy link
Member

Choose a reason for hiding this comment

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

This line isn't important to readers. You can suppress it with a leading #, and delete the following blank line.


readlink(&path);
readlinkat(dirfd, &path);
```

### Fixed
### Removed

Expand Down
31 changes: 16 additions & 15 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,35 @@ 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 len = libc::PATH_MAX as usize;
let mut v = Vec::with_capacity(len);
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, len as size_t) }
Copy link
Member

Choose a reason for hiding this comment

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

You seem to be unaware of the capacity method. You should write it like this:

let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
let res = path.with_nix_path(|cstr| {
    unsafe { libc::readlink(cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.capacity() as size_t) }

})?;

wrap_readlink_result(buffer, res)
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 len = libc::PATH_MAX as usize;
let mut v = Vec::with_capacity(len);
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, 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