Skip to content
Open
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
1 change: 1 addition & 0 deletions src/backend/libc/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Dir {
/// - [POSIX]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/dirfd.html
#[cfg(not(target_os = "vita"))]
#[inline]
#[doc(alias = "dirfd")]
pub fn fd<'a>(&'a self) -> io::Result<BorrowedFd<'a>> {
Expand Down
16 changes: 9 additions & 7 deletions src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ use crate::fs::SealFlags;
use crate::fs::StatFs;
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
use crate::fs::Timestamps;
#[cfg(not(any(
target_os = "espidf",
target_os = "horizon",
target_os = "vita",
target_os = "wasi"
)))]
#[cfg(not(target_os = "wasi"))]
use crate::fs::{Dev, FileType};
use crate::fs::{Mode, OFlags, SeekFrom, Stat};
#[cfg(not(target_os = "wasi"))]
Expand Down Expand Up @@ -1187,6 +1182,13 @@ pub(crate) fn chownat(
))
}
}
#[cfg(not(target_os = "wasi"))]
pub(crate) fn mknod(path: &CStr, file_type: FileType, mode: Mode, dev: Dev) -> io::Result<()> {
let pathname = c_str(path);
let mode = (mode.bits() | file_type.as_raw_mode()) as c::mode_t;
let dev = dev.try_into().map_err(|_| io::Errno::PERM)?;
unsafe { ret(c::mknod(pathname, mode, dev)) }
}

#[cfg(not(any(
target_os = "espidf",
Expand All @@ -1204,7 +1206,7 @@ pub(crate) fn mknodat(
let dirfd = borrowed_fd(dirfd);
let pathname = c_str(path);
let mode = (mode.bits() | file_type.as_raw_mode()) as c::mode_t;
let dev = dev.try_into().map_err(|_e| io::Errno::PERM)?;
let dev = dev.try_into().map_err(|_| io::Errno::PERM)?;

// Apple platforms before macOS 13.0, iOS 16.0, tvOS 16.0, and watchOS 9.0
// lack `mknodat`.
Expand Down
5 changes: 5 additions & 0 deletions src/backend/linux_raw/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ pub(crate) fn mknodat(
}
}

#[inline]
pub(crate) fn mknod(path: &CStr, file_type: FileType, mode: Mode, dev: u64) -> io::Result<()> {
mknodat(crate::fs::CWD, path, file_type, mode, dev)
}

#[inline]
pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
let (whence, offset) = match pos {
Expand Down
30 changes: 30 additions & 0 deletions src/fs/abs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::fs::Access;
use crate::fs::StatFs;
#[cfg(not(any(target_os = "wasi")))]
use crate::fs::StatVfs;
#[cfg(not(target_os = "wasi"))]
use crate::fs::{Dev, FileType};
use crate::fs::{Mode, OFlags, Stat};
#[cfg(not(target_os = "wasi"))]
use crate::ugid::{Gid, Uid};
Expand Down Expand Up @@ -231,6 +233,34 @@ pub fn mkdir<P: path::Arg>(path: P, mode: Mode) -> io::Result<()> {
path.into_with_c_str(|path| backend::fs::syscalls::mkdir(path, mode))
}

/// `mkfifo(path, mode)`—Creates a FIFO special file.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/mkfifo.html
/// [Linux]: https://man7.org/linux/man-pages/man3/mkfifo.3.html
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn mkfifo<P: path::Arg>(path: P, mode: Mode) -> io::Result<()> {
mknod(path, FileType::Fifo, mode, 0)
}

/// `mknod(path, mode, dev)`—Creates a special or normal file.
///
/// # References
/// - [POSIX]
/// - [Linux]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/mknod.html
/// [Linux]: https://man7.org/linux/man-pages/man2/mknod.2.html
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn mknod<P: path::Arg>(path: P, file_type: FileType, mode: Mode, dev: Dev) -> io::Result<()> {
path.into_with_c_str(|path| backend::fs::syscalls::mknod(path, file_type, mode, dev))
}

/// `access(path, access)`—Tests permissions for a file or directory.
///
/// # References
Expand Down
1 change: 1 addition & 0 deletions tests/fs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod long_paths;
#[cfg(not(any(target_os = "haiku", target_os = "wasi")))]
mod makedev;
mod mkdirat;
#[cfg(not(target_os = "wasi"))]
mod mknodat;
mod negative_timestamp;
#[cfg(linux_kernel)]
Expand Down
13 changes: 13 additions & 0 deletions tests/fs/mknodat.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#[test]
fn test_mknod() {
use rustix::fs::{mknod, stat, unlink, FileType, Mode};

let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("file");

mknod(&path, FileType::Fifo, Mode::empty(), 0).unwrap();
let stat = stat(&path).unwrap();
assert_eq!(FileType::from_raw_mode(stat.st_mode), FileType::Fifo);
unlink(&path).unwrap();
}

#[cfg(not(any(apple, target_os = "wasi")))]
#[test]
fn test_mknodat() {
Expand Down
Loading