Skip to content

[WIP] add support for rename/renameat #752

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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ 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::fcntl::{rename, renameat}.
([#752](https://github.com/nix-rust/nix/pull/752))


### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
29 changes: 29 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@ libc_bitflags!(
}
);

/// Change the name or location of a file
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html)).
pub fn rename<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::rename(old.as_ptr() as *const c_char, new.as_ptr() as *const c_char)
}
)
)));

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

/// Change the name or location of a file
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/renameat.html)).
pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(olddirfd: RawFd, oldpath: &P1, newdirfd: RawFd, newpath: &P2) -> Result<()> {
let res = try!(try!(oldpath.with_nix_path(|old|
newpath.with_nix_path(|new|
unsafe {
libc::renameat(olddirfd, old.as_ptr() as *const c_char,
newdirfd, new.as_ptr() as *const c_char)
}
)
)));

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

pub enum FcntlArg<'a> {
F_DUPFD(RawFd),
F_DUPFD_CLOEXEC(RawFd),
Expand Down
21 changes: 20 additions & 1 deletion test/test_fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use nix::fcntl::{openat, open, OFlag, O_RDONLY, readlink, readlinkat};
use nix::fcntl::{openat, open, OFlag, O_RDONLY, readlink, readlinkat, rename, renameat};
use nix::sys::stat::Mode;
use nix::unistd::{close, read};
use tempdir::TempDir;
use tempfile::NamedTempFile;
use std::fs::File;
use std::io::prelude::*;
use std::os::unix::fs;

Expand Down Expand Up @@ -47,6 +48,24 @@ fn test_readlink() {
src.to_str().unwrap());
}

#[test]
fn test_rename_renameat() {
let tempdir = TempDir::new("nix-test_rename")
.unwrap_or_else(|e| panic!("tempdir failed: {}", e));
let src = tempdir.path().join("a");
let dst = tempdir.path().join("b");
File::create(&src).unwrap();

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

let dir = open(tempdir.path(),
OFlag::empty(),
Mode::empty()).unwrap();
renameat(dir, "b", dir, "a").unwrap();
assert!(src.exists());
}

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux_android {
use std::io::prelude::*;
Expand Down