Skip to content

Commit

Permalink
blobfs: ensure blob cahche dir exist
Browse files Browse the repository at this point in the history
Signed-off-by: gexuyang <[email protected]>
  • Loading branch information
gexuyang committed Mar 29, 2022
1 parent 0f945f6 commit 2feb3b0
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions blobfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::any::Any;
use std::ffi::CStr;
use std::ffi::CString;
#[cfg(feature = "virtiofs")]
use std::fs::File;
use std::fs::{create_dir_all, File};
use std::io;
#[cfg(feature = "virtiofs")]
use std::mem::MaybeUninit;
Expand Down Expand Up @@ -168,6 +168,20 @@ pub struct BlobFs {
}

impl BlobFs {
fn ensure_path_exist(path: &Path) -> io::Result<()> {
if path.as_os_str().is_empty() {
return Err(einval!("path is empty"));
}
if !path.exists() {
create_dir_all(path).map_err(|e| {
error!("create dir error: {:?}", path);
e
})?;
}

Ok(())
}

/// Create a Blob file system instance.
pub fn new(cfg: Config) -> io::Result<BlobFs> {
trace!("BlobFs config is: {:?}", cfg);
Expand All @@ -184,9 +198,10 @@ impl BlobFs {
let blob_ondemand_conf = BlobOndemandConfig::from_str(&cfg.blob_ondemand_cfg)?;
// check if blob cache dir exists.
let path = Path::new(blob_ondemand_conf.blob_cache_dir.as_str());
if !path.exists() || blob_ondemand_conf.blob_cache_dir == String::default() {
return Err(einval!("no valid blob cache dir"));
}
Self::ensure_path_exist(path).map_err(|e| {
error!("blob_cache_dir not exist");
e
})?;

let path = Path::new(blob_ondemand_conf.bootstrap_path.as_str());
if !path.exists() || blob_ondemand_conf.bootstrap_path == String::default() {
Expand Down

0 comments on commit 2feb3b0

Please sign in to comment.