diff --git a/CHANGELOG.md b/CHANGELOG.md index d93a5ce6bb..3f89df4aab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,23 @@ 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)) + + ```rust + # use nix::fcntl::{readlink, readlinkat}; + // the buffer argument of `readlink` and `readlinkat` has been removed, + // and the return value is now an owned type (`OsString`). + // Existing code can be updated by removing the buffer argument + // and removing any clone or similar operation on the output + + // old code `readlink(&path, &mut buf)` can be replaced with the following + let _: OsString = readlink(&path); + + // old code `readlinkat(dirfd, &path, &mut buf)` can be replaced with the following + let _: OsString = readlinkat(dirfd, &path); + ``` + ### Fixed ### Removed diff --git a/src/fcntl.rs b/src/fcntl.rs index be6ee0f73a..08c339a912 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -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 @@ -177,34 +177,33 @@ pub fn renameat(old_dirfd: Option Result<&OsStr> { +fn wrap_readlink_result(v: &mut Vec, res: ssize_t) -> Result { 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 { + let mut v = Vec::with_capacity(libc::PATH_MAX as usize); 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.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 { + let mut v = Vec::with_capacity(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.capacity() 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`. diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 6b2bbd679f..1bcf12cb5b 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -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"))] diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 46196dec7c..e1e03f3f93 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -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() ); @@ -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(),