Skip to content

Commit

Permalink
Lock the entire db in pkg_jobs.
Browse files Browse the repository at this point in the history
Because we dont want the state of the system to change between the time we
computed the dependencies/conflcits and install/delete time.
  • Loading branch information
jlaffaye committed Apr 22, 2012
1 parent 7fe7d74 commit fda1cbe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
32 changes: 23 additions & 9 deletions libpkg/pkg_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
assert(db != NULL);
assert(t != PKG_JOBS_INSTALL || db->type == PKGDB_REMOTE);

if (pkgdb_lock(db) != EPKG_OK)
return (EPKG_FATAL);

if((*j = calloc(1, sizeof(struct pkg_jobs))) == NULL) {
pkg_emit_errno("calloc", "pkg_jobs");
return (EPKG_FATAL);
Expand All @@ -69,6 +72,8 @@ pkg_jobs_free(struct pkg_jobs *j)
if (j == NULL)
return;

pkgdb_unlock(j->db);

while (!STAILQ_EMPTY(&j->jobs)) {
p = STAILQ_FIRST(&j->jobs);
STAILQ_REMOVE_HEAD(&j->jobs, next);
Expand Down Expand Up @@ -295,15 +300,24 @@ pkg_jobs_deinstall(struct pkg_jobs *j, int force)
int
pkg_jobs_apply(struct pkg_jobs *j, int force)
{
if (j->type == PKG_JOBS_INSTALL)
return (pkg_jobs_install(j, force));
if (j->type == PKG_JOBS_DEINSTALL)
return (pkg_jobs_deinstall(j, force));
if (j->type == PKG_JOBS_FETCH)
return (pkg_jobs_fetch(j));

pkg_emit_error("bad jobs argument");
return (EPKG_FATAL);
int rc;

switch (j->type) {
case PKG_JOBS_INSTALL:
rc = pkg_jobs_install(j, force);
break;
case PKG_JOBS_DEINSTALL:
rc = pkg_jobs_deinstall(j, force);
break;
case PKG_JOBS_FETCH:
rc = pkg_jobs_fetch(j);
break;
default:
rc = EPKG_FATAL;
pkg_emit_error("bad jobs argument");
}

return (rc);
}

static int
Expand Down
12 changes: 12 additions & 0 deletions libpkg/pkgdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3208,3 +3208,15 @@ pkgshell_open(const char **reponame)
snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);
*reponame = strdup(localpath);
}

int
pkgdb_lock(struct pkgdb *db)
{
return sql_exec(db->sqlite, "PRAGMA main.locking_mode=EXCLUSIVE;BEGIN IMMEDIATE;COMMIT;");
}

int
pkgdb_unlock(struct pkgdb *db)
{
return sql_exec(db->sqlite, "PRAGMA main.locking_mode=NORMAL;BEGIN IMMEDIATE;COMMIT;");
}
3 changes: 3 additions & 0 deletions libpkg/private/pkgdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ struct pkgdb_it {
int type;
};

int pkgdb_lock(struct pkgdb *db);
int pkgdb_unlock(struct pkgdb *db);

void pkgshell_open(const char **r);
#endif

0 comments on commit fda1cbe

Please sign in to comment.