#[cfg(not(target_os = "wasi"))]
use crate::fs::Mode;
use crate::fs::{OFlags, Timespec};
use crate::io::SeekFrom;
#[cfg(not(target_os = "wasi"))]
use crate::process::{Gid, Uid};
use crate::{backend, io};
use backend::fd::{AsFd, BorrowedFd};
#[cfg(not(target_os = "wasi"))]
pub use backend::fs::types::FlockOperation;
#[cfg(not(any(
netbsdlike,
solarish,
target_os = "aix",
target_os = "dragonfly",
target_os = "redox",
)))]
pub use backend::fs::types::FallocateFlags;
pub use backend::fs::types::Stat;
#[cfg(not(any(
solarish,
target_os = "haiku",
target_os = "netbsd",
target_os = "redox",
target_os = "wasi",
)))]
pub use backend::fs::types::StatFs;
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
pub use backend::fs::types::{StatVfs, StatVfsMountFlags};
#[cfg(linux_kernel)]
pub use backend::fs::types::FsWord;
#[repr(C)]
#[derive(Clone, Debug)]
pub struct Timestamps {
pub last_access: Timespec,
pub last_modification: Timespec,
}
#[cfg(linux_kernel)]
pub const PROC_SUPER_MAGIC: FsWord = backend::c::PROC_SUPER_MAGIC as FsWord;
#[cfg(linux_kernel)]
pub const NFS_SUPER_MAGIC: FsWord = backend::c::NFS_SUPER_MAGIC as FsWord;
#[inline]
#[doc(alias = "lseek")]
pub fn seek<Fd: AsFd>(fd: Fd, pos: SeekFrom) -> io::Result<u64> {
backend::fs::syscalls::seek(fd.as_fd(), pos)
}
#[inline]
#[doc(alias = "lseek")]
pub fn tell<Fd: AsFd>(fd: Fd) -> io::Result<u64> {
backend::fs::syscalls::tell(fd.as_fd())
}
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn fchmod<Fd: AsFd>(fd: Fd, mode: Mode) -> io::Result<()> {
backend::fs::syscalls::fchmod(fd.as_fd(), mode)
}
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn fchown<Fd: AsFd>(fd: Fd, owner: Option<Uid>, group: Option<Gid>) -> io::Result<()> {
backend::fs::syscalls::fchown(fd.as_fd(), owner, group)
}
#[inline]
pub fn fstat<Fd: AsFd>(fd: Fd) -> io::Result<Stat> {
backend::fs::syscalls::fstat(fd.as_fd())
}
#[cfg(not(any(
solarish,
target_os = "haiku",
target_os = "netbsd",
target_os = "redox",
target_os = "wasi",
)))]
#[inline]
pub fn fstatfs<Fd: AsFd>(fd: Fd) -> io::Result<StatFs> {
backend::fs::syscalls::fstatfs(fd.as_fd())
}
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
#[inline]
pub fn fstatvfs<Fd: AsFd>(fd: Fd) -> io::Result<StatVfs> {
backend::fs::syscalls::fstatvfs(fd.as_fd())
}
#[inline]
pub fn futimens<Fd: AsFd>(fd: Fd, times: &Timestamps) -> io::Result<()> {
backend::fs::syscalls::futimens(fd.as_fd(), times)
}
#[cfg(not(any(
netbsdlike,
solarish,
target_os = "aix",
target_os = "dragonfly",
target_os = "redox",
)))] #[inline]
#[doc(alias = "posix_fallocate")]
pub fn fallocate<Fd: AsFd>(fd: Fd, mode: FallocateFlags, offset: u64, len: u64) -> io::Result<()> {
backend::fs::syscalls::fallocate(fd.as_fd(), mode, offset, len)
}
#[inline]
pub fn is_file_read_write<Fd: AsFd>(fd: Fd) -> io::Result<(bool, bool)> {
_is_file_read_write(fd.as_fd())
}
pub(crate) fn _is_file_read_write(fd: BorrowedFd<'_>) -> io::Result<(bool, bool)> {
let mode = backend::fs::syscalls::fcntl_getfl(fd)?;
#[cfg(any(linux_kernel, target_os = "fuchsia", target_os = "emscripten"))]
if mode.contains(OFlags::PATH) {
return Ok((false, false));
}
match mode & OFlags::RWMODE {
OFlags::RDONLY => Ok((true, false)),
OFlags::RDWR => Ok((true, true)),
OFlags::WRONLY => Ok((false, true)),
_ => unreachable!(),
}
}
#[inline]
pub fn fsync<Fd: AsFd>(fd: Fd) -> io::Result<()> {
backend::fs::syscalls::fsync(fd.as_fd())
}
#[cfg(not(any(
apple,
target_os = "dragonfly",
target_os = "haiku",
target_os = "redox",
)))]
#[inline]
pub fn fdatasync<Fd: AsFd>(fd: Fd) -> io::Result<()> {
backend::fs::syscalls::fdatasync(fd.as_fd())
}
#[inline]
pub fn ftruncate<Fd: AsFd>(fd: Fd, length: u64) -> io::Result<()> {
backend::fs::syscalls::ftruncate(fd.as_fd(), length)
}
#[cfg(not(any(target_os = "solaris", target_os = "wasi")))]
#[inline]
pub fn flock<Fd: AsFd>(fd: Fd, operation: FlockOperation) -> io::Result<()> {
backend::fs::syscalls::flock(fd.as_fd(), operation)
}
#[cfg(linux_kernel)]
#[inline]
pub fn syncfs<Fd: AsFd>(fd: Fd) -> io::Result<()> {
backend::fs::syscalls::syncfs(fd.as_fd())
}