Skip to content

[WIP] add support for link|linkat #755

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#739](https://github.com/nix-rust/nix/pull/739))
- Added nix::sys::ptrace::detach.
([#749](https://github.com/nix-rust/nix/pull/749))
- Added `nix::unistd::{link, linkat}`
([#755](https://github.com/nix-rust/nix/pull/755))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
32 changes: 31 additions & 1 deletion src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use errno;
use {Errno, Error, Result, NixPath};
use fcntl::{fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
use fcntl::{AtFlags, fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::F_SETFD;
use libc::{self, c_char, c_void, c_int, c_long, c_uint, size_t, pid_t, off_t,
uid_t, gid_t, mode_t};
Expand Down Expand Up @@ -812,6 +812,36 @@ pub fn lseek64(fd: RawFd, offset: libc::off64_t, whence: Whence) -> Result<libc:
Errno::result(res).map(|r| r as libc::off64_t)
}

/// Call the link function to create a link to a file
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html)).
pub fn link<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(oldpath: &P1, newpath: &P2) -> Result<()> {
let res = try!(try!(oldpath.with_nix_path(|old|
newpath.with_nix_path(|new|
unsafe {
libc::link(old.as_ptr() as *const c_char, new.as_ptr() as *const c_char)
}
)
)));

Errno::result(res).map(drop)
}

/// Call the link function to create a link to a file
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/linkat.html)).
pub fn linkat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(olddirfd: RawFd, oldpath: &P1,
newdirfd: RawFd, newpath: &P2, flags: AtFlags) -> Result<()> {
let res = try!(try!(oldpath.with_nix_path(|old|
newpath.with_nix_path(|new|
unsafe {
libc::linkat(olddirfd, old.as_ptr() as *const c_char,
newdirfd, new.as_ptr() as *const c_char, flags.bits())
}
)
)));

Errno::result(res).map(drop)
}

pub fn pipe() -> Result<(RawFd, RawFd)> {
unsafe {
let mut fds: [c_int; 2] = mem::uninitialized();
Expand Down
31 changes: 31 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate tempdir;

use nix::fcntl;
use nix::unistd::*;
use nix::unistd::ForkResult::*;
use nix::sys::wait::*;
Expand Down Expand Up @@ -250,6 +251,36 @@ fn test_fpathconf_limited() {
assert!(path_max.expect("fpathconf failed").expect("PATH_MAX is unlimited") > 0);
}

#[test]
fn test_linkat() {
let tempdir = TempDir::new("nix-test_linkat").unwrap();
let src = tempdir.path().join("foo");
let dst = tempdir.path().join("bar");
File::create(&src).unwrap();

let dirfd = fcntl::open(tempdir.path(),
fcntl::OFlag::empty(),
stat::Mode::empty());
linkat(dirfd.unwrap(),
&src.file_name(),
dirfd.unwrap(),
&dst.file_name(),
fcntl::AtFlags::empty()).unwrap();
assert!(dst.exists());
}

#[test]
fn test_link() {
let tempdir = TempDir::new("nix-test_link").unwrap();
let src = tempdir.path().join("foo");
let dst = tempdir.path().join("bar");
File::create(&src).unwrap();

link(&src, &dst).unwrap();
assert!(dst.exists());
}


#[test]
fn test_pathconf_limited() {
// AFAIK, PATH_MAX is limited on all platforms, so it makes a good test
Expand Down