2 use lazy_static
::lazy_static
;
3 use std
::collections
::HashMap
;
4 use serde
::{Serialize, Deserialize}
;
6 use proxmox
::api
::{api, schema::*}
;
7 use proxmox
::tools
::{fs::replace_file, fs::CreateOptions}
;
9 use crate::api2
::types
::*;
10 use crate::section_config
::{SectionConfig, SectionConfigData, SectionConfigPlugin}
;
13 static ref CONFIG
: SectionConfig
= init();
16 // fixme: define better schemas
18 pub const DIR_NAME_SCHEMA
: Schema
= StringSchema
::new("Directory name").schema();
19 pub const COMMENT_SCHEMA
: Schema
= StringSchema
::new("Datastore comment").schema();
25 schema
: COMMENT_SCHEMA
,
28 schema
: DIR_NAME_SCHEMA
,
32 #[derive(Serialize,Deserialize)]
33 /// Datastore configuration properties.
34 pub struct DataStoreConfig
{
35 pub comment
: Option
<String
>,
39 fn init() -> SectionConfig
{
40 let obj_schema
= match DataStoreConfig
::API_SCHEMA
{
41 Schema
::Object(ref obj_schema
) => obj_schema
,
45 let plugin
= SectionConfigPlugin
::new("datastore".to_string(), obj_schema
);
46 let mut config
= SectionConfig
::new(&DATASTORE_SCHEMA
);
47 config
.register_plugin(plugin
);
52 const DATASTORE_CFG_FILENAME
: &str = "/etc/proxmox-backup/datastore.cfg";
54 pub fn config() -> Result
<SectionConfigData
, Error
> {
55 let content
= match std
::fs
::read_to_string(DATASTORE_CFG_FILENAME
) {
58 if err
.kind() == std
::io
::ErrorKind
::NotFound
{
61 bail
!("unable to read '{}' - {}", DATASTORE_CFG_FILENAME
, err
);
66 CONFIG
.parse(DATASTORE_CFG_FILENAME
, &content
)
69 pub fn save_config(config
: &SectionConfigData
) -> Result
<(), Error
> {
70 let raw
= CONFIG
.write(DATASTORE_CFG_FILENAME
, &config
)?
;
72 let backup_user
= crate::backup
::backup_user()?
;
73 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0640);
74 // set the correct owner/group/permissions while saving file
75 // owner(rw) = root, group(r)= backup
76 let options
= CreateOptions
::new()
78 .owner(nix
::unistd
::ROOT
)
79 .group(backup_user
.gid
);
81 replace_file(DATASTORE_CFG_FILENAME
, raw
.as_bytes(), options
)?
;
86 // shell completion helper
87 pub fn complete_datastore_name(_arg
: &str, _param
: &HashMap
<String
, String
>) -> Vec
<String
> {
89 Ok(data
) => data
.sections
.iter().map(|(id
, _
)| id
.to_string()).collect(),
90 Err(_
) => return vec
![],