use async_trait::async_trait;
use tracing::debug;
use crate::error::Result;
use crate::filesystem::disk_adaptor::{DirectDiskAdaptor, DiskAdaptor};
use crate::filesystem::file_allocation::AllocationStrategy;
use crate::filesystem::file_allocation_iterator::FileAllocationIterator;
use crate::filesystem::multi_disk_adaptor::MultiDiskAdaptor;
pub struct MultiFileAllocationIterator {
multi_adaptor: MultiDiskAdaptor,
entry_index: usize,
inner_iter: Option<PerFileIter>,
strategy: AllocationStrategy,
secure_falloc: bool,
}
enum PerFileIter {
Adaptive(
crate::filesystem::file_allocation_iterator::AdaptiveFileAllocationIterator<
DirectDiskAdaptor,
>,
),
Falloc(
crate::filesystem::file_allocation_iterator::FallocFileAllocationIterator<
DirectDiskAdaptor,
>,
),
Trunc(
crate::filesystem::file_allocation_iterator::TruncFileAllocationIterator<DirectDiskAdaptor>,
),
None,
}
impl MultiFileAllocationIterator {
pub fn new(
multi_adaptor: MultiDiskAdaptor,
strategy: AllocationStrategy,
secure_falloc: bool,
) -> Self {
Self {
multi_adaptor,
entry_index: 0,
inner_iter: None,
strategy,
secure_falloc,
}
}
async fn advance_to_next_file(&mut self) -> Result<bool> {
if self.strategy == AllocationStrategy::None {
self.entry_index = self.multi_adaptor.get_disk_writer_entries().len();
return Ok(false);
}
self.inner_iter = None;
let entries = self.multi_adaptor.get_disk_writer_entries();
while self.entry_index < entries.len() {
let entry = &entries[self.entry_index];
if !entry.has_disk_writer() {
self.entry_index += 1;
continue;
}
let target_length = entry.get_file_entry().get_length();
let current_size = match tokio::fs::metadata(entry.get_file_path()).await {
Ok(meta) => meta.len(),
Err(_) => 0,
};
if !entry.needs_file_allocation() || current_size >= target_length {
debug!(
"Skipping allocation for {:?}: current={}, target={}",
entry.get_file_path(),
current_size,
target_length
);
self.entry_index += 1;
continue;
}
debug!(
"Allocating file {:?}: target size={}, current size={}",
entry.get_file_path(),
target_length,
current_size
);
if let Some(parent) = entry.get_file_path().parent()
&& !parent.exists()
{
tokio::fs::create_dir_all(parent).await.map_err(|e| {
crate::error::Aria2Error::DirCreate(format!(
"create_dir_all {:?}: {}",
parent, e
))
})?;
}
let mut adaptor = DirectDiskAdaptor::new();
adaptor.open(entry.get_file_path()).await?;
self.inner_iter = Some(self.create_per_file_iter(adaptor, current_size, target_length));
return Ok(true);
}
Ok(false)
}
fn create_per_file_iter(
&self,
adaptor: DirectDiskAdaptor,
offset: u64,
total_length: u64,
) -> PerFileIter {
match self.strategy {
AllocationStrategy::Falloc | AllocationStrategy::Mmap => PerFileIter::Falloc(
crate::filesystem::file_allocation_iterator::FallocFileAllocationIterator::new_with_secure_falloc(
adaptor,
offset,
total_length,
self.secure_falloc,
),
),
AllocationStrategy::Trunc => PerFileIter::Trunc(
crate::filesystem::file_allocation_iterator::TruncFileAllocationIterator::new(
adaptor,
offset,
total_length,
),
),
AllocationStrategy::Prealloc => PerFileIter::Adaptive(
crate::filesystem::file_allocation_iterator::AdaptiveFileAllocationIterator::new_with_secure_falloc(
adaptor,
offset,
total_length,
self.secure_falloc,
),
),
AllocationStrategy::None => PerFileIter::None,
}
}
pub fn strategy(&self) -> AllocationStrategy {
self.strategy
}
pub fn multi_adaptor(&self) -> &MultiDiskAdaptor {
&self.multi_adaptor
}
}
#[async_trait]
impl FileAllocationIterator for MultiFileAllocationIterator {
async fn allocate_chunk(&mut self) -> Result<()> {
if let Some(inner) = &mut self.inner_iter {
let done = match inner {
PerFileIter::Adaptive(a) => {
a.allocate_chunk().await?;
a.finished()
}
PerFileIter::Falloc(f) => {
f.allocate_chunk().await?;
f.finished()
}
PerFileIter::Trunc(t) => {
t.allocate_chunk().await?;
t.finished()
}
PerFileIter::None => true,
};
if done {
self.entry_index += 1;
if let Some(inner) = self.inner_iter.take() {
match inner {
PerFileIter::Adaptive(mut a) => {
let _ = a.close().await;
}
PerFileIter::Falloc(mut f) => {
let _ = f.adaptor_mut().close().await;
}
PerFileIter::Trunc(mut t) => {
let _ = t.adaptor_mut().close().await;
}
PerFileIter::None => {}
}
tokio::task::yield_now().await;
}
self.advance_to_next_file().await?;
}
} else {
let found = self.advance_to_next_file().await?;
if found {
return self.allocate_chunk().await;
}
}
Ok(())
}
fn finished(&self) -> bool {
if self.strategy == AllocationStrategy::None {
return true;
}
if self.entry_index == 0 && self.inner_iter.is_none() {
let entries = self.multi_adaptor.get_disk_writer_entries();
if entries.is_empty() {
return true;
}
return false;
}
let entries = self.multi_adaptor.get_disk_writer_entries();
let all_done = self.entry_index >= entries.len();
let inner_done = self.inner_iter.as_ref().is_none_or(|i| match i {
PerFileIter::Adaptive(a) => a.finished(),
PerFileIter::Falloc(f) => f.finished(),
PerFileIter::Trunc(t) => t.finished(),
PerFileIter::None => true,
});
all_done && inner_done
}
fn current_length(&self) -> u64 {
match &self.inner_iter {
Some(PerFileIter::Adaptive(a)) => a.current_length(),
Some(PerFileIter::Falloc(f)) => f.current_length(),
Some(PerFileIter::Trunc(t)) => t.current_length(),
Some(PerFileIter::None) | None => 0,
}
}
fn total_length(&self) -> u64 {
match &self.inner_iter {
Some(PerFileIter::Adaptive(a)) => a.total_length(),
Some(PerFileIter::Falloc(f)) => f.total_length(),
Some(PerFileIter::Trunc(t)) => t.total_length(),
Some(PerFileIter::None) | None => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::filesystem::disk_adaptor::DirectDiskAdaptor;
use crate::filesystem::file_allocation_iterator::FileAllocationIterator;
use crate::filesystem::multi_disk_adaptor::FileEntry;
#[tokio::test]
async fn test_direct_disk_adaptor_truncate_extend() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_extend.bin");
tokio::fs::write(&path, vec![0u8; 512]).await.unwrap();
let mut adaptor = DirectDiskAdaptor::new();
adaptor.open(&path).await.unwrap();
adaptor.truncate(1024).await.unwrap();
adaptor.flush().await.unwrap();
adaptor.close().await.unwrap();
let meta = tokio::fs::metadata(&path).await.unwrap();
assert_eq!(meta.len(), 1024, "file should be extended to 1024 bytes");
}
#[tokio::test]
async fn test_multi_file_single_entry_trunc() {
let dir = tempfile::tempdir().unwrap();
let file_path = dir.path().join("file1.bin");
tokio::fs::write(&file_path, vec![0u8; 512]).await.unwrap();
let entries = vec![FileEntry::new(file_path.clone(), 1024, 0, true)];
let mut adaptor = MultiDiskAdaptor::new(1024);
adaptor.set_file_entries(entries);
let mut iter = MultiFileAllocationIterator::new(adaptor, AllocationStrategy::Trunc, false);
while !iter.finished() {
iter.allocate_chunk().await.unwrap();
}
let meta = tokio::fs::metadata(&file_path).await.unwrap();
assert_eq!(meta.len(), 1024, "file should be extended to 1024 bytes");
}
#[tokio::test]
async fn test_multi_file_skips_allocated() {
let dir = tempfile::tempdir().unwrap();
let file_path = dir.path().join("file1.bin");
tokio::fs::write(&file_path, vec![0u8; 1024]).await.unwrap();
let entries = vec![FileEntry::new(file_path.clone(), 1024, 0, true)];
let mut adaptor = MultiDiskAdaptor::new(1024);
adaptor.set_file_entries(entries);
let mut iter = MultiFileAllocationIterator::new(adaptor, AllocationStrategy::Trunc, false);
while !iter.finished() {
iter.allocate_chunk().await.unwrap();
}
let meta = tokio::fs::metadata(&file_path).await.unwrap();
assert_eq!(meta.len(), 1024);
}
}