]> git.proxmox.com Git - proxmox-backup.git/blob - src/config/datastore.rs
0098dca7905dfca3e737a6cb0952e5b6c83a1597
[proxmox-backup.git] / src / config / datastore.rs
1 use failure::*;
2
3 //use std::fs::{OpenOptions};
4 use std::io::Read;
5
6 //use std::sync::Arc;
7 use crate::tools;
8 use crate::api::schema::*;
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
36 const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg";
37
38 pub fn config() -> Result<SectionConfigData, Error> {
39
40 let mut contents = String::new();
41
42 try_block!({
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 }
54 }).map_err(|e| format_err!("unable to read '{}' - {}", DATASTORE_CFG_FILENAME, e))?;
55
56 CONFIG.parse(DATASTORE_CFG_FILENAME, &contents)
57 }
58
59 pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
60
61 let raw = CONFIG.write(DATASTORE_CFG_FILENAME, &config)?;
62
63 tools::file_set_contents(DATASTORE_CFG_FILENAME, raw.as_bytes(), None)?;
64
65 Ok(())
66 }
67
68 // shell completion helper
69 pub fn complete_datastore_name(_arg: &str) -> Vec<String> {
70 match config() {
71 Ok(data) => data.sections.iter().map(|(id,_)| id.to_string()).collect(),
72 Err(_) => return vec![],
73 }
74 }