2018-12-08 12:58:45 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2018-12-08 13:44:55 +00:00
|
|
|
use std::fs::{OpenOptions};
|
2018-12-09 11:51:31 +00:00
|
|
|
use std::io::{Read, Write};
|
2018-12-08 12:58:45 +00:00
|
|
|
|
|
|
|
//use std::sync::Arc;
|
|
|
|
use crate::api::schema::*;
|
|
|
|
|
|
|
|
use crate::section_config::*;
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
lazy_static!{
|
|
|
|
static ref CONFIG: SectionConfig = init();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
|
|
|
|
|
|
|
let plugin = SectionConfigPlugin::new(
|
|
|
|
"datastore".to_string(),
|
|
|
|
ObjectSchema::new("DataStore properties")
|
|
|
|
.required("path", StringSchema::new("Directory name"))
|
|
|
|
);
|
|
|
|
|
|
|
|
let id_schema = StringSchema::new("DataStore ID schema.")
|
|
|
|
.min_length(3)
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let mut config = SectionConfig::new(id_schema);
|
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2018-12-08 13:44:55 +00:00
|
|
|
const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg";
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2018-12-08 13:51:08 +00:00
|
|
|
pub fn config() -> Result<SectionConfigData, Error> {
|
2018-12-08 12:58:45 +00:00
|
|
|
|
|
|
|
let mut file = match OpenOptions::new()
|
|
|
|
.create(true)
|
2018-12-09 09:22:39 +00:00
|
|
|
.read(true)
|
2018-12-08 12:58:45 +00:00
|
|
|
.write(true)
|
2018-12-08 13:44:55 +00:00
|
|
|
.open(DATASTORE_CFG_FILENAME) {
|
2018-12-08 12:58:45 +00:00
|
|
|
Ok(file) => file,
|
|
|
|
Err(err) => bail!("Unable to open '{}' - {}",
|
2018-12-08 13:44:55 +00:00
|
|
|
DATASTORE_CFG_FILENAME, err),
|
2018-12-08 12:58:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut contents = String::new();
|
2018-12-09 09:22:39 +00:00
|
|
|
file.read_to_string(&mut contents)?;
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2018-12-08 13:44:55 +00:00
|
|
|
CONFIG.parse(DATASTORE_CFG_FILENAME, &contents)
|
2018-12-08 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 11:51:31 +00:00
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
2018-12-08 12:58:45 +00:00
|
|
|
|
2018-12-09 11:51:31 +00:00
|
|
|
let raw = CONFIG.write(DATASTORE_CFG_FILENAME, &config)?;
|
|
|
|
|
|
|
|
let mut file = match OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.open(DATASTORE_CFG_FILENAME) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(err) => bail!("Unable to open '{}' - {}",
|
|
|
|
DATASTORE_CFG_FILENAME, err),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)?;
|
|
|
|
|
|
|
|
//fixme: compute and compare digest
|
|
|
|
|
|
|
|
//fixme: impl file_set_contents() (atomic write)
|
|
|
|
file.set_len(0)?;
|
|
|
|
file.write_all(raw.as_bytes())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|