From 60f831bc6f47dafdeba1edcc1692ae534312adbd Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 19 Aug 2024 11:25:45 +0100 Subject: [PATCH] dummy file is Option now instead of additional open --- crates/librqbit/src/storage/filesystem/fs.rs | 30 +++++++++++++++---- .../librqbit/src/storage/filesystem/mmap.rs | 3 +- .../src/storage/filesystem/opened_file.rs | 23 +++----------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/crates/librqbit/src/storage/filesystem/fs.rs b/crates/librqbit/src/storage/filesystem/fs.rs index 4831a76..5d202b0 100644 --- a/crates/librqbit/src/storage/filesystem/fs.rs +++ b/crates/librqbit/src/storage/filesystem/fs.rs @@ -54,7 +54,12 @@ impl TorrentStorage for FilesystemStorage { #[cfg(target_family = "unix")] { use std::os::unix::fs::FileExt; - Ok(of.file.read().read_exact_at(buf, offset)?) + Ok(of + .file + .read() + .as_ref() + .context("file is None")? + .read_exact_at(buf, offset)?) } #[cfg(not(target_family = "unix"))] { @@ -70,14 +75,21 @@ impl TorrentStorage for FilesystemStorage { #[cfg(target_family = "unix")] { use std::os::unix::fs::FileExt; - Ok(of.file.read().write_all_at(buf, offset)?) + Ok(of + .file + .read() + .as_ref() + .context("file is None")? + .write_all_at(buf, offset)?) } #[cfg(target_family = "windows")] { use std::os::windows::fs::FileExt; let mut remaining = buf.len(); + let g = of.file.read(); + let f = g.as_ref().context("file is None")?; while remaining > 0 { - remaining -= of.file.read().seek_write(buf, offset)?; + remaining -= f.seek_write(buf, offset)?; } Ok(()) } @@ -85,8 +97,9 @@ impl TorrentStorage for FilesystemStorage { { use std::io::{Read, Seek, SeekFrom, Write}; let mut g = of.file.write(); - g.seek(SeekFrom::Start(offset))?; - Ok(g.write_all(buf)?) + let mut f = g.as_ref().context("file is None")?; + f.seek(SeekFrom::Start(offset))?; + Ok(f.write_all(buf)?) } } @@ -95,7 +108,12 @@ impl TorrentStorage for FilesystemStorage { } fn ensure_file_length(&self, file_id: usize, len: u64) -> anyhow::Result<()> { - Ok(self.opened_files[file_id].file.write().set_len(len)?) + Ok(self.opened_files[file_id] + .file + .write() + .as_ref() + .context("file is None")? + .set_len(len)?) } fn take(&self) -> anyhow::Result> { diff --git a/crates/librqbit/src/storage/filesystem/mmap.rs b/crates/librqbit/src/storage/filesystem/mmap.rs index 07efe1e..1e68b24 100644 --- a/crates/librqbit/src/storage/filesystem/mmap.rs +++ b/crates/librqbit/src/storage/filesystem/mmap.rs @@ -102,9 +102,10 @@ impl TorrentStorage for MmapFilesystemStorage { let mut mmaps = Vec::new(); for (idx, file) in self.fs.opened_files.iter().enumerate() { let fg = file.file.write(); + let fg = fg.as_ref().context("file is None")?; 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")?; + let mmap = unsafe { MmapOptions::new().map_mut(fg) }.context("error mapping file")?; mmaps.push(RwLock::new(mmap)); } diff --git a/crates/librqbit/src/storage/filesystem/opened_file.rs b/crates/librqbit/src/storage/filesystem/opened_file.rs index 88f24dd..f1f4e06 100644 --- a/crates/librqbit/src/storage/filesystem/opened_file.rs +++ b/crates/librqbit/src/storage/filesystem/opened_file.rs @@ -1,37 +1,22 @@ use std::fs::File; -use anyhow::Context; use parking_lot::RwLock; #[derive(Debug)] pub(crate) struct OpenedFile { - pub file: RwLock, -} - -pub(crate) fn dummy_file() -> anyhow::Result { - #[cfg(target_os = "windows")] - const DEVNULL: &str = "NUL"; - #[cfg(not(target_os = "windows"))] - const DEVNULL: &str = "/dev/null"; - - std::fs::OpenOptions::new() - .read(true) - .open(DEVNULL) - .with_context(|| format!("error opening {}", DEVNULL)) + pub file: RwLock>, } impl OpenedFile { pub fn new(f: File) -> Self { Self { - file: RwLock::new(f), + file: RwLock::new(Some(f)), } } - pub fn take(&self) -> anyhow::Result { + pub fn take(&self) -> anyhow::Result> { let mut f = self.file.write(); - let dummy = dummy_file()?; - let f = std::mem::replace(&mut *f, dummy); - Ok(f) + Ok(f.take()) } pub fn take_clone(&self) -> anyhow::Result {