Date: Sat, 13 Jul 2013 07:45:26 GMT From: mattbw@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r254742 - in soc2013/mattbw: . backend backend/actions backend/query Message-ID: <201307130745.r6D7jQR2096787@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: mattbw Date: Sat Jul 13 07:45:25 2013 New Revision: 254742 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=254742 Log: refactor jobs query code Added: soc2013/mattbw/backend/query/jobs.c soc2013/mattbw/backend/query/jobs.h Modified: soc2013/mattbw/ (props changed) soc2013/mattbw/README soc2013/mattbw/backend/ (props changed) soc2013/mattbw/backend/Makefile soc2013/mattbw/backend/actions/ (props changed) soc2013/mattbw/backend/actions/install_packages.c soc2013/mattbw/backend/db.h soc2013/mattbw/backend/query/ (props changed) soc2013/mattbw/backend/query/core.c soc2013/mattbw/backend/query/core.h Modified: soc2013/mattbw/README ============================================================================== --- soc2013/mattbw/README Sat Jul 13 04:25:03 2013 (r254741) +++ soc2013/mattbw/README Sat Jul 13 07:45:25 2013 (r254742) @@ -17,9 +17,8 @@ - pkgng (currently building against version 1.1.3; higher versions might work) - FreeBSD (tested with 9.1; should work with higher versions) -Tested primarily with clang in C99 mode; C89 mode likely will not work and C11 -would not work due to header file problems. GNU extensions probably won't break -the build. +Tested primarily with clang in C11 mode; C89 mode likely will not work and C99 +likely would need GNU/clang extensions. -------------------------------------------------------------------------------- Modified: soc2013/mattbw/backend/Makefile ============================================================================== --- soc2013/mattbw/backend/Makefile Sat Jul 13 04:25:03 2013 (r254741) +++ soc2013/mattbw/backend/Makefile Sat Jul 13 07:45:25 2013 (r254742) @@ -25,6 +25,7 @@ SRCS+= \ query/core.c \ query/do.c \ + query/jobs.c \ query/match.c LIBDIR= /usr/local/lib/packagekit-backend Modified: soc2013/mattbw/backend/actions/install_packages.c ============================================================================== --- soc2013/mattbw/backend/actions/install_packages.c Sat Jul 13 04:25:03 2013 (r254741) +++ soc2013/mattbw/backend/actions/install_packages.c Sat Jul 13 07:45:25 2013 (r254742) @@ -31,7 +31,6 @@ static bool job (struct pkg_jobs *jobs, struct query *q); static bool sim_job(struct pkg_jobs *jobs, struct query *q); -static bool solve_job(struct query *q, struct pkg_jobs *jobs); /* * The thread that performs an InstallPackages operation. Should be invoked @@ -78,21 +77,20 @@ backend = query_backend(q); query_set_percentage(q, 0); - if (solve_job(q, jobs) == false) - goto cleanup; - pkg_event_register(event_cb, backend); (void)pk_backend_set_status(backend, PK_STATUS_ENUM_INSTALL); - if (pkg_jobs_apply(jobs) != EPKG_OK) { + if (pkg_jobs_count(jobs) == 0) + ERR(backend, + PK_ERROR_ENUM_PACKAGE_ALREADY_INSTALLED, + "nothing to do"); + else if (pkg_jobs_apply(jobs) != EPKG_OK) ERR(backend, PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL, "job failed"); - goto cleanup; - } - success = true; + else + success = true; -cleanup: pkg_event_register(NULL, NULL); query_set_percentage(q, 100); return success; @@ -109,12 +107,8 @@ struct pkg *pkg; backend = query_backend(q); - success = false; query_set_percentage(q, 0); - if (solve_job(q, jobs) == false) - goto cleanup; - (void)pk_backend_set_status(backend, PK_STATUS_ENUM_RUNNING); pkg = NULL; while (pkg_jobs(jobs, &pkg) == EPKG_OK) @@ -124,34 +118,6 @@ success = true; -cleanup: query_set_percentage(q, 100); return success; } - -/* - * Solves a job and ensures it has packages available. - */ -static bool -solve_job(struct query *q, struct pkg_jobs *jobs) -{ - bool success; - PkBackend *backend; - - success = false; - backend = query_backend(q); - - (void)pk_backend_set_status(backend, PK_STATUS_ENUM_DEP_RESOLVE); - if (pkg_jobs_solve(jobs) != EPKG_OK) - ERR(backend, - PK_ERROR_ENUM_DEP_RESOLUTION_FAILED, - "could not solve the job"); - else if (pkg_jobs_count(jobs) == 0) - ERR(backend, - PK_ERROR_ENUM_INTERNAL_ERROR, - "job contains no packages"); - else - success = true; - - return success; -} Modified: soc2013/mattbw/backend/db.h ============================================================================== --- soc2013/mattbw/backend/db.h Sat Jul 13 04:25:03 2013 (r254741) +++ soc2013/mattbw/backend/db.h Sat Jul 13 07:45:25 2013 (r254742) @@ -1,5 +1,4 @@ /*- - * * Copyright (C) 2013 Matt Windsor <mattbw@FreeBSD.org> * * Licensed under the GNU General Public License Version 2 Modified: soc2013/mattbw/backend/query/core.c ============================================================================== --- soc2013/mattbw/backend/query/core.c Sat Jul 13 04:25:03 2013 (r254741) +++ soc2013/mattbw/backend/query/core.c Sat Jul 13 07:45:25 2013 (r254742) @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include <assert.h> /* assert */ #include <stdbool.h> #include <string.h> #include <glib.h> @@ -29,6 +30,7 @@ #include "../utils.h" /* string_match */ #include "../pkgutils.h" /* pkgutils_... */ #include "core.h" /* Prototypes */ +#include "jobs.h" /* query_jobs...*/ enum repo_type { REPO_INVALID, @@ -50,16 +52,26 @@ }; static bool can_remote_iterate(struct query *q); -static bool emit_to_job(struct pkg *pkg, struct query *q); -static const gchar *query_repo(struct query *q); +static bool emit(struct pkg *pkg, const char *match_id, struct query *q); static enum repo_type type_of_repo_name(const char *name); static gchar *match_pkg(struct pkg *pkg, struct query *q); static gchar **init_unpack_source(PkBackend *backend, struct query_source *s); -static int jobs_add_pkg(struct pkg_jobs *jobs, match_t type, struct pkg *pkg); -static int jobs_repo_from_query(struct pkg_jobs *jobs, struct query *q); static struct pkg *match_iterator(struct pkgdb_it *it, struct query *q, gchar **match_id_p); /* + * Returns the backend stored inside the given query. + */ +PkBackend * +query_backend(struct query *q) +{ + + assert (q != NULL); + assert (q->backend != NULL); + + return q->backend; +} + +/* * Runs an assembled query. */ bool @@ -77,6 +89,8 @@ struct pkgdb *db; struct pkgdb_it *it; + assert(q != NULL); + success = false; match = MATCH_EXACT; match_id = NULL; @@ -120,12 +134,8 @@ ERR(q->backend, PK_ERROR_ENUM_PACKAGE_NOT_FOUND, "package not found"); - else { - if (q->t->type == QUERY_EMIT) - success = q->t->emit.f(pkg, match_id, q); - else if (q->t->type == QUERY_JOB) - success = emit_to_job(pkg, q); - } + else + emit(pkg, match_id, q); pkg_free(pkg); g_free(match_id); @@ -133,12 +143,31 @@ return success; } -/* Returns the backend stored inside the struct query. */ -PkBackend * -query_backend(struct query *q) +/* + * Retrieves the repository for the query, or NULL if none is specified. + */ +const char * +query_repo(struct query *q) +{ + const gchar *repo; + + assert(q != NULL); + + repo = q->id_strv[PK_PACKAGE_ID_DATA]; + return ((repo == NULL || repo[0] == '\0') ? NULL : repo); +} + +/* + * Returns the database stored inside the given query. + */ +struct pkgdb * +query_db(struct query *q) { - return (q == NULL ? NULL : q->backend); + assert(q != NULL); + assert(q->db != NULL); + + return q->db; } /* @@ -155,6 +184,11 @@ bool success; struct query *q; + assert(backend != NULL); + assert(db != NULL); + assert(s != NULL); + assert(t != NULL); + success = false; q = calloc(1, sizeof(struct query)); @@ -217,6 +251,8 @@ { guint scaled_percent; + assert(q != NULL); + if (percent == PK_BACKEND_PERCENTAGE_INVALID) scaled_percent = PK_BACKEND_PERCENTAGE_INVALID; else { @@ -239,6 +275,8 @@ { bool sane; + assert(q != NULL); + /* Innocent until proven guilty */ sane = true; @@ -257,49 +295,32 @@ } /* - * For adapting an emitter function into one that solves and applies a job. + * Sends a query result to the appropriate emitter. */ static bool -emit_to_job(struct pkg *pkg, struct query *q) +emit(struct pkg *pkg, const char *match_id, struct query *q) { bool success; - struct pkg_jobs *jobs; - PkBackend *backend; - - success = false; - jobs = NULL; - - if (q == NULL) - goto cleanup; - backend = query_backend(q); - - if (pkg_jobs_new(&jobs, q->t->job.type, q->db) != EPKG_OK) { - ERR(backend, - PK_ERROR_ENUM_INTERNAL_ERROR, - "could not init pkg_jobs"); - goto cleanup; - } - if (jobs_repo_from_query(jobs, q) != EPKG_OK) { - ERR(backend, - PK_ERROR_ENUM_REPO_NOT_FOUND, - "could not set repo"); - goto cleanup; - } - if (jobs_add_pkg(jobs, MATCH_EXACT, pkg) != EPKG_OK) { - ERR(backend, - PK_ERROR_ENUM_INTERNAL_ERROR, - "could not add to job"); - goto cleanup; + assert(match_id != NULL); + assert(pkg != NULL); + assert(q != NULL); + + switch(q->t->type) { + case QUERY_EMIT: + success = q->t->emit.f(pkg, match_id, q); + break; + case QUERY_JOB: + success = query_jobs_run(q, pkg, q->t->job.type, q->t->job.f); + break; } - success = q->t->job.f(jobs, q); -cleanup: - pkg_jobs_free(jobs); return success; } -/* Finds the type of the given PackageKit repository name. */ +/* + * Finds the type of the given PackageKit repository name. + */ static enum repo_type type_of_repo_name(const char *name) { @@ -330,6 +351,9 @@ gchar *match_id; const char **pkg_id_bits; + assert(pkg != NULL); + assert(q != NULL); + pkg_id_bits = calloc(4, sizeof(const char *)); match_id = pkgutils_pkg_to_id_through(pkg, pkg_id_bits); @@ -361,43 +385,6 @@ return match_id; } -/* Retrieves the repository for the query, or NULL if none is specified. */ -static const gchar * -query_repo(struct query *q) -{ - const gchar *repo; - - repo = q->id_strv[PK_PACKAGE_ID_DATA]; - return ((repo == NULL || repo[0] == '\0') ? NULL : repo); -} - -/* Adds a single package to a jobs structure. */ -static int -jobs_add_pkg(struct pkg_jobs *jobs, match_t type, struct pkg *pkg) -{ - char *name; - - name = NULL; - pkg_get(pkg, PKG_NAME, &name); - return pkg_jobs_add(jobs, type, &name, 1); -} - -/* Sets a jobset to target the repository the given query is looking in. */ -static int -jobs_repo_from_query(struct pkg_jobs *jobs, struct query *q) -{ - int err; - gchar *repo; - - repo = q->id_strv[PK_PACKAGE_ID_DATA]; - if (repo == NULL || repo[0] == '\0') - err = EPKG_OK; - else - err = pkg_jobs_set_repository(jobs, repo); - - return err; -} - /* * Tries to find a query-matching package in a database iterator. Returns the * package if one matches, or NULL; if match_id_p is non-null, its full @@ -410,10 +397,11 @@ gchar *match_id; struct pkg *pkg; - if (q->t->type == QUERY_EMIT) - load_flags = q->t->emit.load_flags; - else - load_flags = PKG_LOAD_BASIC; + assert(it != NULL); + assert(q != NULL); + + load_flags = (q->t->type == QUERY_EMIT) ? + q->t->emit.load_flags : PKG_LOAD_BASIC; match_id = NULL; pkg = NULL; Modified: soc2013/mattbw/backend/query/core.h ============================================================================== --- soc2013/mattbw/backend/query/core.h Sat Jul 13 04:25:03 2013 (r254741) +++ soc2013/mattbw/backend/query/core.h Sat Jul 13 07:45:25 2013 (r254742) @@ -72,9 +72,10 @@ }; PkBackend *query_backend(struct query *q); -bool query_run (struct query *q); +bool query_run(struct query *q); +const char *query_repo(struct query *q); +struct pkgdb *query_db(struct query *q); struct query *query_init(PkBackend *backend, struct pkgdb *db, struct query_source *s, struct query_target *t); void query_free(struct query **q_p); void query_set_percentage(struct query *q, unsigned char percent); - #endif /* !_PKGNG_BACKEND_QUERY_CORE_H_ */ Added: soc2013/mattbw/backend/query/jobs.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2013/mattbw/backend/query/jobs.c Sat Jul 13 07:45:25 2013 (r254742) @@ -0,0 +1,106 @@ +/*- + * 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 <assert.h> /* assert */ +#include <stdbool.h> /* bool, true, false */ +#include "../pk-backend.h" /* pk..., Pk... */ +#include "pkg.h" /* pkg... */ + +#include "../utils.h" /* ERR */ +#include "core.h" /* query_... */ +#include "jobs.h" /* query_jobs_... */ + +static int add_pkg(struct pkg_jobs *jobs, match_t type, struct pkg *pkg); +static int repo_from_query(struct pkg_jobs *jobs, struct query *q); + +/* + * Creates a job of type "type" for the given package, solves it, and sends it + * to the emitter function "f" alongside its parent query. + */ +bool +query_jobs_run(struct query *q, struct pkg *pkg, pkg_jobs_t type, + job_emit_ptr f) +{ + bool success; + struct pkg_jobs *jobs; + PkBackend *backend; + + assert (q != NULL); + assert (pkg != NULL); + assert (f != NULL); + + success = false; + jobs = NULL; + + backend = query_backend(q); + + if (pkg_jobs_new(&jobs, type, query_db(q)) != EPKG_OK) { + ERR(backend, + PK_ERROR_ENUM_INTERNAL_ERROR, + "could not init pkg_jobs"); + goto cleanup; + } + if (repo_from_query(jobs, q) != EPKG_OK) { + ERR(backend, + PK_ERROR_ENUM_REPO_NOT_FOUND, + "could not set repo"); + goto cleanup; + } + if (add_pkg(jobs, MATCH_EXACT, pkg) != EPKG_OK) { + ERR(backend, + PK_ERROR_ENUM_INTERNAL_ERROR, + "could not add to job"); + goto cleanup; + } + if (pkg_jobs_solve(jobs) != EPKG_OK) { + ERR(backend, + PK_ERROR_ENUM_DEP_RESOLUTION_FAILED, + "could not solve the job"); + goto cleanup; + } + + success = f(jobs, q); + +cleanup: + pkg_jobs_free(jobs); + return success; +} + +/* Adds a single package to a jobs structure. */ +static int +add_pkg(struct pkg_jobs *jobs, match_t type, struct pkg *pkg) +{ + char *name; + + name = NULL; + pkg_get(pkg, PKG_NAME, &name); + return pkg_jobs_add(jobs, type, &name, 1); +} + +/* Sets a jobset to target the repository the given query is looking in. */ +static int +repo_from_query(struct pkg_jobs *jobs, struct query *q) +{ + const char *repo; + + repo = query_repo(q); + + return (repo == NULL ? EPKG_OK : pkg_jobs_set_repository(jobs, repo)); +} Added: soc2013/mattbw/backend/query/jobs.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2013/mattbw/backend/query/jobs.h Sat Jul 13 07:45:25 2013 (r254742) @@ -0,0 +1,30 @@ +/*- + * 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_QUERY_JOBS_H_ +#define _PKGNG_BACKEND_QUERY_JOBS_H_ + +#include "pkg.h" /* struct pkg */ + +#include "core.h" /* struct query */ + +bool query_jobs_run(struct query *q, struct pkg *pkg, pkg_jobs_t type, job_emit_ptr f); + +#endif /* !_PKGNG_BACKEND_QUERY_JOBS_H_ */
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201307130745.r6D7jQR2096787>