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

add support for mknodat #751

Closed
wants to merge 1 commit into from
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::sys::stat::mknod
([#747](https://github.com/nix-rust/nix/pull/747))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
13 changes: 13 additions & 0 deletions src/sys/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t)
Errno::result(res).map(drop)
}

/// Create a special or ordinary file
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html)).
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
pub fn mknodat<P: ?Sized + NixPath>(dirfd: &RawFd, path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
unsafe {
libc::mknodat(*dirfd, cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
}
}));

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

#[cfg(target_os = "linux")]
pub fn major(dev: dev_t) -> u64 {
((dev >> 32) & 0xfffff000) |
Expand Down
28 changes: 27 additions & 1 deletion test/test_stat.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::fs::File;
use std::os::unix::fs::symlink;
use std::os::unix::prelude::AsRawFd;
use std::path::Path;

use libc::{S_IFMT, S_IFLNK};

use nix::fcntl;
use nix::sys::stat::{self, stat, fstat, lstat};
use nix::sys::stat::{self, stat, fstat, lstat, mknod};
use nix::sys::stat::FileStat;
use nix::Result;
use tempdir::TempDir;
Expand Down Expand Up @@ -110,3 +111,28 @@ fn test_stat_fstat_lstat() {
let fstat_result = fstat(link.as_raw_fd());
assert_stat_results(fstat_result);
}

fn assert_fifo(path: &Path) {
let stats = stat(path).unwrap();
let typ = stat::SFlag::from_bits_truncate(stats.st_mode);
assert!(typ == stat::S_IFIFO);
}

#[test]
fn test_mknod() {
let tempdir = TempDir::new("nix-test_mknodat").unwrap();
let mknod_fifo = tempdir.path().join("mknod");

mknod(&mknod_fifo, stat::S_IFIFO, stat::S_IRUSR, 0).unwrap();
assert_fifo(&mknod_fifo);
}

#[test]
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
fn test_mknod_mknodat() {
let tempdir = TempDir::new("nix-test_mknodat").unwrap();
let mknodat_fifo = tempdir.path().join("mknodat");

stat::mknodat(&0, &mknodat_fifo, stat::S_IFIFO, stat::S_IRUSR, 0).unwrap();
assert_fifo(&mknodat_fifo);
}