Skip to content

Commit c815ae9

Browse files
committed
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.
2 parents bd7d2b1 + 08c41c8 commit c815ae9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3434
([#773](https://github.com/nix-rust/nix/pull/773))
3535
- Add nix::sys::fallocate
3636
([#768](https:://github.com/nix-rust/nix/pull/768))
37+
- Added `nix::unistd::mkfifo`.
38+
([#602](https://github.com/nix-rust/nix/pull/774))
3739

3840
### Changed
3941
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/unistd.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,49 @@ pub fn mkdir<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
438438
Errno::result(res).map(drop)
439439
}
440440

441+
/// Creates new fifo special file (named pipe) with path `path` and access rights `mode`.
442+
///
443+
/// # Errors
444+
///
445+
/// There are several situations where mkfifo might fail:
446+
///
447+
/// - current user has insufficient rights in the parent directory
448+
/// - the path already exists
449+
/// - the path name is too long (longer than `PATH_MAX`, usually 4096 on linux, 1024 on OS X)
450+
///
451+
/// For a full list consult
452+
/// [posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifo.html)
453+
///
454+
/// # Example
455+
///
456+
/// ```rust
457+
/// extern crate tempdir;
458+
/// extern crate nix;
459+
///
460+
/// use nix::unistd;
461+
/// use nix::sys::stat;
462+
/// use tempdir::TempDir;
463+
///
464+
/// fn main() {
465+
/// let tmp_dir = TempDir::new("test_fifo").unwrap();
466+
/// let fifo_path = tmp_dir.path().join("foo.pipe");
467+
///
468+
/// // create new fifo and give read, write and execute rights to the owner
469+
/// match unistd::mkfifo(&fifo_path, stat::S_IRWXU) {
470+
/// Ok(_) => println!("created {:?}", fifo_path),
471+
/// Err(err) => println!("Error creating fifo: {}", err),
472+
/// }
473+
/// }
474+
/// ```
475+
#[inline]
476+
pub fn mkfifo<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
477+
let res = try!(path.with_nix_path(|cstr| {
478+
unsafe { libc::mkfifo(cstr.as_ptr(), mode.bits() as mode_t) }
479+
}));
480+
481+
Errno::result(res).map(drop)
482+
}
483+
441484
/// Returns the current directory as a PathBuf
442485
///
443486
/// Err is returned if the current user doesn't have the permission to read or search a component

test/test_unistd.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ fn test_mkstemp_directory() {
8585
assert!(mkstemp(&env::temp_dir()).is_err());
8686
}
8787

88+
#[test]
89+
fn test_mkfifo() {
90+
let tempdir = TempDir::new("nix-test_mkfifo").unwrap();
91+
let mkfifo_fifo = tempdir.path().join("mkfifo_fifo");
92+
93+
mkfifo(&mkfifo_fifo, stat::S_IRUSR).unwrap();
94+
95+
let stats = stat::stat(&mkfifo_fifo).unwrap();
96+
let typ = stat::SFlag::from_bits_truncate(stats.st_mode);
97+
assert!(typ == stat::S_IFIFO);
98+
}
99+
100+
#[test]
101+
fn test_mkfifo_directory() {
102+
// mkfifo should fail if a directory is given
103+
assert!(mkfifo(&env::temp_dir(), stat::S_IRUSR).is_err());
104+
}
105+
88106
#[test]
89107
fn test_getpid() {
90108
let pid: ::libc::pid_t = getpid().into();

0 commit comments

Comments
 (0)