From 1150d9e15aaea2ae1f259995d11442f491ef0af7 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 20 Apr 2011 19:54:01 -0500 Subject: Move database 'version' check to registration time This is another step toward doing both local database validation (ensuring we don't have depends files) and sync database validation (via signatures if present) when the database is registered. Signed-off-by: Dan McGee --- lib/libalpm/be_local.c | 111 ++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 53 deletions(-) (limited to 'lib/libalpm/be_local.c') diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 4b2a3017..0ff51deb 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -314,6 +314,56 @@ static int is_dir(const char *path, struct dirent *entry) return 0; } +static int local_db_validate(pmdb_t *db) +{ + struct dirent *ent = NULL; + const char *dbpath; + DIR *dbdir; + int ret = -1; + + dbpath = _alpm_db_path(db); + if(dbpath == NULL) { + RET_ERR(db->handle, PM_ERR_DB_OPEN, -1); + } + dbdir = opendir(dbpath); + if(dbdir == NULL) { + if(errno == ENOENT) { + /* database dir doesn't exist yet */ + return 0; + } else { + RET_ERR(db->handle, PM_ERR_DB_OPEN, -1); + } + } + + while((ent = readdir(dbdir)) != NULL) { + const char *name = ent->d_name; + char path[PATH_MAX]; + + if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { + continue; + } + if(!is_dir(dbpath, ent)) { + continue; + } + + snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name); + if(access(path, F_OK) == 0) { + /* we found a depends file- bail */ + db->handle->pm_errno = PM_ERR_DB_VERSION; + goto done; + } + } + /* we found no depends file after full scan */ + ret = 0; + +done: + if(dbdir) { + closedir(dbdir); + } + + return ret; +} + static int local_db_populate(pmdb_t *db) { size_t est_count; @@ -328,6 +378,7 @@ static int local_db_populate(pmdb_t *db) /* pm_errno set in _alpm_db_path() */ return -1; } + dbdir = opendir(dbpath); if(dbdir == NULL) { if(errno == ENOENT) { @@ -867,62 +918,9 @@ int _alpm_local_db_remove(pmdb_t *db, pmpkg_t *info) return ret; } -static int local_db_version(pmdb_t *db) -{ - struct dirent *ent = NULL; - const char *dbpath; - DIR *dbdir; - int version; - - dbpath = _alpm_db_path(db); - if(dbpath == NULL) { - RET_ERR(db->handle, PM_ERR_DB_OPEN, -1); - } - dbdir = opendir(dbpath); - if(dbdir == NULL) { - if(errno == ENOENT) { - /* database dir doesn't exist yet */ - version = 2; - goto done; - } else { - RET_ERR(db->handle, PM_ERR_DB_OPEN, -1); - } - } - - while((ent = readdir(dbdir)) != NULL) { - const char *name = ent->d_name; - char path[PATH_MAX]; - - if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { - continue; - } - if(!is_dir(dbpath, ent)) { - continue; - } - - snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name); - if(access(path, F_OK) == 0) { - /* we found a depends file- bail */ - version = 1; - goto done; - } - } - /* we found no depends file after full scan */ - version = 2; - -done: - if(dbdir) { - closedir(dbdir); - } - - _alpm_log(db->handle, PM_LOG_DEBUG, "local database version %d\n", version); - return version; -} - struct db_operations local_db_ops = { .populate = local_db_populate, .unregister = _alpm_db_unregister, - .version = local_db_version, }; pmdb_t *_alpm_db_register_local(pmhandle_t *handle) @@ -933,11 +931,18 @@ pmdb_t *_alpm_db_register_local(pmhandle_t *handle) db = _alpm_db_new("local", 1); if(db == NULL) { + handle->pm_errno = PM_ERR_DB_CREATE; return NULL; } db->ops = &local_db_ops; db->handle = handle; + if(local_db_validate(db)) { + /* pm_errno set in local_db_validate() */ + _alpm_db_free(db); + return NULL; + } + handle->db_local = db; return db; } -- cgit v1.2.3 From 79e98316ea89486d107466858543e965bcfbb0a9 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 7 Jun 2011 20:42:15 -0500 Subject: Add a 'valid' flag to the database object Start by converting all of our flags to a 'status' bitmask (pkgcache status, grpcache status). Add a new 'valid' flag as well. This will let us keep track if the database itself has been marked valid in whatever fashion. For local databases at the moment we ensure there are no depends files; for sync databases we ensure the PGP signature is valid if required/requested. The loading of the pkgcache is prohibited if the database is invalid. Signed-off-by: Dan McGee --- lib/libalpm/be_local.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/libalpm/be_local.c') diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c index 0ff51deb..96f04c51 100644 --- a/lib/libalpm/be_local.c +++ b/lib/libalpm/be_local.c @@ -321,6 +321,10 @@ static int local_db_validate(pmdb_t *db) DIR *dbdir; int ret = -1; + if(db->status & DB_STATUS_VALID) { + return 0; + } + dbpath = _alpm_db_path(db); if(dbpath == NULL) { RET_ERR(db->handle, PM_ERR_DB_OPEN, -1); @@ -329,6 +333,7 @@ static int local_db_validate(pmdb_t *db) if(dbdir == NULL) { if(errno == ENOENT) { /* database dir doesn't exist yet */ + db->status |= DB_STATUS_VALID; return 0; } else { RET_ERR(db->handle, PM_ERR_DB_OPEN, -1); @@ -354,6 +359,7 @@ static int local_db_validate(pmdb_t *db) } } /* we found no depends file after full scan */ + db->status |= DB_STATUS_VALID; ret = 0; done: -- cgit v1.2.3