pub fn new(path: PathBuf) -> OckamResult<Self> {
        let mut map = BTreeMap::<usize, Box<dyn Secret>>::new();
        let mut next_id: usize = 0;

        let create_path = path.clone();
        fs::create_dir_all(create_path).or_else(|_| Err(Error::IOError.into()))?;

        let mut vault = DefaultVault::default();
        let to_secret = |data: &[u8]| -> OckamResult<(SecretKey, SecretAttributes)> {
            if data.len() < ATTRS_BYTE_LENGTH {
                return Err(Error::InvalidSecret.into());
            }

            let mut attrs = [0u8; ATTRS_BYTE_LENGTH];
            attrs.copy_from_slice(&data[0..ATTRS_BYTE_LENGTH]);
            let attributes = SecretAttributes::try_from(attrs)?;

            Ok((
                SecretKey::new(data[ATTRS_BYTE_LENGTH..].to_vec()),
                attributes,
            ))
        };
        let fs_path = path.clone();

        path.read_dir()
            .or_else(|_| Err(Error::IOError.into()))?
            .filter(|r| {
                // ignore directories within vault path
                if let Ok(e) = r {
                    match fs::metadata(e.path()) {
                        Ok(md) => md.is_file(),
                        Err(_) => false,
                    }
                } else {
                    false
                }
            })
            .for_each(|entry| {
                if let Ok(entry) = entry {
                    match fs::read(entry.path()) {
                        Ok(data) => {
                            let (secret, attrs) =
                                &to_secret(data.as_slice()).unwrap_or_else(|_| {
                                    panic!("failed to get secret {:?} from file", entry.file_name())
                                });
                            // Files are read in any order
                            let fname = entry.file_name();
                            let t: &std::path::Path = fname.as_os_str().as_ref();
                            let mut item_id: Option<usize> = None;
                            if let Some(stem) = t.file_stem() {
                                if let Some(str) = stem.to_str() {
                                    if let Ok(id) = str.parse::<usize>() {
                                        item_id = Some(id);
                                    }
                                }
                            }
                            match item_id {
                                Some(id) => {
                                    let secret = vault.secret_import(secret.as_ref(), *attrs);
                                    match secret {
                                        Ok(secret) => {
                                            map.insert(id, secret);
                                            next_id = max(next_id, id);
                                        }
                                        Err(e) => eprintln!("{}", e),
                                    }
                                }
                                None => eprintln!("invalid key file name: {:?}", entry),
                            }
                        }
                        Err(e) => eprintln!("{}", e),
                    }
                }
            });

        Ok(Self {
            v: vault,
            map,
            path: fs_path,
            next_id,
        })
    }
/// Creates a new FilesystemVault using the provided path on disk to store secrets.
Creates a new FilesystemVault using the provided path on disk to store secrets.
Creates a new FilesystemVault using the provided path on disk to store secrets .
pub fn new(path: PathBuf) -> OckamResult<Self> {
        let mut map = BTreeMap::<usize, Box<dyn Secret>>::new();
        let mut next_id: usize = 0;
        let create_path = path.clone();
        fs::create_dir_all(create_path).or_else(|_| Err(Error::IOError.into()))?;
        let mut vault = DefaultVault::default();
        let to_secret = |data: &[u8]| -> OckamResult<(SecretKey, SecretAttributes)> {
            if data.len() < ATTRS_BYTE_LENGTH {
                return Err(Error::InvalidSecret.into());
            }
            let mut attrs = [0u8; ATTRS_BYTE_LENGTH];
            attrs.copy_from_slice(&data[0..ATTRS_BYTE_LENGTH]);
            let attributes = SecretAttributes::try_from(attrs)?;
            Ok((
                SecretKey::new(data[ATTRS_BYTE_LENGTH..].to_vec()),
                attributes,
            ))
        };
        let fs_path = path.clone();
        path.read_dir()
            .or_else(|_| Err(Error::IOError.into()))?
            .filter(|r| {
                // ignore directories within vault path
                if let Ok(e) = r {
                    match fs::metadata(e.path()) {
                        Ok(md) => md.is_file(),
                        Err(_) => false,
                    }
                } else {
                    false
                }
            })
            .for_each(|entry| {
                if let Ok(entry) = entry {
                    match fs::read(entry.path()) {
                        Ok(data) => {
                            let (secret, attrs) =
                                &to_secret(data.as_slice()).unwrap_or_else(|_| {
                                    panic!("failed to get secret {:?} from file", entry.file_name())
                                });
                            // Files are read in any order
                            let fname = entry.file_name();
                            let t: &std::path::Path = fname.as_os_str().as_ref();
                            let mut item_id: Option<usize> = None;
                            if let Some(stem) = t.file_stem() {
                                if let Some(str) = stem.to_str() {
                                    if let Ok(id) = str.parse::<usize>() {
                                        item_id = Some(id);
                                    }
                                }
                            }
                            match item_id {
                                Some(id) => {
                                    let secret = vault.secret_import(secret.as_ref(), *attrs);
                                    match secret {
                                        Ok(secret) => {
                                            map.insert(id, secret);
                                            next_id = max(next_id, id);
                                        }
                                        Err(e) => eprintln!("{}", e),
                                    }
                                }
                                None => eprintln!("invalid key file name: {:?}", entry),
                            }
                        }
                        Err(e) => eprintln!("{}", e),
                    }
                }
            });
        Ok(Self {
            v: vault,
            map,
            path: fs_path,
            next_id,
        })
    }
pub fn new ( path : PathBuf ) -> OckamResult < Self > { let mut map = BTreeMap :: < usize , Box < dyn Secret > > :: new ( ) ; let mut next_id : usize = 0 ; let create_path = path . clone ( ) ; fs :: create_dir_all ( create_path ) . or_else ( | _ | Err ( Error :: IOError . into ( ) ) ) ? ; let mut vault = DefaultVault :: default ( ) ; let to_secret = | data : & [ u8 ] | -> OckamResult < ( SecretKey , SecretAttributes ) > { if data . len ( ) < ATTRS_BYTE_LENGTH { return Err ( Error :: InvalidSecret . into ( ) ) ; } let mut attrs = [ 0u8 ; ATTRS_BYTE_LENGTH ] ; attrs . copy_from_slice ( & data [ 0 .. ATTRS_BYTE_LENGTH ] ) ; let attributes = SecretAttributes :: try_from ( attrs ) ? ; Ok ( ( SecretKey :: new ( data [ ATTRS_BYTE_LENGTH .. ] . to_vec ( ) ) , attributes , ) ) } ; let fs_path = path . clone ( ) ; path . read_dir ( ) . or_else ( | _ | Err ( Error :: IOError . into ( ) ) ) ? . filter ( | r | { // ignore directories within vault path if let Ok ( e ) = r { match fs :: metadata ( e . path ( ) ) { Ok ( md ) => md . is_file ( ) , Err ( _ ) => false , } } else { false } } ) . for_each ( | entry | { if let Ok ( entry ) = entry { match fs :: read ( entry . path ( ) ) { Ok ( data ) => { let ( secret , attrs ) = & to_secret ( data . as_slice ( ) ) . unwrap_or_else ( | _ | { panic ! ( " " entry file_name ( ) ) } ) ; // Files are read in any order let fname = entry . file_name ( ) ; let t : & std :: path :: Path = fname . as_os_str ( ) . as_ref ( ) ; let mut item_id : Option < usize > = None ; if let Some ( stem ) = t . file_stem ( ) { if let Some ( str ) = stem . to_str ( ) { if let Ok ( id ) = str . parse :: < usize > ( ) { item_id = Some ( id ) ; } } } match item_id { Some ( id ) => { let secret = vault . secret_import ( secret . as_ref ( ) , * attrs ) ; match secret { Ok ( secret ) => { map . insert ( id , secret ) ; next_id = max ( next_id , id ) ; } Err ( e ) => eprintln ! ( " " e ) , } } None => eprintln ! ( " " entry ) , } } Err ( e ) => eprintln ! ( " " e ) , } } } ) ; Ok ( Self { v : vault , map , path : fs_path , next_id , } ) }
Creates a new FilesystemVault using the provided path on disk to store secrets.
Creates a new FilesystemVault using the provided path on disk to store secrets .
path
PathBuf
path
PathBuf