use async_trait::async_trait;
use crate::error::Result;
use crate::filesystem::disk_adaptor::DiskAdaptor;
const BUF_SIZE: usize = 256 * 1024;
#[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<D: DiskAdaptor> {
adaptor: D,
offset: u64,
total_length: u64,
buffer: Option<Vec<u8>>,
}
impl<D: DiskAdaptor + Default> SingleFileAllocationIterator<D> {
pub fn new(adaptor: D, offset: u64, total_length: u64) -> Self {
Self {
adaptor,
offset,
total_length,
buffer: None,
}
}
pub fn init(&mut self) {
if self.buffer.is_none() {
self.buffer = Some(vec![0u8; BUF_SIZE]);
}
}
pub fn adaptor_mut(&mut self) -> &mut D {
&mut self.adaptor
}
pub fn into_inner(self) -> D {
self.adaptor
}
}
#[async_trait]
impl<D: DiskAdaptor + Default> FileAllocationIterator for SingleFileAllocationIterator<D> {
async fn allocate_chunk(&mut self) -> Result<()> {
let buf = self.buffer.as_deref().unwrap_or_else(|| {
static EMPTY: &[u8] = &[0u8; BUF_SIZE];
EMPTY
});
self.adaptor.write(self.offset, buf).await?;
self.offset += BUF_SIZE as u64;
if self.total_length < self.offset {
self.adaptor.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<D: DiskAdaptor> {
adaptor: D,
offset: u64,
total_length: u64,
secure_falloc: bool,
done: bool,
}
impl<D: DiskAdaptor> FallocFileAllocationIterator<D> {
pub fn new(adaptor: D, offset: u64, total_length: u64) -> Self {
Self::new_with_secure_falloc(adaptor, offset, total_length, false)
}
pub fn new_with_secure_falloc(
adaptor: D,
offset: u64,
total_length: u64,
secure_falloc: bool,
) -> Self {
Self {
adaptor,
offset,
total_length,
secure_falloc,
done: false,
}
}
pub fn adaptor_mut(&mut self) -> &mut D {
&mut self.adaptor
}
pub fn into_inner(self) -> D {
self.adaptor
}
}
#[async_trait]
impl<D: DiskAdaptor> FileAllocationIterator for FallocFileAllocationIterator<D> {
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.adaptor,
std::path::Path::new(""),
length,
crate::filesystem::file_allocation::AllocationStrategy::Falloc,
self.secure_falloc,
)
.await?;
self.offset = self.total_length;
} else {
self.adaptor.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<D: DiskAdaptor> {
adaptor: D,
offset: u64,
total_length: u64,
done: bool,
}
impl<D: DiskAdaptor> TruncFileAllocationIterator<D> {
pub fn new(adaptor: D, offset: u64, total_length: u64) -> Self {
Self {
adaptor,
offset,
total_length,
done: false,
}
}
pub fn adaptor_mut(&mut self) -> &mut D {
&mut self.adaptor
}
}
#[async_trait]
impl<D: DiskAdaptor> FileAllocationIterator for TruncFileAllocationIterator<D> {
async fn allocate_chunk(&mut self) -> Result<()> {
self.adaptor.truncate(self.total_length).await?;
self.adaptor.flush().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<D: DiskAdaptor + Default> {
adaptor: D,
offset: u64,
total_length: u64,
secure_falloc: bool,
inner: Option<AdaptiveInner<D>>,
}
enum AdaptiveInner<D: DiskAdaptor + Default> {
Falloc(FallocFileAllocationIterator<D>),
Single(SingleFileAllocationIterator<D>),
}
impl<D: DiskAdaptor + Default> AdaptiveFileAllocationIterator<D> {
pub fn new(adaptor: D, offset: u64, total_length: u64) -> Self {
Self::new_with_secure_falloc(adaptor, offset, total_length, false)
}
pub fn new_with_secure_falloc(
adaptor: D,
offset: u64,
total_length: u64,
secure_falloc: bool,
) -> Self {
Self {
adaptor,
offset,
total_length,
secure_falloc,
inner: None,
}
}
async fn probe_and_select(&mut self) -> Result<()> {
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(
&mut self.adaptor,
std::path::Path::new(""),
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(
std::mem::take(&mut self.adaptor),
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(
std::mem::take(&mut self.adaptor),
self.offset,
self.total_length,
);
single.init();
self.inner = Some(AdaptiveInner::Single(single));
}
}
Ok(())
}
}
#[async_trait]
impl<D: DiskAdaptor + Default> FileAllocationIterator for AdaptiveFileAllocationIterator<D> {
async fn allocate_chunk(&mut self) -> Result<()> {
if self.inner.is_none() {
self.probe_and_select().await?;
}
match &mut self.inner {
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 {
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 {
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<D: DiskAdaptor + Default> AdaptiveFileAllocationIterator<D> {
pub fn into_inner(self) -> D {
match self.inner {
Some(AdaptiveInner::Falloc(iterator)) => iterator.into_inner(),
Some(AdaptiveInner::Single(iterator)) => iterator.into_inner(),
None => self.adaptor,
}
}
pub async fn close(&mut self) -> Result<()> {
match &mut self.inner {
Some(AdaptiveInner::Falloc(iterator)) => iterator.adaptor_mut().close().await,
Some(AdaptiveInner::Single(iterator)) => iterator.adaptor_mut().close().await,
None => self.adaptor.close().await,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::filesystem::disk_adaptor::DirectDiskAdaptor;
#[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 = DirectDiskAdaptor::new();
adaptor.open(&path).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 = DirectDiskAdaptor::new();
adaptor.open(&path).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 = DirectDiskAdaptor::new();
adaptor.open(&path).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 = DirectDiskAdaptor::new();
adaptor.open(&path).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);
}
#[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 = DirectDiskAdaptor::new();
adaptor.open(&path).await.unwrap();
let mut iter = TruncFileAllocationIterator::new(adaptor, 5, 1024);
iter.allocate_chunk().await.unwrap();
assert!(iter.finished());
assert_eq!(iter.current_length(), 1024);
}
}