From: Elias Rohrer Date: Wed, 23 Aug 2023 11:58:54 +0000 (+0200) Subject: f Account for `KVStore::read` returning a `Vec` X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=adc7b72a293b7813c8e6d029da5a09b8d8280d27;p=rust-lightning f Account for `KVStore::read` returning a `Vec` --- diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index 11cf2d402..2cb273f26 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -50,9 +50,7 @@ impl FilesystemStore { } impl KVStore for FilesystemStore { - type Reader = FilesystemReader; - - fn read(&self, namespace: &str, key: &str) -> std::io::Result { + fn read(&self, namespace: &str, key: &str) -> std::io::Result> { if key.is_empty() { let msg = format!("Failed to read {}/{}: key may not be empty.", PrintableString(namespace), PrintableString(key)); @@ -74,8 +72,15 @@ impl KVStore for FilesystemStore { let mut outer_lock = self.locks.lock().unwrap(); let inner_lock_ref = Arc::clone(&outer_lock.entry(dest_file_path.clone()).or_default()); + let _guard = inner_lock_ref.read().unwrap(); + + let mut buf = Vec::new(); + let f = fs::File::open(dest_file_path.clone())?; + let mut reader = BufReader::new(f); + let nread = reader.read_to_end(&mut buf)?; + debug_assert_ne!(nread, 0); - FilesystemReader::new(dest_file_path, inner_lock_ref) + Ok(buf) } fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> { @@ -266,27 +271,6 @@ impl KVStore for FilesystemStore { } } -/// A buffered [`Read`] implementation as returned from [`FilesystemStore::read`]. -pub struct FilesystemReader { - inner: BufReader, - lock_ref: Arc>, -} - -impl FilesystemReader { - fn new(dest_file_path: PathBuf, lock_ref: Arc>) -> std::io::Result { - let f = fs::File::open(dest_file_path.clone())?; - let inner = BufReader::new(f); - Ok(Self { inner, lock_ref }) - } -} - -impl Read for FilesystemReader { - fn read(&mut self, buf: &mut [u8]) -> std::io::Result { - let _guard = self.lock_ref.read().unwrap(); - self.inner.read(buf) - } -} - #[cfg(test)] mod tests { use super::*;