summaryrefslogtreecommitdiff
path: root/lib/libalpm/be_files.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/be_files.c')
-rw-r--r--lib/libalpm/be_files.c174
1 files changed, 52 insertions, 122 deletions
diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index bfe10867..f624d7cf 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -47,76 +47,10 @@
#include "dload.h"
-/*
- * Return the last update time as number of seconds from the epoch.
- * Returns 0 if the value is unknown or can't be read.
- */
-static time_t getlastupdate(const pmdb_t *db)
-{
- FILE *fp;
- char *file;
- time_t ret = 0;
-
- ALPM_LOG_FUNC;
-
- if(db == NULL) {
- return(ret);
- }
-
- /* db->path + '.lastupdate' + NULL */
- MALLOC(file, strlen(db->path) + 12, RET_ERR(PM_ERR_MEMORY, ret));
- sprintf(file, "%s.lastupdate", db->path);
-
- /* get the last update time, if it's there */
- if((fp = fopen(file, "r")) == NULL) {
- free(file);
- return(ret);
- } else {
- char line[64];
- if(fgets(line, sizeof(line), fp)) {
- ret = atol(line);
- }
- }
- fclose(fp);
- free(file);
- return(ret);
-}
-
-/*
- * writes the dbpath/.lastupdate file with the value in time
- */
-static int setlastupdate(const pmdb_t *db, time_t time)
-{
- FILE *fp;
- char *file;
- int ret = 0;
-
- ALPM_LOG_FUNC;
-
- if(db == NULL || time == 0) {
- return(-1);
- }
-
- /* db->path + '.lastupdate' + NULL */
- MALLOC(file, strlen(db->path) + 12, RET_ERR(PM_ERR_MEMORY, ret));
- sprintf(file, "%s.lastupdate", db->path);
-
- if((fp = fopen(file, "w")) == NULL) {
- free(file);
- return(-1);
- }
- if(fprintf(fp, "%ju", (uintmax_t)time) <= 0) {
- ret = -1;
- }
- fclose(fp);
- free(file);
- return(ret);
-}
-
static int checkdbdir(pmdb_t *db)
{
struct stat buf;
- char *path = db->path;
+ 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",
@@ -148,7 +82,7 @@ static int checkdbdir(pmdb_t *db)
* pmdb_t *db;
* int result;
* db = alpm_list_getdata(alpm_option_get_syncdbs());
- * if(alpm_trans_init(PM_TRANS_TYPE_SYNC, 0, NULL, NULL, NULL) == 0) {
+ * if(alpm_trans_init(0, NULL, NULL, NULL) == 0) {
* result = alpm_db_update(0, db);
* alpm_trans_release();
*
@@ -174,7 +108,6 @@ static int checkdbdir(pmdb_t *db)
int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
{
char *dbfile, *dbfilepath;
- time_t newmtime = 0, lastupdate = 0;
const char *dbpath;
size_t len;
@@ -191,33 +124,22 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
*/
ASSERT(handle->trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
ASSERT(handle->trans->state == STATE_INITIALIZED, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1));
- ASSERT(handle->trans->type == PM_TRANS_TYPE_SYNC, RET_ERR(PM_ERR_TRANS_TYPE, -1));
if(!alpm_list_find_ptr(handle->dbs_sync, db)) {
RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
}
- if(!force) {
- /* get the lastupdate time */
- lastupdate = getlastupdate(db);
- if(lastupdate == 0) {
- _alpm_log(PM_LOG_DEBUG, "failed to get lastupdate time for %s\n",
- db->treename);
- }
- }
-
len = strlen(db->treename) + strlen(DBEXT) + 1;
MALLOC(dbfile, len, RET_ERR(PM_ERR_MEMORY, -1));
sprintf(dbfile, "%s" DBEXT, db->treename);
dbpath = alpm_option_get_dbpath();
- ret = _alpm_download_single_file(dbfile, db->servers, dbpath,
- lastupdate, &newmtime);
+ ret = _alpm_download_single_file(dbfile, db->servers, dbpath, force);
free(dbfile);
if(ret == 1) {
- /* mtimes match, do nothing */
+ /* files match, do nothing */
pm_errno = 0;
return(1);
} else if(ret == -1) {
@@ -225,12 +147,16 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
_alpm_log(PM_LOG_DEBUG, "failed to sync db: %s\n", alpm_strerrorlast());
return(-1);
} else {
+ const char *syncdbpath = _alpm_db_path(db);
/* remove the old dir */
- if(_alpm_rmrf(db->path) != 0) {
+ if(_alpm_rmrf(syncdbpath) != 0) {
_alpm_log(PM_LOG_ERROR, _("could not remove database %s\n"), db->treename);
RET_ERR(PM_ERR_DB_REMOVE, -1);
+ } else {
+ _alpm_log(PM_LOG_DEBUG, "database dir %s removed\n", _alpm_db_path(db));
}
+
/* Cache needs to be rebuilt */
_alpm_db_free_pkgcache(db);
@@ -241,20 +167,12 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
/* uncompress the sync database */
checkdbdir(db);
- ret = _alpm_unpack(dbfilepath, db->path, NULL);
+ ret = _alpm_unpack(dbfilepath, syncdbpath, NULL, 0);
if(ret) {
free(dbfilepath);
RET_ERR(PM_ERR_SYSTEM, -1);
}
- unlink(dbfilepath);
free(dbfilepath);
-
- /* if we have a new mtime, set the DB last update value */
- if(newmtime) {
- _alpm_log(PM_LOG_DEBUG, "sync: new mtime for %s: %ju\n",
- db->treename, (uintmax_t)newmtime);
- setlastupdate(db, newmtime);
- }
}
return(0);
@@ -306,13 +224,15 @@ int _alpm_db_populate(pmdb_t *db)
struct dirent *ent = NULL;
struct stat sbuf;
char path[PATH_MAX];
+ const char *dbpath;
DIR *dbdir;
ALPM_LOG_FUNC;
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
- dbdir = opendir(db->path);
+ dbpath = _alpm_db_path(db);
+ dbdir = opendir(dbpath);
if(dbdir == NULL) {
return(0);
}
@@ -324,7 +244,7 @@ int _alpm_db_populate(pmdb_t *db)
continue;
}
/* stat the entry, make sure it's a directory */
- snprintf(path, PATH_MAX, "%s%s", db->path, name);
+ snprintf(path, PATH_MAX, "%s%s", dbpath, name);
if(stat(path, &sbuf) != 0 || !S_ISDIR(sbuf.st_mode)) {
continue;
}
@@ -342,6 +262,13 @@ int _alpm_db_populate(pmdb_t *db)
continue;
}
+ /* duplicated database entries are not allowed */
+ if(_alpm_pkg_find(db->pkgcache, pkg->name)) {
+ _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
+ _alpm_pkg_free(pkg);
+ continue;
+ }
+
/* explicitly read with only 'BASE' data, accessors will handle the rest */
if(_alpm_db_read(db, pkg, INFRQ_BASE) == -1) {
_alpm_log(PM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
@@ -367,10 +294,12 @@ static char *get_pkgpath(pmdb_t *db, pmpkg_t *info)
{
size_t len;
char *pkgpath;
+ const char *dbpath;
- len = strlen(db->path) + strlen(info->name) + strlen(info->version) + 3;
+ 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/", db->path, info->name, info->version);
+ sprintf(pkgpath, "%s%s-%s/", dbpath, info->name, info->version);
return(pkgpath);
}
@@ -379,6 +308,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
FILE *fp = NULL;
char path[PATH_MAX];
char line[513];
+ int sline = sizeof(line)-1;
char *pkgpath = NULL;
ALPM_LOG_FUNC;
@@ -410,7 +340,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
info->name, inforeq);
/* clear out 'line', to be certain - and to make valgrind happy */
- memset(line, 0, 513);
+ memset(line, 0, sline+1);
pkgpath = get_pkgpath(db, info);
@@ -434,7 +364,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
}
_alpm_strtrim(line);
if(strcmp(line, "%NAME%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
if(strcmp(_alpm_strtrim(line), info->name) != 0) {
@@ -442,7 +372,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
"mismatch on package %s\n"), db->treename, info->name);
}
} else if(strcmp(line, "%VERSION%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
if(strcmp(_alpm_strtrim(line), info->version) != 0) {
@@ -450,44 +380,44 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
"mismatch on package %s\n"), db->treename, info->name);
}
} else if(strcmp(line, "%FILENAME%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
STRDUP(info->filename, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%DESC%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
STRDUP(info->desc, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%GROUPS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->groups = alpm_list_add(info->groups, linedup);
}
} else if(strcmp(line, "%URL%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
STRDUP(info->url, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%LICENSE%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->licenses = alpm_list_add(info->licenses, linedup);
}
} else if(strcmp(line, "%ARCH%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
STRDUP(info->arch, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%BUILDDATE%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
_alpm_strtrim(line);
- char first = tolower(line[0]);
+ char first = tolower((unsigned char)line[0]);
if(first > 'a' && first < 'z') {
struct tm tmp_tm = {0}; /* initialize to null in case of failure */
setlocale(LC_TIME, "C");
@@ -498,12 +428,12 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
info->builddate = atol(line);
}
} else if(strcmp(line, "%INSTALLDATE%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
_alpm_strtrim(line);
- char first = tolower(line[0]);
+ char first = tolower((unsigned char)line[0]);
if(first > 'a' && first < 'z') {
struct tm tmp_tm = {0}; /* initialize to null in case of failure */
setlocale(LC_TIME, "C");
@@ -514,12 +444,12 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
info->installdate = atol(line);
}
} else if(strcmp(line, "%PACKAGER%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
STRDUP(info->packager, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%REASON%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
@@ -529,7 +459,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
* is currently only used in sync databases, and SIZE is
* only used in local databases.
*/
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
info->size = atol(_alpm_strtrim(line));
@@ -540,19 +470,19 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
} else if(strcmp(line, "%ISIZE%") == 0) {
/* ISIZE (installed size) tag only appears in sync repositories,
* not the local one. */
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
info->isize = atol(_alpm_strtrim(line));
} else if(strcmp(line, "%MD5SUM%") == 0) {
/* MD5SUM tag only appears in sync repositories,
* not the local one. */
- if(fgets(line, 512, fp) == NULL) {
+ if(fgets(line, sline, fp) == NULL) {
goto error;
}
STRDUP(info->md5sum, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%REPLACES%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->replaces = alpm_list_add(info->replaces, linedup);
@@ -575,13 +505,13 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
while(fgets(line, 256, fp)) {
_alpm_strtrim(line);
if(strcmp(line, "%FILES%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->files = alpm_list_add(info->files, linedup);
}
} else if(strcmp(line, "%BACKUP%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->backup = alpm_list_add(info->backup, linedup);
@@ -603,24 +533,24 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
fgets(line, 255, fp);
_alpm_strtrim(line);
if(strcmp(line, "%DEPENDS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
info->depends = alpm_list_add(info->depends, dep);
}
} else if(strcmp(line, "%OPTDEPENDS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->optdepends = alpm_list_add(info->optdepends, linedup);
}
} else if(strcmp(line, "%CONFLICTS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->conflicts = alpm_list_add(info->conflicts, linedup);
}
} else if(strcmp(line, "%PROVIDES%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error);
info->provides = alpm_list_add(info->provides, linedup);
@@ -639,7 +569,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
fgets(line, 255, fp);
_alpm_strtrim(line);
if(strcmp(line, "%DELTAS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) {
pmdelta_t *delta = _alpm_delta_parse(line);
if(delta) {
info->deltas = alpm_list_add(info->deltas, delta);