- merged db_open and db_create into one single function
- moved the .lastupdate support to the frontend
This commit is contained in:
parent
2e128d3a41
commit
6e76fd8af3
4 changed files with 20 additions and 115 deletions
|
@ -187,18 +187,10 @@ pmdb_t *alpm_db_register(char *treename)
|
||||||
RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
|
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) {
|
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) {
|
if(strcmp(treename, "local") == 0) {
|
||||||
handle->db_local = db;
|
handle->db_local = db;
|
||||||
|
@ -270,7 +262,6 @@ void *alpm_db_getinfo(PM_DB *db, unsigned char parm)
|
||||||
|
|
||||||
switch(parm) {
|
switch(parm) {
|
||||||
case PM_DB_TREENAME: data = db->treename; break;
|
case PM_DB_TREENAME: data = db->treename; break;
|
||||||
case PM_DB_LASTUPDATE: data = db->lastupdate; break;
|
|
||||||
default:
|
default:
|
||||||
data = NULL;
|
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
|
* @param ts timestamp of the last modification time of the tarball
|
||||||
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
* @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;
|
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);
|
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 */
|
/* remove the old dir */
|
||||||
_alpm_log(PM_LOG_FLOW2, "flushing database %s/%s", handle->dbpath, db->treename);
|
_alpm_log(PM_LOG_FLOW2, "flushing database %s/%s", handle->dbpath, db->treename);
|
||||||
for(lp = db_get_pkgcache(db); lp; lp = lp->next) {
|
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);
|
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);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -105,8 +105,7 @@ int alpm_get_option(unsigned char parm, long *data);
|
||||||
|
|
||||||
/* Info parameters */
|
/* Info parameters */
|
||||||
enum {
|
enum {
|
||||||
PM_DB_TREENAME = 1,
|
PM_DB_TREENAME = 1
|
||||||
PM_DB_LASTUPDATE
|
|
||||||
};
|
};
|
||||||
|
|
||||||
PM_DB *alpm_db_register(char *treename);
|
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);
|
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_PKG *alpm_db_readpkg(PM_DB *db, char *name);
|
||||||
PM_LIST *alpm_db_getpkgcache(PM_DB *db);
|
PM_LIST *alpm_db_getpkgcache(PM_DB *db);
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
#include "alpm.h"
|
#include "alpm.h"
|
||||||
|
|
||||||
/* Open a database and return a pmdb_t handle */
|
/* 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;
|
pmdb_t *db;
|
||||||
|
|
||||||
|
@ -55,18 +55,24 @@ pmdb_t *db_open(char *root, char *dbpath, char *treename)
|
||||||
|
|
||||||
db->dir = opendir(db->path);
|
db->dir = opendir(db->path);
|
||||||
if(db->dir == NULL) {
|
if(db->dir == 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->path);
|
||||||
FREE(db);
|
FREE(db);
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
STRNCPY(db->treename, treename, DB_TREENAME_LEN);
|
STRNCPY(db->treename, treename, DB_TREENAME_LEN);
|
||||||
|
|
||||||
db->pkgcache = NULL;
|
db->pkgcache = NULL;
|
||||||
db->grpcache = NULL;
|
db->grpcache = NULL;
|
||||||
|
|
||||||
db_getlastupdate(db, db->lastupdate);
|
|
||||||
|
|
||||||
return(db);
|
return(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,81 +98,6 @@ void db_close(pmdb_t *db)
|
||||||
return;
|
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)
|
void db_rewind(pmdb_t *db)
|
||||||
{
|
{
|
||||||
if(db == NULL || db->dir == NULL) {
|
if(db == NULL || db->dir == NULL) {
|
||||||
|
|
|
@ -35,7 +35,8 @@
|
||||||
#define INFRQ_ALL 0xFF
|
#define INFRQ_ALL 0xFF
|
||||||
|
|
||||||
#define DB_TREENAME_LEN 128
|
#define DB_TREENAME_LEN 128
|
||||||
#define DB_UPDATE_LEN 16
|
|
||||||
|
#define DB_O_CREATE 0x01
|
||||||
|
|
||||||
/* Database */
|
/* Database */
|
||||||
typedef struct __pmdb_t {
|
typedef struct __pmdb_t {
|
||||||
|
@ -44,15 +45,10 @@ typedef struct __pmdb_t {
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
PMList *pkgcache;
|
PMList *pkgcache;
|
||||||
PMList *grpcache;
|
PMList *grpcache;
|
||||||
char lastupdate[DB_UPDATE_LEN];
|
|
||||||
} pmdb_t;
|
} 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);
|
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);
|
void db_rewind(pmdb_t *db);
|
||||||
pmpkg_t *db_scan(pmdb_t *db, char *target, unsigned int inforeq);
|
pmpkg_t *db_scan(pmdb_t *db, char *target, unsigned int inforeq);
|
||||||
|
|
Loading…
Add table
Reference in a new issue