Folders and files are now deleted more comprehensively

This commit is contained in:
Igor Katson 2024-06-21 13:18:30 +01:00
parent 7147f16042
commit ace4bed0c6
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
13 changed files with 253 additions and 103 deletions

View file

@ -22,19 +22,11 @@ fn dummy_mmap() -> anyhow::Result<MmapMut> {
impl StorageFactory for MmapFilesystemStorageFactory {
type Storage = MmapFilesystemStorage;
fn init_storage(&self, meta: &ManagedTorrentInfo) -> anyhow::Result<Self::Storage> {
let fs_storage = FilesystemStorageFactory::default().init_storage(meta)?;
let mut mmaps = Vec::new();
for (idx, file) in fs_storage.opened_files.iter().enumerate() {
let fg = file.file.write();
fg.set_len(meta.file_infos[idx].len)
.context("mmap storage: error setting length")?;
let mmap = unsafe { MmapOptions::new().map_mut(&*fg) }.context("error mapping file")?;
mmaps.push(RwLock::new(mmap));
}
fn create(&self, meta: &ManagedTorrentInfo) -> anyhow::Result<Self::Storage> {
let fs_storage = FilesystemStorageFactory::default().create(meta)?;
Ok(MmapFilesystemStorage {
opened_mmaps: mmaps,
opened_mmaps: Vec::new(),
fs: fs_storage,
})
}
@ -82,6 +74,10 @@ impl TorrentStorage for MmapFilesystemStorage {
self.fs.remove_file(file_id, filename)
}
fn remove_directory_if_empty(&self, path: &Path) -> anyhow::Result<()> {
self.fs.remove_directory_if_empty(path)
}
fn ensure_file_length(&self, file_id: usize, len: u64) -> anyhow::Result<()> {
self.fs.ensure_file_length(file_id, len)
}
@ -100,4 +96,19 @@ impl TorrentStorage for MmapFilesystemStorage {
fs: self.fs.take_fs()?,
}))
}
fn init(&mut self, meta: &ManagedTorrentInfo) -> anyhow::Result<()> {
self.fs.init(meta)?;
let mut mmaps = Vec::new();
for (idx, file) in self.fs.opened_files.iter().enumerate() {
let fg = file.file.write();
fg.set_len(meta.file_infos[idx].len)
.context("mmap storage: error setting length")?;
let mmap = unsafe { MmapOptions::new().map_mut(&*fg) }.context("error mapping file")?;
mmaps.push(RwLock::new(mmap));
}
self.opened_mmaps = mmaps;
Ok(())
}
}