Skip to content

Commit

Permalink
fix: fix freespace() on non-exist path (#430)
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <mrcroxx@outlook.com>
  • Loading branch information
MrCroxx committed Apr 26, 2024
1 parent 0ba93f5 commit 09943a3
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions foyer-storage/src/device/fs.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -314,9 +318,6 @@ impl FsDevice {

#[cfg(test)]
mod tests {

use std::env::current_dir;

use bytes::BufMut;

use super::*;
Expand Down Expand Up @@ -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:?}");

Expand Down

0 comments on commit 09943a3

Please sign in to comment.