Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Jun 2013 05:47:16 GMT
From:      mattbw@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r253477 - in soc2013/mattbw/backend: . actions
Message-ID:  <201306250547.r5P5lGwG032110@socsvn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mattbw
Date: Tue Jun 25 05:47:16 2013
New Revision: 253477
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=253477

Log:
  make generic package iterator handler ready for other get-xyz jobs.

Added:
  soc2013/mattbw/backend/iterate.c
  soc2013/mattbw/backend/iterate.h
Modified:
  soc2013/mattbw/backend/Makefile
  soc2013/mattbw/backend/actions/get-details.c

Modified: soc2013/mattbw/backend/Makefile
==============================================================================
--- soc2013/mattbw/backend/Makefile	Tue Jun 25 04:44:42 2013	(r253476)
+++ soc2013/mattbw/backend/Makefile	Tue Jun 25 05:47:16 2013	(r253477)
@@ -2,7 +2,7 @@
 
 LIB=		pk_backend_pkgng
 SHLIB_MAJOR=	1
-SRCS=		pk-backend-pkgng.c groups.c db.c licenses.c
+SRCS=		pk-backend-pkgng.c groups.c db.c licenses.c iterate.c
 SRCS+=		actions/get-details.c
 
 LIBDIR=		/usr/local/lib/packagekit-backend

Modified: soc2013/mattbw/backend/actions/get-details.c
==============================================================================
--- soc2013/mattbw/backend/actions/get-details.c	Tue Jun 25 04:44:42 2013	(r253476)
+++ soc2013/mattbw/backend/actions/get-details.c	Tue Jun 25 05:47:16 2013	(r253477)
@@ -19,13 +19,13 @@
  */
 
 #include <string.h>
-
 #include <glib.h>
 #include "../pk-backend.h"
 #include "pkg.h"
 
 #include "../db.h"		/* open_remote_db */
 #include "../groups.h"		/* group_from_origin */
+#include "../iterate.h"		/* Package iteration */
 #include "../licenses.h"	/* license_from_pkg */
 
 #include "get-details.h"	/* get_details_thread prototype */
@@ -34,7 +34,7 @@
 
 /* TODO: move out of get-details? */
 const char     *null_if_empty(const char *in);
-gboolean	string_match(const char *left, const char *right);
+
 static gboolean
 get_details_query(const gchar *name,
 		  const gchar *version,
@@ -56,107 +56,32 @@
 
 
 /*
- * Checks two strings with strcmp and emits TRUE if they match. If either
- * string is NULL, emit TRUE as well (this is so that missing PackageID
- * elements trigger matches).
+ * Emits the given package's details. To be used as an iterating function.
  */
-gboolean
-string_match(const char *left, const char *right)
-{
-	int		result;
-
-	if (left == NULL || right == NULL)
-		result = TRUE;
-	else
-		result = (strcmp(left, right) == 0 ? TRUE : FALSE);
-
-	return result;
-}
-
-/*
- * Go through a database iterator of possible package matches and emit any
- * that match the split PackageID.
- */
-gboolean
-get_details_check_matches(struct pkgdb_it *matches,
-			  const gchar *id_name,
-			  const gchar *id_version,
-			  const gchar *id_arch,
-			  const gchar *id_data,
-			  PkBackend *backend)
-{
-	gboolean	found;
-	int		err;
-	struct pkg     *match;
-
-	found = FALSE;
-	match = NULL;
-	do {
-		err = pkgdb_it_next(matches, &match, LOAD_FLAGS);
-		if (err == EPKG_OK) {
-			const char     *arch;
-			const char     *data;
-			const char     *description;
-			const char     *name;
-			const char     *origin;
-			const char     *reponame;
-			const char     *version;
-			const char     *www;
-			int64_t		flatsize;
-
-			pkg_get(match,
-				PKG_ARCH, &arch,
-				PKG_DESC, &description,
-				PKG_FLATSIZE, &flatsize,
-				PKG_NAME, &name,
-				PKG_ORIGIN, &origin,
-				PKG_REPONAME, &reponame,
-				PKG_VERSION, &version,
-				PKG_WWW, &www);
-
-			switch (pkg_type(match)) {
-			case PKG_FILE:
-				data = "local";
-				break;
-			case PKG_INSTALLED:
-				data = "installed";
-				break;
-			default:
-				data = reponame;
-				break;
-			}
-
-			/*
-			 * Emit if this package's PackageID fields match the
-			 * original PackageID.  Of course, the original ID
-			 * might have missing fields (NULLs), so we treat a
-			 * comparison involving one as a success.
-			 */
-			if (string_match(name, id_name) &&
-			    string_match(version, id_version) &&
-			    string_match(arch, id_arch) &&
-			    string_match(data, id_data)) {
-				gchar          *new_id;
-
-				found = TRUE;
-				new_id = pk_package_id_build(name, version, arch, data);
-
-				/* TODO: implement category, size and licence */
-				pk_backend_details(backend,
-						   new_id,
-						   license_name_from_pkg(match),
-						   group_from_origin(origin),
-						   description,
-						   www,
-						   flatsize);
-
-				g_free(new_id);
-			}
-		}
-	} while (err == EPKG_OK && found == FALSE);
-	pkg_free(match);
-
-	return found;
+static void
+emit_get_details(struct pkg *pkg,
+		 const gchar *id,
+		 PkBackend *backend)
+{
+	const char     *description;
+	const char     *origin;
+	const char     *www;
+	int64_t		flatsize;
+
+	/* Information not already part of the PackageID */
+	pkg_get(pkg,
+		PKG_DESC, &description,
+		PKG_FLATSIZE, &flatsize,
+		PKG_ORIGIN, &origin,
+		PKG_WWW, &www);
+
+	pk_backend_details(backend,
+			   id,
+			   license_name_from_pkg(pkg),
+			   group_from_origin(origin),
+			   description,
+			   www,
+			   flatsize);
 }
 
 /*
@@ -186,12 +111,14 @@
 	else
 		it = pkgdb_rquery(db, name, MATCH_EXACT, reponame);
 	if (it)
-		success = get_details_check_matches(it,
-						    name,
-						    version,
-						    arch,
-						    reponame,
-						    backend);
+		success = iterate_id_matches(it,
+					     backend,
+					     name,
+					     version,
+					     arch,
+					     reponame,
+					     LOAD_FLAGS,
+					     emit_get_details);
 	return success;
 }
 

Added: soc2013/mattbw/backend/iterate.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/iterate.c	Tue Jun 25 05:47:16 2013	(r253477)
@@ -0,0 +1,132 @@
+/*-
+ * Copyright (C) 2013 Matt Windsor <mattbw@FreeBSD.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <string.h>
+#include <glib.h>
+#include "pk-backend.h"
+#include "pkg.h"
+
+#include "iterate.h"		/* Prototypes */
+
+static gboolean
+try_id_match(struct pkg *pkg, const gchar *name, const gchar *version, const
+	     gchar *arch, const gchar *data, gchar **match_id);
+
+static gboolean	string_match(const char *left, const char *right);
+
+gboolean
+iterate_id_matches(struct pkgdb_it *iterator,
+		   PkBackend *backend,
+		   const gchar *name,
+		   const gchar *version,
+		   const gchar *arch,
+		   const gchar *data,
+		   int fetch_flags,
+		   iterate_f_pointer iterate_f)
+{
+	/* TODO: Filters */
+	gboolean	found;
+	int		err;
+	struct pkg     *pkg;
+	gchar          *match_id;
+
+	found = FALSE;
+	pkg = NULL;
+	match_id = NULL;
+	do {
+		err = pkgdb_it_next(iterator, &pkg, fetch_flags);
+
+		if (err == EPKG_OK && try_id_match(pkg,
+						   name,
+						   version,
+						   arch,
+						   data,
+						   &match_id) == TRUE) {
+			found = TRUE;
+			iterate_f(pkg, match_id, backend);
+		}
+		if (match_id != NULL)
+			g_free(match_id);
+	} while (err == EPKG_OK && found == FALSE);
+	pkg_free(pkg);
+
+	return found;
+}
+
+static gboolean
+try_id_match(struct pkg *pkg, const gchar *name, const gchar *version, const
+	     gchar *arch, const gchar *data, gchar **match_id)
+{
+	const char     *pkg_arch;
+	const char     *pkg_data;
+	const char     *pkg_name;
+	const char     *pkg_reponame;
+	const char     *pkg_version;
+
+	pkg_get(pkg,
+		PKG_ARCH, &pkg_arch,
+		PKG_NAME, &pkg_name,
+		PKG_REPONAME, &pkg_reponame,
+		PKG_VERSION, &pkg_version);
+
+	switch (pkg_type(pkg)) {
+	case PKG_FILE:
+		pkg_data = "local";
+		break;
+	case PKG_INSTALLED:
+		pkg_data = "installed";
+		break;
+	default:
+		pkg_data = pkg_reponame;
+		break;
+	}
+
+	if (*match_id != NULL)
+		g_free(*match_id);
+	*match_id = pk_package_id_build(pkg_name, pkg_version, pkg_arch, pkg_data);
+
+	/*
+	 * Succeed if this package's PackageID fields match the original
+	 * PackageID.  Of course, the original ID might have missing fields
+	 * (NULLs), so we treat a comparison involving one as a success.
+	 */
+	return (string_match(name, pkg_name) &&
+		string_match(version, pkg_version) &&
+		string_match(arch, pkg_arch) &&
+		string_match(data, pkg_data)) ? TRUE : FALSE;
+}
+
+/*
+ * Checks two strings with strcmp and emits TRUE if they match. If either
+ * string is NULL, emit TRUE as well (this is so that missing PackageID
+ * elements trigger matches).
+ */
+static gboolean
+string_match(const char *left, const char *right)
+{
+	int		result;
+
+	if (left == NULL || right == NULL)
+		result = TRUE;
+	else
+		result = (strcmp(left, right) == 0 ? TRUE : FALSE);
+
+	return result;
+}

Added: soc2013/mattbw/backend/iterate.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/iterate.h	Tue Jun 25 05:47:16 2013	(r253477)
@@ -0,0 +1,42 @@
+/*-
+ * Copyright (C) 2013 Matt Windsor <mattbw@FreeBSD.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _PKGNG_BACKEND_ITERATE_H_
+#define _PKGNG_BACKEND_ITERATE_H_
+
+#include <glib.h>
+#include "pk-backend.h"
+#include "pkg.h"
+
+typedef void    (*iterate_f_pointer) (struct pkg *pkg,
+				      		const		char  *id,
+				      		PkBackend    *backend);
+
+gboolean
+iterate_id_matches(struct pkgdb_it *iterator,
+		   PkBackend *backend,
+		   const gchar *name,
+		   const gchar *version,
+		   const gchar *arch,
+		   const gchar *data,
+		   int fetch_flags,
+		   iterate_f_pointer iterate_f);
+
+#endif				/* !_PKGNG_BACKEND_ITERATE_H_ */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201306250547.r5P5lGwG032110>