checkindsc.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*  This file is part of "mirrorer" (TODO: find better title)
 *  Copyright (C) 2003 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 as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
17

18 19 20 21 22 23 24 25 26 27 28 29
#include <config.h>

#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <malloc.h>
30
#include <ctype.h>
31
#include <zlib.h>
32
#include <db.h>
33
#include "error.h"
34
#include "strlist.h"
35
#include "md5sum.h"
36 37
#include "names.h"
#include "dirs.h"
38 39
#include "chunks.h"
#include "checkindsc.h"
40 41 42 43 44 45
#include "reference.h"
#include "packages.h"
#include "signature.h"
#include "sources.h"
#include "files.h"
#include "guesscomponent.h"
46 47 48

extern int verbose;

Bernhard Link's avatar
Bernhard Link committed
49 50 51
/* This file includes the code to include sources, i.e.
 to create the chunk for the Sources.gz-file and 
 to put it in the various databases.
52

Bernhard Link's avatar
Bernhard Link committed
53 54 55 56 57
things to do with .dsc's checkin by hand: (by comparison with apt-ftparchive)
* Get all from .dsc (search the chunk with
  the Source:-field. end the chunk artifical
  before the pgp-end-block.(in case someone
  missed the newline there))
58 59 60 61 62 63

* check to have source,version,maintainer,
  standards-version, files. And also look
  at binary,architecture and build*, as
  described in policy 5.4

Bernhard Link's avatar
Bernhard Link committed
64 65 66 67
* Get overwrite information, ecspecially
  the priority(if there is a binaries field,
  check the one with the highest) and the section 
  (...what else...?)
68

Bernhard Link's avatar
Bernhard Link committed
69
* Rename Source-Field to Package-Field
70

Bernhard Link's avatar
Bernhard Link committed
71
* add dsc to files-list. (check other files md5sum and size)
72

Bernhard Link's avatar
Bernhard Link committed
73
* add Directory-field
74

Bernhard Link's avatar
Bernhard Link committed
75
* Add Priority and Status
76

Bernhard Link's avatar
Bernhard Link committed
77
* apply possible maintainer-updates from the overwrite-file
78 79
  or arbitrary tag changes from the extra-overwrite-file

Bernhard Link's avatar
Bernhard Link committed
80
* keep rest (perhaps sort alphabetical)
81 82

*/
83 84 85 86 87 88

static inline retvalue getvalue(const char *filename,const char *chunk,const char *field,char **value) {
	retvalue r;

	r = chunk_getvalue(chunk,field,value);
	if( r == RET_NOTHING ) {
89
		fprintf(stderr,"Missing '%s'-header in %s!\n",field,filename);
90 91 92 93 94 95 96 97 98 99
		r = RET_ERROR;
	}
	return r;
}

static inline retvalue checkvalue(const char *filename,const char *chunk,const char *field) {
	retvalue r;

	r = chunk_checkfield(chunk,field);
	if( r == RET_NOTHING ) {
100
		fprintf(stderr,"Cannot find '%s'-header in %s!\n",field,filename);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		r = RET_ERROR;
	}
	return r;
}

static inline retvalue getvalue_n(const char *chunk,const char *field,char **value) {
	retvalue r;

	r = chunk_getvalue(chunk,field,value);
	if( r == RET_NOTHING ) {
		*value = NULL;
	}
	return r;
}

116 117 118 119 120 121 122 123 124 125
struct dscpackage {
	/* things to be set by dsc_read: */
	char *package,*version;
	char *control;
	struct strlist basenames,md5sums;
	/* things that might still be NULL then: */
	char *section;
	char *priority;
	/* things that will still be NULL then: */
	char *component; //This might be const, too and save some strdups, but...
126
	/* calculated by dsc_copyfiles or set by dsc_checkfiles */
127
	char *dscmd5sum;
128
	/* Things that may be calculated by dsc_calclocations: */
129 130 131 132 133
	char *directory, *dscbasename, *dscfilekey;
	struct strlist filekeys;
};

static void dsc_free(struct dscpackage *pkg) {
134 135 136
	if( pkg ) {
		free(pkg->package);free(pkg->version);
		free(pkg->control);
137 138 139 140 141 142 143
		strlist_done(&pkg->basenames);strlist_done(&pkg->md5sums);
		free(pkg->section);
		free(pkg->priority);
		free(pkg->component);
		free(pkg->dscmd5sum);
		free(pkg->directory);free(pkg->dscbasename);free(pkg->dscfilekey);
		strlist_done(&pkg->filekeys);
144 145 146 147
	}
	free(pkg);
}

148
static retvalue dsc_read(struct dscpackage **pkg, const char *filename) {
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	retvalue r;
	struct dscpackage *dsc;


	dsc = calloc(1,sizeof(struct dscpackage));

	r = signature_readsignedchunk(filename,&dsc->control);
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
	if( verbose > 100 ) {
		fprintf(stderr,"Extracted control chunk from '%s': '%s'\n",filename,dsc->control);
	}

	/* first look for fields that should be there */

166 167 168 169 170
	r = chunk_getname(dsc->control,"Source",&dsc->package,0);
	if( r == RET_NOTHING ) {
		fprintf(stderr,"Missing 'Source'-header in %s!\n",filename);
		r = RET_ERROR;
	}
171 172 173 174 175 176 177 178 179
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
	r = checkvalue(filename,dsc->control,"Maintainer");
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
Bernhard Link's avatar
Bernhard Link committed
180 181 182
	/* only recommended, so ignore errors with this: */
	(void) checkvalue(filename,dsc->control,"Standards-Version");

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	r = getvalue(filename,dsc->control,"Version",&dsc->version);
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}

	r = getvalue_n(dsc->control,"Section",&dsc->section);
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
	r = getvalue_n(dsc->control,"Priority",&dsc->priority);
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
199 200 201 202 203
	r = sources_parse_getmd5sums(dsc->control,&dsc->basenames,&dsc->md5sums);
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
204 205 206 207 208
	*pkg = dsc;

	return RET_OK;
}

209
static retvalue dsc_calclocations(struct dscpackage *pkg,const char *filekey,const char *basename,const char *directory) {
210 211 212 213
	retvalue r;

	assert( pkg != NULL && pkg->package != NULL && pkg->version != NULL );
	assert( pkg->component != NULL );
214 215 216 217 218

	if( basename )
		pkg->dscbasename = strdup(basename);
	else
		pkg->dscbasename = calc_source_basename(pkg->package,pkg->version);
219 220 221 222
	if( pkg->dscbasename == NULL ) {
		return RET_ERROR_OOM;
	}

223 224 225 226
	if( directory )
		pkg->directory = strdup(directory);
	else
		pkg->directory = calc_sourcedir(pkg->component,pkg->package);
227 228 229 230 231 232 233 234 235
	if( pkg->directory == NULL ) {
		return RET_ERROR_OOM;
	}
	
	/* Calculate the filekeys: */
	r = calc_dirconcats(pkg->directory,&pkg->basenames,&pkg->filekeys);
	if( RET_WAS_ERROR(r) ) {
		return r;
	}
236 237 238 239
	if( filekey )
		pkg->dscfilekey = strdup(filekey);
	else
		pkg->dscfilekey = calc_dirconcat(pkg->directory,pkg->dscbasename);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	if( pkg->dscfilekey == NULL ) {
		return RET_ERROR_OOM;
	}
	return RET_OK;
}

/* Add the dsc-file to basenames,filekeys and md5sums, so that it will
 * be referenced and listed in the Sources.gz */
static retvalue dsc_adddsc(struct dscpackage *pkg) {
	retvalue r;

	r = strlist_include(&pkg->basenames,pkg->dscbasename);
	if( RET_WAS_ERROR(r) )
		return r;
	pkg->dscbasename = NULL;

	r = strlist_include(&pkg->md5sums,pkg->dscmd5sum);
	if( RET_WAS_ERROR(r) )
		return r;
	pkg->dscmd5sum = NULL;

	r = strlist_include(&pkg->filekeys,pkg->dscfilekey);
	if( RET_WAS_ERROR(r) )
		return r;
	pkg->dscfilekey = NULL;

	return RET_OK;
}


static retvalue dsc_complete(struct dscpackage *pkg) {
	retvalue r;
272 273 274
	struct fieldtoadd *name;
	struct fieldtoadd *dir;
	char *newchunk,*newchunk2;
275
	char *newfilelines;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

	assert(pkg->section != NULL && pkg->priority != NULL);

	/* first replace the "Source" with a "Package": */
	name = addfield_new("Package",pkg->package,NULL);
	if( !name )
		return RET_ERROR_OOM;
	name = deletefield_new("Source",name);
	if( !name )
		return RET_ERROR_OOM;
	newchunk2  = chunk_replacefields(pkg->control,name,"Format");
	addfield_free(name);
	if( newchunk2 == NULL ) {
		return RET_ERROR_OOM;
	}

292 293
	r = sources_calcfilelines(&pkg->basenames,&pkg->md5sums,&newfilelines);
	if( RET_WAS_ERROR(r) ) {
294 295 296
		free(newchunk2);
		return RET_ERROR_OOM;
	}
297 298 299 300 301 302 303 304 305
	dir = addfield_new("Files",newfilelines,NULL);
	if( dir )
		dir = addfield_new("Directory",pkg->directory,dir);
	if( dir )
		dir = deletefield_new("Status",dir);
	if( dir )
		dir = addfield_new("Section",pkg->section,dir);
	if( dir )
		dir = addfield_new("Priority",pkg->priority,dir);
306
	if( !dir ) {
307
		free(newfilelines);
308 309 310 311 312 313 314
		free(newchunk2);
		return RET_ERROR_OOM;
	}
		
	// TODO: add overwriting of other fields here, (before the rest)
	
	newchunk  = chunk_replacefields(newchunk2,dir,"Files");
315
	free(newfilelines);
316 317 318 319 320 321 322 323 324 325 326 327
	free(newchunk2);
	addfield_free(dir);
	if( newchunk == NULL ) {
		return RET_ERROR_OOM;
	}

	free(pkg->control);
	pkg->control = newchunk;

	return RET_OK;
}

328 329
/* Get the files from the directory dscfilename is residing it, and copy
 * them into the pool, also setting pkg->dscmd5sum */
330
static retvalue dsc_copyfiles(filesdb filesdb,
331 332 333 334
			struct dscpackage *pkg,const char *dscfilename) {
	char *sourcedir;
	retvalue r;

335
	r = files_checkin(filesdb,pkg->dscfilekey,dscfilename,&pkg->dscmd5sum);
336 337 338 339 340 341 342
	if( RET_WAS_ERROR(r) )
		return r;

	r = dirs_getdirectory(dscfilename,&sourcedir);
	if( RET_WAS_ERROR(r) )
		return r;

343
	r = files_checkinfiles(filesdb,sourcedir,&pkg->basenames,&pkg->filekeys,&pkg->md5sums);
344 345 346 347 348 349 350

	free(sourcedir);

	return r;
}

/* Check the files needed and set the required fields */
351
static retvalue dsc_checkfiles(filesdb filesdb,
352 353 354 355 356 357 358 359 360
			struct dscpackage *pkg,const char *dscmd5sum) {
	retvalue r;

	/* The code we got should have already put the .dsc in the pool
	 * and calculated its md5sum, so we just use it here: */
	pkg->dscmd5sum = strdup(dscmd5sum);
	if( pkg->dscmd5sum == NULL )
		return RET_ERROR_OOM;

361
	r = files_expectfiles(filesdb,&pkg->filekeys,&pkg->md5sums);
362 363 364 365

	return r;
}

366
/* insert the given .dsc into the mirror in <component> in the <distribution>
367 368 369 370
 * if component is NULL, guessing it from the section.
 * If basename, filekey and directory are != NULL, then they are used instead 
 * of beeing newly calculated. 
 * (And all files are expected to already be in the pool). */
371

372
retvalue dsc_add(const char *dbdir,DB *references,filesdb filesdb,const char *forcecomponent,const char *forcesection,const char *forcepriority,struct distribution *distribution,const char *dscfilename,const char *filekey,const char *basename,const char *directory,const char *md5sum,int force){
373
	retvalue r;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
	struct dscpackage *pkg;

	/* First taking a closer look to the file: */

	r = dsc_read(&pkg,dscfilename);
	if( RET_WAS_ERROR(r) ) {
		return r;
	}
	if( forcesection ) {
		free(pkg->section);
		pkg->section = strdup(forcesection);
		if( pkg->section == NULL ) {
			dsc_free(pkg);
			return RET_ERROR_OOM;
		}
	}
	if( forcepriority ) {
		free(pkg->priority);
		pkg->priority = strdup(forcepriority);
		if( pkg->priority == NULL ) {
			dsc_free(pkg);
			return RET_ERROR_OOM;
		}
	}

	/* look for overwrites */

	// TODO: look for overwrites and things like this here...
	// TODO: set pkg->section to new value if doing so.
	
	if( pkg->section == NULL ) {
		fprintf(stderr,"No section was given for '%s', skipping.\n",pkg->package);
		dsc_free(pkg);
		return RET_ERROR;
	}
	if( pkg->priority == NULL ) {
		fprintf(stderr,"No priority was given for '%s', skipping.\n",pkg->package);
		dsc_free(pkg);
		return RET_ERROR;
	}
	
	/* decide where it has to go */

	r = guess_component(distribution->codename,&distribution->components,
			pkg->package,pkg->section,forcecomponent,
			&pkg->component);
	if( RET_WAS_ERROR(r) ) {
		dsc_free(pkg);
		return r;
	}
	if( verbose > 0 && forcecomponent == NULL ) {
		fprintf(stderr,"%s: component guessed as '%s'\n",dscfilename,pkg->component);
	}

428
	r = dsc_calclocations(pkg,filekey,basename,directory);
429 430 431

	/* then looking if we already have this, or copy it in */

432
	if( !RET_WAS_ERROR(r) ) {
433
		if( filekey && basename && directory && md5sum)
434
			r = dsc_checkfiles(filesdb,pkg,md5sum);
435
		else
436
			r = dsc_copyfiles(filesdb,pkg,dscfilename);
437 438 439
	}

	/* Calculate the chunk to include: */
440
	
441 442 443 444 445
	if( !RET_WAS_ERROR(r) )
		r = dsc_adddsc(pkg);

	if( !RET_WAS_ERROR(r) )
		r = dsc_complete(pkg);
446

447
	/* finaly put it into the source distribution */
448
	if( !RET_WAS_ERROR(r) ) {
449
		struct target *t = distribution_getpart(distribution,pkg->component,"source");
450 451 452 453
		r = target_initpackagesdb(t,dbdir,NULL);
		if( !RET_WAS_ERROR(r) )
		r = target_addpackage(t,references,NULL,pkg->package,pkg->version,pkg->control,&pkg->filekeys,NULL,force,0);
	}
454 455
	dsc_free(pkg);

456
	return r;
457
}