Validates an [alpm-install-scriptlet] at `path`. Naively checks whether at least one of the required function signatures is present in the file. # Note The file at `path` is _neither sourced nor fully evaluated_. This function only provides a _very limited_ validity check! # Errors Returns an error, if - `path` can not be opened for reading, - `path` can not be read to String, - none of the r
(path: impl AsRef<Path>)
| 38 | /// |
| 39 | /// [alpm-install-scriptlet]: https://alpm.archlinux.page/specifications/alpm-install-scriptlet.5.html |
| 40 | pub fn check_scriptlet(path: impl AsRef<Path>) -> Result<(), Error> { |
| 41 | let path = path.as_ref(); |
| 42 | let mut file = File::open(path).map_err(|source| Error::IoPath { |
| 43 | path: path.to_path_buf(), |
| 44 | context: t!("error-io-open-scriptlet"), |
| 45 | source, |
| 46 | })?; |
| 47 | let mut buf = String::new(); |
| 48 | file.read_to_string(&mut buf) |
| 49 | .map_err(|source| Error::IoPath { |
| 50 | path: path.to_path_buf(), |
| 51 | context: t!("error-io-read-to-string"), |
| 52 | source, |
| 53 | })?; |
| 54 | |
| 55 | for line in buf.lines() { |
| 56 | for function_name in REQUIRED_FUNCTION_SIGNATURES { |
| 57 | if line.starts_with(&format!("{function_name}()")) |
| 58 | || line.starts_with(&format!("{function_name}() {{")) |
| 59 | || line.starts_with(&format!("function {function_name}()")) |
| 60 | || line.starts_with(&format!("function {function_name}() {{")) |
| 61 | { |
| 62 | return Ok(()); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | Err(Error::InstallScriptlet { |
| 68 | path: path.to_path_buf(), |
| 69 | context: format!( |
| 70 | "it must implement at least one of the functions: {}", |
| 71 | REQUIRED_FUNCTION_SIGNATURES.join(", ") |
| 72 | ), |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | #[cfg(test)] |
| 77 | mod test { |
no test coverage detected