diff options
author | Aurelien Foret <aurelien@archlinux.org> | 2006-02-15 23:51:28 +0100 |
---|---|---|
committer | Aurelien Foret <aurelien@archlinux.org> | 2006-02-15 23:51:28 +0100 |
commit | 6e76fd8af3113a28280077ba3da016c01a760ba2 (patch) | |
tree | 5c818cd1736168a7e4ace2d50d8180a3584bae6f | |
parent | 2e128d3a41602b2e5c2f793b685175decce1bac0 (diff) | |
download | pacman-6e76fd8af3113a28280077ba3da016c01a760ba2.tar.gz pacman-6e76fd8af3113a28280077ba3da016c01a760ba2.tar.xz |
- merged db_open and db_create into one single function
- moved the .lastupdate support to the frontend
-rw-r--r-- | lib/libalpm/alpm.c | 27 | ||||
-rw-r--r-- | lib/libalpm/alpm.h | 5 | ||||
-rw-r--r-- | lib/libalpm/db.c | 93 | ||||
-rw-r--r-- | lib/libalpm/db.h | 10 |
4 files changed, 20 insertions, 115 deletions
diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c index 788e504c..5a071c32 100644 --- a/lib/libalpm/alpm.c +++ b/lib/libalpm/alpm.c @@ -187,17 +187,9 @@ pmdb_t *alpm_db_register(char *treename) RET_ERR(PM_ERR_DB_NOT_NULL, NULL); } - db = db_open(handle->root, handle->dbpath, treename); + db = db_open(handle->root, handle->dbpath, treename, DB_O_CREATE); if(db == NULL) { - /* couldn't open the db directory - try creating it */ - if(db_create(handle->root, handle->dbpath, treename) == -1) { - RET_ERR(PM_ERR_DB_CREATE, NULL); - } - db = db_open(handle->root, handle->dbpath, treename); - if(db == NULL) { - /* couldn't open the db directory */ - RET_ERR(PM_ERR_DB_OPEN, NULL); - } + RET_ERR(PM_ERR_DB_OPEN, NULL); } if(strcmp(treename, "local") == 0) { @@ -270,7 +262,6 @@ void *alpm_db_getinfo(PM_DB *db, unsigned char parm) switch(parm) { case PM_DB_TREENAME: data = db->treename; break; - case PM_DB_LASTUPDATE: data = db->lastupdate; break; default: data = NULL; } @@ -284,7 +275,7 @@ void *alpm_db_getinfo(PM_DB *db, unsigned char parm) * @param ts timestamp of the last modification time of the tarball * @return 0 on success, -1 on error (pm_errno is set accordingly) */ -int alpm_db_update(PM_DB *db, char *archive, char *ts) +int alpm_db_update(PM_DB *db, char *archive) { PMList *lp; @@ -298,12 +289,6 @@ int alpm_db_update(PM_DB *db, char *archive, char *ts) RET_ERR(PM_ERR_DB_NOT_FOUND, -1); } - if(ts && strlen(ts) != 0) { - if(strcmp(ts, db->lastupdate) == 0) { - RET_ERR(PM_ERR_DB_UPTODATE, -1); - } - } - /* remove the old dir */ _alpm_log(PM_LOG_FLOW2, "flushing database %s/%s", handle->dbpath, db->treename); for(lp = db_get_pkgcache(db); lp; lp = lp->next) { @@ -328,12 +313,6 @@ int alpm_db_update(PM_DB *db, char *archive, char *ts) RET_ERR(PM_ERR_SYSTEM, -1); } - if(ts && strlen(ts) != 0) { - if(db_setlastupdate(db, ts) == -1) { - RET_ERR(PM_ERR_SYSTEM, -1); - } - } - return(0); } diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h index b69d5fde..cdb00975 100644 --- a/lib/libalpm/alpm.h +++ b/lib/libalpm/alpm.h @@ -105,8 +105,7 @@ int alpm_get_option(unsigned char parm, long *data); /* Info parameters */ enum { - PM_DB_TREENAME = 1, - PM_DB_LASTUPDATE + PM_DB_TREENAME = 1 }; PM_DB *alpm_db_register(char *treename); @@ -114,7 +113,7 @@ int alpm_db_unregister(PM_DB *db); void *alpm_db_getinfo(PM_DB *db, unsigned char parm); -int alpm_db_update(PM_DB *db, char *archive, char *ts); +int alpm_db_update(PM_DB *db, char *archive); PM_PKG *alpm_db_readpkg(PM_DB *db, char *name); PM_LIST *alpm_db_getpkgcache(PM_DB *db); 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) { diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h index 4cde6d2a..cd53d01c 100644 --- a/lib/libalpm/db.h +++ b/lib/libalpm/db.h @@ -35,7 +35,8 @@ #define INFRQ_ALL 0xFF #define DB_TREENAME_LEN 128 -#define DB_UPDATE_LEN 16 + +#define DB_O_CREATE 0x01 /* Database */ typedef struct __pmdb_t { @@ -44,15 +45,10 @@ typedef struct __pmdb_t { DIR *dir; PMList *pkgcache; PMList *grpcache; - char lastupdate[DB_UPDATE_LEN]; } pmdb_t; -pmdb_t *db_open(char *root, char *dbpath, char *treename); +pmdb_t *db_open(char *root, char *dbpath, char *treename, int mode); void db_close(pmdb_t *db); -int db_create(char *root, char *dbpath, char *treename); - -int db_getlastupdate(pmdb_t *db, char *ts); -int db_setlastupdate(pmdb_t *db, char *ts); void db_rewind(pmdb_t *db); pmpkg_t *db_scan(pmdb_t *db, char *target, unsigned int inforeq); |