apt: allow filter to select different package version

To get package details for a specific version instead of only the
candidate.

Also cleanup filter function with extra struct instead of unnamed &str
parameters.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
This commit is contained in:
Stefan Reiter 2020-10-21 11:41:10 +02:00 committed by Fabian Grünbichler
parent 4ebda996e5
commit efeb92efee
1 changed files with 98 additions and 81 deletions

View File

@ -71,7 +71,16 @@ fn get_changelog_url(
bail!("unknown origin ({}) or component ({})", origin, component) bail!("unknown origin ({}) or component ({})", origin, component)
} }
fn list_installed_apt_packages<F: Fn(&str, &str, &str) -> bool>(filter: F) struct FilterData<'a> {
// this is version info returned by APT
installed_version: &'a str,
candidate_version: &'a str,
// this is the version info the filter is supposed to check
active_version: &'a str,
}
fn list_installed_apt_packages<F: Fn(FilterData) -> bool>(filter: F)
-> Vec<APTUpdateInfo> { -> Vec<APTUpdateInfo> {
let mut ret = Vec::new(); let mut ret = Vec::new();
@ -88,19 +97,22 @@ fn list_installed_apt_packages<F: Fn(&str, &str, &str) -> bool>(filter: F)
None => break None => break
}; };
let current_version = match view.current_version() { let current_version = view.current_version();
Some(vers) => vers, let candidate_version = view.candidate_version();
None => continue
}; let (current_version, candidate_version) = match (current_version, candidate_version) {
let candidate_version = match view.candidate_version() { (Some(cur), Some(can)) => (cur, can), // package installed and there is an update
Some(vers) => vers, (Some(cur), None) => (cur.clone(), cur), // package installed and up-to-date
// if there's no candidate (i.e. no update) get info of currently (None, Some(_)) => continue, // package could be installed
// installed version instead (None, None) => continue, // broken
None => current_version.clone()
}; };
// get additional information via nested APT 'iterators'
let mut view_iter = view.versions();
while let Some(ver) = view_iter.next() {
let package = view.name(); let package = view.name();
if filter(&package, &current_version, &candidate_version) { let version = ver.version();
let mut origin_res = "unknown".to_owned(); let mut origin_res = "unknown".to_owned();
let mut section_res = "unknown".to_owned(); let mut section_res = "unknown".to_owned();
let mut priority_res = "unknown".to_owned(); let mut priority_res = "unknown".to_owned();
@ -108,10 +120,13 @@ fn list_installed_apt_packages<F: Fn(&str, &str, &str) -> bool>(filter: F)
let mut short_desc = package.clone(); let mut short_desc = package.clone();
let mut long_desc = "".to_owned(); let mut long_desc = "".to_owned();
// get additional information via nested APT 'iterators' let fd = FilterData {
let mut view_iter = view.versions(); installed_version: &current_version,
while let Some(ver) = view_iter.next() { candidate_version: &candidate_version,
if ver.version() == candidate_version { active_version: &version,
};
if filter(fd) {
if let Some(section) = ver.section() { if let Some(section) = ver.section() {
section_res = section; section_res = section;
} }
@ -136,7 +151,9 @@ fn list_installed_apt_packages<F: Fn(&str, &str, &str) -> bool>(filter: F)
} }
// the package files appear in priority order, meaning // the package files appear in priority order, meaning
// the one for the candidate version is first // the one for the candidate version is first - this is fine
// however, as the source package should be the same for all
// versions anyway
let mut pkg_iter = origin.file(); let mut pkg_iter = origin.file();
let pkg_file = pkg_iter.next(); let pkg_file = pkg_iter.next();
if let Some(pkg_file) = pkg_file { if let Some(pkg_file) = pkg_file {
@ -152,17 +169,13 @@ fn list_installed_apt_packages<F: Fn(&str, &str, &str) -> bool>(filter: F)
// build changelog URL from gathered information // build changelog URL from gathered information
// ignore errors, use empty changelog instead // ignore errors, use empty changelog instead
let url = get_changelog_url(&package, &filename, &source_pkg, let url = get_changelog_url(&package, &filename, &source_pkg,
&candidate_version, &source_ver, &origin_res, &component); &version, &source_ver, &origin_res, &component);
if let Ok(url) = url { if let Ok(url) = url {
change_log_url = url; change_log_url = url;
} }
} }
} }
break;
}
}
let info = APTUpdateInfo { let info = APTUpdateInfo {
package, package,
title: short_desc, title: short_desc,
@ -170,14 +183,15 @@ fn list_installed_apt_packages<F: Fn(&str, &str, &str) -> bool>(filter: F)
description: long_desc, description: long_desc,
change_log_url, change_log_url,
origin: origin_res, origin: origin_res,
version: candidate_version, version: candidate_version.clone(),
old_version: current_version, old_version: current_version.clone(),
priority: priority_res, priority: priority_res,
section: section_res, section: section_res,
}; };
ret.push(info); ret.push(info);
} }
} }
}
return ret; return ret;
} }
@ -201,8 +215,11 @@ fn list_installed_apt_packages<F: Fn(&str, &str, &str) -> bool>(filter: F)
)] )]
/// List available APT updates /// List available APT updates
fn apt_update_available(_param: Value) -> Result<Value, Error> { fn apt_update_available(_param: Value) -> Result<Value, Error> {
let ret = list_installed_apt_packages(|_pkg, cur_ver, can_ver| cur_ver != can_ver); let all_upgradeable = list_installed_apt_packages(|data|
Ok(json!(ret)) data.candidate_version == data.active_version &&
data.installed_version != data.candidate_version
);
Ok(json!(all_upgradeable))
} }
#[api( #[api(