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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use config_file_handler::{self, FileHandler};
use error::InternalError;
use routing::XorName;
use rust_sodium::crypto::sign;
use std::ffi::OsString;
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Config {
pub wallet_address: Option<XorName>,
pub max_capacity: Option<u64>,
pub chunk_store_root: Option<String>,
pub invite_key: Option<[u8; sign::PUBLICKEYBYTES]>,
pub dev: Option<DevConfig>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct DevConfig {
pub disable_mutation_limit: bool,
}
pub fn read_config_file() -> Result<Config, InternalError> {
let file_handler = FileHandler::new(&get_file_name()?, false)?;
let cfg = file_handler.read_file()?;
Ok(cfg)
}
#[cfg(test)]
#[allow(dead_code)]
pub fn write_config_file(config: &Config) -> Result<::std::path::PathBuf, InternalError> {
use serde_json;
use std::fs::File;
use std::io::Write;
let mut config_path = config_file_handler::current_bin_dir()?;
config_path.push(get_file_name()?);
let mut file = File::create(&config_path)?;
write!(&mut file, "{}", serde_json::to_string_pretty(&config)?)?;
file.sync_all()?;
Ok(config_path)
}
fn get_file_name() -> Result<OsString, InternalError> {
let mut name = config_file_handler::exe_file_stem()?;
name.push(".vault.config");
Ok(name)
}
#[cfg(test)]
mod test {
use super::Config;
use serde_json;
use std::fs::File;
use std::io::Read;
use std::path::Path;
#[test]
fn parse_sample_config_file() {
let path = Path::new("installer/common/sample.vault.config").to_path_buf();
let mut file = unwrap!(File::open(&path), "Error opening {}:", path.display());
let mut encoded_contents = String::new();
let _ = unwrap!(
file.read_to_string(&mut encoded_contents),
"Error reading {}:",
path.display()
);
let config: Config = unwrap!(
serde_json::from_str(&encoded_contents),
"Error parsing {} as JSON:",
path.display()
);
assert!(
config.wallet_address.is_some(),
"{} is missing `wallet_address` field.",
path.display()
);
assert!(
config.max_capacity.is_some(),
"{} is missing `max_capacity` field.",
path.display()
);
assert!(
config.chunk_store_root.is_some(),
"{} is missing `chunk_store_root` field.",
path.display()
);
assert!(
config.invite_key.is_some(),
"{} is missing `invite_key` field.",
path.display()
);
assert!(
config.dev.is_some(),
"{} is missing `dev` field.",
path.display()
);
}
}