MCPcopy Create free account
hub / github.com/archlinux/alpm / check_scriptlet

Function check_scriptlet

alpm-package/src/scriptlet.rs:40–74  ·  view source on GitHub ↗

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>)

Source from the content-addressed store, hash-verified

38///
39/// [alpm-install-scriptlet]: https://alpm.archlinux.page/specifications/alpm-install-scriptlet.5.html
40pub 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)]
77mod test {

Callers 2

get_install_scriptletFunction · 0.85
valid_scriptletFunction · 0.85

Calls 2

as_refMethod · 0.45
to_path_bufMethod · 0.45

Tested by

no test coverage detected