socsvn commit: r254230 - in soc2013/mattbw/backend: . actions

mattbw at FreeBSD.org mattbw at FreeBSD.org
Sat Jul 6 09:03:23 UTC 2013


Author: mattbw
Date: Sat Jul  6 09:03:22 2013
New Revision: 254230
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=254230

Log:
  split off package utility functions; properly determine info enum from package; for some silly reason this seems to be failing to unlock the database

Added:
  soc2013/mattbw/backend/pkgutils.c
  soc2013/mattbw/backend/pkgutils.h
Modified:
  soc2013/mattbw/backend/Makefile
  soc2013/mattbw/backend/actions/install-packages.c
  soc2013/mattbw/backend/db.c
  soc2013/mattbw/backend/query.c

Modified: soc2013/mattbw/backend/Makefile
==============================================================================
--- soc2013/mattbw/backend/Makefile	Sat Jul  6 08:59:27 2013	(r254229)
+++ soc2013/mattbw/backend/Makefile	Sat Jul  6 09:03:22 2013	(r254230)
@@ -2,8 +2,16 @@
 
 LIB=		pk_backend_pkgng
 SHLIB_MAJOR=	1
-SRCS=		pk-backend-pkgng.c groups.c db.c licenses.c query.c utils.c
-SRCS+=		actions/get-details.c		\
+SRCS=		pk-backend-pkgng.c
+SRCS+= 						\
+		db.c				\
+		groups.c			\
+		licenses.c			\
+		pkgutils.c			\
+		query.c				\
+		utils.c
+SRCS+=						\
+		actions/get-details.c		\
 		actions/get-files.c		\
 		actions/get-repo-list.c		\
 		actions/install-packages.c

Modified: soc2013/mattbw/backend/actions/install-packages.c
==============================================================================
--- soc2013/mattbw/backend/actions/install-packages.c	Sat Jul  6 08:59:27 2013	(r254229)
+++ soc2013/mattbw/backend/actions/install-packages.c	Sat Jul  6 09:03:22 2013	(r254230)
@@ -23,6 +23,7 @@
 #include "pkg.h"
 
 #include "../hash_traverse.h"	/* HASH_FOR */
+#include "../pkgutils.h"	/* pkgutils_... */
 #include "../query.h"		/* Package querying */
 #include "../utils.h"		/* INTENTIONALLY_IGNORE */
 
@@ -90,39 +91,28 @@
 
 	pk_backend_set_status(backend, PK_STATUS_ENUM_DEP_RESOLVE);
 	err = pkg_jobs_solve(jobs);
-	if (err == EPKG_OK) {
-		char           *desc;
-		gchar          *id;
-		struct pkg     *pkg;
-
-		pkg = NULL;
-		id = NULL;
-		if (pkg_jobs_count(jobs) == 0)
-			pk_backend_error_code(backend,
-					      PK_ERROR_ENUM_INTERNAL_ERROR,
-					      "job contains no packages");
-		else {
-			while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
-				pkg_get(pkg, PKG_COMMENT, &desc);
-				query_pkg_to_id(pkg, &id);
-				pk_backend_package(backend,
-						   PK_INFO_ENUM_INSTALLING,
-						   id,
-						   desc);
-			}
-			g_free(id);
-		}
-	} else
+	if (err != EPKG_OK)
 		pk_backend_error_code(backend,
 				      PK_ERROR_ENUM_DEP_RESOLUTION_FAILED,
 				      "could not solve the job");
-
-	/* TODO: actual install */
-	if (err == EPKG_OK && simulate == FALSE)
+	else if (pkg_jobs_count(jobs) == 0)
 		pk_backend_error_code(backend,
-				      PK_ERROR_ENUM_NOT_SUPPORTED,
-				      "not implemented yet");
+				      PK_ERROR_ENUM_INTERNAL_ERROR,
+				      "job contains no packages");
+	else {
+		struct pkg     *pkg;
+
+		pkg = NULL;
+		while (pkg_jobs(jobs, &pkg) == EPKG_OK)
+			pkgutils_emit(pkg,
+				      backend,
+				      pkgutils_pkg_install_state(pkg));
 
+		if (simulate == FALSE)
+			pk_backend_error_code(backend,
+					      PK_ERROR_ENUM_NOT_SUPPORTED,
+					      "not implemented yet");
+	}
 }
 
 /*

Modified: soc2013/mattbw/backend/db.c
==============================================================================
--- soc2013/mattbw/backend/db.c	Sat Jul  6 08:59:27 2013	(r254229)
+++ soc2013/mattbw/backend/db.c	Sat Jul  6 09:03:22 2013	(r254230)
@@ -47,24 +47,28 @@
 
 	success = FALSE;
 
+	pk_backend_set_status(backend, PK_STATUS_ENUM_WAITING_FOR_AUTH);
 	access_return = pkgdb_access(PKGDB_MODE_READ | PKGDB_MODE_WRITE,
 				     PKGDB_DB_LOCAL | PKGDB_DB_REPO);
 	if (access_return != EPKG_OK)
 		pk_backend_error_code(backend,
-				      PK_ERROR_ENUM_INTERNAL_ERROR,
+				      PK_ERROR_ENUM_NOT_AUTHORIZED,
 				      "cannot access database");
 	else {
+		pk_backend_set_status(backend, PK_STATUS_ENUM_WAITING_FOR_LOCK);
 		open_return = pkgdb_open(db, PKGDB_REMOTE);
 		if (open_return != EPKG_OK)
 			pk_backend_error_code(backend,
-					      PK_ERROR_ENUM_INTERNAL_ERROR,
-					    "pkgdb_open returned an error");
+					      PK_ERROR_ENUM_CANNOT_GET_LOCK,
+					      "cannot open database");
 		else if (*db == NULL)
 			pk_backend_error_code(backend,
 					      PK_ERROR_ENUM_INTERNAL_ERROR,
 				       "pkgdb_open gave us a null pointer");
-		else
+		else {
+			pk_backend_set_status(backend, PK_STATUS_ENUM_RUNNING);
 			success = TRUE;
+		}
 	}
 
 	return success;

Added: soc2013/mattbw/backend/pkgutils.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/pkgutils.c	Sat Jul  6 09:03:22 2013	(r254230)
@@ -0,0 +1,182 @@
+/*-
+ * Copyright (C) 2013 Matt Windsor <mattbw at 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 "pkgutils.h"		/* Prototypes */
+
+static const char *repo_of_remote(struct pkg *pkg);
+
+/*
+ * Infers the correct PkInfoEnum to emit for this package if it is being
+ * installed, mainly from its current installation information.
+ */
+PkInfoEnum
+pkgutils_pkg_install_state(struct pkg *pkg)
+{
+	PkInfoEnum	info;
+	const char     *old;
+
+	/* Sane default */
+	info = PK_INFO_ENUM_INSTALLING;
+
+	/* Does this package already have a version installed? */
+	old = NULL;
+	pkg_get(pkg, PKG_OLD_VERSION, &old);
+
+	if (old != NULL) {
+		/* Yes, but how does it compare to the new one? */
+		switch (pkg_version_change(pkg)) {
+		case PKG_DOWNGRADE:
+			info = PK_INFO_ENUM_DOWNGRADING;
+			break;
+		case PKG_REINSTALL:
+			info = PK_INFO_ENUM_REINSTALLING;
+			break;
+		case PKG_UPGRADE:
+			info = PK_INFO_ENUM_UPDATING;
+			break;
+		default:
+			/* Stick with the above default. */
+			break;
+		}
+	}
+	return info;
+}
+
+/*
+ * Gets the PackageKit repository name for the package.
+ */
+const char     *
+pkgutils_pk_repo_of(struct pkg *pkg)
+{
+	const char     *repo;
+
+	switch (pkg_type(pkg)) {
+	case PKG_FILE:
+		repo = "local";
+		break;
+	case PKG_INSTALLED:
+		repo = "installed";
+		break;
+	case PKG_REMOTE:
+		repo = repo_of_remote(pkg);
+		break;
+	default:
+		repo = "unknown";
+		break;
+	}
+
+	return repo;
+}
+
+/*
+ * Emits a package through the backend with the given info enum.
+ */
+void
+pkgutils_emit(struct pkg *pkg, PkBackend *backend, PkInfoEnum info)
+{
+	char           *comment;
+	gchar          *id;
+
+	pkg_get(pkg, PKG_COMMENT, &comment);
+	id = pkgutils_pkg_to_id(pkg);
+	pk_backend_package(backend, info, id, comment);
+	g_free(id);
+}
+
+/*
+ * Converts a package to a PackageID.
+ */
+gchar          *
+pkgutils_pkg_to_id(struct pkg *pkg)
+{
+	gchar          *id;
+	const gchar   **id_bits;
+
+	/* Make sure the initial string vector's pointers are all NULL */
+	id_bits = g_new0(const gchar *, 4);
+
+	/*
+	 * This is split through an intermediate function so the same code
+	 * can be used for the ID-checking logic.
+	 */
+	id = pkgutils_pkg_to_id_through(pkg, id_bits);
+
+	/* we don't own any of the strings, so we don't use g_strfreev. */
+	g_free(id_bits);
+
+	return id;
+}
+
+/*
+ * Converts a package to a PackageID, dumping the intermediate information
+ * into the pointed-to string vector (which must have at least 4 places, and
+ * they should be NULL).
+ */
+gchar          *
+pkgutils_pkg_to_id_through(struct pkg *pkg, const gchar **strv)
+{
+	pkg_get(pkg,
+		PKG_NAME, strv + PK_PACKAGE_ID_NAME,
+		PKG_VERSION, strv + PK_PACKAGE_ID_VERSION,
+		PKG_ARCH, strv + PK_PACKAGE_ID_ARCH);
+
+	strv[PK_PACKAGE_ID_DATA] = pkgutils_pk_repo_of(pkg);
+
+	return pk_package_id_build(strv[PK_PACKAGE_ID_NAME],
+				   strv[PK_PACKAGE_ID_VERSION],
+				   strv[PK_PACKAGE_ID_ARCH],
+				   strv[PK_PACKAGE_ID_DATA]);
+}
+
+/*
+ * Gets the PackageKit repository name for the (remote) package.
+ * 
+ * Currently this is actually the pkgng repository ident.  This might change.
+ * 
+ * This does not need to be freed (possibly, TODO: check).
+ */
+static const char *
+repo_of_remote(struct pkg *pkg)
+{
+	const char     *repo;
+	const char     *repo_name;
+	struct pkg_repo *repo_struct;
+
+	repo = NULL;
+
+	/*
+	 * We can get the repo NAME directly, but we need the repo IDENT.
+	 * Short of chopping bits of the string off in the assumption that
+	 * the name is repo-IDENT, we'll have to grab it from the repo
+	 * structure itself.
+	 */
+	pkg_get(pkg, PKG_REPONAME, &repo_name);
+
+	repo_struct = pkg_repo_find_name(repo_name);
+	if (repo_struct)
+		repo = pkg_repo_ident(repo_struct);
+
+	return repo;
+}

Added: soc2013/mattbw/backend/pkgutils.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/mattbw/backend/pkgutils.h	Sat Jul  6 09:03:22 2013	(r254230)
@@ -0,0 +1,34 @@
+/*-
+ * Copyright (C) 2013 Matt Windsor <mattbw at 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_PKGUTILS_H_
+#define _PKGNG_BACKEND_PKGUTILS_H_
+
+#include <glib.h>
+#include "pk-backend.h"
+#include "pkg.h"
+
+PkInfoEnum	pkgutils_pkg_install_state(struct pkg *pkg);
+const char     *pkgutils_pk_repo_of(struct pkg *pkg);
+gchar          *pkgutils_pkg_to_id(struct pkg *pkg);
+gchar          *pkgutils_pkg_to_id_through(struct pkg *pkg, const gchar **strv);
+void		pkgutils_emit(struct pkg *pkg, PkBackend *backend, PkInfoEnum info);
+
+#endif				/* !_PKGNG_BACKEND_PKGUTILS_H_ */

Modified: soc2013/mattbw/backend/query.c
==============================================================================
--- soc2013/mattbw/backend/query.c	Sat Jul  6 08:59:27 2013	(r254229)
+++ soc2013/mattbw/backend/query.c	Sat Jul  6 09:03:22 2013	(r254230)
@@ -28,6 +28,7 @@
 #include "db.h"			/* open_remote_db */
 #include "hash_traverse.h"	/* HASH_FOR */
 #include "utils.h"		/* string_match */
+#include "pkgutils.h"		/* pkgutils_... */
 
 struct query {
 	int		load_flags;
@@ -43,14 +44,11 @@
 	gboolean	local_repo;
 };
 
-static const char *get_repo_of(struct pkg *pkg);
-static const char *get_repo_of_remote(struct pkg *pkg);
 static gboolean	match(struct query *q, gchar **match_id_p, struct pkg **match_pkg_p);
 static gboolean	match_id_in_it(struct pkgdb_it *it, struct query *q, gchar **match_id_p, struct pkg **match_pkg_p);
 static gboolean	try_id_match(struct pkg *pkg, struct query *q, gchar **match_id);
 static int	jobs_add_pkg(struct pkg_jobs *jobs, match_t type, struct pkg *pkg);
 static void	query_free_contents(struct query *q);
-static void	query_pkg_to_id_through(struct pkg *pkg, gchar **name_p, const gchar **strv);
 
 /* Returns the backend stored inside the struct query. */
 PkBackend      *
@@ -105,6 +103,7 @@
 
 	db = NULL;
 	q = NULL;
+
 	no_error_yet = open_remote_db(&db, backend);
 
 	pk_backend_set_percentage(backend, 0);
@@ -118,6 +117,7 @@
 			no_error_yet = body(q);
 		pk_backend_set_percentage(backend, ((i * 100) / len));
 	}
+	query_free(q);
 	pkgdb_close(db);
 
 	pk_backend_finished(backend);
@@ -169,7 +169,7 @@
 			q->local_repo = TRUE;
 		else if (pkg_repo_find_ident(q->data) == NULL) {
 			pk_backend_error_code(backend,
-					   PK_ERROR_ENUM_PACKAGE_ID_INVALID,
+					   PK_ERROR_ENUM_REPO_NOT_FOUND,
 					      "no such repository");
 			success = FALSE;
 		}
@@ -229,7 +229,6 @@
 				      "could not init pkg_jobs");
 
 	pkg_jobs_free(jobs);
-
 }
 
 /* Deallocates a struct query and any contents it owns. */
@@ -242,80 +241,6 @@
 	}
 }
 
-/* Converts a package to a PackageID. */
-void
-query_pkg_to_id(struct pkg *pkg, gchar **id_p)
-{
-	const gchar   **id_bits;
-
-	/* Make sure the initial string vector's pointers are all NULL */
-	id_bits = g_new0(const gchar *, 4);
-
-	/*
-	 * This is split through an intermediate function so the same code
-	 * can be used for the ID-checking logic.
-	 */
-	query_pkg_to_id_through(pkg, id_p, id_bits);
-
-	/* we don't own any of the strings, so we don't use g_strfreev. */
-	g_free(id_bits);
-}
-
-/* Gets the PackageKit repository name for the package. */
-static const char *
-get_repo_of(struct pkg *pkg)
-{
-	const char     *repo;
-
-	switch (pkg_type(pkg)) {
-	case PKG_FILE:
-		repo = "local";
-		break;
-	case PKG_INSTALLED:
-		repo = "installed";
-		break;
-	case PKG_REMOTE:
-		repo = get_repo_of_remote(pkg);
-		break;
-	default:
-		repo = "unknown";
-		break;
-	}
-
-	return repo;
-}
-
-/*
- * Gets the PackageKit repository name for the (remote) package.
- * 
- * Currently this is actually the pkgng repository ident.  This might change.
- * 
- * This does not need to be freed (possibly, TODO: check).
- */
-static const char *
-get_repo_of_remote(struct pkg *pkg)
-{
-	const char     *repo;
-	const char     *repo_name;
-	struct pkg_repo *repo_struct;
-
-	repo = NULL;
-
-	/*
-	 * We can get the repo NAME directly, but we need the repo IDENT.
-	 * Short of chopping bits of the string off in the assumption that
-	 * the name is repo-IDENT, we'll have to grab it from the repo
-	 * structure itself.
-	 */
-	pkg_get(pkg, PKG_REPONAME, &repo_name);
-
-	repo_struct = pkg_repo_find_name(repo_name);
-	if (repo_struct)
-		repo = pkg_repo_ident(repo_struct);
-
-	return repo;
-}
-
 /*
  * Performs a package database query against a (potentially partial)
  * PackageID, retrieving the matched package and its full PackageID.
@@ -403,7 +328,9 @@
 	const gchar   **pkg_id_bits;
 
 	pkg_id_bits = g_new0(const gchar *, 4);
-	query_pkg_to_id_through(pkg, match_id, pkg_id_bits);
+
+	g_free(*match_id);
+	*match_id = pkgutils_pkg_to_id_through(pkg, pkg_id_bits);
 
 	/*
 	 * Succeed if this package's PackageID fields match the original
@@ -432,30 +359,6 @@
 	return pkg_jobs_add(jobs, type, &name, 1);
 }
 
-/*
- * Converts a package to a PackageID, dumping the intermediate information
- * into the pointed-to string vector (which must have at least 4 places, and
- * they should be NULL).
- */
-static void
-query_pkg_to_id_through(struct pkg *pkg, gchar **id_p, const gchar **strv)
-{
-
-	pkg_get(pkg,
-		PKG_NAME, strv + PK_PACKAGE_ID_NAME,
-		PKG_VERSION, strv + PK_PACKAGE_ID_VERSION,
-		PKG_ARCH, strv + PK_PACKAGE_ID_ARCH);
-
-	strv[PK_PACKAGE_ID_DATA] = get_repo_of(pkg);
-
-	if (*id_p != NULL)
-		g_free(*id_p);
-	*id_p = pk_package_id_build(strv[PK_PACKAGE_ID_NAME],
-				    strv[PK_PACKAGE_ID_VERSION],
-				    strv[PK_PACKAGE_ID_ARCH],
-				    strv[PK_PACKAGE_ID_DATA]);
-}
-
 static void
 query_free_contents(struct query *q)
 {


More information about the svn-soc-all mailing list