From 09943a359c56951953cab5ad70a9d270730c76d7 Mon Sep 17 00:00:00 2001 From: Croxx Date: Fri, 26 Apr 2024 17:53:33 +0800 Subject: [PATCH] fix: fix `freespace()` on non-exist path (#430) Signed-off-by: MrCroxx --- foyer-storage/src/device/fs.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/foyer-storage/src/device/fs.rs b/foyer-storage/src/device/fs.rs index 0a4b32ba..5fa44c43 100644 --- a/foyer-storage/src/device/fs.rs +++ b/foyer-storage/src/device/fs.rs @@ -78,7 +78,11 @@ impl FsDeviceConfigBuilder { let align = self.align.unwrap_or(Self::DEFAULT_ALIGN); - let capacity = self.capacity.unwrap_or(freespace(&dir).unwrap() / 10 * 8); + let capacity = self.capacity.unwrap_or({ + // Create an empty directory before to get freespace. + create_dir_all(&dir).unwrap(); + freespace(&dir).unwrap() / 10 * 8 + }); let capacity = align_v(capacity, align); let file_size = self.file_size.unwrap_or(Self::DEFAULT_FILE_SIZE).clamp(align, capacity); @@ -314,9 +318,6 @@ impl FsDevice { #[cfg(test)] mod tests { - - use std::env::current_dir; - use bytes::BufMut; use super::*; @@ -356,8 +357,20 @@ mod tests { #[test] fn test_config_builder() { - let dir = current_dir().unwrap(); - let config = FsDeviceConfigBuilder::new(dir).build(); + let dir = tempfile::tempdir().unwrap(); + + let config = FsDeviceConfigBuilder::new(dir.path()).build(); + + println!("{config:?}"); + + config.assert(); + } + + #[test] + fn test_config_builder_noent() { + let dir = tempfile::tempdir().unwrap(); + + let config = FsDeviceConfigBuilder::new(dir.path().join("noent")).build(); println!("{config:?}");