Commit 759bf82f authored by Bernhard Link's avatar Bernhard Link

start of some refactoring

parent 320973f2
......@@ -16,12 +16,12 @@ AM_CPPFLAGS = -D_GNU_SOURCE=1 -Wall $(ARCHIVECPP) $(DBCPPFLAGS)
reprepro_LDADD = $(ARCHIVELIBS) $(DBLIBS)
changestool_LDADD = $(ARCHIVELIBS)
reprepro_SOURCES = freespace.c log.c changes.c incoming.c uploaderslist.c guesscomponent.c files.c md5.c md5sum.c dirs.c chunks.c reference.c packages.c binaries.c sources.c names.c dpkgversions.c release.c mprintf.c updates.c strlist.c signature.c distribution.c checkindeb.c checkindsc.c checkin.c copyfile.c upgradelist.c target.c aptmethod.c downloadcache.c main.c override.c terms.c ignore.c filterlist.c exports.c tracking.c optionsfile.c readrelease.c donefile.c pull.c contents.c filelist.c $(ARCHIVE_USED)
reprepro_SOURCES = database.c freespace.c log.c changes.c incoming.c uploaderslist.c guesscomponent.c files.c md5.c md5sum.c dirs.c chunks.c reference.c packages.c binaries.c sources.c names.c dpkgversions.c release.c mprintf.c updates.c strlist.c signature.c distribution.c checkindeb.c checkindsc.c checkin.c copyfile.c upgradelist.c target.c aptmethod.c downloadcache.c main.c override.c terms.c ignore.c filterlist.c exports.c tracking.c optionsfile.c readrelease.c donefile.c pull.c contents.c filelist.c $(ARCHIVE_USED)
EXTRA_reprepro_SOURCE = $(ARCHIVE_UNUSED)
changestool_SOURCES = tool.c chunkedit.c strlist.c md5sum.c md5.c mprintf.c chunks.c signature.c dirs.c ignore.c names.c copyfile.c $(ARCHIVE_USED)
noinst_HEADERS = freespace.h log.h changes.h incoming.h guesscomponent.h md5.h md5sum.h dirs.h files.h chunks.h reference.h packages.h binaries.h sources.h names.h release.h error.h mprintf.h updates.h strlist.h signature.h distribution.h debfile.h checkindeb.h checkindsc.h copyfile.h upgradelist.h target.h aptmethod.h downloadcache.h override.h terms.h ignore.h filterlist.h dpkgversions.h checkin.h exports.h globals.h tracking.h trackingt.h optionsfile.h readrelease.h donefile.h pull.h ar.h filelist.h contents.h chunkedit.h uploaderslist.h
noinst_HEADERS = database_p.h database.h freespace.h log.h changes.h incoming.h guesscomponent.h md5.h md5sum.h dirs.h files.h chunks.h reference.h packages.h binaries.h sources.h names.h release.h error.h mprintf.h updates.h strlist.h signature.h distribution.h debfile.h checkindeb.h checkindsc.h copyfile.h upgradelist.h target.h aptmethod.h downloadcache.h override.h terms.h ignore.h filterlist.h dpkgversions.h checkin.h exports.h globals.h tracking.h trackingt.h optionsfile.h readrelease.h donefile.h pull.h ar.h filelist.h contents.h chunkedit.h uploaderslist.h
MAINTAINERCLEANFILES = Makefile.in configure install-sh stamp-h.in aclocal.m4 config.h.in mkinstalldirs config.guess config.sub missing
......
/* This file is part of "reprepro"
* Copyright (C) 2007 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "globals.h"
#include "error.h"
#include "strlist.h"
#include "names.h"
#include "database.h"
#include "dirs.h"
#include "files.h"
#include "reference.h"
#include "database_p.h"
extern int verbose;
static void database_free(struct database *db) {
if( db == NULL )
return;
free(db->directory);
free(db);
}
retvalue database_create(struct database **result, const char *dbdir) {
struct database *n;
n = calloc(1, sizeof(struct database));
if( n == NULL )
return RET_ERROR_OOM;
n->directory = strdup(dbdir);
if( n->directory == NULL ) {
free(n);
return RET_ERROR_OOM;
}
*result = n;
return RET_OK;
}
/**********************/
/* lock file handling */
/**********************/
retvalue database_lock(struct database *db, size_t waitforlock) {
char *lockfile;
int fd;
retvalue r;
size_t tries = 0;
// TODO: create directory
r = dirs_make_recursive(db->directory);
if( RET_WAS_ERROR(r) )
return r;
lockfile = calc_dirconcat(db->directory, "lockfile");
if( lockfile == NULL )
return RET_ERROR_OOM;
fd = open(lockfile,O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY,S_IRUSR|S_IWUSR);
while( fd < 0 ) {
int e = errno;
if( e == EEXIST ) {
if( tries < waitforlock && ! interrupted() ) {
if( verbose >= 0 )
printf("Could not aquire lock: %s already exists!\nWaiting 10 seconds before trying again.\n", lockfile);
sleep(10);
tries++;
fd = open(lockfile,O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY,S_IRUSR|S_IWUSR);
continue;
}
fprintf(stderr,
"The lockfile '%s' already exists, there might be another instance with the\n"
"same database dir running. To avoid locking overhead, only one process\n"
"can access the database at the same time. Only delete the lockfile if\n"
"you are sure no other version is still running!\n", lockfile);
} else
fprintf(stderr,"Error creating lockfile '%s': %d=%m!\n",lockfile,e);
free(lockfile);
return RET_ERRNO(e);
}
// TODO: do some more locking of this file to avoid problems
// with the non-atomity of O_EXCL with nfs-filesystems...
if( close(fd) != 0 ) {
int e = errno;
fprintf(stderr,"(Late) Error creating lockfile '%s': %d=%m!\n",lockfile,e);
(void)unlink(lockfile);
free(lockfile);
return RET_ERRNO(e);
}
free(lockfile);
db->locked = TRUE;
return RET_OK;
}
static void releaselock(struct database *db) {
char *lockfile;
lockfile = calc_dirconcat(db->directory, "lockfile");
if( lockfile == NULL )
return;
if( unlink(lockfile) != 0 ) {
int e = errno;
fprintf(stderr,"Error deleting lockfile '%s': %d=%m!\n",lockfile,e);
(void)unlink(lockfile);
}
free(lockfile);
db->locked = FALSE;
}
retvalue database_close(struct database *db) {
retvalue result = RET_OK, r;
if( db->references != NULL) {
r = references_done(db->references);
db->references = NULL;
RET_UPDATE(result, r);
}
if( db->files ) {
r = files_done(db->files);
db->files = NULL;
RET_UPDATE(result, r);
}
if( db->locked )
releaselock(db);
database_free(db);
return RET_OK;
}
retvalue database_openfiles(struct database *db, const char *mirrordir) {
retvalue r;
assert( db->files == NULL );
r = files_initialize(&db->files, db->directory, mirrordir);
if( !RET_IS_OK(r) )
db->files = NULL;
return r;
}
retvalue database_openreferences(struct database *db) {
retvalue r;
assert( db->references == NULL );
r = references_initialize(&db->references, db->directory);
if( !RET_IS_OK(r) )
db->references = NULL;
return r;
}
#ifndef REPREPRO_DATABASE_H
#define REPREPRO_DATABASE_H
#ifndef REPREPRO_GLOBALS_H
#include "globals.h"
#endif
#ifndef REPREPRO_ERROR_H
#include "error.h"
#endif
struct database;
retvalue database_create(struct database **, const char *dbdir);
retvalue database_lock(struct database *, size_t waitforlock);
retvalue database_close(struct database *);
retvalue database_openfiles(struct database *, const char *mirrordir);
retvalue database_openreferences(struct database *);
#endif
#ifndef REPREPRO_DATABASE_P_H
#define REPREPRO_DATABASE_P_H
#ifndef REPREPRO_DATABASE_H
#include "database.h"
#endif
struct references;
struct filesdb;
struct database {
char *directory;
struct filesdb *files;
struct references *references;
bool_t locked;
};
#if LIBDB_VERSION == 44
#define DB_OPEN(database,filename,name,type,flags) database->open(database,NULL,filename,name,type,flags,0664)
#elif LIBDB_VERSION == 43
#define DB_OPEN(database,filename,name,type,flags) database->open(database,NULL,filename,name,type,flags,0664)
#else
#if LIBDB_VERSION == 3
#define DB_OPEN(database,filename,name,type,flags) database->open(database,filename,name,type,flags,0664)
#else
#error Unexpected LIBDB_VERSION!
#endif
#endif
#endif
......@@ -25,17 +25,17 @@ struct target;
struct logger;
struct upgradelist;
retvalue upgradelist_initialize(struct upgradelist **ul,/*@dependent@*/struct target *target,const char *dbdir);
retvalue upgradelist_initialize(struct upgradelist **ul,/*@dependent@*/struct target *target,struct database *);
retvalue upgradelist_free(/*@only@*/struct upgradelist *upgrade);
void upgradelist_dump(struct upgradelist *upgrade);
retvalue upgradelist_listmissing(struct upgradelist *upgrade,filesdb files);
retvalue upgradelist_listmissing(struct upgradelist *upgrade,struct database *);
/* Take all items in 'filename' into account, and remember them coming from 'method' */
retvalue upgradelist_update(struct upgradelist *upgrade,/*@dependent@*/struct aptmethod *method,const char *filename,upgrade_decide_function *predecide,void *decide_data);
/* Take all items in source into account */
retvalue upgradelist_pull(struct upgradelist *upgrade,struct target *source,upgrade_decide_function *predecide,void *decide_data,const char *dbdir);
retvalue upgradelist_pull(struct upgradelist *upgrade,struct target *source,upgrade_decide_function *predecide,void *decide_data,struct database *);
/* mark all packages as deleted, so they will vanis unless readded or reholded */
retvalue upgradelist_deleteall(struct upgradelist *upgrade);
......@@ -45,11 +45,11 @@ retvalue upgradelist_deleteall(struct upgradelist *upgrade);
//longer available upstream)
/* request all wanted files refering the methods given before */
retvalue upgradelist_enqueue(struct upgradelist *upgrade,struct downloadcache *cache,filesdb filesdb);
retvalue upgradelist_enqueue(struct upgradelist *upgrade,struct downloadcache *cache,struct database *);
retvalue upgradelist_install(struct upgradelist *upgrade,/*@null@*/struct logger *,const char *dbdir,filesdb files,references refs,bool_t ignoredelete, struct strlist *dereferencedfilekeys);
retvalue upgradelist_install(struct upgradelist *upgrade,/*@null@*/struct logger *,struct database *,bool_t ignoredelete, struct strlist *dereferencedfilekeys);
/* remove all packages that would either be removed or upgraded by an upgrade */
retvalue upgradelist_predelete(struct upgradelist *upgrade,/*@null@*/struct logger *logger,const char *dbdir,references refs,struct strlist *dereferencedfilekeys);
retvalue upgradelist_predelete(struct upgradelist *upgrade,/*@null@*/struct logger *logger,struct database *,struct strlist *dereferencedfilekeys);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment