Artifact Content
Not logged in

Artifact 1f0899e192b45e290f979ae09a941b33a91b82fc:

// Copyright 2016 limited.
// This SAFE Network Software is licensed to you under (1) the Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

use config_file_handler::{self, FileHandler};
use error::InternalError;
use routing::XorName;
use std::ffi::OsString;

/// Lets a vault configure a wallet address and storage limit.
#[derive(Clone, Debug, Default, RustcDecodable, RustcEncodable)]
pub struct Config {
    /// Used to store the address where SafeCoin will be sent.
    pub wallet_address: Option<XorName>,
    /// Upper limit for allowed network storage on this vault.
    pub max_capacity: Option<u64>, // measured by Bytes
    /// root directory for chunk_store directories
    pub chunk_store_root: Option<String>,

/// Reads the default vault config file.
pub fn read_config_file() -> Result<Config, InternalError> {
    // if the config file is not present, a default one will be generated
    let file_handler = FileHandler::new(&get_file_name()?, false)?;
    let cfg = file_handler.read_file()?;

/// Writes a Vault config file **for use by tests and examples**.
/// The file is written to the `current_bin_dir()`
/// with the appropriate file name.
/// N.B. This method should only be used as a utility for test and examples.  In normal use cases,
/// the config file should be created by the Vault's installer.
pub fn write_config_file(config: Config) -> Result<::std::path::PathBuf, InternalError> {
    use rustc_serialize::json;
    use std::fs::File;
    use std::io::Write;

    let mut config_path = config_file_handler::current_bin_dir()?;
    let mut file = File::create(&config_path)?;
    write!(&mut file, "{}", json::as_pretty_json(&config))?;

fn get_file_name() -> Result<OsString, InternalError> {
    let mut name = config_file_handler::exe_file_stem()?;

mod test {
    fn parse_sample_config_file() {
        use std::path::Path;
        use std::fs::File;
        use std::io::Read;
        use super::Config;
        use rustc_serialize::json;

        let path = Path::new("installer/common/sample.vault.config").to_path_buf();

        let mut file = match File::open(path) {
            Ok(file) => file,
            Err(what) => {
                panic!(format!("Error opening safe_vault.vault.config: {:?}", what));

        let mut encoded_contents = String::new();

        if let Err(what) = file.read_to_string(&mut encoded_contents) {
            panic!(format!("Error reading safe_vault.vault.config: {:?}", what));

        if let Err(what) = json::decode::<Config>(&encoded_contents) {
            panic!(format!("Error parsing safe_vault.vault.config: {:?}", what));