From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 1D4A3616D9 for ; Wed, 21 Oct 2020 11:42:01 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1123B16869 for ; Wed, 21 Oct 2020 11:41:31 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id DFD081682B for ; Wed, 21 Oct 2020 11:41:28 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A22CA45EA8 for ; Wed, 21 Oct 2020 11:41:28 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Wed, 21 Oct 2020 11:41:10 +0200 Message-Id: <20201021094116.32501-2-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201021094116.32501-1-s.reiter@proxmox.com> References: <20201021094116.32501-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.033 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [apt.rs] Subject: [pbs-devel] [PATCH proxmox-backup 1/7] apt: allow filter to select different package version X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Oct 2020 09:42:01 -0000 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 --- src/api2/node/apt.rs | 179 +++++++++++++++++++++++-------------------- 1 file changed, 98 insertions(+), 81 deletions(-) diff --git a/src/api2/node/apt.rs b/src/api2/node/apt.rs index 04beaa4d..40791126 100644 --- a/src/api2/node/apt.rs +++ b/src/api2/node/apt.rs @@ -71,7 +71,16 @@ fn get_changelog_url( bail!("unknown origin ({}) or component ({})", origin, component) } -fn list_installed_apt_packages 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 bool>(filter: F) -> Vec { let mut ret = Vec::new(); @@ -88,19 +97,22 @@ fn list_installed_apt_packages bool>(filter: F) None => break }; - let current_version = match view.current_version() { - Some(vers) => vers, - None => continue - }; - let candidate_version = match view.candidate_version() { - Some(vers) => vers, - // if there's no candidate (i.e. no update) get info of currently - // installed version instead - None => current_version.clone() + let current_version = view.current_version(); + let candidate_version = view.candidate_version(); + + let (current_version, candidate_version) = match (current_version, candidate_version) { + (Some(cur), Some(can)) => (cur, can), // package installed and there is an update + (Some(cur), None) => (cur.clone(), cur), // package installed and up-to-date + (None, Some(_)) => continue, // package could be installed + (None, None) => continue, // broken }; - let package = view.name(); - if filter(&package, ¤t_version, &candidate_version) { + // 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 version = ver.version(); let mut origin_res = "unknown".to_owned(); let mut section_res = "unknown".to_owned(); let mut priority_res = "unknown".to_owned(); @@ -108,74 +120,76 @@ fn list_installed_apt_packages bool>(filter: F) let mut short_desc = package.clone(); let mut long_desc = "".to_owned(); - // get additional information via nested APT 'iterators' - let mut view_iter = view.versions(); - while let Some(ver) = view_iter.next() { - if ver.version() == candidate_version { - if let Some(section) = ver.section() { - section_res = section; - } - - if let Some(prio) = ver.priority_type() { - priority_res = prio; - } - - // assume every package has only one origin file (not - // origin, but origin *file*, for some reason those seem to - // be different concepts in APT) - let mut origin_iter = ver.origin_iter(); - let origin = origin_iter.next(); - if let Some(origin) = origin { - - if let Some(sd) = origin.short_desc() { - short_desc = sd; - } - - if let Some(ld) = origin.long_desc() { - long_desc = ld; - } - - // the package files appear in priority order, meaning - // the one for the candidate version is first - let mut pkg_iter = origin.file(); - let pkg_file = pkg_iter.next(); - if let Some(pkg_file) = pkg_file { - if let Some(origin_name) = pkg_file.origin() { - origin_res = origin_name; - } - - let filename = pkg_file.file_name(); - let source_pkg = ver.source_package(); - let source_ver = ver.source_version(); - let component = pkg_file.component(); - - // build changelog URL from gathered information - // ignore errors, use empty changelog instead - let url = get_changelog_url(&package, &filename, &source_pkg, - &candidate_version, &source_ver, &origin_res, &component); - if let Ok(url) = url { - change_log_url = url; - } - } - } - - break; - } - } - - let info = APTUpdateInfo { - package, - title: short_desc, - arch: view.arch(), - description: long_desc, - change_log_url, - origin: origin_res, - version: candidate_version, - old_version: current_version, - priority: priority_res, - section: section_res, + let fd = FilterData { + installed_version: ¤t_version, + candidate_version: &candidate_version, + active_version: &version, }; - ret.push(info); + + if filter(fd) { + if let Some(section) = ver.section() { + section_res = section; + } + + if let Some(prio) = ver.priority_type() { + priority_res = prio; + } + + // assume every package has only one origin file (not + // origin, but origin *file*, for some reason those seem to + // be different concepts in APT) + let mut origin_iter = ver.origin_iter(); + let origin = origin_iter.next(); + if let Some(origin) = origin { + + if let Some(sd) = origin.short_desc() { + short_desc = sd; + } + + if let Some(ld) = origin.long_desc() { + long_desc = ld; + } + + // the package files appear in priority order, meaning + // 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 pkg_file = pkg_iter.next(); + if let Some(pkg_file) = pkg_file { + if let Some(origin_name) = pkg_file.origin() { + origin_res = origin_name; + } + + let filename = pkg_file.file_name(); + let source_pkg = ver.source_package(); + let source_ver = ver.source_version(); + let component = pkg_file.component(); + + // build changelog URL from gathered information + // ignore errors, use empty changelog instead + let url = get_changelog_url(&package, &filename, &source_pkg, + &version, &source_ver, &origin_res, &component); + if let Ok(url) = url { + change_log_url = url; + } + } + } + + let info = APTUpdateInfo { + package, + title: short_desc, + arch: view.arch(), + description: long_desc, + change_log_url, + origin: origin_res, + version: candidate_version.clone(), + old_version: current_version.clone(), + priority: priority_res, + section: section_res, + }; + ret.push(info); + } } } @@ -201,8 +215,11 @@ fn list_installed_apt_packages bool>(filter: F) )] /// List available APT updates fn apt_update_available(_param: Value) -> Result { - let ret = list_installed_apt_packages(|_pkg, cur_ver, can_ver| cur_ver != can_ver); - Ok(json!(ret)) + let all_upgradeable = list_installed_apt_packages(|data| + data.candidate_version == data.active_version && + data.installed_version != data.candidate_version + ); + Ok(json!(all_upgradeable)) } #[api( -- 2.20.1