Skip to content
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

Implement mkfifoat #1133

Merged
merged 3 commits into from Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added `NixPath::is_empty`.
([#1107](https://github.com/nix-rust/nix/pull/1107))

- Added `mkfifoat`
([#1133](https://github.com/nix-rust/nix/pull/1133))

### Changed
- `Signal::from_c_int` has been replaced by `Signal::try_from`
([#1113](https://github.com/nix-rust/nix/pull/1113))
Expand Down
20 changes: 20 additions & 0 deletions src/unistd.rs
Expand Up @@ -506,6 +506,26 @@ pub fn mkfifo<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
Errno::result(res).map(drop)
}

/// Creates new fifo special file (named pipe) with path `path` and access rights `mode`.
///
/// If `dirfd` has a value, then `path` is relative to directory associated with the file descriptor.
///
/// If `dirfd` is `None`, then `path` is relative to the current working directory.
///
/// # References
///
/// [mkfifoat(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifoat.html).
// mkfifoat is not implemented in OSX or android
#[inline]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
pub fn mkfifoat<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P, mode: Mode) -> Result<()> {
let res = path.with_nix_path(|cstr| unsafe {
libc::mkfifoat(at_rawfd(dirfd), cstr.as_ptr(), mode.bits() as mode_t)
})?;

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

/// Creates a symbolic link at `path2` which points to `path1`.
///
/// If `dirfd` has a value, then `path2` is relative to directory associated
Expand Down
52 changes: 52 additions & 0 deletions test/test_unistd.rs
Expand Up @@ -98,6 +98,58 @@ fn test_mkfifo_directory() {
assert!(mkfifo(&env::temp_dir(), Mode::S_IRUSR).is_err());
}

#[test]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
fn test_mkfifoat_none() {
let tempdir = tempfile::tempdir().unwrap();
let mkfifoat_fifo = tempdir.path().join("mkfifoat_fifo");

mkfifoat(None, &mkfifoat_fifo, Mode::S_IRUSR).unwrap();

let stats = stat::stat(&mkfifoat_fifo).unwrap();
let typ = stat::SFlag::from_bits_truncate(stats.st_mode);
assert_eq!(typ, SFlag::S_IFIFO);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one blank line, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

#[test]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
fn test_mkfifoat() {
let tempdir = tempfile::tempdir().unwrap();
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should really be a separate test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

let mkfifoat_name = "mkfifoat_name";

mkfifoat(Some(dirfd), mkfifoat_name, Mode::S_IRUSR).unwrap();

let stats = stat::fstatat(dirfd, mkfifoat_name, fcntl::AtFlags::empty()).unwrap();
let typ = stat::SFlag::from_bits_truncate(stats.st_mode);
assert_eq!(typ, SFlag::S_IFIFO);
}

#[test]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
fn test_mkfifoat_directory_none() {
// mkfifoat should fail if a directory is given
assert_eq!(
mkfifoat(None, &env::temp_dir(), Mode::S_IRUSR).is_ok(),
false
);
}

#[test]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
fn test_mkfifoat_directory() {
// mkfifoat should fail if a directory is given
let tempdir = tempfile::tempdir().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should be a separate test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap();
let mkfifoat_dir = "mkfifoat_dir";
stat::mkdirat(dirfd, mkfifoat_dir, Mode::S_IRUSR).unwrap();

assert_eq!(
mkfifoat(Some(dirfd), mkfifoat_dir, Mode::S_IRUSR).is_ok(),
false
);
}

#[test]
fn test_getpid() {
let pid: ::libc::pid_t = getpid().into();
Expand Down