From 66b5e4b548b7eb472d61d09191b40ca3d0b4328a Mon Sep 17 00:00:00 2001 From: Tamo Date: Wed, 22 Feb 2023 13:48:51 +0100 Subject: [PATCH] fix a bug where the filestore could try to parse its own tmp file and fail --- file-store/src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/file-store/src/lib.rs b/file-store/src/lib.rs index 4b7e52e5d..75db9bb5f 100644 --- a/file-store/src/lib.rs +++ b/file-store/src/lib.rs @@ -116,10 +116,20 @@ impl FileStore { /// List the Uuids of the files in the FileStore pub fn all_uuids(&self) -> Result>> { - Ok(self.path.read_dir()?.map(|entry| { - Ok(Uuid::from_str( - entry?.file_name().to_str().ok_or(Error::CouldNotParseFileNameAsUtf8)?, - )?) + Ok(self.path.read_dir()?.filter_map(|entry| { + let file_name = match entry { + Ok(entry) => entry.file_name(), + Err(e) => return Some(Err(e.into())), + }; + let file_name = match file_name.to_str() { + Some(file_name) => file_name, + None => return Some(Err(Error::CouldNotParseFileNameAsUtf8)), + }; + if file_name.starts_with('.') { + None + } else { + Some(Uuid::from_str(file_name).map_err(|e| e.into())) + } })) } } @@ -135,3 +145,34 @@ impl File { Ok(()) } } + +#[cfg(test)] +mod test { + use std::io::Write; + + use tempfile::TempDir; + + use super::*; + + #[test] + fn all_uuids() { + let dir = TempDir::new().unwrap(); + let fs = FileStore::new(dir.path()).unwrap(); + let (uuid, mut file) = fs.new_update().unwrap(); + file.write_all(b"Hello world").unwrap(); + file.persist().unwrap(); + let all_uuids = fs.all_uuids().unwrap().collect::>>().unwrap(); + assert_eq!(all_uuids, vec![uuid]); + + let (uuid2, file) = fs.new_update().unwrap(); + let all_uuids = fs.all_uuids().unwrap().collect::>>().unwrap(); + assert_eq!(all_uuids, vec![uuid]); + + file.persist().unwrap(); + let mut all_uuids = fs.all_uuids().unwrap().collect::>>().unwrap(); + all_uuids.sort(); + let mut expected = vec![uuid, uuid2]; + expected.sort(); + assert_eq!(all_uuids, expected); + } +}