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

mattbw at FreeBSD.org mattbw at FreeBSD.org
Sun Jul 28 22:05:51 UTC 2013


Author: mattbw
Date: Sun Jul 28 22:05:50 2013
New Revision: 255285
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=255285

Log:
  Complete and use new-style job processing boilerplate.
  
  Jobs now go entirely through 'job.c' without touching 'query.c'.
  UpdatePackages has been implemented, but not yet properly tested, and will
  likely need some more work.
  
  There do seem to be some teething problems with this, which I'll hopefully
  fix on Monday/Tuesday or at the start of next half-term.  Resolution seems
  to be a bit broken, for instance.
  
  The old-style jobs system code is still intact and will be pulled down
  later.
  

Modified:
  soc2013/mattbw/backend/Makefile
  soc2013/mattbw/backend/actions.h
  soc2013/mattbw/backend/actions/install_packages.c
  soc2013/mattbw/backend/actions/remove_packages.c
  soc2013/mattbw/backend/actions/update_packages.c
  soc2013/mattbw/backend/actions/update_system.c
  soc2013/mattbw/backend/jobs.c
  soc2013/mattbw/backend/jobs.h
  soc2013/mattbw/backend/pk-backend-pkgng.c

Modified: soc2013/mattbw/backend/Makefile
==============================================================================
--- soc2013/mattbw/backend/Makefile	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/Makefile	Sun Jul 28 22:05:50 2013	(r255285)
@@ -45,6 +45,7 @@
 		actions/search_files.c		\
 		actions/search_groups.c		\
 		actions/search_names.c		\
+		actions/update_packages.c	\
 		actions/update_system.c
 
 SRCS+=						\

Modified: soc2013/mattbw/backend/actions.h
==============================================================================
--- soc2013/mattbw/backend/actions.h	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/actions.h	Sun Jul 28 22:05:50 2013	(r255285)
@@ -43,6 +43,8 @@
 gboolean	simulate_install_files_thread(PkBackend *backend);
 gboolean	simulate_install_packages_thread(PkBackend *backend);
 gboolean	simulate_remove_packages_thread(PkBackend *backend);
+gboolean	simulate_update_packages_thread(PkBackend *backend);
+gboolean	update_packages_thread(PkBackend *backend);
 gboolean	update_system_thread(PkBackend *backend);
 
 #endif				/* !_PKGNG_BACKEND_ACTIONS_H_ */

Modified: soc2013/mattbw/backend/actions/install_packages.c
==============================================================================
--- soc2013/mattbw/backend/actions/install_packages.c	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/actions/install_packages.c	Sun Jul 28 22:05:50 2013	(r255285)
@@ -25,11 +25,10 @@
 #include "pkg.h"		/* pkg... */
 
 #include "../actions.h"		/* install_packages_thread prototype */
+#include "../jobs.h"		/* jobs_... */
 #include "../pkgutils.h"	/* pkgutils_... */
-#include "../query.h"		/* query_... */
 
-static bool	job(struct pkg_jobs *jobs, struct query *q);
-static bool	sim_job(struct pkg_jobs *jobs, struct query *q);
+static gboolean	install_packages(PkBackend *backend, bool simulate);
 
 /*
  * The thread that performs an InstallPackages operation. Should be invoked
@@ -38,63 +37,42 @@
 gboolean
 install_packages_thread(PkBackend *backend)
 {
-	bool		success;
-
-	assert(backend != NULL);
 
-	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
-	success = query_match_id_to_job(backend, PKG_JOBS_INSTALL, job);
-
-	(void)pk_backend_finished(backend);
-	return success ? TRUE : FALSE;
+	return install_packages(backend, false);
 }
 
 /*
- * The thread that performs a Simulate InstallPackages operation. Should be
+ * The thread that performs a SimulateInstallPackages operation. Should be
  * invoked by the pk_backend_simulate_install_packages hook.
  */
 gboolean
 simulate_install_packages_thread(PkBackend *backend)
 {
-	bool		success;
-
-	assert(backend != NULL);
 
-	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
-	success = query_match_id_to_job(backend, PKG_JOBS_INSTALL, sim_job);
-
-	(void)pk_backend_finished(backend);
-	return success ? TRUE : FALSE;
+	return install_packages(backend, true);
 }
 
-/*
- * Tries to process the given solved installation jobs.
- */
-static bool
-job(struct pkg_jobs *jobs, struct query *q)
+static gboolean
+install_packages(PkBackend *backend, bool simulate)
 {
+	struct jobs_spec spec;
+	bool		success;
 
-	assert(jobs != NULL);
-	assert(q != NULL);
+	assert(backend != NULL);
 
-	return query_jobs_apply_emitter(jobs,
-	    q,
-	    PK_STATUS_ENUM_INSTALL,
-	    PK_ERROR_ENUM_PACKAGE_ALREADY_INSTALLED,
-	    PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL);
-}
+	spec.backend = backend;
+	spec.info = pkgutils_pkg_install_state;
+	spec.jobs_failed_error = PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL;
+	spec.no_jobs_error = PK_ERROR_ENUM_PACKAGE_ALREADY_INSTALLED;
+	spec.reject_non_updates = false;
+	spec.simulate = simulate;
+	spec.status = PK_STATUS_ENUM_INSTALL;
+	spec.type = PKG_JOBS_INSTALL;
+	spec.use_package_ids = true;
 
-/*
- * Tries to simulate processing the given installation jobs.
- */
-static bool
-sim_job(struct pkg_jobs *jobs, struct query *q)
-{
-
-	assert(jobs != NULL);
-	assert(q != NULL);
+	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
+	success = jobs_do(&spec);
+	(void)pk_backend_finished(backend);
 
-	return query_jobs_simulate_emitter(jobs,
-	    q,
-	    pkgutils_pkg_install_state);
+	return success ? TRUE : FALSE;
 }

Modified: soc2013/mattbw/backend/actions/remove_packages.c
==============================================================================
--- soc2013/mattbw/backend/actions/remove_packages.c	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/actions/remove_packages.c	Sun Jul 28 22:05:50 2013	(r255285)
@@ -25,11 +25,10 @@
 #include "pkg.h"		/* pkg... */
 
 #include "../actions.h"		/* remove_packages_thread prototype */
+#include "../jobs.h"		/* jobs... */
 #include "../pkgutils.h"	/* pkgutils_... */
-#include "../query.h"		/* query_... */
 
-static bool	job (struct pkg_jobs *jobs, struct query *q);
-static bool	sim_job(struct pkg_jobs *jobs, struct query *q);
+static gboolean	remove_packages(PkBackend *backend, bool simulate);
 
 /*
  * The thread that performs an removePackages operation. Should be invoked
@@ -38,63 +37,42 @@
 gboolean
 remove_packages_thread(PkBackend *backend)
 {
-	bool		success;
-
-	assert(backend != NULL);
 
-	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
-	success = query_match_id_to_job(backend, PKG_JOBS_DEINSTALL, job);
-
-	(void)pk_backend_finished(backend);
-	return success ? TRUE : FALSE;
+	return remove_packages(backend, false);
 }
 
 /*
- * The thread that performs a Simulate RemovePackages operation. Should be
+ * The thread that performs a SimulateRemovePackages operation. Should be
  * invoked by the pk_backend_simulate_remove_packages hook.
  */
 gboolean
 simulate_remove_packages_thread(PkBackend *backend)
 {
-	bool		success;
-
-	assert(backend != NULL);
 
-	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
-	success = query_match_id_to_job(backend, PKG_JOBS_DEINSTALL, sim_job);
-
-	(void)pk_backend_finished(backend);
-	return success ? TRUE : FALSE;
+	return remove_packages(backend, true);
 }
 
-/*
- * Tries to process the given solved removeation jobs.
- */
-static bool
-job(struct pkg_jobs *jobs, struct query *q)
+static gboolean
+remove_packages(PkBackend *backend, bool simulate)
 {
+	struct jobs_spec spec;
+	bool		success;
 
-	assert(jobs != NULL);
-	assert(q != NULL);
+	assert(backend != NULL);
 
-	return query_jobs_apply_emitter(jobs,
-	    q,
-	    PK_STATUS_ENUM_REMOVE,
-	    PK_ERROR_ENUM_PACKAGE_NOT_INSTALLED,
-	    PK_ERROR_ENUM_PACKAGE_FAILED_TO_REMOVE);
-}
+	spec.backend = backend;
+	spec.info = pkgutils_pkg_remove_state;
+	spec.jobs_failed_error = PK_ERROR_ENUM_PACKAGE_FAILED_TO_REMOVE;
+	spec.no_jobs_error = PK_ERROR_ENUM_PACKAGE_NOT_INSTALLED;
+	spec.reject_non_updates = false;
+	spec.simulate = simulate;
+	spec.status = PK_STATUS_ENUM_REMOVE;
+	spec.type = PKG_JOBS_DEINSTALL;
+	spec.use_package_ids = true;
 
-/*
- * Tries to simulate processing the given removal jobs.
- */
-static bool
-sim_job(struct pkg_jobs *jobs, struct query *q)
-{
-
-	assert(jobs != NULL);
-	assert(q != NULL);
+	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
+	success = jobs_do(&spec);
+	(void)pk_backend_finished(backend);
 
-	return query_jobs_simulate_emitter(jobs,
-	    q,
-	    pkgutils_pkg_remove_state);
+	return success ? TRUE : FALSE;
 }

Modified: soc2013/mattbw/backend/actions/update_packages.c
==============================================================================
--- soc2013/mattbw/backend/actions/update_packages.c	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/actions/update_packages.c	Sun Jul 28 22:05:50 2013	(r255285)
@@ -25,76 +25,53 @@
 #include "pkg.h"		/* pkg... */
 
 #include "../actions.h"		/* update_packages_thread prototype */
+#include "../jobs.h"		/* jobs_... */
 #include "../pkgutils.h"	/* pkgutils_... */
-#include "../query.h"		/* query_... */
 
-static bool	job(struct pkg_jobs *jobs, struct query *q);
-static bool	sim_job(struct pkg_jobs *jobs, struct query *q);
+static gboolean	update_packages(PkBackend *backend, bool simulate);
 
 /*
- * The thread that performs an UpdatePackages operation. Should be invoked
+ * The thread that performs an InstallPackages operation. Should be invoked
  * by the pk_backend_update_packages hook.
  */
 gboolean
 update_packages_thread(PkBackend *backend)
 {
-	bool		success;
-
-	assert(backend != NULL);
 
-	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
-	success = query_match_id_to_job(backend, PKG_JOBS_INSTALL, job);
-
-	(void)pk_backend_finished(backend);
-	return success ? TRUE : FALSE;
+	return update_packages(backend, false);
 }
 
 /*
- * The thread that performs a SimulateUpdatePackages operation. Should be
+ * The thread that performs a SimulateInstallPackages operation. Should be
  * invoked by the pk_backend_simulate_update_packages hook.
  */
 gboolean
 simulate_update_packages_thread(PkBackend *backend)
 {
-	bool		success;
-
-	assert(backend != NULL);
 
-	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
-	success = query_match_id_to_job(backend, PKG_JOBS_UPGRADE, sim_job);
-
-	(void)pk_backend_finished(backend);
-	return success ? TRUE : FALSE;
+	return update_packages(backend, true);
 }
 
-/*
- * Tries to process the given solved installation jobs.
- */
-static bool
-job(struct pkg_jobs *jobs, struct query *q)
+static gboolean
+update_packages(PkBackend *backend, bool simulate)
 {
+	struct jobs_spec spec;
+	bool		success;
 
-	assert(jobs != NULL);
-	assert(q != NULL);
+	assert(backend != NULL);
 
-	return query_jobs_apply_emitter(jobs,
-	    q,
-	    PK_STATUS_ENUM_UPDATE,
-	    PK_ERROR_ENUM_NO_PACKAGES_TO_UPDATE,
-	    PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL);
-}
+	spec.backend = backend;
+	spec.info = pkgutils_pkg_install_state;
+	spec.jobs_failed_error = PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL;
+	spec.no_jobs_error = PK_ERROR_ENUM_NO_PACKAGES_TO_UPDATE;
+	spec.reject_non_updates = true;
+	spec.simulate = simulate;
+	spec.type = PKG_JOBS_INSTALL;
+	spec.use_package_ids = true;
 
-/*
- * Tries to simulate processing the given installation jobs.
- */
-static bool
-sim_job(struct pkg_jobs *jobs, struct query *q)
-{
-
-	assert(jobs != NULL);
-	assert(q != NULL);
+	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
+	success = jobs_do(&spec);
+	(void)pk_backend_finished(backend);
 
-	return query_jobs_simulate_emitter(jobs,
-	    q,
-	    pkgutils_pkg_install_state);
+	return success ? TRUE : FALSE;
 }

Modified: soc2013/mattbw/backend/actions/update_system.c
==============================================================================
--- soc2013/mattbw/backend/actions/update_system.c	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/actions/update_system.c	Sun Jul 28 22:05:50 2013	(r255285)
@@ -25,10 +25,8 @@
 #include "pkg.h"		/* pkg... */
 
 #include "../actions.h"		/* update_system_thread prototype */
-#include "../db.h"		/* db_open_remote */
-#include "../utils.h"		/* ERR */
-#include "../pkgutils.h"	/* pkgutils_... */
 #include "../jobs.h"		/* jobs_... */
+#include "../pkgutils.h"	/* pkgutils_... */
 
 /*
  * The thread that performs an UpdateSystem operation. Should be invoked
@@ -37,42 +35,24 @@
 gboolean
 update_system_thread(PkBackend *backend)
 {
+	struct jobs_spec spec;
 	bool		success;
-	struct pkgdb   *db;
-	struct pkg_jobs *jobs;
 
 	assert(backend != NULL);
 
-	success = false;
+	spec.backend = backend;
+	spec.info = pkgutils_pkg_install_state;
+	spec.jobs_failed_error = PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL;
+	spec.no_jobs_error = PK_ERROR_ENUM_NO_PACKAGES_TO_UPDATE;
+	spec.reject_non_updates = false;
+	spec.simulate = false;
+	spec.status = PK_STATUS_ENUM_UPDATE;
+	spec.type = PKG_JOBS_UPGRADE;
+	spec.use_package_ids = false;
 
-	db = db_open_remote(backend);
-	if (db == NULL)
-		goto cleanup;
-
-	/* UpdateSystem is a job with no packages, so there's no need to use
-	 * the full query approach.
-	 */
-	jobs = NULL;
-	if (pkg_jobs_new(&jobs, PKG_JOBS_UPGRADE, db) != EPKG_OK) {
-		ERR(backend,
-		    PK_ERROR_ENUM_INTERNAL_ERROR,
-		    "could not init pkg_jobs");
-		goto cleanup;
-	}
-	if (pkg_jobs_solve(jobs) != EPKG_OK) {
-		ERR(backend,
-		    PK_ERROR_ENUM_DEP_RESOLUTION_FAILED,
-		    "could not solve the job");
-		goto cleanup;
-	}	
-
-	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_UPDATE);
-
-	success = jobs_apply(jobs, backend,
-	    PK_ERROR_ENUM_NO_PACKAGES_TO_UPDATE,
-	    PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL);
-
-cleanup:
+	(void)pk_backend_set_status(backend, PK_STATUS_ENUM_QUERY);
+	success = jobs_do(&spec);
 	(void)pk_backend_finished(backend);
+
 	return success ? TRUE : FALSE;
 }

Modified: soc2013/mattbw/backend/jobs.c
==============================================================================
--- soc2013/mattbw/backend/jobs.c	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/jobs.c	Sun Jul 28 22:05:50 2013	(r255285)
@@ -27,10 +27,13 @@
 #include "pk-backend.h"		/* pk_..., Pk... */
 #include "pkg.h"		/* pkg_... */
 
+#include "db.h"			/* db_... */
 #include "event.h"		/* event_cb */
 #include "utils.h"		/* ERR */
-#include "pkgutils.h"		/* pkgutils_... */
 #include "jobs.h"		/* jobs_... */
+#include "pkgutils.h"		/* pkgutils_... */
+
+static void free_namevers(char ***namev_p, unsigned int namec);
 
 /* Applies a job with the given error enums and the standard event callback. */
 bool
@@ -140,6 +143,91 @@
 	return success;
 }
 
+/* Performs a job from a job specification. */
+bool
+jobs_do(const struct jobs_spec *spec)
+{
+	bool		success;
+	unsigned int	count;
+	gchar	      **package_ids;
+	struct pkgdb   *db;
+	struct pkg_jobs *jobs;
+	char	      **namevers;
+
+	assert(spec != NULL);
+	assert(spec->backend != NULL);
+	assert(spec->info != NULL);
+
+	success = false;
+	namevers = NULL;
+	jobs = NULL;
+
+	package_ids = NULL;
+	if (spec->use_package_ids)
+		package_ids = pk_backend_get_strv(spec->backend, "package_ids");
+
+	count = 0;
+	if (package_ids != NULL)
+		count = g_strv_length(package_ids);
+
+	db = db_open_remote(spec->backend);
+	if (db == NULL)
+		goto cleanup;
+
+	/* UpdateSystem is a job with no packages, so there's no need to use
+	 * the full query approach.
+	 */
+	jobs = NULL;
+	if (pkg_jobs_new(&jobs, spec->type, db) != EPKG_OK) {
+		ERR(spec->backend,
+		    PK_ERROR_ENUM_INTERNAL_ERROR,
+		    "could not init pkg_jobs");
+		goto cleanup;
+	}
+	if (package_ids != NULL) {
+		STATUS(spec->backend, PK_STATUS_ENUM_DEP_RESOLVE);
+
+		namevers = jobs_add_package_ids(jobs, package_ids);
+		if (namevers == NULL) {
+			ERR(spec->backend,
+			    PK_ERROR_ENUM_DEP_RESOLUTION_FAILED,
+			    "could not find all requested packages");
+			goto cleanup;
+		}
+	}
+	if (pkg_jobs_solve(jobs) != EPKG_OK) {
+		ERR(spec->backend,
+		    PK_ERROR_ENUM_DEP_RESOLUTION_FAILED,
+		    "could not solve the job");
+		goto cleanup;
+	}	
+	if (package_ids != NULL && 
+	    (jobs_check_package_ids(jobs, package_ids,
+	    spec->reject_non_updates) == false)) {
+		ERR(spec->backend,
+		    PK_ERROR_ENUM_DEP_RESOLUTION_FAILED,
+		    "packages failed suitability check");
+		goto cleanup;
+	}
+
+	STATUS(spec->backend, spec->status);
+
+	if (spec->simulate) {
+		success = true;
+		jobs_emit_packages(jobs, spec->backend, spec->info);
+	} else
+		success = jobs_apply(jobs, spec->backend,
+		    PK_ERROR_ENUM_NO_PACKAGES_TO_UPDATE,
+		    PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL);
+
+cleanup:
+	pkg_jobs_free(jobs);
+	pkgdb_close(db);
+	free_namevers(&namevers, count);
+
+	return success;
+} 
+
 /* Adds each PackageID into an already created job.  Returns NULL on failure
  * and a vector of added package name-versions to be freed after solution
  * on success.
@@ -163,13 +251,13 @@
 		goto cleanup;
 
 	/* Need to convert PackageIDs into name-version pairs. */
-	namevers = calloc(count, sizeof(gchar *));
+	namevers = calloc(count, sizeof(char *));
 	if (namevers == NULL)
 		goto cleanup;
 
 	for (i = 0; i < count; i++) {
 		namevers[i] = pkgutils_package_id_namever(package_ids[i]);
-		if (namevers[i] != NULL)	
+		if (namevers[i] == NULL)	
 			break;
 	}
 
@@ -182,12 +270,8 @@
 
 cleanup:
 	if (!success) {
-		if (namevers != NULL) {
-			for (i = 0; i < count; i++)
-				free(namevers[i]);
-			free(namevers);
-		}
-		namevers = NULL;
+		if (namevers != NULL)
+			free_namevers(&namevers, count);
 	}
 
 	return namevers;
@@ -211,3 +295,17 @@
  	}
 }
 
+static void
+free_namevers(char ***namev_p, unsigned int namec)
+{
+	unsigned int		i;
+
+	assert(namev_p != NULL);
+
+	if (*namev_p != NULL) {
+		for (i = 0; i < namec; i++)
+			free(*namev_p[i]);
+		free(*namev_p);
+		*namev_p = NULL;
+	}
+}

Modified: soc2013/mattbw/backend/jobs.h
==============================================================================
--- soc2013/mattbw/backend/jobs.h	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/jobs.h	Sun Jul 28 22:05:50 2013	(r255285)
@@ -26,8 +26,22 @@
 
 typedef		PkInfoEnum (*pkg_info_ptr) (struct pkg *pkg);
 
-bool		jobs_apply(struct pkg_jobs *jobs, PkBackend *backend, PkErrorEnum no_jobs, PkErrorEnum job_failed);
+struct jobs_spec {
+	pkg_jobs_t	type;
+	PkErrorEnum	jobs_failed_error;
+	PkErrorEnum	no_jobs_error;
+	PkStatusEnum	status;
+	PkBackend      *backend;
+	pkg_info_ptr	info;
+	bool		reject_non_updates;
+	bool		simulate;
+	bool		use_package_ids;
+	bool		ignore;			/* Alignment */
+};
+
+bool		jobs_apply(struct pkg_jobs *jobs, PkBackend *backend, PkErrorEnum no_jobs, PkErrorEnum jobs_failed);
 bool		jobs_check_package_ids(struct pkg_jobs *jobs, gchar **package_ids, bool reject_non_updates);
+bool		jobs_do(const struct jobs_spec *spec);
 char	      **jobs_add_package_ids(struct pkg_jobs *jobs, gchar **package_ids);
 void		jobs_emit_packages(struct pkg_jobs *jobs, PkBackend *backend, pkg_info_ptr info);
 

Modified: soc2013/mattbw/backend/pk-backend-pkgng.c
==============================================================================
--- soc2013/mattbw/backend/pk-backend-pkgng.c	Sun Jul 28 20:11:31 2013	(r255284)
+++ soc2013/mattbw/backend/pk-backend-pkgng.c	Sun Jul 28 22:05:50 2013	(r255285)
@@ -270,8 +270,6 @@
 	THREAD(backend, simulate_remove_packages_thread);
 }
 
-#if 0
-
 void
 pk_backend_simulate_update_packages(PkBackend *backend, gchar **package_ids)
 {
@@ -292,8 +290,6 @@
 	THREAD(backend, update_packages_thread);
 }
 
-#endif
-
 void
 pk_backend_update_system(PkBackend *backend, gboolean only_trusted)
 {


More information about the svn-soc-all mailing list