diff options
Diffstat (limited to 'plugins/tedge_apt_plugin/src/module_check.rs')
-rw-r--r-- | plugins/tedge_apt_plugin/src/module_check.rs | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/plugins/tedge_apt_plugin/src/module_check.rs b/plugins/tedge_apt_plugin/src/module_check.rs index ecb20497..69ccb84e 100644 --- a/plugins/tedge_apt_plugin/src/module_check.rs +++ b/plugins/tedge_apt_plugin/src/module_check.rs @@ -1,5 +1,5 @@ use crate::error::InternalError; -use std::process::{Command, Stdio}; +use std::process::Command; use std::{ ffi::OsStr, path::{Path, PathBuf}, @@ -22,41 +22,55 @@ impl PackageMetadata { }) } - fn metadata_contains_all(&self, patterns: &[&str]) -> bool { + fn metadata_contains_all(&self, patterns: &[&str]) -> Result<(), InternalError> { for pattern in patterns { if !&self.metadata.contains(pattern) { - return false; - }; + let given_metadata_split: Vec<&str> = pattern.split(':').collect(); + // Extract the expected meta data value for the given key + // For example Package name, Version etc. + let expected_metadata_split: Vec<&str> = self + .metadata + .split(&given_metadata_split[0]) + .collect::<Vec<&str>>()[1] + .split('\n') + .collect::<Vec<&str>>()[0] + .split(':') + .collect(); + + return Err(InternalError::MetaDataMismatch { + package: self.file_path().to_string_lossy().to_string(), + expected_key: given_metadata_split[0].to_string(), + expected_value: expected_metadata_split[1].to_string(), + provided_value: given_metadata_split[1].to_string(), + }); + } } - true + Ok(()) } fn get_module_metadata(file_path: &str) -> Result<Vec<u8>, InternalError> { - Ok(Command::new("dpkg") - .arg("-I") - .arg(file_path) - .stdout(Stdio::piped()) - .output()? - .stdout) + let res = Command::new("dpkg").arg("-I").arg(file_path).output()?; + match res.status.success() { + true => Ok(res.stdout), + false => Err(InternalError::ParsingError { + file: file_path.to_string(), + error: String::from_utf8_lossy(&res.stderr).to_string(), + }), + } } pub fn validate_package(&mut self, contain_args: &[&str]) -> Result<(), InternalError> { - if self.metadata_contains_all(contain_args) { - // In the current implementation using `apt-get` it is required that the file has '.deb' extension (if we use dpkg extension doesn't matter). - if self.file_path.extension() != Some(OsStr::new("deb")) { - let new_path = PathBuf::from(format!("{}.deb", self.file_path().to_string_lossy())); + self.metadata_contains_all(contain_args)?; + // In the current implementation using `apt-get` it is required that the file has '.deb' extension (if we use dpkg extension doesn't matter). + if self.file_path.extension() != Some(OsStr::new("deb")) { + let new_path = PathBuf::from(format!("{}.deb", self.file_path().to_string_lossy())); - let _res = std::os::unix::fs::symlink(self.file_path(), &new_path); - self.file_path = new_path; - self.remove_modified = true; - } - - Ok(()) - } else { - Err(InternalError::ParsingError { - file: self.file_path().to_string_lossy().to_string(), - }) + let _res = std::os::unix::fs::symlink(self.file_path(), &new_path); + self.file_path = new_path; + self.remove_modified = true; } + + Ok(()) } pub fn file_path(&self) -> &Path { |