Skip to content

Commit

Permalink
Merge pull request #358 from MercyMM/master
Browse files Browse the repository at this point in the history
blobfs: ensure blob cache dir exist
  • Loading branch information
liubogithub authored Mar 30, 2022
2 parents 2774e4a + 6d02fba commit 5c0d572
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions blobfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use std::any::Any;
#[cfg(feature = "virtiofs")]
use std::ffi::CStr;
use std::ffi::CString;
use std::fs::create_dir_all;
#[cfg(feature = "virtiofs")]
use std::fs::File;
use std::io;
Expand Down Expand Up @@ -168,6 +169,25 @@ 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. directory is {:?}. {}:{}",
path,
file!(),
line!()
);
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 +204,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 5c0d572

Please sign in to comment.