>From c69a3efc4d6e08ea967a936afc8f9622e1e6b268 Mon Sep 17 00:00:00 2001
From: Xavier Chantry <shiningxc@gmail.com>
Date: Fri, 30 May 2008 08:52:27 +0200
Subject: [PATCH] Get rid of double / in database paths.
Errors like the following one happen regularly (for unknown reasons...) :
error: could not open file /var/lib/pacman/local//glibc-2.7-9/depends: No
such file or directory
Anyway, every time an user reported an error like that, it always seemed
like he thought the error was caused by the double /, which is obviously
wrong.
Since db->path always include a trailing /, there is no need to add one when
concatenating paths in be_files.c or add.c.
Additionally, some static strings were switched to dynamic.
And the computation of the "dbpath"/"pkgname"-"pkgversion" was refactored
in db_read, db_write and db_remove with a get_pkgpath static function.
/* get the last update time, if it's there */
if((fp = fopen(file, "r")) == NULL) {
@@ -90,6 +92,7 @@ int setlastupdate(const pmdb_t *db, time_t time)
FILE *fp;
char *file;
int ret = 0;
+ int len = 0;
- ret = _alpm_download_single_file(path, db->servers, dbpath,
+ ret = _alpm_download_single_file(dbfile, db->servers, dbpath,
lastupdate, &newmtime);
+ free(dbfile);
if(ret == 1) {
/* mtimes match, do nothing */
@@ -169,9 +179,6 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
_alpm_log(PM_LOG_DEBUG, "failed to sync db: %s
", alpm_strerrorlast());
return(-1);
} else {
- /* form the path to the db location */
- snprintf(path, PATH_MAX, "%s%s" DBEXT, dbpath, db->treename);
-
/* remove the old dir */
_alpm_log(PM_LOG_DEBUG, "flushing database %s
", db->path);
for(lp = _alpm_db_get_pkgcache(db); lp; lp = lp->next) {
@@ -186,11 +193,19 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
/* Cache needs to be rebuilt */
_alpm_db_free_pkgcache(db);
+ /* form the path to the db location */
+ len = strlen(dbpath) + strlen(db->treename) + strlen(DBEXT) + 1;
+ CALLOC(dbfilepath, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, -1));
+ snprintf(dbfilepath, len, "%s%s" DBEXT, dbpath, db->treename);
+
/* uncompress the sync database */
- if(_alpm_unpack(path, db->path, NULL)) {
+ ret = _alpm_unpack(dbfilepath, db->path, NULL);
+ if(ret) {
+ free(dbfilepath);
RET_ERR(PM_ERR_SYSTEM, -1);
}
- unlink(path);
+ unlink(dbfilepath);
+ free(dbfilepath);
/* if we have a new mtime, set the DB last update value */
if(newmtime) {
@@ -293,7 +308,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", db->path, name);
if(stat(path, &sbuf) != 0 || !S_ISDIR(sbuf.st_mode)) {
continue;
}
@@ -329,12 +344,24 @@ int _alpm_db_populate(pmdb_t *db)
return(count);
}
@@ -367,17 +394,19 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
pmdbinfrq_t inforeq)
/* clear out 'line', to be certain - and to make valgrind happy */
memset(line, 0, 513);
- snprintf(path, PATH_MAX, "%s/%s-%s", db->path, info->name, info->version);
- if(stat(path, &buf)) {
+ pkgpath = get_pkgpath(db, info);
+
+ if(stat(pkgpath, &buf)) {
/* directory doesn't exist or can't be opened */
_alpm_log(PM_LOG_DEBUG, "cannot find '%s-%s' in db '%s'
",
info->name, info->version, db->treename);
+ free(pkgpath);
return(-1);
}