use crate::error::Result;
use crate::filesystem::positioned_disk_writer::PositionedDiskWriter;
#[cfg(unix)]
use crate::error::Aria2Error;
#[cfg(all(unix, not(target_os = "linux")))]
use crate::filesystem::disk_writer::SeekableDiskWriter;
#[cfg(unix)]
use super::strategies;
#[cfg_attr(target_os = "linux", allow(unused_variables))]
pub(crate) async fn fallocate(
writer: &mut PositionedDiskWriter,
length: u64,
secure: bool,
) -> Result<()> {
#[cfg(target_os = "linux")]
{
if let Some(fd) = writer.raw_fd() {
if length > i64::MAX as u64 {
return Err(Aria2Error::Io(
"fallocate length exceeds off_t range".into(),
));
}
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
let duplicated_fd = unsafe { libc::dup(fd) };
if duplicated_fd < 0 {
return Err(Aria2Error::Io(std::io::Error::last_os_error().to_string()));
}
let owned_fd = unsafe { OwnedFd::from_raw_fd(duplicated_fd) };
let syscall_result = tokio::task::spawn_blocking(move || {
let ret = unsafe {
libc::fallocate(
owned_fd.as_raw_fd(),
0 as libc::c_int,
0,
length as libc::off_t,
)
};
if ret == 0 {
Ok(())
} else {
Err(unsafe { *libc::__errno_location() })
}
})
.await
.map_err(|error| Aria2Error::Io(format!("fallocate task failed: {error}")))?;
if syscall_result.is_ok() {
return Ok(());
}
let errno = syscall_result.expect_err("failed fallocate must carry errno");
if errno == libc::EOPNOTSUPP || errno == libc::ENOSYS {
tracing::warn!(
length,
"fallocate(2) not supported by filesystem; \
falling back to async zero-fill"
);
return strategies::zero_fill_allocation(writer, length).await;
}
Err(Aria2Error::Io(
std::io::Error::from_raw_os_error(errno).to_string(),
))
} else {
strategies::zero_fill_allocation(writer, length).await
}
}
#[cfg(all(unix, not(target_os = "linux")))]
{
#[cfg(target_os = "macos")]
{
match writer.raw_fd() {
Some(fd) => {
if length > i64::MAX as u64 {
return Err(Aria2Error::Io(
"F_PREALLOCATE length exceeds off_t range".into(),
));
}
let existing_length = writer.len().await?.min(length);
writer.truncate(length).await?;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
let duplicated_fd = unsafe { libc::dup(fd) };
if duplicated_fd < 0 {
return Err(Aria2Error::Io(std::io::Error::last_os_error().to_string()));
}
let owned_fd = unsafe { OwnedFd::from_raw_fd(duplicated_fd) };
let fcntl_result = tokio::task::spawn_blocking(move || {
let fstore = libc::fstore_t {
fst_flags: libc::F_ALLOCATEALL as libc::c_uint,
fst_posmode: libc::F_PEOFPOSMODE,
fst_offset: 0,
fst_length: length as libc::off_t,
fst_bytesalloc: 0,
};
let ret = unsafe {
libc::fcntl(owned_fd.as_raw_fd(), libc::F_PREALLOCATE, &fstore)
};
if ret == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
})
.await
.map_err(|error| {
Aria2Error::Io(format!("F_PREALLOCATE task failed: {error}"))
})?;
if let Err(error) = fcntl_result {
tracing::warn!(
length,
error = %error,
"F_PREALLOCATE failed on macOS; falling back to zero-fill"
);
return strategies::async_zero_fill_from(writer, existing_length, length)
.await;
}
if secure {
strategies::async_zero_fill_from(writer, existing_length, length).await
} else {
super::SECURE_FALLOC_WARN_ONCE.call_once(|| {
tracing::warn!(
"F_PREALLOCATE succeeded but does not zero-fill; \
residual disk data may be exposed. \
Enable --secure-falloc=true to zero-fill (at a \
performance cost). This warning is logged once."
);
});
Ok(())
}
}
None => strategies::zero_fill_allocation(writer, length).await,
}
}
#[cfg(not(target_os = "macos"))]
{
strategies::zero_fill_allocation(writer, length).await
}
}
#[cfg(not(unix))]
{
super::windows::fallocate_windows(writer, length, secure).await
}
}