diff options
author | initard <alex.solomes@softwareag.com> | 2022-07-26 07:54:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-26 07:54:44 +0100 |
commit | f39825fbed5772511340f1f6aca3ed927eea837b (patch) | |
tree | 0c8d97b4f5cdf1e33da04d074af60f3c813f35ef /crates/core/c8y_smartrest | |
parent | 87ed970eab78f8c2dc043ec2259d104da2352cbd (diff) | |
parent | 8cd372ead92ce8aa4cc8c4dce7a40f6c028f817f (diff) |
Merge pull request #1250 from initard/improvement/1173/inotify-tedge-mapper-dynamic-discovery-of-operations
tedge mapper dynamic discovery of operations
Diffstat (limited to 'crates/core/c8y_smartrest')
-rw-r--r-- | crates/core/c8y_smartrest/src/operations.rs | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/crates/core/c8y_smartrest/src/operations.rs b/crates/core/c8y_smartrest/src/operations.rs index 87e6aee9..ea0202b7 100644 --- a/crates/core/c8y_smartrest/src/operations.rs +++ b/crates/core/c8y_smartrest/src/operations.rs @@ -48,6 +48,16 @@ pub struct Operations { operations_by_trigger: HashMap<String, usize>, } +/// depending on which editor you use, temporary files could be created that contain the name of +/// the file. +/// this `operation_name_is_valid` fn will ensure that only files that do not contain +/// any special characters are allowed. +pub fn is_valid_operation_name(operation: &str) -> bool { + operation + .chars() + .all(|c| c.is_ascii_alphabetic() || c.is_numeric() || c.eq(&'_')) +} + impl Operations { pub fn add_operation(&mut self, operation: Operation) { if self.operations.iter().any(|o| o.name.eq(&operation.name)) { @@ -106,23 +116,28 @@ fn get_operations(dir: impl AsRef<Path>, cloud_name: &str) -> Result<Operations, .collect::<Vec<PathBuf>>(); for path in dir_entries { - let mut details = match fs::read(&path) { - Ok(bytes) => toml::from_slice::<Operation>(bytes.as_slice()) - .map_err(|e| OperationsError::TomlError(path.to_path_buf(), e))?, + if let Some(file_name) = path.file_name().and_then(|file_name| file_name.to_str()) { + if !is_valid_operation_name(file_name) { + continue; + } + + let mut details = match fs::read(&path) { + Ok(bytes) => toml::from_slice::<Operation>(bytes.as_slice()) + .map_err(|e| OperationsError::TomlError(path.to_path_buf(), e))?, - Err(err) => return Err(OperationsError::FromIo(err)), - }; + Err(err) => return Err(OperationsError::FromIo(err)), + }; - details.name = path - .file_name() - .and_then(|filename| filename.to_str()) - .ok_or_else(|| OperationsError::InvalidOperationName(path.to_owned()))? - .to_owned(); - operations.add_operation(details); + details.name = path + .file_name() + .and_then(|filename| filename.to_str()) + .ok_or_else(|| OperationsError::InvalidOperationName(path.to_owned()))? + .to_owned(); + operations.add_operation(details); + } } Ok(operations) } - pub fn get_operation(path: PathBuf) -> Result<Operation, OperationsError> { let mut details = match fs::read(&path) { Ok(bytes) => toml::from_slice::<Operation>(bytes.as_slice()) @@ -231,4 +246,16 @@ mod tests { assert_eq!(operations.operations.len(), ops_count); } + + #[test_case("file_a?", false)] + #[test_case("~file_b", false)] + #[test_case("c8y_Command", true)] + #[test_case("c8y_CommandA~", false)] + #[test_case(".c8y_CommandB", false)] + #[test_case("c8y_CommandD?", false)] + #[test_case("c8y_CommandE?!£$%^&*(", false)] + #[test_case("?!£$%^&*(c8y_CommandF?!£$%^&*(", false)] + fn operation_name_should_contain_only_alphabetic_chars(operation: &str, expected_result: bool) { + assert_eq!(is_valid_operation_name(operation), expected_result) + } } |