]>
Commit | Line | Data |
---|---|---|
678d72df DM |
1 | use failure::*; |
2 | ||
aada2a97 | 3 | //use std::fs::{OpenOptions}; |
a27a3ee4 | 4 | use std::io::Read; |
678d72df DM |
5 | |
6 | //use std::sync::Arc; | |
f12f8ff1 | 7 | use crate::tools; |
dc9a007b | 8 | use crate::api_schema::schema::*; |
678d72df DM |
9 | |
10 | use crate::section_config::*; | |
11 | ||
12 | use lazy_static::lazy_static; | |
13 | ||
14 | lazy_static!{ | |
15 | static ref CONFIG: SectionConfig = init(); | |
16 | } | |
17 | ||
18 | fn init() -> SectionConfig { | |
19 | ||
20 | let plugin = SectionConfigPlugin::new( | |
21 | "datastore".to_string(), | |
22 | ObjectSchema::new("DataStore properties") | |
23 | .required("path", StringSchema::new("Directory name")) | |
24 | ); | |
25 | ||
26 | let id_schema = StringSchema::new("DataStore ID schema.") | |
27 | .min_length(3) | |
28 | .into(); | |
29 | ||
30 | let mut config = SectionConfig::new(id_schema); | |
31 | config.register_plugin(plugin); | |
32 | ||
33 | config | |
34 | } | |
35 | ||
6ce50400 | 36 | const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg"; |
678d72df | 37 | |
567713b4 | 38 | pub fn config() -> Result<SectionConfigData, Error> { |
678d72df | 39 | |
678d72df | 40 | let mut contents = String::new(); |
9b50c161 DM |
41 | |
42 | try_block!({ | |
d11594db DM |
43 | match std::fs::File::open(DATASTORE_CFG_FILENAME) { |
44 | Ok(mut file) => file.read_to_string(&mut contents), | |
45 | Err(err) => { | |
46 | if err.kind() == std::io::ErrorKind::NotFound { | |
47 | contents = String::from(""); | |
48 | Ok(0) | |
49 | } else { | |
50 | Err(err) | |
51 | } | |
52 | } | |
53 | } | |
9b50c161 | 54 | }).map_err(|e| format_err!("unable to read '{}' - {}", DATASTORE_CFG_FILENAME, e))?; |
678d72df | 55 | |
6ce50400 | 56 | CONFIG.parse(DATASTORE_CFG_FILENAME, &contents) |
678d72df DM |
57 | } |
58 | ||
652c1190 | 59 | pub fn save_config(config: &SectionConfigData) -> Result<(), Error> { |
678d72df | 60 | |
652c1190 DM |
61 | let raw = CONFIG.write(DATASTORE_CFG_FILENAME, &config)?; |
62 | ||
f12f8ff1 | 63 | tools::file_set_contents(DATASTORE_CFG_FILENAME, raw.as_bytes(), None)?; |
652c1190 DM |
64 | |
65 | Ok(()) | |
66 | } | |
30d2e99c DM |
67 | |
68 | // shell completion helper | |
728797d0 | 69 | pub fn complete_datastore_name(_arg: &str) -> Vec<String> { |
e3d40bb8 DM |
70 | match config() { |
71 | Ok(data) => data.sections.iter().map(|(id,_)| id.to_string()).collect(), | |
30d2e99c | 72 | Err(_) => return vec![], |
30d2e99c | 73 | } |
30d2e99c | 74 | } |