use async_trait::async_trait;
use crate::error::Result;
use crate::filesystem::disk_writer::SeekableDiskWriter;
use crate::filesystem::positioned_disk_writer::PositionedDiskWriter;
const BUF_SIZE: usize = 256 * 1024;
#[allow(clippy::double_must_use)]
#[async_trait]
pub trait FileAllocationIterator: Send {
async fn allocate_chunk(&mut self) -> Result<()>;
fn finished(&self) -> bool;
fn current_length(&self) -> u64;
fn total_length(&self) -> u64;
}
pub struct SingleFileAllocationIterator {
writer: PositionedDiskWriter,
offset: u64,
total_length: u64,
buffer: Option<Vec<u8>>,
}
impl SingleFileAllocationIterator {
pub fn new(writer: PositionedDiskWriter, offset: u64, total_length: u64) -> Self {
Self {
writer,
offset,
total_length,
buffer: None,
}
}
pub fn init(&mut self) {
if self.buffer.is_none() {
self.buffer = Some(vec![0u8; BUF_SIZE]);
}
}
pub fn writer_mut(&mut self) -> &mut PositionedDiskWriter {
&mut self.writer
}
pub fn into_inner(self) -> PositionedDiskWriter {
self.writer
}
}
#[async_trait]
impl FileAllocationIterator for SingleFileAllocationIterator {
async fn allocate_chunk(&mut self) -> Result<()> {
let buf = self.buffer.as_deref().unwrap_or_else(|| {
static EMPTY: &[u8] = &[0u8; BUF_SIZE];
EMPTY
});
self.writer.write_at(self.offset, buf).await?;
self.offset += BUF_SIZE as u64;
if self.total_length < self.offset {
self.writer.truncate(self.total_length).await?;
self.offset = self.total_length;
}
Ok(())
}
fn finished(&self) -> bool {
self.offset >= self.total_length
}
fn current_length(&self) -> u64 {
self.offset.min(self.total_length)
}
fn total_length(&self) -> u64 {
self.total_length
}
}
pub struct FallocFileAllocationIterator {
writer: PositionedDiskWriter,
offset: u64,
total_length: u64,
secure_falloc: bool,
done: bool,
}
impl FallocFileAllocationIterator {
pub fn new(writer: PositionedDiskWriter, offset: u64, total_length: u64) -> Self {
Self::new_with_secure_falloc(writer, offset, total_length, false)
}
pub fn new_with_secure_falloc(
writer: PositionedDiskWriter,
offset: u64,
total_length: u64,
secure_falloc: bool,
) -> Self {
#[cfg(windows)]
let _ = secure_falloc;
Self {
writer,
offset,
total_length,
secure_falloc,
done: false,
}
}
pub fn writer_mut(&mut self) -> &mut PositionedDiskWriter {
&mut self.writer
}
pub fn into_inner(self) -> PositionedDiskWriter {
self.writer
}
}
#[async_trait]
impl FileAllocationIterator for FallocFileAllocationIterator {
async fn allocate_chunk(&mut self) -> Result<()> {
if self.offset < self.total_length {
let length = self.total_length;
crate::filesystem::file_allocation::allocate_file(
&mut self.writer,
length,
crate::filesystem::file_allocation::AllocationStrategy::Falloc,
self.secure_falloc,
)
.await?;
self.offset = self.total_length;
} else {
self.writer.truncate(self.total_length).await?;
self.offset = self.total_length;
}
self.done = true;
Ok(())
}
fn finished(&self) -> bool {
self.done
}
fn current_length(&self) -> u64 {
self.offset
}
fn total_length(&self) -> u64 {
self.total_length
}
}
pub struct TruncFileAllocationIterator {
writer: PositionedDiskWriter,
offset: u64,
total_length: u64,
done: bool,
}
impl TruncFileAllocationIterator {
pub fn new(writer: PositionedDiskWriter, offset: u64, total_length: u64) -> Self {
Self {
writer,
offset,
total_length,
done: false,
}
}
pub fn writer_mut(&mut self) -> &mut PositionedDiskWriter {
&mut self.writer
}
}
#[async_trait]
impl FileAllocationIterator for TruncFileAllocationIterator {
async fn allocate_chunk(&mut self) -> Result<()> {
self.writer.truncate(self.total_length).await?;
self.writer.sync_all().await?;
self.offset = self.total_length;
self.done = true;
Ok(())
}
fn finished(&self) -> bool {
self.done
}
fn current_length(&self) -> u64 {
self.offset
}
fn total_length(&self) -> u64 {
self.total_length
}
}
pub struct AdaptiveFileAllocationIterator {
writer: Option<PositionedDiskWriter>,
offset: u64,
total_length: u64,
#[cfg(not(windows))]
secure_falloc: bool,
inner: Option<AdaptiveInner>,
}
enum AdaptiveInner {
#[cfg(not(windows))]
Falloc(FallocFileAllocationIterator),
Single(SingleFileAllocationIterator),
}
impl AdaptiveFileAllocationIterator {
pub fn new(writer: PositionedDiskWriter, offset: u64, total_length: u64) -> Self {
Self::new_with_secure_falloc(writer, offset, total_length, false)
}
pub fn new_with_secure_falloc(
writer: PositionedDiskWriter,
offset: u64,
total_length: u64,
secure_falloc: bool,
) -> Self {
#[cfg(windows)]
let _ = secure_falloc;
Self {
writer: Some(writer),
offset,
total_length,
#[cfg(not(windows))]
secure_falloc,
inner: None,
}
}
async fn probe_and_select(&mut self) -> Result<()> {
#[cfg(windows)]
{
let mut single = SingleFileAllocationIterator::new(
self.writer.take().expect("adaptive writer is present"),
self.offset,
self.total_length,
);
single.init();
self.inner = Some(AdaptiveInner::Single(single));
Ok(())
}
#[cfg(not(windows))]
{
let probe_len = 4 * 1024u64;
let remaining = self.total_length.saturating_sub(self.offset);
let probe_len = probe_len.min(remaining);
if probe_len == 0 {
return Ok(());
}
let probe_result = crate::filesystem::file_allocation::allocate_file(
self.writer.as_mut().expect("adaptive writer is present"),
self.offset + probe_len,
crate::filesystem::file_allocation::AllocationStrategy::Falloc,
self.secure_falloc,
)
.await;
match probe_result {
Ok(()) => {
self.offset += probe_len;
if self.offset >= self.total_length {
return Ok(());
}
self.inner = Some(AdaptiveInner::Falloc(
FallocFileAllocationIterator::new_with_secure_falloc(
self.writer.take().expect("adaptive writer is present"),
self.offset,
self.total_length,
self.secure_falloc,
),
));
}
Err(_) => {
tracing::debug!("Adaptive: fallocate probe failed, falling back to zero-fill");
let mut single = SingleFileAllocationIterator::new(
self.writer.take().expect("adaptive writer is present"),
self.offset,
self.total_length,
);
single.init();
self.inner = Some(AdaptiveInner::Single(single));
}
}
Ok(())
}
}
}
#[async_trait]
impl FileAllocationIterator for AdaptiveFileAllocationIterator {
async fn allocate_chunk(&mut self) -> Result<()> {
if self.inner.is_none() {
self.probe_and_select().await?;
}
match &mut self.inner {
#[cfg(not(windows))]
Some(AdaptiveInner::Falloc(falloc)) => falloc.allocate_chunk().await,
Some(AdaptiveInner::Single(single)) => single.allocate_chunk().await,
None => {
Ok(())
}
}
}
fn finished(&self) -> bool {
match &self.inner {
#[cfg(not(windows))]
Some(AdaptiveInner::Falloc(falloc)) => falloc.finished(),
Some(AdaptiveInner::Single(single)) => single.finished(),
None => self.offset >= self.total_length,
}
}
fn current_length(&self) -> u64 {
match &self.inner {
#[cfg(not(windows))]
Some(AdaptiveInner::Falloc(falloc)) => falloc.current_length(),
Some(AdaptiveInner::Single(single)) => single.current_length(),
None => self.offset,
}
}
fn total_length(&self) -> u64 {
self.total_length
}
}
impl AdaptiveFileAllocationIterator {
pub fn into_inner(self) -> PositionedDiskWriter {
match self.inner {
#[cfg(not(windows))]
Some(AdaptiveInner::Falloc(iterator)) => iterator.into_inner(),
Some(AdaptiveInner::Single(iterator)) => iterator.into_inner(),
None => self.writer.expect("adaptive writer is present"),
}
}
pub async fn close(&mut self) -> Result<()> {
match &mut self.inner {
#[cfg(not(windows))]
Some(AdaptiveInner::Falloc(iterator)) => {
iterator
.writer_mut()
.close_without_sync("adaptive allocation close")
.await
}
Some(AdaptiveInner::Single(iterator)) => {
iterator
.writer_mut()
.close_without_sync("adaptive allocation close")
.await
}
None => {
self.writer
.as_mut()
.expect("adaptive writer is present")
.close_without_sync("adaptive allocation close")
.await
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::filesystem::positioned_disk_writer::PositionedDiskWriter;
#[tokio::test]
async fn test_trunc_iterator_completes_in_one_chunk() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_trunc_iter.bin");
let mut adaptor = PositionedDiskWriter::new(&path, None);
adaptor.open().await.unwrap();
let mut iter = TruncFileAllocationIterator::new(adaptor, 0, 4096);
assert!(!iter.finished());
iter.allocate_chunk().await.unwrap();
assert!(iter.finished());
assert_eq!(iter.current_length(), 4096);
assert_eq!(iter.total_length(), 4096);
let meta = tokio::fs::metadata(&path).await.unwrap();
assert_eq!(meta.len(), 4096);
}
#[tokio::test]
async fn test_single_file_iterator_chunked_allocation() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_single_iter.bin");
let mut adaptor = PositionedDiskWriter::new(&path, None);
adaptor.open().await.unwrap();
adaptor.truncate(0).await.unwrap();
let mut iter = SingleFileAllocationIterator::new(adaptor, 0, 512 * 1024);
iter.init();
assert!(!iter.finished());
iter.allocate_chunk().await.unwrap();
assert!(!iter.finished());
assert_eq!(iter.current_length(), 256 * 1024);
iter.allocate_chunk().await.unwrap();
assert!(iter.finished());
assert_eq!(iter.current_length(), 512 * 1024);
}
#[tokio::test]
async fn test_single_file_iterator_truncates_overshoot() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_overshoot.bin");
let mut adaptor = PositionedDiskWriter::new(&path, None);
adaptor.open().await.unwrap();
adaptor.truncate(0).await.unwrap();
let target = BUF_SIZE as u64 + 1;
let mut iter = SingleFileAllocationIterator::new(adaptor, 0, target);
iter.init();
iter.allocate_chunk().await.unwrap();
assert!(!iter.finished());
iter.allocate_chunk().await.unwrap();
assert!(iter.finished());
assert_eq!(iter.current_length(), target);
}
#[tokio::test]
async fn test_adaptive_iterator_fallback_to_zero_fill() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_adaptive.bin");
let mut adaptor = PositionedDiskWriter::new(&path, None);
adaptor.open().await.unwrap();
adaptor.truncate(0).await.unwrap();
let mut iter = AdaptiveFileAllocationIterator::new(adaptor, 0, 1024 * 1024);
while !iter.finished() {
iter.allocate_chunk().await.unwrap();
}
assert_eq!(iter.current_length(), 1024 * 1024);
let content = tokio::fs::read(&path).await.unwrap();
assert!(content.iter().all(|byte| *byte == 0));
}
#[tokio::test]
async fn test_trunc_iterator_with_existing_offset() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_trunc_existing.bin");
tokio::fs::write(&path, b"hello").await.unwrap();
let mut adaptor = PositionedDiskWriter::new(&path, None);
adaptor.open().await.unwrap();
let mut iter = TruncFileAllocationIterator::new(adaptor, 5, 1024);
iter.allocate_chunk().await.unwrap();
assert!(iter.finished());
assert_eq!(iter.current_length(), 1024);
}
}