diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2017-10-11 13:24:50 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2017-10-11 13:24:50 +0000 |
commit | c815ae9904f04a2307fdfb3098e9e70b87b233b8 (patch) | |
tree | 89cb1c8550b6898a6317d41ca9538cf39f507a7e | |
parent | bd7d2b13f86a29369d9cdb21f171369b4a8d9b98 (diff) | |
parent | 08c41c853b092f20cad6f3a3ff9153b3b34564f2 (diff) | |
download | nix-c815ae9904f04a2307fdfb3098e9e70b87b233b8.zip |
Merge #774
774: #602 added unistd::mkfifo r=asomers a=jpastuszek
Since libc has mkfifo already I have just copied mkdir and adapted to mkfifo. I have tested that on MacOS only.
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/unistd.rs | 43 | ||||
-rw-r--r-- | test/test_unistd.rs | 18 |
3 files changed, 63 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 664fdaff..9617a8a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#773](https://github.com/nix-rust/nix/pull/773)) - Add nix::sys::fallocate ([#768](https:://github.com/nix-rust/nix/pull/768)) +- Added `nix::unistd::mkfifo`. + ([#602](https://github.com/nix-rust/nix/pull/774)) ### Changed - Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692)) diff --git a/src/unistd.rs b/src/unistd.rs index 14da5ffd..401357d3 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -438,6 +438,49 @@ pub fn mkdir<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`. +/// +/// # Errors +/// +/// There are several situations where mkfifo might fail: +/// +/// - current user has insufficient rights in the parent directory +/// - the path already exists +/// - the path name is too long (longer than `PATH_MAX`, usually 4096 on linux, 1024 on OS X) +/// +/// For a full list consult +/// [posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifo.html) +/// +/// # Example +/// +/// ```rust +/// extern crate tempdir; +/// extern crate nix; +/// +/// use nix::unistd; +/// use nix::sys::stat; +/// use tempdir::TempDir; +/// +/// fn main() { +/// let tmp_dir = TempDir::new("test_fifo").unwrap(); +/// let fifo_path = tmp_dir.path().join("foo.pipe"); +/// +/// // create new fifo and give read, write and execute rights to the owner +/// match unistd::mkfifo(&fifo_path, stat::S_IRWXU) { +/// Ok(_) => println!("created {:?}", fifo_path), +/// Err(err) => println!("Error creating fifo: {}", err), +/// } +/// } +/// ``` +#[inline] +pub fn mkfifo<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> { + let res = try!(path.with_nix_path(|cstr| { + unsafe { libc::mkfifo(cstr.as_ptr(), mode.bits() as mode_t) } + })); + + Errno::result(res).map(drop) +} + /// Returns the current directory as a PathBuf /// /// Err is returned if the current user doesn't have the permission to read or search a component diff --git a/test/test_unistd.rs b/test/test_unistd.rs index adf73579..627eb09b 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -86,6 +86,24 @@ fn test_mkstemp_directory() { } #[test] +fn test_mkfifo() { + let tempdir = TempDir::new("nix-test_mkfifo").unwrap(); + let mkfifo_fifo = tempdir.path().join("mkfifo_fifo"); + + mkfifo(&mkfifo_fifo, stat::S_IRUSR).unwrap(); + + let stats = stat::stat(&mkfifo_fifo).unwrap(); + let typ = stat::SFlag::from_bits_truncate(stats.st_mode); + assert!(typ == stat::S_IFIFO); +} + +#[test] +fn test_mkfifo_directory() { + // mkfifo should fail if a directory is given + assert!(mkfifo(&env::temp_dir(), stat::S_IRUSR).is_err()); +} + +#[test] fn test_getpid() { let pid: ::libc::pid_t = getpid().into(); let ppid: ::libc::pid_t = getppid().into(); |