checkindsc.c 12.5 KB
Newer Older
Bernhard Link's avatar
Bernhard Link committed
1
/*  This file is part of "reprepro"
Bernhard Link's avatar
Bernhard Link committed
2
 *  Copyright (C) 2003,2004 Bernhard R. Link
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *  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
#include "reference.h"
#include "signature.h"
#include "sources.h"
#include "files.h"
#include "guesscomponent.h"
45 46 47

extern int verbose;

Bernhard Link's avatar
Bernhard Link committed
48 49 50
/* 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.
51

Bernhard Link's avatar
Bernhard Link committed
52 53 54 55 56
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))
57 58 59 60 61 62

* 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
63 64 65 66
* Get overwrite information, ecspecially
  the priority(if there is a binaries field,
  check the one with the highest) and the section 
  (...what else...?)
67

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

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

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

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

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

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

*/
82 83 84 85 86 87

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 ) {
88
		fprintf(stderr,"Missing '%s'-header in %s!\n",field,filename);
89 90 91 92 93 94 95 96 97 98
		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 ) {
99
		fprintf(stderr,"Cannot find '%s'-header in %s!\n",field,filename);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		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;
}

115 116 117 118 119 120 121 122 123 124
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...
125
	/* calculated by dsc_copyfiles or set by dsc_checkfiles */
126
	char *dscmd5sum;
127
	/* Things that may be calculated by dsc_calclocations: */
128 129 130 131 132
	char *directory, *dscbasename, *dscfilekey;
	struct strlist filekeys;
};

static void dsc_free(struct dscpackage *pkg) {
133 134 135
	if( pkg ) {
		free(pkg->package);free(pkg->version);
		free(pkg->control);
136 137 138 139 140 141 142
		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);
143 144 145 146
	}
	free(pkg);
}

147
static retvalue dsc_read(struct dscpackage **pkg, const char *filename) {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	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 */

165 166 167 168 169
	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;
	}
Bernhard Link's avatar
Bernhard Link committed
170 171
	if( RET_IS_OK(r) )
		r = properpackagename(dsc->package);
172 173 174 175 176 177 178 179 180
	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
181 182 183
	/* only recommended, so ignore errors with this: */
	(void) checkvalue(filename,dsc->control,"Standards-Version");

184
	r = getvalue(filename,dsc->control,"Version",&dsc->version);
Bernhard Link's avatar
Bernhard Link committed
185 186 187
	if( RET_IS_OK(r) ) {
		r = propername(dsc->version);
	}
188 189 190 191 192
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}

193
	r = getvalue_n(dsc->control,SECTION_FIELDNAME,&dsc->section);
194 195 196 197
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
198
	r = getvalue_n(dsc->control,PRIORITY_FIELDNAME,&dsc->priority);
199 200 201 202
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
203
	r = sources_parse_getmd5sums(dsc->control,&dsc->basenames,&dsc->md5sums);
Bernhard Link's avatar
Bernhard Link committed
204 205
	if( RET_IS_OK(r) )
		r = propernames(&dsc->basenames);
206 207 208 209
	if( RET_WAS_ERROR(r) ) {
		dsc_free(dsc);
		return r;
	}
210 211 212 213 214
	*pkg = dsc;

	return RET_OK;
}

215
static retvalue dsc_calclocations(struct dscpackage *pkg,const char *filekey,const char *basename,const char *directory) {
216 217 218 219
	retvalue r;

	assert( pkg != NULL && pkg->package != NULL && pkg->version != NULL );
	assert( pkg->component != NULL );
220 221 222 223 224

	if( basename )
		pkg->dscbasename = strdup(basename);
	else
		pkg->dscbasename = calc_source_basename(pkg->package,pkg->version);
225 226 227 228
	if( pkg->dscbasename == NULL ) {
		return RET_ERROR_OOM;
	}

229 230 231 232
	if( directory )
		pkg->directory = strdup(directory);
	else
		pkg->directory = calc_sourcedir(pkg->component,pkg->package);
233 234 235 236 237 238 239 240 241
	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;
	}
242 243 244 245
	if( filekey )
		pkg->dscfilekey = strdup(filekey);
	else
		pkg->dscfilekey = calc_dirconcat(pkg->directory,pkg->dscbasename);
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 272 273 274 275
	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;
}


276
static retvalue dsc_complete(struct dscpackage *pkg,const struct overrideinfo *override) {
277
	retvalue r;
278
	struct fieldtoadd *name;
279
	struct fieldtoadd *replace;
280
	char *newchunk,*newchunk2;
281
	char *newfilelines;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

	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;
	}

298 299
	r = sources_calcfilelines(&pkg->basenames,&pkg->md5sums,&newfilelines);
	if( RET_WAS_ERROR(r) ) {
300 301 302
		free(newchunk2);
		return RET_ERROR_OOM;
	}
303 304 305 306 307 308 309 310 311 312 313 314
	replace = addfield_new("Files",newfilelines,NULL);
	if( replace )
		replace = addfield_new("Directory",pkg->directory,replace);
	if( replace )
		replace = deletefield_new("Status",replace);
	if( replace )
		replace = addfield_new(SECTION_FIELDNAME,pkg->section,replace);
	if( replace )
		replace = addfield_new(PRIORITY_FIELDNAME,pkg->priority,replace);
	if( replace )
		replace = override_addreplacefields(override,replace);
	if( !replace ) {
315
		free(newfilelines);
316 317 318 319
		free(newchunk2);
		return RET_ERROR_OOM;
	}
	
320
	newchunk  = chunk_replacefields(newchunk2,replace,"Files");
321
	free(newfilelines);
322
	free(newchunk2);
323
	addfield_free(replace);
324 325 326 327 328 329 330 331 332 333
	if( newchunk == NULL ) {
		return RET_ERROR_OOM;
	}

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

	return RET_OK;
}

334 335
/* Get the files from the directory dscfilename is residing it, and copy
 * them into the pool, also setting pkg->dscmd5sum */
336
static retvalue dsc_copyfiles(filesdb filesdb,
337
			struct dscpackage *pkg,const char *dscfilename,int delete) {
338 339 340
	char *sourcedir;
	retvalue r;

341
	r = files_include(filesdb,dscfilename,pkg->dscfilekey,NULL,&pkg->dscmd5sum,delete);
342 343 344 345 346 347 348
	if( RET_WAS_ERROR(r) )
		return r;

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

349
	r = files_includefiles(filesdb,sourcedir,&pkg->basenames,&pkg->filekeys,&pkg->md5sums,delete);
350 351 352 353 354 355 356

	free(sourcedir);

	return r;
}

/* Check the files needed and set the required fields */
357
static retvalue dsc_checkfiles(filesdb filesdb,
358 359 360 361 362 363 364 365 366
			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;

367
	r = files_expectfiles(filesdb,&pkg->filekeys,&pkg->md5sums);
368 369 370 371

	return r;
}

372
/* insert the given .dsc into the mirror in <component> in the <distribution>
373 374 375 376
 * 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). */
377

378
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,const struct overrideinfo *srcoverride,int force,int delete){
379
	retvalue r;
380
	struct dscpackage *pkg;
381
	const struct overrideinfo *oinfo;
382 383 384 385 386 387 388

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

	r = dsc_read(&pkg,dscfilename);
	if( RET_WAS_ERROR(r) ) {
		return r;
	}
389 390 391 392 393 394 395 396 397

	oinfo = override_search(srcoverride,pkg->package);
	if( forcesection == NULL ) {
		forcesection = override_get(oinfo,SECTION_FIELDNAME);
	}
	if( forcepriority == NULL ) {
		forcepriority = override_get(oinfo,PRIORITY_FIELDNAME);
	}

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 428 429 430 431 432 433 434 435 436 437 438
	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;
		}
	}

	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);
	}

439
	r = dsc_calclocations(pkg,filekey,basename,directory);
440 441 442

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

443
	if( !RET_WAS_ERROR(r) ) {
444 445
		if( filekey && basename && directory && md5sum) {
			assert( delete == D_INPLACE );
446
			r = dsc_checkfiles(filesdb,pkg,md5sum);
447 448
		} else
			r = dsc_copyfiles(filesdb,pkg,dscfilename,delete);
449 450 451
	}

	/* Calculate the chunk to include: */
452
	
453 454 455 456
	if( !RET_WAS_ERROR(r) )
		r = dsc_adddsc(pkg);

	if( !RET_WAS_ERROR(r) )
457
		r = dsc_complete(pkg,oinfo);
458

459
	/* finaly put it into the source distribution */
460
	if( !RET_WAS_ERROR(r) ) {
461
		struct target *t = distribution_getpart(distribution,pkg->component,"source","dsc");
462
		r = target_initpackagesdb(t,dbdir);
463 464 465 466 467 468
		if( !RET_WAS_ERROR(r) ) {
			retvalue r2;
			r = target_addpackage(t,references,pkg->package,pkg->version,pkg->control,&pkg->filekeys,force,0);
			r2 = target_closepackagesdb(t);
			RET_ENDUPDATE(r,r2);
		}
469
	}
470 471
	dsc_free(pkg);

472
	return r;
473
}