Commit 607a9b0e authored by Bernhard Link's avatar Bernhard Link

putting .deb's in the file database

parent c22cc38d
......@@ -26,6 +26,7 @@
#include <getopt.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <zlib.h>
#include <db.h>
#include "error.h"
......@@ -169,66 +170,22 @@ static inline retvalue getvalue_d(const char *defaul,const char *chunk,const cha
return r;
}
static inline char *calcfilekey(const char *component,const char *package,
const char *source,const char *version,const char *arch) {
char *basename,*filekey;
//TODO: the following is anything but in-situ.
// think about if this has to be changed or is good the way it is.
basename = calc_package_basename(package,version,arch);
if( basename == NULL )
return NULL;
filekey = calc_filekey(component,source,basename);
free(basename);
return filekey;
}
static inline char *addfiledata(const char *chunk,const char *filekey,
int pkgsize,const char*pkgmd5) {
char *fieldstoadd,*newchunk;
//TODO: the following is anything but in-situ.
// think about if this has to be changed or is good the way it is.
fieldstoadd = mprintf("Filename: %s\nSize: %d\nMD5Sum: %s",
filekey,pkgsize,pkgmd5);
if( fieldstoadd == NULL )
return NULL;
newchunk = chunk_insertdata(chunk,"Description",fieldstoadd);
free(fieldstoadd);
return newchunk;
}
void deb_free(struct debpackage *pkg) {
if( pkg ) {
free(pkg->package);free(pkg->version);
free(pkg->source);free(pkg->arch);
free(pkg->filekey);free(pkg->control);
free(pkg->basename);free(pkg->control);
}
free(pkg);
}
retvalue deb_read(struct debpackage **pkg, const char *component, const char *filename) {
char *newchunk;
retvalue deb_read(struct debpackage **pkg, const char *filename) {
retvalue r;
char pkgmd5[33];off_t pkgsize;
struct debpackage *deb;
r = md5sum(pkgmd5,&pkgsize,filename,0);
// TODO: is RETNOTHING pssoible here?
if( !RET_IS_OK(r) )
return r;
deb = calloc(1,sizeof(struct debpackage));
deb->md5andsize = mprintf("%s %lu",pkgmd5,(long)pkgsize);
if( deb->md5andsize == NULL ) {
deb_free(deb);
return r;
}
r = extractcontrol(&deb->control,filename);
if( RET_WAS_ERROR(r) ) {
deb_free(deb);
......@@ -270,6 +227,12 @@ retvalue deb_read(struct debpackage **pkg, const char *component, const char *fi
return r;
}
deb->basename = calc_package_basename(deb->package,deb->version,deb->arch);
if( deb->basename == NULL ) {
deb_free(deb);
return r;
}
/* check for priority and section, compare with defaults
* and overrides */
......@@ -285,27 +248,38 @@ retvalue deb_read(struct debpackage **pkg, const char *component, const char *fi
deb_free(deb);
return r;
}
*pkg = deb;
//TODO: add checks here if section implies another component or
// do it another place??
/* Add Filename, md5sum and size-headers... */
deb->filekey = calcfilekey(component,deb->package,deb->source,deb->version,deb->arch);
if( deb->filekey == NULL ) {
deb_free(deb);
return r;
}
return RET_OK;
}
newchunk = addfiledata(deb->control,deb->filekey,pkgsize,pkgmd5);
if( newchunk == NULL ) {
deb_free(deb);
retvalue deb_complete(struct debpackage *pkg, const char *filekey, const char *md5andsize) {
retvalue r;
const char *size;
struct fieldtoadd *file;
size = md5andsize;
while( !isblank(*size) && *size )
size++;
file = addfield_newn("MD5Sum",md5andsize, size-md5andsize,NULL);
if( !file )
return RET_ERROR_OOM;
while( *size && isblank(*size) )
size++;
file = addfield_new("Size",size,file);
if( !file )
return RET_ERROR_OOM;
file = addfield_new("Filename",filekey,file);
if( !file )
return RET_ERROR_OOM;
// TODO: add overwriting of other fields here, (before the rest)
r = chunk_replacefields(&pkg->control,file,"Description");
addfield_free(file);
if( RET_WAS_ERROR(r) ) {
return r;
}
free(deb->control);
deb->control = newchunk;
*pkg = deb;
return RET_OK;
}
......
......@@ -18,12 +18,13 @@ retvalue checkindeb_insert( DB *references,const char *referee,
struct debpackage {
char *package,*version,*source,*arch;
char *filekey;
char *md5andsize;
char *basename;
char *control;
};
/* read the data from a .deb, add Filename, Size and md5sum to the control-item */
retvalue deb_read(struct debpackage **pkg, const char *component, const char *filename);
/* read the data from a .deb, make some checks and extract some data */
retvalue deb_read(struct debpackage **pkg, const char *filename);
/* do overwrites, add Filename, Size and md5sum to the control-item */
retvalue deb_complete(struct debpackage *pkg, const char *filekey, const char *md5andsize);
void deb_free(struct debpackage *pkg);
#endif
......@@ -30,6 +30,7 @@
#include "packages.h"
#include "names.h"
#include "files.h"
#include "copyfile.h"
#define CLEARDBT(dbt) {memset(&dbt,0,sizeof(dbt));}
#define SETDBT(dbt,datastr) {const char *my = datastr;memset(&dbt,0,sizeof(dbt));dbt.data=(void *)my;dbt.size=strlen(my)+1;}
......@@ -134,33 +135,45 @@ retvalue files_check(DB *filesdb,const char *filekey,const char *md5sum_and_size
}
}
/* look for file, calculate its md5sum and add it */
retvalue files_detect(DB *filesdb,const char *mirrordir,const char *filekey) {
char *filename,*md5andsize;
static retvalue files_calcmd5(char **md5andsize,const char *mirrordir,const char *filekey) {
char *filename;
retvalue ret;
filename = calc_fullfilename(mirrordir,filekey);
if( !filename )
return -1;
return RET_ERROR_OOM;
md5andsize = NULL;
ret = md5sum_and_size(&md5andsize,filename,0);
*md5andsize = NULL;
ret = md5sum_and_size(md5andsize,filename,0);
if( ret == RET_NOTHING ) {
fprintf(stderr,"Error accessing file \"%s\": %m\n",filename);
free(filename);
free(md5andsize);
return ret;
return RET_ERROR;
}
if( RET_WAS_ERROR(ret) ) {
fprintf(stderr,"Error checking file \"%s\": %m\n",filename);
free(filename);
free(md5andsize);
return ret;
}
free(filename);
if( verbose > 20 ) {
fprintf(stderr,"Md5sum of '%s' is '%s'.\n",filename,*md5andsize);
}
return ret;
}
/* look for file, calculate its md5sum and add it */
retvalue files_detect(DB *filesdb,const char *mirrordir,const char *filekey) {
char *md5andsize;
retvalue ret;
ret = files_calcmd5(&md5andsize,mirrordir,filekey);
if( RET_WAS_ERROR(ret) )
return ret;
ret = files_check(filesdb,filekey,md5andsize);
......@@ -296,3 +309,67 @@ retvalue files_foreach(DB* filesdb,per_file_action action,void *privdata) {
}
return result;
}
/* Copy file <origfilename> to <mirrordir>/<filekey> and add it to
* the database <filesdb>. Return RET_ERROR_WRONG_MD5 if already there
* with other md5sum, return other error when the file does not exists
* or the database had an error. return RET_NOTHING, if already there
* with correct md5sum. Return <md5andsize> with the data of this file,
* if no error (that is if RET_OK or RET_NOTHING) */
retvalue files_checkin(DB *filesdb,const char *mirrordir,const char *filekey,
const char *origfilename, char **md5andsize) {
int dbret;
DBT key,data;
retvalue r;
/* First check for possible entries in the database... */
SETDBT(key,filekey);
CLEARDBT(data);
if( (dbret = filesdb->get(filesdb, NULL, &key, &data, 0)) == 0){
if( verbose > 10 ) {
fprintf(stderr,"Database says: '%s' already here as '%s'!\n",filekey,(const char *)data.data);
}
r = files_calcmd5(md5andsize,mirrordir,filekey);
if( RET_WAS_ERROR(r) )
return r;
if( strcmp(*md5andsize,data.data) != 0 ) {
fprintf(stderr,"File \"%s\" is already registered with other md5sum!\n(expect: '%s', database:'%s')!\n",filekey,*md5andsize,(const char *)data.data);
free(*md5andsize);
*md5andsize = NULL;
return RET_ERROR_WRONG_MD5;
} else if( verbose >= 0 ) {
fprintf(stderr,"'%s' is already registered in the database, so doing nothing!\n",filekey);
return RET_NOTHING;
}
} else if( dbret != DB_NOTFOUND ){
filesdb->err(filesdb, dbret, "files.db:");
return RET_DBERR(dbret);
}
/* File is not yet in database, check if perhaps in tree */
// TODO
/* copy file in and calculate it's md5sum */
r = copyfile(mirrordir,filekey,origfilename);
if( RET_WAS_ERROR(r) ) {
fprintf(stderr,"Error copying file %s to %s/%s\n",origfilename,mirrordir,filekey);
return r;
}
r = files_calcmd5(md5andsize,mirrordir,filekey);
if( RET_WAS_ERROR(r) )
return r;
r = files_add(filesdb,filekey,*md5andsize);
if( RET_WAS_ERROR(r) ) {
free(*md5andsize);
*md5andsize = NULL;
return r;
}
return RET_OK;
}
......@@ -29,6 +29,15 @@ retvalue files_detect(DB *filesdb,const char *mirrordir,const char *filekey);
/* check for file in the database and if not found there, if it can be detected */
retvalue files_expect(DB *filesdb,const char *mirrordir,const char *filekey,const char *md5andsize);
/* Copy file <origfilename> to <mirrordir>/<filekey> and add it to
* the database <filesdb>. Return RET_ERROR_WRONG_MD5 if already there
* with other md5sum, return other error when the file does not exists
* or the database had an error. return RET_NOTHING, if already there
* with correct md5sum. Return <md5andsize> with the data of this file,
* if no error (that is if RET_OK or RET_NOTHING) */
retvalue files_checkin(DB *filesdb,const char *mirrordir,const char *filekey,
const char *origfilename, char **md5andsize);
typedef retvalue per_file_action(void *data,const char *filekey,const char *md5andsize);
/* callback for each registered file */
......
......@@ -40,7 +40,6 @@
#include "signature.h"
#include "extractcontrol.h"
#include "checkindeb.h"
#include "copyfile.h"
#ifndef STD_BASE_DIR
......@@ -1335,6 +1334,7 @@ static int adddeb(int argc,char *argv[]) {
retvalue r;
struct debpackage *pkg;
DB *files;
char *filekey,*md5andsize;
if( argc < 4 ) {
fprintf(stderr,"mirrorer _adddeb <distribution> <part> <package>\n");
......@@ -1343,12 +1343,17 @@ static int adddeb(int argc,char *argv[]) {
/* First taking a closer look to the file: */
r = deb_read(&pkg,argv[2],argv[3]);
r = deb_read(&pkg,argv[3]);
if( RET_WAS_ERROR(r) ) {
return EXIT_RET(r);
}
fprintf(stderr,"%s,%s,%s,%s\n%s",pkg->package,pkg->source,pkg->version,pkg->arch,pkg->control);
// TODO: look for overwrites and things like this here...
/* decide where it has to go */
filekey = calc_filekey(argv[2],pkg->source,pkg->basename);
/* then looking if we already have this, or copy it in */
......@@ -1356,30 +1361,25 @@ static int adddeb(int argc,char *argv[]) {
if( !files )
return 1;
r = files_expect(files,mirrordir,pkg->filekey,pkg->md5andsize);
r = files_checkin(files,mirrordir,filekey,argv[3],&md5andsize);
files_done(files);
if( RET_WAS_ERROR(r) ) {
fprintf(stderr,"Error looking for file %s in archive\n",pkg->filekey);
deb_free(pkg);
return r;
return EXIT_RET(r);
}
if( r == RET_NOTHING ) {
if( verbose > 1 ) {
fprintf(stderr,"Trying to add as '%s'\n",pkg->filekey);
}
r = copyfile(mirrordir,pkg->filekey,argv[3]);
if( RET_WAS_ERROR(r) ) {
fprintf(stderr,"Error moving file %s to %s/%s\n",argv[3],mirrordir,pkg->filekey);
deb_free(pkg);
return r;
}
// TODO: add it to database here.
// (hm, how to handle md5sum?)
}
files_done(files);
r = deb_complete(pkg,filekey,md5andsize);
if( RET_WAS_ERROR(r) ) {
deb_free(pkg);
return EXIT_RET(r);
}
/* finaly put it into a distribution */
/* Finaly add it to the database */
fprintf(stderr,"\n%s\n",pkg->control);
//TODO...
free(md5andsize);
deb_free(pkg);
return 0;
......
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