From 8bb029bbc0865998dcf8be463d094daf2170ca04 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 14 Oct 2024 15:53:50 +0100 Subject: [PATCH] do not create padding files --- crates/librqbit/src/storage/filesystem/fs.rs | 26 ++++++++++++------- .../src/storage/filesystem/opened_file.rs | 6 +++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/librqbit/src/storage/filesystem/fs.rs b/crates/librqbit/src/storage/filesystem/fs.rs index ef9248d..791e01e 100644 --- a/crates/librqbit/src/storage/filesystem/fs.rs +++ b/crates/librqbit/src/storage/filesystem/fs.rs @@ -157,14 +157,20 @@ impl TorrentStorage for FilesystemStorage { full_path.push(relative_path); std::fs::create_dir_all(full_path.parent().context("bug: no parent")?)?; - let file = if meta.options.allow_overwrite { - OpenOptions::new() - .create(true) - .truncate(false) - .read(true) - .write(true) - .open(&full_path) - .with_context(|| format!("error opening {full_path:?} in read/write mode"))? + let file = if file_details.attrs.padding { + OpenedFile::new_dummy() + } else if meta.options.allow_overwrite { + OpenedFile::new( + OpenOptions::new() + .create(true) + .truncate(false) + .read(true) + .write(true) + .open(&full_path) + .with_context(|| { + format!("error opening {full_path:?} in read/write mode") + })?, + ) } else { // create_new does not seem to work with read(true), so calling this twice. OpenOptions::new() @@ -177,9 +183,9 @@ impl TorrentStorage for FilesystemStorage { &full_path ) })?; - OpenOptions::new().read(true).write(true).open(&full_path)? + OpenedFile::new(OpenOptions::new().read(true).write(true).open(&full_path)?) }; - files.push(OpenedFile::new(file)); + files.push(file); } self.opened_files = files; diff --git a/crates/librqbit/src/storage/filesystem/opened_file.rs b/crates/librqbit/src/storage/filesystem/opened_file.rs index f1f4e06..051c652 100644 --- a/crates/librqbit/src/storage/filesystem/opened_file.rs +++ b/crates/librqbit/src/storage/filesystem/opened_file.rs @@ -14,6 +14,12 @@ impl OpenedFile { } } + pub fn new_dummy() -> Self { + Self { + file: RwLock::new(None), + } + } + pub fn take(&self) -> anyhow::Result> { let mut f = self.file.write(); Ok(f.take())