From 6e76fd8af3113a28280077ba3da016c01a760ba2 Mon Sep 17 00:00:00 2001 From: Aurelien Foret Date: Wed, 15 Feb 2006 22:51:28 +0000 Subject: - merged db_open and db_create into one single function - moved the .lastupdate support to the frontend --- lib/libalpm/db.c | 93 ++++++++------------------------------------------------ 1 file changed, 12 insertions(+), 81 deletions(-) (limited to 'lib/libalpm/db.c') diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c index b1da6b71..5951e32b 100644 --- a/lib/libalpm/db.c +++ b/lib/libalpm/db.c @@ -38,7 +38,7 @@ #include "alpm.h" /* Open a database and return a pmdb_t handle */ -pmdb_t *db_open(char *root, char *dbpath, char *treename) +pmdb_t *db_open(char *root, char *dbpath, char *treename, int mode) { pmdb_t *db; @@ -55,9 +55,17 @@ pmdb_t *db_open(char *root, char *dbpath, char *treename) db->dir = opendir(db->path); if(db->dir == NULL) { - FREE(db->path); - FREE(db); - return(NULL); + if(mode & DB_O_CREATE) { + _alpm_log(PM_LOG_WARNING, "could not open database '%s' -- try creating it", treename); + if(_alpm_makepath(db->path) == 0) { + db->dir = opendir(db->path); + } + } + if(!(mode & DB_O_CREATE) || db->dir == NULL) { + FREE(db->path); + FREE(db); + return(NULL); + } } STRNCPY(db->treename, treename, DB_TREENAME_LEN); @@ -65,8 +73,6 @@ pmdb_t *db_open(char *root, char *dbpath, char *treename) db->pkgcache = NULL; db->grpcache = NULL; - db_getlastupdate(db, db->lastupdate); - return(db); } @@ -92,81 +98,6 @@ void db_close(pmdb_t *db) return; } -int db_create(char *root, char *dbpath, char *treename) -{ - char path[PATH_MAX]; - - if(root == NULL || dbpath == NULL || treename == NULL) { - return(-1); - } - - snprintf(path, PATH_MAX, "%s%s/%s", root, dbpath, treename); - if(_alpm_makepath(path) != 0) { - return(-1); - } - - return(0); -} - -/* reads dbpath/.lastupdate and populates *ts with the contents. - * *ts should be malloc'ed and should be at least 15 bytes. - * - * Returns 0 on success, 1 on error - * - */ -int db_getlastupdate(pmdb_t *db, char *ts) -{ - FILE *fp; - char path[PATH_MAX]; - - if(db == NULL || ts == NULL) { - return(-1); - } - - /* get the last update time, if it's there */ - snprintf(path, PATH_MAX, "%s/.lastupdate", db->path); - if((fp = fopen(path, "r")) == NULL) { - return(-1); - } else { - char line[256]; - if(fgets(line, sizeof(line), fp)) { - STRNCPY(ts, line, 15); /* YYYYMMDDHHMMSS */ - ts[14] = '\0'; - } else { - fclose(fp); - return(-1); - } - } - fclose(fp); - return(0); -} - -/* writes the dbpath/.lastupdate with the contents of *ts - */ -int db_setlastupdate(pmdb_t *db, char *ts) -{ - FILE *fp; - char file[PATH_MAX]; - - if(db == NULL || ts == NULL || strlen(ts) == 0) { - return(-1); - } - - snprintf(file, PATH_MAX, "%s/.lastupdate", db->path); - if((fp = fopen(file, "w")) == NULL) { - return(-1); - } - if(fputs(ts, fp) <= 0) { - fclose(fp); - return(-1); - } - fclose(fp); - - STRNCPY(db->lastupdate, ts, DB_UPDATE_LEN); - - return(0); -} - void db_rewind(pmdb_t *db) { if(db == NULL || db->dir == NULL) { -- cgit v1.2.3-24-g4f1b