Skip to content

Commit

Permalink
Merge #1257
Browse files Browse the repository at this point in the history
1257: Add fchown(2) wrapper r=asomers a=nateavers

Mentioned [in #1029](#1029 (comment)).

Co-authored-by: Nate Avers <nathan.avers@alumni.case.edu>
  • Loading branch information
bors[bot] and nateavers committed Jun 12, 2020
2 parents 93af76f + ba3f3e5 commit b71059f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased] - ReleaseDate
### Added
- Added `fchown(2)` wrapper.
(#[1257](https://github.com/nix-rust/nix/pull/1257))
- Added support on linux systems for `MAP_HUGE_`_`SIZE`_ family of flags.
(#[1211](https://github.com/nix-rust/nix/pull/1211))
- Added support for `F_OFD_*` `fcntl` commands on Linux and Android.
Expand Down
14 changes: 14 additions & 0 deletions src/unistd.rs
Expand Up @@ -651,6 +651,20 @@ pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<Uid>, group: Option<Gi
Errno::result(res).map(drop)
}

/// Change the ownership of the file referred to by the open file descriptor `fd` to be owned by
/// the specified `owner` (user) and `group` (see
/// [fchown(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchown.html)).
///
/// The owner/group for the provided file will not be modified if `None` is
/// provided for that argument. Ownership change will be attempted for the path
/// only if `Some` owner/group is provided.
#[inline]
pub fn fchown(fd: RawFd, owner: Option<Uid>, group: Option<Gid>) -> Result<()> {
let (uid, gid) = chown_raw_ids(owner, group);
let res = unsafe { libc::fchown(fd, uid, gid) };
Errno::result(res).map(drop)
}

/// Flags for `fchownat` function.
#[derive(Clone, Copy, Debug)]
pub enum FchownatFlags {
Expand Down
18 changes: 18 additions & 0 deletions test/test_unistd.rs
Expand Up @@ -17,6 +17,7 @@ use std::ffi::CString;
use std::fs::DirBuilder;
use std::fs::{self, File};
use std::io::Write;
use std::mem;
use std::os::unix::prelude::*;
use tempfile::{tempdir, tempfile};
use libc::{_exit, off_t};
Expand Down Expand Up @@ -409,6 +410,23 @@ fn test_chown() {
chown(&path, uid, gid).unwrap_err();
}

#[test]
fn test_fchown() {
// Testing for anything other than our own UID/GID is hard.
let uid = Some(getuid());
let gid = Some(getgid());

let path = tempfile().unwrap();
let fd = path.as_raw_fd();

fchown(fd, uid, gid).unwrap();
fchown(fd, uid, None).unwrap();
fchown(fd, None, gid).unwrap();

mem::drop(path);
fchown(fd, uid, gid).unwrap_err();
}

#[test]
#[cfg(not(target_os = "redox"))]
fn test_fchownat() {
Expand Down

0 comments on commit b71059f

Please sign in to comment.