Skip to content

Commit

Permalink
[fixed #3] frontend specific locking logic moved from libpkg
Browse files Browse the repository at this point in the history
General global require_lock flag in libpkg to trigger DB locking.
Frontend sets this flag.
  • Loading branch information
scher committed Aug 7, 2012
1 parent 7a442b6 commit b82d38b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 53 deletions.
2 changes: 1 addition & 1 deletion libpkg/pkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct pkg_user;
struct pkg_group;
struct pkg_shlib;

extern const char * pkg_subcmd;
extern bool require_lock;
struct pkgdb;
struct pkgdb_it;

Expand Down
55 changes: 17 additions & 38 deletions libpkg/pkgdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,7 @@
#define PKGLT 1<<2
#define PKGEQ 1<<3

const char * pkg_subcmd = NULL;
const char * const subcmd_lockers[] = {
"add",
"audit",
"check",
"create",
"delete",
"fetch",
"install",
"register",
"remove",
"repo",
"set",
"update",
"upgrade"
};
const int subcmd_lockers_len = sizeof(subcmd_lockers)/sizeof(subcmd_lockers[0]);
bool require_lock = false;

static struct pkgdb_it * pkgdb_it_new(struct pkgdb *, sqlite3_stmt *, int);
static void pkgdb_regex(sqlite3_context *, int, sqlite3_value **, int);
Expand Down Expand Up @@ -737,8 +721,8 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
assert(!(*db_p)->locked);
reopen = true;
db = *db_p;
if (db->type == type)
goto lock_test;
if ( (db->type == type) && (require_lock) )
pkgdb_lock(db);
}

if (pkg_config_string(PKG_CONFIG_DBDIR, &dbdir) != EPKG_OK)
Expand Down Expand Up @@ -781,6 +765,15 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
return (EPKG_FATAL);
}

/* check if it is necessary to lock the database
and possibly locks the database */
if (require_lock) {
printf("Current command requires a lock!\nLocking database...\n");
pkgdb_lock(db);
printf("DB is locked\n");
getchar();
}

/* Wait up to 5 seconds if database is busy */
sqlite3_busy_timeout(db->sqlite, 5000);

Expand Down Expand Up @@ -845,18 +838,6 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)

*db_p = db;

lock_test:
/* check if it is necessary to lock the database
and possibly locks the database */
if (pkg_subcmd != NULL) {
if (req_lock(subcmd_lockers , subcmd_lockers_len, pkg_subcmd)) {
// printf("Sub command: %s requires a lock!\n", pkg_subcmd);
pkgdb_lock(db);
// printf("DB is locked\n");
// getchar();
}
}

return (EPKG_OK);
}

Expand All @@ -871,13 +852,11 @@ pkgdb_close(struct pkgdb *db)

/* check if it is necessary to UNlock the database
and possibly UNlocks the database */
if (pkg_subcmd != NULL) {
if (req_lock(subcmd_lockers , subcmd_lockers_len, pkg_subcmd)) {
// printf("Sub command: %s requires a UNlock!\n", pkg_subcmd);
assert(pkgdb_unlock(db) == EPKG_OK);
// printf("DB is UNlocked\n");
// getchar();
}
if (require_lock) {
printf("Current command requires an UNlock!\nUnLocking database...\n");
assert(pkgdb_unlock(db) == EPKG_OK);
printf("DB is UNlocked\n");
getchar();
}

if (db->sqlite != NULL) {
Expand Down
3 changes: 0 additions & 3 deletions libpkg/private/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ struct dns_srvinfo {
struct dns_srvinfo *next;
};

bool req_lock(const char * const commands[], const int commands_len,
const char * const cmd);

void sbuf_init(struct sbuf **);
int sbuf_set(struct sbuf **, const char *);
char * sbuf_get(struct sbuf *);
Expand Down
10 changes: 0 additions & 10 deletions libpkg/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@
#include "private/event.h"
#include "private/utils.h"

bool req_lock(const char * const commands[],const int commands_len ,
const char * const cmd)
{
for (int i = 0; i < commands_len; i++) {
if (strcmp(commands[i], cmd) == 0)
return true;
}
return false;
}

void
sbuf_init(struct sbuf **buf)
{
Expand Down
20 changes: 19 additions & 1 deletion pkg/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ static void usage_help(void);
static int exec_help(int, char **);
bool quiet = false;

const char * const subcmd_lockers[] = {
"add",
"audit",
"check",
"create",
"delete",
"fetch",
"install",
"register",
"remove",
"repo",
"set",
"update",
"upgrade"
};
const int subcmd_lockers_len = sizeof(subcmd_lockers)/sizeof(subcmd_lockers[0]);

static struct commands {
const char * const name;
const char * const desc;
Expand Down Expand Up @@ -309,7 +326,8 @@ main(int argc, char **argv)
return (ret); /* Not reached but makes scanbuild happy */
}

pkg_subcmd = command->name;
if (req_lock(subcmd_lockers , subcmd_lockers_len, command->name))
require_lock = true;

if (ambiguous <= 1) {
assert(command->exec != NULL);
Expand Down
3 changes: 3 additions & 0 deletions pkg/pkgcli.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,7 @@ int analyse_query_string(char *qstr, struct query_flags *q_flags,
const unsigned int q_flags_len, int *flags,
char *multiline);

bool req_lock(const char * const commands[], const int commands_len,
const char * const cmd);

#endif
10 changes: 10 additions & 0 deletions pkg/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@

#include "pkgcli.h"

bool req_lock(const char * const commands[],const int commands_len ,
const char * const cmd)
{
for (int i = 0; i < commands_len; i++) {
if (strcmp(commands[i], cmd) == 0)
return true;
}
return false;
}

bool
query_yesno(const char *msg, ...)
{
Expand Down

0 comments on commit b82d38b

Please sign in to comment.