From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 0FD4695E1 for ; Fri, 25 Aug 2023 12:56:41 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DCA276924 for ; Fri, 25 Aug 2023 12:56:10 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Fri, 25 Aug 2023 12:56:09 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 191B642C46 for ; Fri, 25 Aug 2023 12:55:55 +0200 (CEST) From: Gabriel Goller To: pbs-devel@lists.proxmox.com Date: Fri, 25 Aug 2023 12:55:49 +0200 Message-Id: <20230825105549.78376-1-g.goller@proxmox.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.678 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_2 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_4 0.1 random spam to be learned in bayes SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [lib.rs] Subject: [pbs-devel] [PATCH proxmox] close #4263: section-config: add comment support in section-config files X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Aug 2023 10:56:41 -0000 Added support for comments in section-config files. Comments start with '#' and need to be over the whole line (f.e. no `attribute value # comment`). We often parse, edit then rewrite the config to the file. In that case we save the comments in a `HashMap` and associate them either to a section or an attribute. When deleting sections, we also delete all the containing comments, when deleting attributes, we don't delete any comments. Signed-off-by: Gabriel Goller --- proxmox-section-config/src/lib.rs | 259 +++++++++++++++++++++++++++++- 1 file changed, 256 insertions(+), 3 deletions(-) diff --git a/proxmox-section-config/src/lib.rs b/proxmox-section-config/src/lib.rs index 4441df1..1e2378a 100644 --- a/proxmox-section-config/src/lib.rs +++ b/proxmox-section-config/src/lib.rs @@ -105,6 +105,7 @@ enum ParseState<'a> { pub struct SectionConfigData { pub sections: HashMap, pub order: Vec, + pub comments: HashMap>, } impl Default for SectionConfigData { @@ -119,6 +120,7 @@ impl SectionConfigData { Self { sections: HashMap::new(), order: Vec::new(), + comments: HashMap::new(), } } @@ -353,6 +355,11 @@ impl SectionConfig { raw += "\n" } + if let Some(comments) = config.comments.get(section_id) { + for c in comments { + raw = format!("{}{}\n", raw, c); + } + } raw += &(self.format_section_header)(type_name, section_id, section_config)?; for (key, value) in section_config.as_object().unwrap() { @@ -361,6 +368,13 @@ impl SectionConfig { continue; // skip writing out id properties, they are in the section header } } + if let Some(comments) = + config.comments.get(&format!("{}:{}", section_id, key)) + { + for c in comments { + raw = format!("{}{}\n", raw, c); + } + } raw += &(self.format_section_content)(type_name, section_id, key, value)?; } } @@ -373,9 +387,21 @@ impl SectionConfig { raw += "\n" } + if let Some(comments) = config.comments.get(section_id) { + for c in comments { + raw = format!("{}{}\n", raw, c); + } + } raw += &(self.format_section_header)(type_name, section_id, section_config)?; for (key, value) in section_config.as_object().unwrap() { + if let Some(comments) = + config.comments.get(&format!("{}:{}", section_id, key)) + { + for c in comments { + raw = format!("{}{}\n", raw, c); + } + } raw += &(self.format_section_content)(type_name, section_id, key, value)?; } } @@ -383,6 +409,25 @@ impl SectionConfig { bail!("unknown section type '{type_name}'"); } } + // Insert the comments with property not found + let keys: Vec<&String> = section_config + .as_object() + .unwrap() + .iter() + .map(|c| c.0) + .collect(); + + for c in &config.comments { + let split: Vec<&str> = c.0.split(':').collect(); + if split[0] == section_id + && split.len() > 1 + && !keys.contains(&&split[1].to_string()) + { + for comment in c.1 { + raw = format!("{}{}\n", raw, comment); + } + } + } } Ok(raw) @@ -421,6 +466,7 @@ impl SectionConfig { try_block!({ let mut result = SectionConfigData::new(); + let mut dangling_comments: Vec = Vec::new(); try_block!({ for line in raw.lines() { @@ -428,6 +474,10 @@ impl SectionConfig { match state { ParseState::BeforeHeader => { + if line.trim().starts_with('#') { + dangling_comments.push(line.to_string()); + continue; + } if line.trim().is_empty() { continue; } @@ -436,6 +486,13 @@ impl SectionConfig { (self.parse_section_header)(line) { //println!("OKLINE: type: {} ID: {}", section_type, section_id); + if !dangling_comments.is_empty() { + result.comments.insert( + section_id.clone().to_string(), + dangling_comments.clone(), + ); + dangling_comments.clear(); + } if let Some(plugin) = self.plugins.get(§ion_type) { let id_schema = plugin.get_id_schema().unwrap_or(self.id_schema); @@ -461,8 +518,28 @@ impl SectionConfig { } } ParseState::InsideSection(plugin, ref mut section_id, ref mut config) => { + if line.trim().starts_with('#') { + dangling_comments.push(line.to_string()); + continue; + } if line.trim().is_empty() { // finish section + match result.comments.get(section_id) { + Some(e) => { + let mut comments = e.clone(); + comments.append(&mut dangling_comments); + result.comments.insert(section_id.to_string(), comments); + } + None if !dangling_comments.is_empty() => { + result.comments.insert( + section_id.to_string(), + dangling_comments.clone(), + ); + } + _ => (), + }; + dangling_comments.clear(); + test_required_properties( config, plugin.properties, @@ -479,6 +556,13 @@ impl SectionConfig { } if let Some((key, value)) = (self.parse_section_content)(line) { //println!("CONTENT: key: {} value: {}", key, value); + if !dangling_comments.is_empty() { + result.comments.insert( + format!("{}:{}", section_id, key), + dangling_comments.clone(), + ); + dangling_comments.clear(); + } let schema = plugin.properties.lookup(&key); let (is_array, prop_schema) = match schema { @@ -522,6 +606,21 @@ impl SectionConfig { ) => { if line.trim().is_empty() { // finish section + match result.comments.get(section_id) { + Some(e) => { + let mut comments = e.clone(); + comments.append(&mut dangling_comments); + result.comments.insert(section_id.to_string(), comments); + } + None if !dangling_comments.is_empty() => { + result.comments.insert( + section_id.to_string(), + dangling_comments.clone(), + ); + } + _ => (), + }; + dangling_comments.clear(); result.set_data(section_id, section_type, config.take())?; result.record_order(section_id); @@ -529,6 +628,13 @@ impl SectionConfig { continue; } if let Some((key, value)) = (self.parse_section_content)(line) { + if !dangling_comments.is_empty() { + result.comments.insert( + format!("{}:{}", section_id, key), + dangling_comments.clone(), + ); + dangling_comments.clear(); + } match &mut config[&key] { Value::Null => config[key] = json!(value), // Assume it's an array schema in order to handle actual array @@ -836,8 +942,8 @@ lvmthin: local-lvm2 let res = config.parse(filename, raw); println!("RES: {:?}", res); - let raw = config.write(filename, &res.unwrap()); - println!("CONFIG:\n{}", raw.unwrap()); + let raw_encoded = config.write(filename, &res.unwrap()).unwrap(); + println!("CONFIG:\n{}", raw_encoded); } // cargo test test_section_config2 -- --nocapture @@ -897,9 +1003,11 @@ fn test_section_config2() { config.register_plugin(plugin); let raw = r" - +# this is a comment user: root@pam + # this is also a comment email root@example.com + #email comment group: mygroup comment a very important group @@ -1248,3 +1356,148 @@ pub fn dump_section_config(config: &SectionConfig) -> String { res } + +#[test] +fn test_comments_error() { + const PROPERTIES: ObjectSchema = ObjectSchema::new( + "lvmthin properties", + &[( + "content", + true, + &StringSchema::new("Storage content types.").schema(), + )], + ); + + let plugin = SectionConfigPlugin::new("lvmthin".to_string(), None, &PROPERTIES); + + const ID_SCHEMA: Schema = StringSchema::new("Storage ID schema.") + .min_length(3) + .schema(); + let mut config = SectionConfig::new(&ID_SCHEMA); + config.register_plugin(plugin); + + let raw = r" + +lvmthin: local-lvm + content rootdir,im#ages + +lvmthin: local-lvm2 +#lvmthin: local-lvm2 + #comment + content r#ootdir,images +"; + + let res = config.parse("test.cfg", raw).unwrap(); + println!("RES: {:?}", res); + + // we only allow full-line comments, so check if these don't change the behavior + assert_eq!( + res.sections + .get("local-lvm") + .unwrap() + .1 + .get("content") + .unwrap(), + &serde_json::Value::String("rootdir,im#ages".to_string()) + ); + assert_eq!( + res.sections + .get("local-lvm2") + .unwrap() + .1 + .get("content") + .unwrap(), + &serde_json::Value::String("r#ootdir,images".to_string()) + ); + + let raw_encoded = config.write("test.cfg", &res).unwrap(); + println!("CONFIG:\n{}", raw_encoded); +} + +#[test] +fn test_comments_delete_section() { + const PROPERTIES: ObjectSchema = ObjectSchema::new( + "lvmthin properties", + &[ + ( + "content", + true, + &StringSchema::new("Storage content types.").schema(), + ), + ( + "test", + true, + &StringSchema::new("Storage content types.").schema(), + ), + ], + ); + + let plugin = SectionConfigPlugin::new("lvmthin".to_string(), None, &PROPERTIES); + + const ID_SCHEMA: Schema = StringSchema::new("Storage ID schema.") + .min_length(3) + .schema(); + let mut config = SectionConfig::new(&ID_SCHEMA); + config.register_plugin(plugin); + + let raw = r" + +# cool local-lvm +lvmthin: local-lvm + content rootdir,im#ages + test blah + # cool, but not in section +# cool, but not in indented + +# lvmthin: local-lvm + # content rootdir,im#ages + # test blah + +lvmthin: local-lvm2 +#lvmthin: local-lvm2 + #comment + content r#ootdir,images + # another one + test blah +"; + + let mut res = config.parse("test.cfg", raw).unwrap(); + println!("RES: {:?}", res); + + // we only allow full-line comments, so check if these don't change the behavior + assert_eq!( + res.sections + .get("local-lvm") + .unwrap() + .1 + .get("content") + .unwrap(), + &serde_json::Value::String("rootdir,im#ages".to_string()) + ); + assert_eq!( + res.sections + .get("local-lvm2") + .unwrap() + .1 + .get("content") + .unwrap(), + &serde_json::Value::String("r#ootdir,images".to_string()) + ); + + res.sections.remove("local-lvm"); + + // removing a single attribute + let mut new_section = res.sections.get("local-lvm2").unwrap().1.clone(); + new_section.as_object_mut().unwrap().remove("test"); + res.sections.insert( + "local-lvm2".to_string(), + ("lvmthin".to_string(), new_section), + ); + + let raw_encoded = config.write("test.cfg", &res).unwrap(); + println!("CONFIG:\n{}", raw_encoded); + assert_eq!(raw_encoded.contains("# cool local-lvm"), false); + assert_eq!(raw_encoded.contains("# cool, but not in section"), false); + assert_eq!(raw_encoded.contains("# cool, but not in indented"), false); + assert_eq!(raw_encoded.contains("# another one"), true); +} -- 2.39.2