From 884155e03bf20f1cca880f39848423091729f6cd Mon Sep 17 00:00:00 2001 From: Nagy Gabor <ngaba@bibl.u-szeged.hu> Date: Mon, 19 Jan 2009 00:35:45 +0100 Subject: [PATCH] db_register: Unlink DbPath/database, if it is not a directory We had a check for this case, but we didn't handle it at all (neither error, nor fix), we also had a TODO in the code. For example, if user has an "unstable" regular file instead of "unstable" directory (FS#12148), from now on pacman will remove that file (and print a warning message). Before this patch pacman just stopped with "could not register database" error. Signed-off-by: Nagy Gabor <ngaba@bibl.u-szeged.hu> --- lib/libalpm/db.c | 18 ++++++++++++++---- 1 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c index 9b91ce4..fe886ae 100644 --- a/lib/libalpm/db.c +++ b/lib/libalpm/db.c @@ -452,13 +452,18 @@ pmdb_t *_alpm_db_register_local(void) RET_ERR(PM_ERR_DB_OPEN, NULL); } snprintf(path, PATH_MAX, "%slocal", dbpath); - /* TODO this is rediculous, we try to do this even if we can't */ - if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) { + + 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, NULL); } + } else if(!S_ISDIR(buf.st_mode)) { + _alpm_log(PM_LOG_WARNING, "removing bogus database: %s\n", path); + if(unlink(path) != 0 || _alpm_makepath(path) != 0) { + RET_ERR(PM_ERR_SYSTEM, NULL); + } } db = _alpm_db_new(dbpath, "local"); @@ -504,13 +509,18 @@ pmdb_t *_alpm_db_register_sync(const char *treename) } /* all sync DBs now reside in the sync/ subdir of the dbpath */ snprintf(path, PATH_MAX, "%ssync/%s", dbpath, treename); - /* TODO this is rediculous, we try to do this even if we can't */ - if(stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) { + + 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, NULL); } + } else if(!S_ISDIR(buf.st_mode)) { + _alpm_log(PM_LOG_WARNING, "removing bogus database: %s\n", path); + if(unlink(path) != 0 || _alpm_makepath(path) != 0) { + RET_ERR(PM_ERR_SYSTEM, NULL); + } } /* Ensure the db gets the real path. */ -- 1.6.1