use super::body::DiskWriteCursor;
pub(crate) trait SinkWrite: Send + Sync + std::fmt::Debug {
fn write_all_at(&self, buf: &mut DiskWriteCursor<'_>, pos: u64) -> std::io::Result<()>;
fn prepare(&self, _expected_download_len: u64) -> std::io::Result<()> {
Ok(())
}
fn finalize(&self, _expected_download_len: u64) -> std::io::Result<()> {
Ok(())
}
}
pub(crate) struct FileSink {
file: std::fs::File,
owns_file: bool,
}
impl FileSink {
pub(crate) fn new(file: std::fs::File, owns_file: bool) -> Self {
Self { file, owns_file }
}
}
impl std::fmt::Debug for FileSink {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FileSink").finish_non_exhaustive()
}
}
fn preallocation_failure_is_fatal(error: &std::io::Error) -> bool {
#[cfg(target_os = "linux")]
{
matches!(
error.raw_os_error(),
Some(libc::ENOSPC) | Some(libc::EDQUOT)
)
}
#[cfg(not(target_os = "linux"))]
{
let _ = error;
false
}
}
impl SinkWrite for FileSink {
fn write_all_at(&self, buf: &mut DiskWriteCursor<'_>, pos: u64) -> std::io::Result<()> {
crate::io::fs::write_all_at(&self.file, buf, pos)
}
fn prepare(&self, expected_download_len: u64) -> std::io::Result<()> {
if self.owns_file {
if let Err(e) = crate::io::fs::preallocate(&self.file, expected_download_len) {
if preallocation_failure_is_fatal(&e) {
return Err(e);
}
tracing::warn!(
target: crate::telemetry::TARGET_TRANSFER_DOWNLOAD,
error = %e,
"failed to preallocate file space",
);
}
}
Ok(())
}
fn finalize(&self, expected_download_len: u64) -> std::io::Result<()> {
self.file.set_len(expected_download_len)
}
}
pub(crate) trait SinkFactory: Send + Sync + std::fmt::Debug {
fn create(&self, file: std::fs::File, owns_file: bool) -> std::io::Result<Box<dyn SinkWrite>>;
}
#[derive(Debug, Default)]
pub(crate) struct FileSinkFactory;
impl SinkFactory for FileSinkFactory {
fn create(&self, file: std::fs::File, owns_file: bool) -> std::io::Result<Box<dyn SinkWrite>> {
if crate::io::fs::is_append_only(&file)? {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"destination file is open in append mode, which downloads to a file do not support",
));
}
Ok(Box::new(FileSink::new(file, owns_file)))
}
}
#[cfg(test)]
mod tests {
#[cfg(any(unix, windows))]
#[cfg_attr(miri, ignore)] #[test]
fn file_sink_factory_rejects_append_mode() {
use super::{FileSinkFactory, SinkFactory};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("out.bin");
std::fs::write(&path, b"header").unwrap();
let file = std::fs::File::options().append(true).open(&path).unwrap();
let error = FileSinkFactory
.create(file, false)
.expect_err("an append-mode destination must be rejected");
assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
assert_eq!(std::fs::read(&path).unwrap(), b"header");
}
#[cfg(target_os = "linux")]
#[test]
fn linux_preallocation_fails_only_for_storage_exhaustion() {
assert!(super::preallocation_failure_is_fatal(
&std::io::Error::from_raw_os_error(libc::ENOSPC)
));
assert!(super::preallocation_failure_is_fatal(
&std::io::Error::from_raw_os_error(libc::EDQUOT)
));
assert!(!super::preallocation_failure_is_fatal(
&std::io::Error::from_raw_os_error(libc::EOPNOTSUPP)
));
}
}