From 0909a72000b03332c5203b16b6a4e862c0662e03 Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Wed, 13 Oct 2010 17:55:55 +1000 Subject: Move database handling utility functions Move splitname, checkdbdir, get_pkgpath into db.{h,c} as these will be needed to parse both the local and sync databases during the initial splitting. They will be moved out of db.{h,c} at to more appropriate locations at a later stage. Signed-off-by: Allan McRae --- lib/libalpm/be_files.c | 74 ------------------------------------------------ lib/libalpm/db.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/libalpm/db.h | 4 +++ 3 files changed, 81 insertions(+), 74 deletions(-) diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c index 5766a4b1..013c8fb2 100644 --- a/lib/libalpm/be_files.c +++ b/lib/libalpm/be_files.c @@ -595,26 +595,6 @@ pmgrp_t *_alpm_db_get_grpfromcache(pmdb_t *db, const char *target) return(NULL); } -static int checkdbdir(pmdb_t *db) -{ - struct stat buf; - const char *path = _alpm_db_path(db); - - if(stat(path, &buf) != 0) { - _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", - path); - if(_alpm_makepath(path) != 0) { - RET_ERR(PM_ERR_SYSTEM, -1); - } - } else if(!S_ISDIR(buf.st_mode)) { - _alpm_log(PM_LOG_WARNING, _("removing invalid database: %s\n"), path); - if(unlink(path) != 0 || _alpm_makepath(path) != 0) { - RET_ERR(PM_ERR_SYSTEM, -1); - } - } - return(0); -} - /* create list of directories in db */ static int dirlist_from_tar(const char *archive, alpm_list_t **dirlist) { @@ -866,46 +846,6 @@ cleanup: return(0); } - -static int splitname(const char *target, pmpkg_t *pkg) -{ - /* the format of a db entry is as follows: - * package-version-rel/ - * package name can contain hyphens, so parse from the back- go back - * two hyphens and we have split the version from the name. - */ - char *tmp, *p, *q; - - if(target == NULL || pkg == NULL) { - return(-1); - } - STRDUP(tmp, target, RET_ERR(PM_ERR_MEMORY, -1)); - p = tmp + strlen(tmp); - - /* do the magic parsing- find the beginning of the version string - * by doing two iterations of same loop to lop off two hyphens */ - for(q = --p; *q && *q != '-'; q--); - for(p = --q; *p && *p != '-'; p--); - if(*p != '-' || p == tmp) { - return(-1); - } - - /* copy into fields and return */ - if(pkg->version) { - FREE(pkg->version); - } - STRDUP(pkg->version, p+1, RET_ERR(PM_ERR_MEMORY, -1)); - /* insert a terminator at the end of the name (on hyphen)- then copy it */ - *p = '\0'; - if(pkg->name) { - FREE(pkg->name); - } - STRDUP(pkg->name, tmp, RET_ERR(PM_ERR_MEMORY, -1)); - - free(tmp); - return(0); -} - int _alpm_db_populate(pmdb_t *db) { int count = 0; @@ -979,20 +919,6 @@ int _alpm_db_populate(pmdb_t *db) return(count); } -/* Note: the return value must be freed by the caller */ -static char *get_pkgpath(pmdb_t *db, pmpkg_t *info) -{ - size_t len; - char *pkgpath; - const char *dbpath; - - dbpath = _alpm_db_path(db); - len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3; - MALLOC(pkgpath, len, RET_ERR(PM_ERR_MEMORY, NULL)); - sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version); - return(pkgpath); -} - int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq) { FILE *fp = NULL; diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c index b61631a5..d2844f8b 100644 --- a/lib/libalpm/db.c +++ b/lib/libalpm/db.c @@ -562,4 +562,81 @@ pmdb_t *_alpm_db_register_sync(const char *treename) return(db); } + +int splitname(const char *target, pmpkg_t *pkg) +{ + /* the format of a db entry is as follows: + * package-version-rel/ + * package name can contain hyphens, so parse from the back- go back + * two hyphens and we have split the version from the name. + */ + char *tmp, *p, *q; + + if(target == NULL || pkg == NULL) { + return(-1); + } + STRDUP(tmp, target, RET_ERR(PM_ERR_MEMORY, -1)); + p = tmp + strlen(tmp); + + /* do the magic parsing- find the beginning of the version string + * by doing two iterations of same loop to lop off two hyphens */ + for(q = --p; *q && *q != '-'; q--); + for(p = --q; *p && *p != '-'; p--); + if(*p != '-' || p == tmp) { + return(-1); + } + + /* copy into fields and return */ + if(pkg->version) { + FREE(pkg->version); + } + STRDUP(pkg->version, p+1, RET_ERR(PM_ERR_MEMORY, -1)); + /* insert a terminator at the end of the name (on hyphen)- then copy it */ + *p = '\0'; + if(pkg->name) { + FREE(pkg->name); + } + STRDUP(pkg->name, tmp, RET_ERR(PM_ERR_MEMORY, -1)); + + free(tmp); + return(0); +} + + +/* TODO: move these two functions to be_local once be_sync no longer uses them */ + +int checkdbdir(pmdb_t *db) +{ + struct stat buf; + const char *path = _alpm_db_path(db); + + if(stat(path, &buf) != 0) { + _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", + path); + if(_alpm_makepath(path) != 0) { + RET_ERR(PM_ERR_SYSTEM, -1); + } + } else if(!S_ISDIR(buf.st_mode)) { + _alpm_log(PM_LOG_WARNING, _("removing invalid database: %s\n"), path); + if(unlink(path) != 0 || _alpm_makepath(path) != 0) { + RET_ERR(PM_ERR_SYSTEM, -1); + } + } + return(0); +} + +/* Note: the return value must be freed by the caller */ +char *get_pkgpath(pmdb_t *db, pmpkg_t *info) +{ + size_t len; + char *pkgpath; + const char *dbpath; + + dbpath = _alpm_db_path(db); + len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3; + MALLOC(pkgpath, len, RET_ERR(PM_ERR_MEMORY, NULL)); + sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version); + return(pkgpath); +} + /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h index b9f89ffd..f4559fe9 100644 --- a/lib/libalpm/db.h +++ b/lib/libalpm/db.h @@ -93,6 +93,10 @@ void _alpm_db_free_grpcache(pmdb_t *db); alpm_list_t *_alpm_db_get_grpcache(pmdb_t *db); pmgrp_t *_alpm_db_get_grpfromcache(pmdb_t *db, const char *target); +int splitname(const char *target, pmpkg_t *pkg); +int checkdbdir(pmdb_t *db); +char *get_pkgpath(pmdb_t *db, pmpkg_t *info); + #endif /* _ALPM_DB_H */ /* vim: set ts=2 sw=2 noet: */ -- cgit v1.2.3-24-g4f1b