Skip to content
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::{chown, lchown, fchown, fchownat}`
([#760](https://github.com/nix-rust/nix/pull/760))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
68 changes: 62 additions & 6 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,17 @@ pub fn getcwd() -> Result<PathBuf> {
}
}

// According to the POSIX, -1 is used to indicate that
// owner and group, respectively, are not to be changed. Since uid_t and
// gid_t are unsigned types, we use wrapping_sub to get '-1'.
fn optional_user(val: Option<Uid>) -> u32 {
val.map(Into::into).unwrap_or((0 as uid_t).wrapping_sub(1))
}

fn optional_group(val: Option<Gid>) -> u32 {
val.map(Into::into).unwrap_or((0 as gid_t).wrapping_sub(1))
}

/// Change the ownership of the file at `path` to be owned by the specified
/// `owner` (user) and `group` (see
/// [chown(2)](http://man7.org/linux/man-pages/man2/lchown.2.html)).
Expand All @@ -506,12 +517,57 @@ pub fn getcwd() -> Result<PathBuf> {
#[inline]
pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<Uid>, group: Option<Gid>) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
// According to the POSIX specification, -1 is used to indicate that
// owner and group, respectively, are not to be changed. Since uid_t and
// gid_t are unsigned types, we use wrapping_sub to get '-1'.
unsafe { libc::chown(cstr.as_ptr(),
owner.map(Into::into).unwrap_or((0 as uid_t).wrapping_sub(1)),
group.map(Into::into).unwrap_or((0 as gid_t).wrapping_sub(1))) }
unsafe {
libc::chown(cstr.as_ptr(),
optional_user(owner),
optional_group(group))
}
}));

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

/// Change ownership of a file
/// (see [lchown(2)](http://man7.org/linux/man-pages/man2/lchown.2.html)).
pub fn lchown<P: ?Sized + NixPath>(path: &P, owner: Option<Uid>, group: Option<Gid>) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
unsafe {
libc::lchown(cstr.as_ptr(),
optional_user(owner),
optional_group(group))
}
}));

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

/// Change ownership of a file
/// (see [fchown(2)](http://man7.org/linux/man-pages/man2/fchown.2.html)).
pub fn fchown(fd: RawFd, owner: Option<Uid>, group: Option<Gid>) -> Result<()> {
let res = unsafe {
libc::fchown(fd,
optional_user(owner),
optional_group(group))
};

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

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

Errno::result(res).map(drop)
Expand Down