use std::fs::File;
use std::io;
use std::path::Path;
#[cfg(unix)]
pub(crate) fn allocated_bytes(metadata: &std::fs::Metadata) -> Option<u64> {
use std::os::unix::fs::MetadataExt;
metadata.blocks().checked_mul(512)
}
#[cfg(not(unix))]
pub(crate) fn allocated_bytes(_metadata: &std::fs::Metadata) -> Option<u64> {
None
}
#[cfg(unix)]
pub(crate) fn filesystem_available_bytes(path: &Path) -> io::Result<Option<u64>> {
use std::os::unix::ffi::OsStrExt;
let path = std::ffi::CString::new(path.as_os_str().as_bytes())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "path contains NUL"))?;
let mut stats = std::mem::MaybeUninit::<libc::statvfs>::uninit();
if unsafe { libc::statvfs(path.as_ptr(), stats.as_mut_ptr()) } != 0 {
return Err(io::Error::last_os_error());
}
let stats = unsafe { stats.assume_init() };
Ok(stats.f_bavail.checked_mul(stats.f_frsize))
}
#[cfg(not(unix))]
pub(crate) fn filesystem_available_bytes(_path: &Path) -> io::Result<Option<u64>> {
Ok(None)
}
#[cfg(unix)]
#[inline]
pub(crate) fn read_exact_at(f: &File, buf: &mut [u8], off: u64) -> io::Result<()> {
std::os::unix::fs::FileExt::read_exact_at(f, buf, off)
}
#[cfg(unix)]
#[inline]
pub(crate) fn write_all_at(f: &File, buf: &[u8], off: u64) -> io::Result<()> {
std::os::unix::fs::FileExt::write_all_at(f, buf, off)
}
#[cfg(target_os = "wasi")]
fn wasi_err(e: wasi::Errno) -> io::Error {
io::Error::from_raw_os_error(e.raw().into())
}
#[cfg(target_os = "wasi")]
pub(crate) fn read_exact_at(f: &File, mut buf: &mut [u8], mut off: u64) -> io::Result<()> {
use std::os::fd::AsRawFd;
let fd = f.as_raw_fd() as u32;
while !buf.is_empty() {
let iov = [wasi::Iovec { buf: buf.as_mut_ptr(), buf_len: buf.len() }];
let n = unsafe { wasi::fd_pread(fd, &iov, off) }.map_err(wasi_err)?;
if n == 0 {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"positioned read hit end of file before filling the buffer",
));
}
buf = &mut buf[n..];
off += n as u64;
}
Ok(())
}
#[cfg(target_os = "wasi")]
pub(crate) fn write_all_at(f: &File, mut buf: &[u8], mut off: u64) -> io::Result<()> {
use std::os::fd::AsRawFd;
let fd = f.as_raw_fd() as u32;
while !buf.is_empty() {
let iov = [wasi::Ciovec { buf: buf.as_ptr(), buf_len: buf.len() }];
let n = unsafe { wasi::fd_pwrite(fd, &iov, off) }.map_err(wasi_err)?;
if n == 0 {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"positioned write made no progress",
));
}
buf = &buf[n..];
off += n as u64;
}
Ok(())
}
#[inline]
pub(crate) fn lock_exclusive(f: &File) -> io::Result<bool> {
#[cfg(unix)]
{
use std::os::unix::io::AsRawFd;
let rc = unsafe { libc::flock(f.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
if rc == 0 {
return Ok(true);
}
let e = io::Error::last_os_error();
match e.raw_os_error() {
Some(c) if c == libc::EWOULDBLOCK || c == libc::EAGAIN => Ok(false),
_ => Err(e),
}
}
#[cfg(not(unix))]
{
let _ = f;
Ok(true)
}
}
#[inline]
pub(crate) fn punch_hole(f: &File, off: u64, len: u64) -> io::Result<()> {
#[cfg(target_os = "linux")]
{
use std::os::unix::io::AsRawFd;
let rc = unsafe {
libc::fallocate(
f.as_raw_fd(),
libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE,
off as libc::off_t,
len as libc::off_t,
)
};
if rc != 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
#[cfg(not(target_os = "linux"))]
{
let _ = (f, off, len);
Err(io::Error::new(
io::ErrorKind::Unsupported,
"hole punching needs fallocate(FALLOC_FL_PUNCH_HOLE), which this platform lacks",
))
}
}
#[cfg(target_os = "linux")]
pub(crate) fn rename_noreplace(from: &Path, to: &Path) -> io::Result<()> {
use std::os::unix::ffi::OsStrExt;
let from = std::ffi::CString::new(from.as_os_str().as_bytes())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "source path contains NUL"))?;
let to = std::ffi::CString::new(to.as_os_str().as_bytes()).map_err(|_| {
io::Error::new(io::ErrorKind::InvalidInput, "destination path contains NUL")
})?;
let rc = unsafe {
libc::syscall(
libc::SYS_renameat2,
libc::AT_FDCWD,
from.as_ptr(),
libc::AT_FDCWD,
to.as_ptr(),
libc::RENAME_NOREPLACE,
)
};
if rc == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) fn rename_noreplace(from: &Path, to: &Path) -> io::Result<()> {
use std::os::unix::ffi::OsStrExt;
let from = std::ffi::CString::new(from.as_os_str().as_bytes())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "source path contains NUL"))?;
let to = std::ffi::CString::new(to.as_os_str().as_bytes()).map_err(|_| {
io::Error::new(io::ErrorKind::InvalidInput, "destination path contains NUL")
})?;
let rc = unsafe { libc::renamex_np(from.as_ptr(), to.as_ptr(), libc::RENAME_EXCL) };
if rc == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "ios")))]
pub(crate) fn rename_noreplace(_from: &Path, _to: &Path) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"atomic no-replace rename is unavailable on this platform",
))
}