socsvn commit: r255636 - soc2013/mattbw/backend

mattbw at FreeBSD.org mattbw at FreeBSD.org
Wed Aug 7 11:02:28 UTC 2013


Author: mattbw
Date: Wed Aug  7 11:02:28 2013
New Revision: 255636
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=255636

Log:
  More splitting up of functions in jobs.c.
  
  Some functions are starting to look more or less maintainable now, for
  example jobs_do_same_repo.
  
  However, jobs.c is now getting large enough where I'm contemplating
  splitting it along the lines of query.c.  I'm currently thinking a
  split of (check, do, core), and there might be some functions that would
  work better in pkgutils.
  

Modified:
  soc2013/mattbw/backend/jobs.c

Modified: soc2013/mattbw/backend/jobs.c
==============================================================================
--- soc2013/mattbw/backend/jobs.c	Wed Aug  7 10:15:54 2013	(r255635)
+++ soc2013/mattbw/backend/jobs.c	Wed Aug  7 11:02:28 2013	(r255636)
@@ -35,14 +35,18 @@
 
 static bool jobs_check_id_on_package(struct pkg *pkg, gchar **split_id, const char *name, const char *version);
 static bool jobs_check_split_package_ids(struct pkg_jobs *jobs, gchar ***id_splits, guint count, bool reject_non_updates);
+static bool jobs_do_apply(const struct jobs_spec *spec, struct pkg_jobs *jobs);
 static bool jobs_do_check(const struct jobs_spec *spec, struct pkg_jobs *jobs, gchar **package_ids, guint count);
 static bool jobs_do_multiple_repos(struct pkgdb *db, const struct jobs_spec *spec, gchar **package_ids, guint count);
-static bool jobs_do_set_repo(const struct jobs_spec *spec, struct pkg_jobs *jobs, const char *reponame);
 static bool jobs_do_same_repo(struct pkgdb *db, const struct jobs_spec *spec, gchar **package_ids, guint count, const char *reponame);
+static bool jobs_do_set_repo(const struct jobs_spec *spec, struct pkg_jobs *jobs, const char *reponame);
+static bool jobs_do_solve(const struct jobs_spec *spec, struct pkg_jobs *jobs);
 static char **jobs_do_populate(const struct jobs_spec *spec, struct pkg_jobs *jobs, gchar **package_ids, guint count);
 static char **namevers_from_package_ids(gchar **package_ids, guint count);
 static struct pkg_jobs *jobs_do_allocate(const struct jobs_spec *spec, struct pkgdb *db);
+static struct pkg_jobs *jobs_do_initialise(const struct jobs_spec *spec, struct pkgdb *db, const char *reponame);
 static void free_namevers(char ***namevers_p, guint count);
+static void jobs_free(struct pkg_jobs **jobs_p);
 
 /* Applies a job with the given error enums and the standard event callback. */
 bool
@@ -291,59 +295,46 @@
 jobs_do_same_repo(struct pkgdb *db, const struct jobs_spec *spec,
     gchar **package_ids, guint count, const char *reponame)
 {
-	bool		check_success;
-	bool		repo_success;
 	bool		success;
 	struct pkg_jobs *jobs;
-	char	      **namevers;
 
 	assert(db != NULL);
 	assert(spec != NULL);
 	assert(package_ids != NULL || 0 == count);
 	assert(package_ids == NULL || 0 < count);
 
-	success = NULL;
-	namevers = NULL;
-
-	jobs = jobs_do_allocate(spec, db);
-	if (jobs == NULL)
-		goto cleanup;
+	success = false;
 
-	repo_success = jobs_do_set_repo(spec, jobs, reponame);
-	if (!repo_success)
-		goto cleanup;
+	jobs = jobs_do_initialise(spec, db, reponame);
+	if (jobs != NULL) {
+		bool		populated;
+		bool		solved;
+		bool		valid;
+		char	      **namevers;
 
-	namevers = jobs_do_populate(spec, jobs, package_ids, count);
-	if (package_ids != NULL && namevers == NULL)
-		goto cleanup;
+		solved = valid = false;
 
-	if (pkg_jobs_solve(jobs) != EPKG_OK) {
-		ERR(spec->backend,
-		    PK_ERROR_ENUM_DEP_RESOLUTION_FAILED,
-		    "could not solve the job");
-		goto cleanup;
-	}	
+		namevers = jobs_do_populate(spec, jobs, package_ids, count);
+		populated = (package_ids == NULL || namevers != NULL);
 
-	check_success = jobs_do_check(spec, jobs, package_ids, count);
-	if (!check_success)
-		goto cleanup;
+		if (populated) {
+			solved = jobs_do_solve(spec, jobs);
+		}
 
-	STATUS(spec->backend, spec->status);
+		if (populated && solved) {
+			valid = jobs_do_check(spec, jobs, package_ids, count);
+		}
 
-	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);
+		if (populated && solved && valid) {
+			success = jobs_do_apply(spec, jobs);
+		}
 
-cleanup:
-	pkg_jobs_free(jobs);
-	free_namevers(&namevers, count);
+		pkg_jobs_free(jobs);
+		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
@@ -392,6 +383,28 @@
 }
 
 static bool
+jobs_do_apply(const struct jobs_spec *spec, struct pkg_jobs *jobs)
+{
+	bool success;
+
+	assert(spec != NULL);
+	assert(jobs != NULL);
+
+	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,
+		    spec->no_jobs_error,
+		    spec->jobs_failed_error);
+	}
+
+	return success;
+}
+
+static bool
 jobs_do_check(const struct jobs_spec *spec, struct pkg_jobs *jobs,
     gchar **package_ids, guint count)
 {
@@ -458,6 +471,25 @@
 	return success;
 }
 
+static bool
+jobs_do_solve(const struct jobs_spec *spec, struct pkg_jobs *jobs)
+{
+	bool		solved;
+	int		err;
+
+	err = pkg_jobs_solve(jobs);
+	if (err == EPKG_OK) {
+		solved = true;
+	} else {
+		solved = false;
+		ERR(spec->backend,
+		    PK_ERROR_ENUM_DEP_RESOLUTION_FAILED,
+		    "could not solve the job");
+	}	
+
+	return solved;
+}
+
 static char **
 jobs_do_populate(const struct jobs_spec *spec, struct pkg_jobs *jobs,
     gchar **package_ids, guint count)
@@ -486,6 +518,29 @@
 	return namevers;
 }
 
+static struct pkg_jobs *
+jobs_do_initialise(const struct jobs_spec *spec, struct pkgdb *db,
+    const char *reponame)
+{
+	struct pkg_jobs *jobs;
+
+	assert(spec != NULL);
+	assert(db != NULL);
+	/* reponame may be NULL */
+
+	jobs = jobs_do_allocate(spec, db);
+	if (jobs != NULL) {
+		bool		repo_success;
+
+		repo_success = jobs_do_set_repo(spec, jobs, reponame);
+		if (!repo_success) {
+			jobs_free(&jobs);
+		}
+	}
+
+	return jobs;
+}
+
 static void
 free_namevers(char ***namevers_p, guint count)
 {
@@ -501,6 +556,16 @@
 	}
 }
 
+/* Overly safe wrapper around pkg_jobs_free. */
+void
+jobs_free(struct pkg_jobs **jobs_p)
+{
+
+	assert(jobs_p != NULL);
+	free(*jobs_p);
+	*jobs_p = NULL;
+}
+
 static char **
 namevers_from_package_ids(gchar **package_ids, guint count)
 {


More information about the svn-soc-all mailing list