瀏覽代碼

libdpkg: Add package singleton retrieval support

Singleton packages are the traditional package sets containing a single
installed package instance, or none.
Guillem Jover 14 年之前
父節點
當前提交
9fa906e106
共有 4 個文件被更改,包括 49 次插入0 次删除
  1. 2 0
      lib/dpkg/dpkg-db.h
  2. 2 0
      lib/dpkg/libdpkg.map
  3. 19 0
      lib/dpkg/pkg-db.c
  4. 26 0
      lib/dpkg/pkg.c

+ 2 - 0
lib/dpkg/dpkg-db.h

@@ -269,6 +269,7 @@ void modstatdb_shutdown(void);
 
 void pkgset_blank(struct pkgset *set);
 int pkgset_installed_instances(struct pkgset *set);
+struct pkginfo *pkgset_get_singleton(struct pkgset *set);
 
 void pkg_blank(struct pkginfo *pp);
 void pkgbin_blank(struct pkgbin *pifp);
@@ -276,6 +277,7 @@ bool pkg_is_informative(struct pkginfo *pkg, struct pkgbin *info);
 
 struct pkginfo *pkg_db_find(const char *name);
 struct pkgset *pkg_db_find_set(const char *name);
+struct pkginfo *pkg_db_find_singleton(const char *name);
 struct pkginfo *pkg_db_get_pkg(struct pkgset *set, const struct dpkg_arch *arch);
 struct pkginfo *pkg_db_find_pkg(const char *name, const struct dpkg_arch *arch);
 int pkg_db_count_set(void);

+ 2 - 0
lib/dpkg/libdpkg.map

@@ -198,6 +198,7 @@ LIBDPKG_PRIVATE {
 
 	# Package struct handling
 	pkgset_installed_instances;
+	pkgset_get_singleton;
 	pkg_blank;
 	pkgbin_blank;
 	pkg_name_is_illegal;
@@ -243,6 +244,7 @@ LIBDPKG_PRIVATE {
 	# Package in-core database functions
 	pkg_db_find;
 	pkg_db_find_set;
+	pkg_db_find_singleton;
 	pkg_db_find_pkg;
 	pkg_db_count_set;
 	pkg_db_count_pkg;

+ 19 - 0
lib/dpkg/pkg-db.c

@@ -105,6 +105,25 @@ pkg_db_find_set(const char *inname)
   return newpkg;
 }
 
+/**
+ * Return the singleton package instance with the given name.
+ *
+ * @param name The package name.
+ *
+ * @return The package instance.
+ */
+struct pkginfo *
+pkg_db_find_singleton(const char *name)
+{
+  struct pkgset *set;
+  struct pkginfo *pkg;
+
+  set = pkg_db_find_set(name);
+  pkg = pkgset_get_singleton(set);
+
+  return pkg;
+}
+
 /**
  * Return the package instance in a set with the given architecture.
  *

+ 26 - 0
lib/dpkg/pkg.c

@@ -174,6 +174,32 @@ pkgset_installed_instances(struct pkgset *set)
 	return set->installed_instances;
 }
 
+/**
+ * Get the singleton package instance of 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.
+ *
+ * @param set The package set to use.
+ *
+ * @return The singleton package instance.
+ */
+struct pkginfo *
+pkgset_get_singleton(struct pkgset *set)
+{
+	struct pkginfo *pkg;
+
+	if (pkgset_installed_instances(set) > 1)
+		return NULL;
+
+	for (pkg = &set->pkg; pkg; pkg = pkg->arch_next) {
+		if (pkg->status > stat_notinstalled)
+			return pkg;
+	}
+
+	return &set->pkg;
+}
+
 static int
 nes(const char *s)
 {