Quellcode durchsuchen

libdpkg: Change pkg_db_get_singleton() to return a better candidate

When there's no installed instances, try to get a betted candidate,
mostly for selection purposes.
Guillem Jover vor 14 Jahren
Ursprung
Commit
94a1d64a0a
1 geänderte Dateien mit 21 neuen und 9 gelöschten Zeilen
  1. 21 9
      lib/dpkg/pkg-db.c

+ 21 - 9
lib/dpkg/pkg-db.c

@@ -108,8 +108,9 @@ pkg_db_find_set(const char *inname)
 /**
  * Return the singleton package instance from a package set.
  *
- * This means, either the first instance if none are installed, the single
- * installed instance, or NULL if more than one instance is installed.
+ * This means, if none are installed either an instance with native or
+ * all arch or the first if none found, the single installed instance,
+ * or NULL if more than one instance is installed.
  *
  * @param set The package set to use.
  *
@@ -120,15 +121,26 @@ pkg_db_get_singleton(struct pkgset *set)
 {
   struct pkginfo *pkg;
 
-  if (pkgset_installed_instances(set) > 1)
+  switch (pkgset_installed_instances(set)) {
+  case 0:
+    /* Pick an available candidate. */
+    for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
+      const struct dpkg_arch *arch = pkg->available.arch;
+
+      if (arch->type == arch_native || arch->type == arch_all)
+        return pkg;
+    }
+    /* Or falling that the first entry. */
+    return &set->pkg;
+  case 1:
+    for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
+      if (pkg->status > stat_notinstalled)
+        return pkg;
+    }
+    internerr("pkgset %s should have one installed instance", set->name);
+  default:
     return NULL;
-
-  for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
-    if (pkg->status > stat_notinstalled)
-      return pkg;
   }
-
-  return &set->pkg;
 }
 
 /**