From: Elias Rohrer Date: Mon, 21 Aug 2023 12:07:16 +0000 (+0200) Subject: f Move empty-key checks before taking locks X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=3f1acb2dc918a436e9bdf286851bf6ab1232ba19;p=rust-lightning f Move empty-key checks before taking locks --- diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index 3c55e5f82..cd3e9dda5 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -52,15 +52,15 @@ impl KVStore for FilesystemStore { type Reader = FilesystemReader; fn read(&self, namespace: &str, key: &str) -> std::io::Result { - let mut outer_lock = self.locks.lock().unwrap(); - let lock_key = (namespace.to_string(), key.to_string()); - let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default()); - if key.is_empty() { let msg = format!("Failed to read {}/{}: key may not be empty.", namespace, key); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } + let mut outer_lock = self.locks.lock().unwrap(); + let lock_key = (namespace.to_string(), key.to_string()); + let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default()); + let mut dest_file_path = self.data_dir.clone(); dest_file_path.push(namespace); dest_file_path.push(key); @@ -68,16 +68,16 @@ impl KVStore for FilesystemStore { } fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> { - let mut outer_lock = self.locks.lock().unwrap(); - let lock_key = (namespace.to_string(), key.to_string()); - let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default()); - let _guard = inner_lock_ref.write().unwrap(); - if key.is_empty() { let msg = format!("Failed to write {}/{}: key may not be empty.", namespace, key); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } + let mut outer_lock = self.locks.lock().unwrap(); + let lock_key = (namespace.to_string(), key.to_string()); + let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default()); + let _guard = inner_lock_ref.write().unwrap(); + let mut dest_file_path = self.data_dir.clone(); dest_file_path.push(namespace); dest_file_path.push(key); @@ -143,17 +143,17 @@ impl KVStore for FilesystemStore { } fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()> { + if key.is_empty() { + let msg = format!("Failed to remove {}/{}: key may not be empty.", namespace, key); + return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); + } + let mut outer_lock = self.locks.lock().unwrap(); let lock_key = (namespace.to_string(), key.to_string()); let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key.clone()).or_default()); let _guard = inner_lock_ref.write().unwrap(); - if key.is_empty() { - let msg = format!("Failed to remove {}/{}: key may not be empty.", namespace, key); - return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); - } - let mut dest_file_path = self.data_dir.clone(); dest_file_path.push(namespace); dest_file_path.push(key);