[3/3] repository: make repo_contains_package query the package database

Message ID 20241122054816.3371334-4-cedric.hombourger@siemens.com
State Under Review, archived
Headers show
Series repository: small fixes for repo_{contains,del}_packages | expand

Commit Message

cedric.hombourger@siemens.com Nov. 22, 2024, 5:48 a.m. UTC
Debian package repositories (created/updated with dak) and reprepro
repositories do not have the same file naming convention (e.g. the
latter does not use Epoch). A full directory scan may also be
expensive for large repositories: use reprepro listfilter to check
for matching packages in the database.

Signed-off-by: Cedric Hombourger <cedric.hombourger@siemens.com>
---
 meta/classes/repository.bbclass | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

Patch

diff --git a/meta/classes/repository.bbclass b/meta/classes/repository.bbclass
index b20ec091..bc5852a5 100644
--- a/meta/classes/repository.bbclass
+++ b/meta/classes/repository.bbclass
@@ -98,7 +98,28 @@  repo_contains_package() {
     local file="$4"
     local package
 
-    package=$(find ${dir} -name ${file##*/})
+    # Extract meta-data from the provided .deb file
+    package=$(dpkg-deb -f ${file} Package Version Architecture)
+
+    # Output for each field is "Field: Value"
+    # odd indexes hold field names, even indexes hold values
+    set -- ${package}
+
+    # lookup ${file} in the database for the current suite
+    package=$(reprepro -b ${dir} --dbdir ${dbdir} \
+                       --list-format '${$fullfilename}\n' \
+                       listfilter ${codename} '
+                           Package (= '${2}'),
+                           Version (= '${4}'),
+                           Architecture (= '${6}'),
+                           $PackageType (= deb)')
+
+    # we only need the first match (should there be more). Use shell builtins to avoid
+    # spawning an additional process (e.g. "head")
+    set -- ${package}
+    package="${1}"
+
+    # package found in the database?
     if [ -n "$package" ]; then
         # yes
         cmp --silent "$package" "$file" && return 0