Skip to content

Commit

Permalink
Ensure database mtimes are updated correctly.
Browse files Browse the repository at this point in the history
Previously the database was updated as soon as it was determined that an
update was required, but if the update failed for any reason then pkgin
was left in a broken state thinking it was up-to-date but with no data,
requiring a "pkgin -f update" to fix.

Move the database updates to immediately after the local pkgdb or remote
repository has been inserted.
  • Loading branch information
Jonathan Perkin committed Jun 13, 2019
1 parent a6a57cc commit 01d5b0c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
3 changes: 2 additions & 1 deletion pkgin.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ int pkgindb_doquery(const char *,
int pkgindb_dovaquery(const char *, ...);
int pkgindb_open(void);
void pkgindb_close(void);
int pkg_db_mtime(void);
int pkg_db_mtime(struct stat *);
void pkg_db_update_mtime(struct stat *);
void repo_record(char **);
time_t pkg_sum_mtime(char *);
void pkgindb_stats(void);
Expand Down
28 changes: 17 additions & 11 deletions pkgindb.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,15 @@ pkgindb_sqlfail(void)
}

int
pkg_db_mtime(void)
pkg_db_mtime(struct stat *st)
{
sqlite3_stmt *stmt;
struct stat st;
time_t db_mtime;
long db_ntime;
int rc;

/* No pkgdb, just return up-to-date so we can start installing. */
if (stat(pkgdb_get_dir(), &st) < 0)
if (stat(pkgdb_get_dir(), st) < 0)
return 0;

/*
Expand Down Expand Up @@ -321,30 +320,37 @@ pkg_db_mtime(void)
sqlite3_finalize(stmt);

/* Databases matched pkgdb, we're up-to-date */
if (db_mtime == st.st_mtime && db_ntime == st.pkgin_nanotime)
if (db_mtime == st->st_mtime && db_ntime == st->pkgin_nanotime)
return 0;

/*
* Update database to current mtime and request a refresh.
*/
/* We're not up to date, request a refresh. */
return 1;
}

/*
* Update database to current mtime after insertion.
*/
void
pkg_db_update_mtime(struct stat *st)
{
sqlite3_stmt *stmt;

curquery = "INSERT INTO PKGDB (PKGDB_MTIME, PKGDB_NTIME) "
"VALUES (?, ?);";

if (sqlite3_prepare_v2(pdb, curquery, -1, &stmt, NULL) != SQLITE_OK)
pkgindb_sqlfail();

if (sqlite3_bind_int64(stmt, 1, st.st_mtime) != SQLITE_OK)
if (sqlite3_bind_int64(stmt, 1, st->st_mtime) != SQLITE_OK)
pkgindb_sqlfail();

if (sqlite3_bind_int64(stmt, 2, st.pkgin_nanotime) != SQLITE_OK)
if (sqlite3_bind_int64(stmt, 2, st->pkgin_nanotime) != SQLITE_OK)
pkgindb_sqlfail();

if (sqlite3_step(stmt) != SQLITE_DONE)
pkgindb_sqlfail();

sqlite3_finalize(stmt);

return 1;
}

void
Expand Down
26 changes: 15 additions & 11 deletions summary.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ typedef struct Insertlist {

SLIST_HEAD(, Insertlist) inserthead;

static struct archive *fetch_summary(char *url);
static struct archive *fetch_summary(char *, time_t *);
static void freecols(void);
static void free_insertlist(void);
static void insert_local_summary(FILE *);
Expand All @@ -97,36 +97,33 @@ static const char *const sumexts[] = { "xz", "bz2", "gz", NULL };
* Open a remote summary and return an open libarchive handler to it.
*/
static struct archive *
fetch_summary(char *cur_repo)
fetch_summary(char *cur_repo, time_t *repo_mtime)
{
struct archive *a;
struct archive_entry *ae;
Sumfile *sum = NULL;
time_t sum_mtime;
int i;
char buf[BUFSIZ];

for (i = 0; sumexts[i] != NULL; i++) { /* try all extensions */
if (!force_fetch)
sum_mtime = pkg_sum_mtime(cur_repo);
*repo_mtime = pkg_sum_mtime(cur_repo);
else
sum_mtime = 0; /* 0 sumtime == force reload */
*repo_mtime = 0; /* 0 sumtime == force reload */

snprintf(buf, BUFSIZ, "%s/%s.%s",
cur_repo, PKG_SUMMARY, sumexts[i]);

if ((sum = sum_open(buf, &sum_mtime)) != NULL)
if ((sum = sum_open(buf, repo_mtime)) != NULL)
break; /* pkg_summary found and not up-to-date */

if (sum_mtime < 0) /* pkg_summary found, but up-to-date */
if (*repo_mtime < 0) /* pkg_summary found, but up-to-date */
return NULL;
}

if (sum == NULL)
errx(EXIT_FAILURE, MSG_COULDNT_FETCH, buf);

pkgindb_dovaquery(UPDATE_REPO_MTIME, (long long)sum_mtime, cur_repo);

if ((a = archive_read_new()) == NULL)
errx(EXIT_FAILURE, "Cannot initialise archive");

Expand Down Expand Up @@ -571,11 +568,12 @@ static void
update_localdb(char **pkgkeep)
{
FILE *pinfo;
struct stat st;
Plistnumbered *keeplisthead, *nokeeplisthead;
Pkglist *pkglist;

/* has the pkgdb (pkgsrc) changed ? if not, continue */
if (!pkg_db_mtime())
if (!pkg_db_mtime(&st))
return;

/* record the keep list */
Expand All @@ -592,6 +590,7 @@ update_localdb(char **pkgkeep)

/* insert the summary to the database */
insert_local_summary(pinfo);
pkg_db_update_mtime(&st);
pclose(pinfo);

/* re-read local packages list as it may have changed */
Expand Down Expand Up @@ -673,6 +672,7 @@ static void
update_remotedb(int verbose)
{
struct archive *a;
time_t repo_mtime;
char **prepos;
uint8_t cleaned = 0;

Expand All @@ -683,7 +683,7 @@ update_remotedb(int verbose)
printf(MSG_PROCESSING_REMOTE_SUMMARY, *prepos);

/* load remote pkg_summary */
if ((a = fetch_summary(*prepos)) == NULL) {
if ((a = fetch_summary(*prepos, &repo_mtime)) == NULL) {
if (verbose)
printf(MSG_DB_IS_UP_TO_DATE, *prepos);
continue;
Expand All @@ -701,8 +701,12 @@ update_remotedb(int verbose)

/* delete remote* associated to this repository */
delete_remote_tbl(sumsw[REMOTE_SUMMARY], *prepos);

/* update remote* table for this repository */
insert_remote_summary(a, *prepos);

/* mark repository as being updated with new mtime */
pkgindb_dovaquery(UPDATE_REPO_MTIME, (long long)repo_mtime, *prepos);
}

/* remove empty rows (duplicates) */
Expand Down

0 comments on commit 01d5b0c

Please sign in to comment.