do not create padding files

This commit is contained in:
Igor Katson 2024-10-14 15:53:50 +01:00
parent af7a0ddb4f
commit 8bb029bbc0
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 22 additions and 10 deletions

View file

@ -157,14 +157,20 @@ impl TorrentStorage for FilesystemStorage {
full_path.push(relative_path); full_path.push(relative_path);
std::fs::create_dir_all(full_path.parent().context("bug: no parent")?)?; std::fs::create_dir_all(full_path.parent().context("bug: no parent")?)?;
let file = if meta.options.allow_overwrite { let file = if file_details.attrs.padding {
OpenOptions::new() OpenedFile::new_dummy()
.create(true) } else if meta.options.allow_overwrite {
.truncate(false) OpenedFile::new(
.read(true) OpenOptions::new()
.write(true) .create(true)
.open(&full_path) .truncate(false)
.with_context(|| format!("error opening {full_path:?} in read/write mode"))? .read(true)
.write(true)
.open(&full_path)
.with_context(|| {
format!("error opening {full_path:?} in read/write mode")
})?,
)
} else { } else {
// create_new does not seem to work with read(true), so calling this twice. // create_new does not seem to work with read(true), so calling this twice.
OpenOptions::new() OpenOptions::new()
@ -177,9 +183,9 @@ impl TorrentStorage for FilesystemStorage {
&full_path &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; self.opened_files = files;

View file

@ -14,6 +14,12 @@ impl OpenedFile {
} }
} }
pub fn new_dummy() -> Self {
Self {
file: RwLock::new(None),
}
}
pub fn take(&self) -> anyhow::Result<Option<File>> { pub fn take(&self) -> anyhow::Result<Option<File>> {
let mut f = self.file.write(); let mut f = self.file.write();
Ok(f.take()) Ok(f.take())