Skip to content

Commit

Permalink
blobfs: ensure blob cache dir exist
Browse files Browse the repository at this point in the history
When blob_cache_dir does not exist, create it instead of return error.

Signed-off-by: gexuyang <[email protected]>
  • Loading branch information
gexuyang committed Mar 29, 2022
1 parent 0f945f6 commit 1fd290e
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 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,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 +199,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 1fd290e

Please sign in to comment.