Add version file to empty local database

If a user manually creates the local database directory, or has an empty
local database for some other reason, we silently add a version file

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Allan McRae 2013-07-16 21:48:38 +10:00
parent 7e9ad22ac2
commit 1660ed3cf9
2 changed files with 35 additions and 12 deletions

View file

@ -398,8 +398,9 @@ static int local_db_validate(alpm_db_t *db)
{ {
struct dirent *ent = NULL; struct dirent *ent = NULL;
const char *dbpath; const char *dbpath;
char dbverpath[PATH_MAX];
FILE *dbverfile;
DIR *dbdir; DIR *dbdir;
int ret = -1;
if(db->status & DB_STATUS_VALID) { if(db->status & DB_STATUS_VALID) {
return 0; return 0;
@ -412,6 +413,7 @@ static int local_db_validate(alpm_db_t *db)
if(dbpath == NULL) { if(dbpath == NULL) {
RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1); RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
} }
dbdir = opendir(dbpath); dbdir = opendir(dbpath);
if(dbdir == NULL) { if(dbdir == NULL) {
if(errno == ENOENT) { if(errno == ENOENT) {
@ -435,6 +437,24 @@ static int local_db_validate(alpm_db_t *db)
db->status |= DB_STATUS_EXISTS; db->status |= DB_STATUS_EXISTS;
db->status &= ~DB_STATUS_MISSING; db->status &= ~DB_STATUS_MISSING;
snprintf(dbverpath, PATH_MAX, "%s.alpm_db_version", dbpath);
if((dbverfile = fopen(dbverpath, "r")) == NULL) {
/* create dbverfile if local database is empty - otherwise version error */
while((ent = readdir(dbdir)) != NULL) {
const char *name = ent->d_name;
if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
} else {
goto version_error;
}
}
local_db_add_version(db, dbpath);
goto version_latest;
}
fclose(dbverfile);
while((ent = readdir(dbdir)) != NULL) { while((ent = readdir(dbdir)) != NULL) {
const char *name = ent->d_name; const char *name = ent->d_name;
char path[PATH_MAX]; char path[PATH_MAX];
@ -449,23 +469,23 @@ static int local_db_validate(alpm_db_t *db)
snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name); snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
if(access(path, F_OK) == 0) { if(access(path, F_OK) == 0) {
/* we found a depends file- bail */ /* we found a depends file- bail */
db->status &= ~DB_STATUS_VALID; goto version_error;
db->status |= DB_STATUS_INVALID;
db->handle->pm_errno = ALPM_ERR_DB_VERSION;
goto done;
} }
} }
/* we found no depends file after full scan */ /* we found no depends file after full scan */
version_latest:
closedir(dbdir);
db->status |= DB_STATUS_VALID; db->status |= DB_STATUS_VALID;
db->status &= ~DB_STATUS_INVALID; db->status &= ~DB_STATUS_INVALID;
ret = 0; return 0;
done: version_error:
if(dbdir) { closedir(dbdir);
closedir(dbdir); db->status &= ~DB_STATUS_VALID;
} db->status |= DB_STATUS_INVALID;
db->handle->pm_errno = ALPM_ERR_DB_VERSION;
return ret; return -1;
} }
static int local_db_populate(alpm_db_t *db) static int local_db_populate(alpm_db_t *db)

View file

@ -180,6 +180,9 @@ class pmtest(object):
for pkg in self.db["local"].pkgs: for pkg in self.db["local"].pkgs:
vprint("\tinstalling %s" % pkg.fullname()) vprint("\tinstalling %s" % pkg.fullname())
pkg.install_package(self.root) pkg.install_package(self.root)
if self.db["local"].pkgs:
path = os.path.join(self.root, util.PM_DBPATH, "local")
util.mkfile(path, ".alpm_db_version", "9")
# Done. # Done.
vprint(" Taking a snapshot of the file system") vprint(" Taking a snapshot of the file system")