Skip to content

add more *at functions. #562

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 10 commits 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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#591](https://github.com/nix-rust/nix/pull/591)
- Added `AioCb::from_boxed_slice`
([#582](https://github.com/nix-rust/nix/pull/582)
- Added `nix::unistd::{openat, fstatat, readlink, readlinkat}`
([#551](https://github.com/nix-rust/nix/pull/551))
- Added `nix::unistd::{openat, fstatat, readlink, readlinkat, rename, renameat, mknodat, unlinkat, mkdirat, link, linkat, symlink, symlinkat, access, faccessat}`
([#552](https://github.com/nix-rust/nix/pull/552), [#561](https://github.com/nix-rust/nix/pull/561))
- Added `nix::stat::{chmod, fchmod, fchmodat, futimens, utimensat}` ([#561](https://github.com/nix-rust/nix/pull/561))
- Added `nix::unistd::{chown, lchown, fchown, fchownat}` ([#561](https://github.com/nix-rust/nix/pull/561))
- Added `nix::pty::{grantpt, posix_openpt, ptsname/ptsname_r, unlockpt}`
([#556](https://github.com/nix-rust/nix/pull/556)
- Added `nix::ptr::openpty`
Expand Down
37 changes: 34 additions & 3 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use sys::uio::IoVec; // For vmsplice
libc_bitflags!{
pub struct AtFlags: c_int {
AT_SYMLINK_NOFOLLOW;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_REMOVEDIR;
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_NO_AUTOMOUNT;
#[cfg(any(target_os = "android", target_os = "linux"))]
AT_EMPTY_PATH;
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_EMPTY_PATH
}
}

Expand Down Expand Up @@ -206,6 +208,35 @@ libc_bitflags!(
}
);

/// Change the name or location of a file
/// ([see rename(2)](http://man7.org/linux/man-pages/man2/rename.2.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
/// ([see renameat(2)](http://man7.org/linux/man-pages/man2/renameat.2.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
117 changes: 117 additions & 0 deletions src/sys/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use libc::{self, mode_t};
use std::mem;
use std::os::unix::io::RawFd;

pub use self::linux::*;

libc_bitflags!(
pub struct SFlag: mode_t {
S_IFIFO;
Expand Down Expand Up @@ -50,6 +52,19 @@ pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t)
Errno::result(res).map(drop)
}

/// Create a special or ordinary file
/// ([see mknodat(2)](http://man7.org/linux/man-pages/man2/mknodat.2.html)).
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
pub fn mknodat<P: ?Sized + NixPath>(dirfd: &RawFd, path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
unsafe {
libc::mknodat(*dirfd, cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
}
}));

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

#[cfg(target_os = "linux")]
pub fn major(dev: dev_t) -> u64 {
((dev >> 32) & 0xfffff000) |
Expand Down Expand Up @@ -121,3 +136,105 @@ pub fn fstatat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, f: AtFlags) -> R
Ok(dst)
}

/// Change permissions of a file
/// ([see chmod(2)](http://man7.org/linux/man-pages/man2/chmod.2.html)).
pub fn chmod<P: ?Sized + NixPath>(pathname: &P, mode: Mode) -> Result<()> {
let res = try!(pathname.with_nix_path(|cstr| {
unsafe { libc::chmod(cstr.as_ptr(), mode.bits()) }
}));

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

/// Change permissions of a file
/// ([see fchmod(2)](http://man7.org/linux/man-pages/man2/fchmod.2.html)).
pub fn fchmod(fd: RawFd, mode: Mode) -> Result<()> {
let res = unsafe { libc::fchmod(fd, mode.bits()) };

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

/// Change permissions of a file
/// ([see fchmodat(2)](http://man7.org/linux/man-pages/man2/fchmodat.2.html)).
pub fn fchmodat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, mode: Mode, flags: AtFlags) -> Result<()> {
let res = try!(pathname.with_nix_path(|cstr| {
unsafe {
libc::fchmodat(dirfd,
cstr.as_ptr(),
mode.bits(),
flags.bits())
}
}));

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

#[cfg(target_os = "linux")]
mod linux {
use {Errno, Result, NixPath};
use std::os::unix::io::RawFd;
use libc;
use fcntl::AtFlags;
use sys::time::TimeSpec;

/// A file timestamp.
pub enum UtimeSpec {
/// File timestamp is set to the current time.
Now,
/// The corresponding file timestamp is left unchanged.
Omit,
/// File timestamp is set to value
Time(TimeSpec)
}

impl <'a> From<&'a UtimeSpec> for libc::timespec {
fn from(time: &'a UtimeSpec) -> libc::timespec {
match time {
&UtimeSpec::Now => libc::timespec {
tv_sec: 0,
tv_nsec: libc::UTIME_NOW,
},
&UtimeSpec::Omit => libc::timespec {
tv_sec: 0,
tv_nsec: libc::UTIME_OMIT,
},
&UtimeSpec::Time(spec) => *spec.as_ref()
}
}
}

/// Change file timestamps with nanosecond precision
/// (see [utimensat(2)](http://man7.org/linux/man-pages/man2/utimensat.2.html)).
pub fn utimensat<P: ?Sized + NixPath>(dirfd: RawFd,
pathname: &P,
atime: &UtimeSpec,
mtime: &UtimeSpec,
flags: AtFlags) -> Result<()> {
let time = [atime.into(), mtime.into()];
let res = try!(pathname.with_nix_path(|cstr| {
unsafe {
libc::utimensat(dirfd,
cstr.as_ptr(),
time.as_ptr() as *const libc::timespec,
flags.bits())
}
}));

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

/// Change file timestamps with nanosecond precision
/// (see [futimens(2)](http://man7.org/linux/man-pages/man2/futimens.2.html)).
pub fn futimens(fd: RawFd,
atime: &UtimeSpec,
mtime: &UtimeSpec) -> Result<()> {
let time = [atime.into(), mtime.into()];
let res = unsafe {
libc::futimens(fd, time.as_ptr() as *const libc::timespec)
};

Errno::result(res).map(drop)
}
}
#[cfg(not(target_os = "linux"))]
mod linux { }
Loading