1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
//! # Storage for `KeyFiles` and `Contracts`

mod keyfile;
mod contracts;
mod storage_ctrl;

pub use self::KeyStorageError;
pub use self::contracts::ContractStorage;
pub use self::keyfile::*;
pub use self::storage_ctrl::StorageController;
use std::boxed::Box;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

/// Base dir for internal data, all chain-related should be store in subdirectories
#[derive(Debug, Clone)]
pub struct Storages {
    /// base dir
    base_dir: PathBuf,
}

/// Default path (*nix)
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
pub fn default_path() -> PathBuf {
    let mut config_dir = env::home_dir().expect("Expect path to home dir");
    config_dir.push(".emerald");
    config_dir
}

/// Default path (Mac OS X)
#[cfg(target_os = "macos")]
pub fn default_path() -> PathBuf {
    let mut config_dir = env::home_dir().expect("Expect path to home dir");
    config_dir.push("Library");
    config_dir.push("Emerald");
    config_dir
}

/// Default path (Windows OS)
#[cfg(target_os = "windows")]
pub fn default_path() -> PathBuf {
    let app_data_var = env::var("APPDATA").expect("Expect 'APPDATA' environment variable");
    let mut config_dir = PathBuf::from(app_data_var);
    config_dir.push(".emerald");
    config_dir
}

/// Build `chain` specific path for selected `folder`
///
/// # Arguments:
///
/// * `base_path` - base folder for storage
/// * `chain` - chain name
/// * `folder` - destination folder
///
pub fn build_path(base_path: &Path, chain: &str, folder: &str) -> PathBuf {
    let mut path = PathBuf::from(base_path);
    path.push(chain);
    path.push(folder);
    path
}

/// Creates specific type of `KeyFile` storage (database or filesystem)
///
/// # Arguments:
///
/// * `keystore_path` - path for `KeyFile` storage
///
pub fn build_keyfile_storage<P>(path: P) -> Result<Box<KeyfileStorage>, KeyStorageError>
where
    P: AsRef<Path>,
{
    #[cfg(feature = "default")]
    {
        let mut p = PathBuf::new();
        p.push(path);
        p.push(".db");
        match DbStorage::new(p) {
            Ok(db) => Ok(Box::new(db)),
            Err(_) => Err(KeyStorageError::StorageError(
                "Can't create database Keyfile storage".to_string(),
            )),
        }
    }
    #[cfg(feature = "fs-storage")]
    match FsStorage::new(path) {
        Ok(fs) => Ok(Box::new(fs)),
        Err(_) => Err(KeyStorageError::StorageError(
            "Can't create filesystem Keyfile storage".to_string(),
        )),
    }
}

/// Creates specific type of `KeyFile` storage (database or filesystem)
///
/// # Arguments:
///
/// * `keystore_path` - path for `KeyFile` storage
///
pub fn build_contract_storage<P>(path: P) -> Result<Box<ContractStorage>, KeyStorageError>
where
    P: AsRef<Path>,
{
    // TODO: implement DB storage. Add conditional compilation.
    let mut p = PathBuf::new();
    p.push(path);
    fs::create_dir_all(&p)?;

    Ok(Box::new(ContractStorage::new(p)))
}