use crate::fs::Timestamps;
use crate::io::SeekFrom;
#[cfg(not(target_os = "wasi"))]
use crate::process::{Gid, Uid};
use crate::{imp, io};
use imp::fd::{AsFd, BorrowedFd};
#[cfg(not(any(
target_os = "dragonfly",
target_os = "illumos",
target_os = "netbsd",
target_os = "openbsd",
target_os = "redox"
)))]
use imp::fs::FallocateFlags;
use imp::fs::Stat;
#[cfg(not(any(
target_os = "illumos",
target_os = "netbsd",
target_os = "redox",
target_os = "wasi"
)))]
use imp::fs::StatFs;
#[cfg(not(target_os = "wasi"))]
use imp::fs::{FlockOperation, Mode};
#[inline]
pub fn seek<Fd: AsFd>(fd: Fd, pos: SeekFrom) -> io::Result<u64> {
imp::fs::syscalls::seek(fd.as_fd(), pos)
}
#[inline]
pub fn tell<Fd: AsFd>(fd: Fd) -> io::Result<u64> {
imp::fs::syscalls::tell(fd.as_fd())
}
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn fchmod<Fd: AsFd>(fd: Fd, mode: Mode) -> io::Result<()> {
imp::fs::syscalls::fchmod(fd.as_fd(), mode)
}
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn fchown<Fd: AsFd>(fd: Fd, owner: Uid, group: Gid) -> io::Result<()> {
imp::fs::syscalls::fchown(fd.as_fd(), owner, group)
}
#[inline]
pub fn fstat<Fd: AsFd>(fd: Fd) -> io::Result<Stat> {
imp::fs::syscalls::fstat(fd.as_fd())
}
#[cfg(not(any(
target_os = "illumos",
target_os = "netbsd",
target_os = "redox",
target_os = "wasi"
)))] #[inline]
pub fn fstatfs<Fd: AsFd>(fd: Fd) -> io::Result<StatFs> {
imp::fs::syscalls::fstatfs(fd.as_fd())
}
#[inline]
pub fn futimens<Fd: AsFd>(fd: Fd, times: &Timestamps) -> io::Result<()> {
imp::fs::syscalls::futimens(fd.as_fd(), times)
}
#[cfg(not(any(
target_os = "dragonfly",
target_os = "illumos",
target_os = "netbsd",
target_os = "openbsd",
target_os = "redox"
)))] #[inline]
#[doc(alias = "posix_fallocate")]
pub fn fallocate<Fd: AsFd>(fd: Fd, mode: FallocateFlags, offset: u64, len: u64) -> io::Result<()> {
imp::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 = imp::fs::syscalls::fcntl_getfl(fd)?;
#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "emscripten"
))]
if mode.contains(crate::fs::OFlags::PATH) {
return Ok((false, false));
}
match mode & crate::fs::OFlags::RWMODE {
crate::fs::OFlags::RDONLY => Ok((true, false)),
crate::fs::OFlags::RDWR => Ok((true, true)),
crate::fs::OFlags::WRONLY => Ok((false, true)),
_ => unreachable!(),
}
}
#[inline]
pub fn fsync<Fd: AsFd>(fd: Fd) -> io::Result<()> {
imp::fs::syscalls::fsync(fd.as_fd())
}
#[cfg(not(any(
target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "redox"
)))]
#[inline]
pub fn fdatasync<Fd: AsFd>(fd: Fd) -> io::Result<()> {
imp::fs::syscalls::fdatasync(fd.as_fd())
}
#[inline]
pub fn ftruncate<Fd: AsFd>(fd: Fd, length: u64) -> io::Result<()> {
imp::fs::syscalls::ftruncate(fd.as_fd(), length)
}
#[cfg(not(target_os = "wasi"))]
#[inline]
pub fn flock<Fd: AsFd>(fd: Fd, operation: FlockOperation) -> io::Result<()> {
imp::fs::syscalls::flock(fd.as_fd(), operation)
}