binaries.c 20.7 KB
Newer Older
Bernhard Link's avatar
Bernhard Link committed
1
/*  This file is part of "reprepro"
2
 *  Copyright (C) 2003,2004,2005,2006,2007 Bernhard R. Link
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License version 2 as
Bernhard Link's avatar
Bernhard Link committed
5
 *  published by the Free Software Foundation.
6 7 8 9 10 11 12 13
 *
 *  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
Bernhard Link's avatar
Bernhard Link committed
14
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
15
 */
Bernhard Link's avatar
Bernhard Link committed
16 17 18 19
#include <config.h>

#include <errno.h>
#include <assert.h>
Bernhard Link's avatar
Bernhard Link committed
20 21
#include <string.h>
#include <strings.h>
22
#include <ctype.h>
Bernhard Link's avatar
Bernhard Link committed
23
#include <stdio.h>
24
#include <stdlib.h>
Bernhard Link's avatar
Bernhard Link committed
25
#include <malloc.h>
26
#include <sys/types.h>
Bernhard Link's avatar
Bernhard Link committed
27
#include "error.h"
28
#include "mprintf.h"
29
#include "strlist.h"
30
#include "names.h"
Bernhard Link's avatar
Bernhard Link committed
31
#include "chunks.h"
32
#include "sources.h"
Bernhard Link's avatar
Bernhard Link committed
33 34 35
#include "binaries.h"
#include "names.h"
#include "dpkgversions.h"
Bernhard Link's avatar
Bernhard Link committed
36
#include "log.h"
37
#include "override.h"
38
#include "tracking.h"
39
#include "debfile.h"
Bernhard Link's avatar
Bernhard Link committed
40 41 42

extern int verbose;

Bernhard Link's avatar
Bernhard Link committed
43 44
static const char * const deb_checksum_headers[cs_COUNT] = {
	"MD5sum", "SHA1", "SHA256", "Size"};
45

46 47 48 49 50 51 52 53 54 55 56 57 58
static char *calc_binary_basename(const char *name, const char *version, architecture_t arch, packagetype_t packagetype) {
	const char *v;
	assert( name != NULL && version != NULL && atom_defined(arch) && atom_defined(packagetype) );
	v = strchr(version, ':');
	if( v != NULL )
		v++;
	else
		v = version;
	return mprintf("%s_%s_%s.%s", name, v, atoms_architectures[arch],
			atoms_packagetypes[packagetype]);
}


59 60 61
/* get checksums out of a "Packages"-chunk. */
static retvalue binaries_parse_checksums(const char *chunk, /*@out@*/struct checksums **checksums_p) {
	retvalue result, r;
62
	char *checksums[cs_COUNT];
63
	enum checksumtype type;
Bernhard Link's avatar
Bernhard Link committed
64

65
	result = RET_NOTHING;
Bernhard Link's avatar
Bernhard Link committed
66

67
	for( type = 0 ; type < cs_COUNT ; type++ ) {
68 69 70 71
		checksums[type] = NULL;
		r = chunk_getvalue(chunk, deb_checksum_headers[type],
				&checksums[type]);
		RET_UPDATE(result, r);
Bernhard Link's avatar
Bernhard Link committed
72
	}
73
	if( checksums[cs_md5sum] == NULL ) {
Bernhard Link's avatar
Bernhard Link committed
74 75
		fprintf(stderr, "Missing 'MD5sum' line in binary control chunk:\n '%s'\n",
				chunk);
76
		RET_UPDATE(result, RET_ERROR_MISSING);
Bernhard Link's avatar
Bernhard Link committed
77
	}
78
	if( checksums[cs_length] == NULL ) {
Bernhard Link's avatar
Bernhard Link committed
79 80
		fprintf(stderr, "Missing 'Size' line in binary control chunk:\n '%s'\n",
				chunk);
81
		RET_UPDATE(result, RET_ERROR_MISSING);
Bernhard Link's avatar
Bernhard Link committed
82
	}
83
	if( RET_WAS_ERROR(result) ) {
84
		for( type = 0 ; type < cs_COUNT ; type++ )
85 86
			free(checksums[type]);
		return result;
Bernhard Link's avatar
Bernhard Link committed
87
	}
88
	return checksums_init(checksums_p, checksums);
Bernhard Link's avatar
Bernhard Link committed
89
}
Bernhard Link's avatar
Bernhard Link committed
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
retvalue binaries_getarchitecture(const char *chunk, architecture_t *architecture_p) {
	char *parch;
	retvalue r;

	r = chunk_getvalue(chunk, "Architecture", &parch);
	if( r == RET_NOTHING ) {
		fprintf(stderr, "Internal Error: Missing Architecture: header in '%s'!\n",
				chunk);
		return RET_ERROR;
	}
	if( RET_WAS_ERROR(r) )
		return r;
	*architecture_p = architecture_find(parch);
	free(parch);

	if( !atom_defined(*architecture_p) ) {
		fprintf(stderr, "Internal Error: Unexpected Architecture: header in '%s'!\n",
				chunk);
		return RET_ERROR;
	}
	return RET_OK;
}

Bernhard Link's avatar
Bernhard Link committed
114
/* get somefields out of a "Packages.gz"-chunk. returns RET_OK on success, RET_NOTHING if incomplete, error otherwise */
115
static retvalue binaries_parse_chunk(const char *chunk, const char *packagename, packagetype_t packagetype_atom, architecture_t package_architecture, const char *version, /*@out@*/char **sourcename_p, /*@out@*/char **basename_p) {
Bernhard Link's avatar
Bernhard Link committed
116 117
	retvalue r;
	char *mysourcename,*mybasename;
Bernhard Link's avatar
Bernhard Link committed
118

119
	assert(packagename!=NULL);
Bernhard Link's avatar
Bernhard Link committed
120 121

	/* get the sourcename */
122
	r = chunk_getname(chunk, "Source", &mysourcename, true);
Bernhard Link's avatar
Bernhard Link committed
123 124
	if( r == RET_NOTHING ) {
		mysourcename = strdup(packagename);
Bernhard Link's avatar
Bernhard Link committed
125
		if( mysourcename == NULL )
Bernhard Link's avatar
Bernhard Link committed
126 127 128 129
			r = RET_ERROR_OOM;
	}
	if( RET_WAS_ERROR(r) ) {
		return r;
Bernhard Link's avatar
Bernhard Link committed
130 131
	}

132 133
	r = properpackagename(packagename);
	if( !RET_WAS_ERROR(r) )
134
		r = properversion(version);
135 136 137
	if( RET_WAS_ERROR(r) ) {
		return r;
	}
138 139
	mybasename = calc_binary_basename(packagename, version,
			package_architecture, packagetype_atom);
Bernhard Link's avatar
Bernhard Link committed
140
	if( mybasename == NULL ) {
Bernhard Link's avatar
Bernhard Link committed
141 142
		free(mysourcename);
		return RET_ERROR_OOM;
Bernhard Link's avatar
Bernhard Link committed
143 144
	}

145 146
	*basename_p = mybasename;
	*sourcename_p = mysourcename;
Bernhard Link's avatar
Bernhard Link committed
147
	return RET_OK;
Bernhard Link's avatar
Bernhard Link committed
148 149
}

150
/* get files out of a "Packages.gz"-chunk. */
151
retvalue binaries_getfilekeys(const char *chunk, struct strlist *files) {
152 153
	retvalue r;
	char *filename;
154

155 156 157 158
	/* Read the filename given there */
	r = chunk_getvalue(chunk,"Filename",&filename);
	if( !RET_IS_OK(r) ) {
		if( r == RET_NOTHING ) {
Bernhard Link's avatar
Bernhard Link committed
159 160
			fprintf(stderr, "Data does not look like binary control: '%s'\n",
					chunk);
161 162 163 164 165 166 167 168
			r = RET_ERROR;
		}
		return r;
	}
	r = strlist_init_singleton(filename,files);
	return r;
}

169
static retvalue calcfilekeys(component_t component_atom, const char *sourcename, const char *basefilename, struct strlist *filekeys) {
170 171 172
	char *filekey;
	retvalue r;

173
	r = propersourcename(sourcename);
174 175 176
	if( RET_WAS_ERROR(r) ) {
		return r;
	}
177
	filekey = calc_filekey(component_atom, sourcename, basefilename);
Bernhard Link's avatar
Bernhard Link committed
178
	if( filekey == NULL )
179 180 181 182 183
		return RET_ERROR_OOM;
	r = strlist_init_singleton(filekey,filekeys);
	return r;
}

184
static inline retvalue calcnewcontrol(const char *chunk, const char *sourcename, const char *basefilename, component_t component_atom, struct strlist *filekeys, char **newchunk) {
185 186
	retvalue r;

187
	r = calcfilekeys(component_atom, sourcename, basefilename, filekeys);
188 189 190
	if( RET_WAS_ERROR(r) )
		return r;

191
	assert( filekeys->count == 1 );
192
	*newchunk = chunk_replacefield(chunk,"Filename",filekeys->values[0]);
193
	if( *newchunk == NULL ) {
194 195 196 197 198 199
		strlist_done(filekeys);
		return RET_ERROR_OOM;
	}
	return RET_OK;
}

200
retvalue binaries_getversion(const char *control, char **version) {
201 202 203 204 205 206
	retvalue r;

	r = chunk_getvalue(control,"Version",version);
	if( RET_WAS_ERROR(r) )
		return r;
	if( r == RET_NOTHING ) {
Bernhard Link's avatar
Bernhard Link committed
207
		fprintf(stderr, "Missing 'Version' field in chunk:'%s'\n", control);
208 209 210 211
		return RET_ERROR;
	}
	return r;
}
212

213
retvalue binaries_getinstalldata(const struct target *t, const char *packagename, const char *version, architecture_t package_architecture, const char *chunk, char **control, struct strlist *filekeys, struct checksumsarray *origfiles) {
214
	char *sourcename IFSTUPIDCC(=NULL), *basefilename IFSTUPIDCC(=NULL);
215
	struct checksumsarray origfilekeys;
216 217
	retvalue r;

218 219 220
	r = binaries_parse_chunk(chunk, packagename,
			t->packagetype_atom, package_architecture,
			version, &sourcename, &basefilename);
221 222 223
	if( RET_WAS_ERROR(r) ) {
		return r;
	} else if( r == RET_NOTHING ) {
Bernhard Link's avatar
Bernhard Link committed
224
		fprintf(stderr, "Does not look like a binary package: '%s'!\n", chunk);
225 226
		return RET_ERROR;
	}
227
	r = binaries_getchecksums(chunk, &origfilekeys);
228
	if( RET_WAS_ERROR(r) ) {
229
		free(sourcename); free(basefilename);
230 231
		return r;
	}
232

233
	r = calcnewcontrol(chunk, sourcename, basefilename,
234
			t->component_atom, filekeys, control);
235
	if( RET_WAS_ERROR(r) ) {
236
		checksumsarray_done(&origfilekeys);
Bernhard Link's avatar
Bernhard Link committed
237 238
	} else {
		assert( r != RET_NOTHING );
239
		checksumsarray_move(origfiles, &origfilekeys);
240
	}
241
	free(sourcename); free(basefilename);
242 243
	return r;
}
244

245
retvalue binaries_getchecksums(const char *chunk, struct checksumsarray *filekeys) {
246
	retvalue r;
247 248 249
	struct checksumsarray a;

	r = binaries_getfilekeys(chunk, &a.names);
250 251
	if( RET_WAS_ERROR(r) )
		return r;
252 253 254 255 256 257
	assert( a.names.count == 1 );
	a.checksums = malloc(sizeof(struct checksums *));
	if( a.checksums == NULL ) {
		strlist_done(&a.names);
		return RET_ERROR_OOM;
	}
258
	r = binaries_parse_checksums(chunk, a.checksums);
259 260 261 262
	assert( r != RET_NOTHING );
	if( RET_WAS_ERROR(r) ) {
		free(a.checksums);
		strlist_done(&a.names);
263
		return r;
264 265 266
	}
	checksumsarray_move(filekeys, &a);
	return RET_OK;
267
}
268

269
retvalue binaries_doreoverride(const struct distribution *distribution,const char *packagename,const char *controlchunk,/*@out@*/char **newcontrolchunk) {
270 271 272
	const struct overrideinfo *o;
	struct fieldtoadd *fields;
	char *newchunk;
273
	retvalue r;
274

275
	if( interrupted() )
276
		return RET_ERROR_INTERRUPTED;
277 278

	o = override_search(distribution->overrides.deb, packagename);
279 280 281
	if( o == NULL )
		return RET_NOTHING;

282 283 284 285
	r = override_allreplacefields(o, &fields);
	if( RET_WAS_ERROR(r) )
		return r;
	newchunk = chunk_replacefields(controlchunk, fields, "Filename");
286 287 288 289 290 291 292
	addfield_free(fields);
	if( newchunk == NULL )
		return RET_ERROR_OOM;
	*newcontrolchunk = newchunk;
	return RET_OK;
}

293
retvalue ubinaries_doreoverride(const struct distribution *distribution,const char *packagename,const char *controlchunk,/*@out@*/char **newcontrolchunk) {
294 295 296 297
	const struct overrideinfo *o;
	struct fieldtoadd *fields;
	char *newchunk;

Bernhard Link's avatar
Bernhard Link committed
298
	if( interrupted() )
299
		return RET_ERROR_INTERRUPTED;
300

301
	o = override_search(distribution->overrides.udeb, packagename);
302 303 304 305 306 307 308 309 310 311 312 313 314
	if( o == NULL )
		return RET_NOTHING;

	fields = override_addreplacefields(o,NULL);
	if( fields == NULL )
		return RET_ERROR_OOM;
	newchunk = chunk_replacefields(controlchunk,fields,"Description");
	addfield_free(fields);
	if( newchunk == NULL )
		return RET_ERROR_OOM;
	*newcontrolchunk = newchunk;
	return RET_OK;
}
315

316
retvalue binaries_retrack(const char *packagename, const char *chunk, trackingdb tracks, struct database *database) {
317 318 319 320 321 322 323 324 325
	retvalue r;
	const char *sourcename;
	char *fsourcename,*sourceversion,*arch,*filekey;
	enum filetype filetype;
	struct trackedpackage *pkg;

	//TODO: elliminate duplicate code!
	assert(packagename!=NULL);

Bernhard Link's avatar
Bernhard Link committed
326
	if( interrupted() )
327
		return RET_ERROR_INTERRUPTED;
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	/* is there a sourcename */
	r = chunk_getnameandversion(chunk,"Source",&fsourcename,&sourceversion);
	if( RET_WAS_ERROR(r) )
		return r;
	if( r == RET_NOTHING ) {
		sourceversion = NULL;
		sourcename = packagename;
		fsourcename = NULL;
	} else {
		sourcename = fsourcename;
	}
	if( sourceversion == NULL ) {
		// Think about binNMUs, can something be done here?
		r = chunk_getvalue(chunk,"Version",&sourceversion);
		if( RET_WAS_ERROR(r) ) {
			free(fsourcename);
			return r;
		}
		if( r == RET_NOTHING ) {
			free(fsourcename);
Bernhard Link's avatar
Bernhard Link committed
349 350
			fprintf(stderr, "Missing 'Version' field in chunk:'%s'\n",
					chunk);
351 352 353 354 355 356
			return RET_ERROR;
		}
	}

	r = chunk_getvalue(chunk,"Architecture",&arch);
	if( r == RET_NOTHING ) {
Bernhard Link's avatar
Bernhard Link committed
357 358
		fprintf(stderr, "No Architecture field in chunk:'%s'\n",
				chunk);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
		r = RET_ERROR;
	}
	if( RET_WAS_ERROR(r) ) {
		free(sourceversion);
		free(fsourcename);
		return r;
	}
	if( strcmp(arch,"all") == 0 ) {
		filetype = ft_ALL_BINARY;
	} else {
		filetype = ft_ARCH_BINARY;
	}
	free(arch);

	r = chunk_getvalue(chunk,"Filename",&filekey);
	if( !RET_IS_OK(r) ) {
		if( r == RET_NOTHING ) {
Bernhard Link's avatar
Bernhard Link committed
376 377
			fprintf(stderr, "No Filename field in chunk: '%s'\n",
					chunk);
378 379 380 381 382 383
			r = RET_ERROR;
		}
		free(sourceversion);
		free(fsourcename);
		return r;
	}
384 385 386
	r = tracking_getornew(tracks,sourcename,sourceversion,&pkg);
	free(fsourcename);
	free(sourceversion);
387 388 389 390
	if( RET_WAS_ERROR(r) ) {
		free(filekey);
		return r;
	}
391
	assert( r != RET_NOTHING );
392
	r = trackedpackage_addfilekey(tracks, pkg, filetype, filekey, true,
393
			database);
394
	if( RET_WAS_ERROR(r) ) {
395 396 397
		trackedpackage_free(pkg);
		return r;
	}
398
	return tracking_save(tracks, pkg);
399 400
}

401
retvalue binaries_getsourceandversion(const char *chunk, const char *packagename, char **source, char **version) {
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	retvalue r;
	char *sourcename,*sourceversion;

	//TODO: elliminate duplicate code!
	assert(packagename!=NULL);

	/* is there a sourcename */
	r = chunk_getnameandversion(chunk,"Source",&sourcename,&sourceversion);
	if( RET_WAS_ERROR(r) )
		return r;
	if( r == RET_NOTHING ) {
		sourceversion = NULL;
		sourcename = strdup(packagename);
		if( sourcename == NULL )
			return RET_ERROR_OOM;
	}
	if( sourceversion == NULL ) {
		r = chunk_getvalue(chunk,"Version",&sourceversion);
		if( RET_WAS_ERROR(r) ) {
			free(sourcename);
			return r;
		}
		if( r == RET_NOTHING ) {
			free(sourcename);
Bernhard Link's avatar
Bernhard Link committed
426
			fprintf(stderr, "No Version field in chunk:'%s'\n", chunk);
427 428 429 430 431 432 433
			return RET_ERROR;
		}
	}
	*source = sourcename;
	*version = sourceversion;
	return RET_OK;
}
434 435 436 437 438 439

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 ) {
Bernhard Link's avatar
Bernhard Link committed
440
		fprintf(stderr, "No %s field in %s's control file!\n",
Bernhard Link's avatar
Bernhard Link committed
441
				field, filename);
442 443 444 445 446 447 448 449 450 451
		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 ) {
Bernhard Link's avatar
Bernhard Link committed
452
		fprintf(stderr, "No %s field in %s's control file!\n",
Bernhard Link's avatar
Bernhard Link committed
453
				field, filename);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
		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;
}

void binaries_debdone(struct deb_headers *pkg) {
	free(pkg->name);free(pkg->version);
	free(pkg->source);free(pkg->sourceversion);
	free(pkg->control);
	free(pkg->section);
	free(pkg->priority);
}

477
retvalue binaries_readdeb(struct deb_headers *deb, const char *filename, bool needssourceversion) {
478
	retvalue r;
479
	char *architecture;
480 481 482 483 484 485

	r = extractcontrol(&deb->control,filename);
	if( RET_WAS_ERROR(r) )
		return r;
	/* first look for fields that should be there */

486
	r = chunk_getname(deb->control, "Package", &deb->name, false);
487
	if( r == RET_NOTHING ) {
Bernhard Link's avatar
Bernhard Link committed
488
		fprintf(stderr, "Missing 'Package' field in %s!\n", filename);
489 490 491 492 493 494 495 496 497 498 499 500 501
		r = RET_ERROR;
	}
	if( RET_WAS_ERROR(r) )
		return r;
	r = checkvalue(filename,deb->control,"Maintainer");
	if( RET_WAS_ERROR(r) )
		return r;
	r = checkvalue(filename,deb->control,"Description");
	if( RET_WAS_ERROR(r) )
		return r;
	r = getvalue(filename,deb->control,"Version",&deb->version);
	if( RET_WAS_ERROR(r) )
		return r;
502 503 504 505 506 507 508 509 510 511
	r = getvalue(filename, deb->control, "Architecture", &architecture);
	if( RET_WAS_ERROR(r) )
		return r;
	r = properfilenamepart(architecture);
	if( RET_WAS_ERROR(r) ) {
		free(architecture);
		return r;
	}
	r = architecture_intern(architecture, &deb->architecture_atom);
	free(architecture);
512 513 514 515 516 517
	if( RET_WAS_ERROR(r) )
		return r;
	/* can be there, otherwise we also know what it is */
	if( needssourceversion )
		r = chunk_getnameandversion(deb->control,"Source",&deb->source,&deb->sourceversion);
	else
518
		r = chunk_getname(deb->control, "Source", &deb->source, true);
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	if( r == RET_NOTHING ) {
		deb->source = strdup(deb->name);
		if( deb->source == NULL )
			r = RET_ERROR_OOM;
	}
	if( RET_WAS_ERROR(r) )
		return r;
	if( needssourceversion && deb->sourceversion == NULL ) {
		deb->sourceversion = strdup(deb->version);
		if( deb->sourceversion == NULL )
			return RET_ERROR_OOM;
	}

	/* normaly there, but optional: */

	r = getvalue_n(deb->control,PRIORITY_FIELDNAME,&deb->priority);
	if( RET_WAS_ERROR(r) )
		return r;
	r = getvalue_n(deb->control,SECTION_FIELDNAME,&deb->section);
	if( RET_WAS_ERROR(r) )
		return r;
	return RET_OK;
}

543
/* do overwrites, add Filename and Checksums to the control-item */
544
retvalue binaries_complete(const struct deb_headers *pkg, const char *filekey, const struct checksums *checksums, const struct overrideinfo *override, const char *section, const char *priority, char **newcontrol) {
545 546
	struct fieldtoadd *replace;
	char *newchunk;
547
	enum checksumtype type;
548 549

	assert( section != NULL && priority != NULL);
550 551 552
	assert( filekey != NULL && checksums != NULL);

	replace = NULL;
553
	for( type = 0 ; type < cs_COUNT ; type++ ) {
554 555
		const char *start;
		size_t len;
556
		if( checksums_getpart(checksums, type, &start, &len) ) {
557 558 559 560 561 562 563
			replace = addfield_newn(deb_checksum_headers[type],
					start, len, replace);
			if( replace == NULL )
				return RET_ERROR_OOM;
		}
	}
	replace = addfield_new("Filename", filekey, replace);
564 565
	if( replace == NULL )
		return RET_ERROR_OOM;
566
	replace = addfield_new(SECTION_FIELDNAME, section, replace);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	if( replace == NULL )
		return RET_ERROR_OOM;
	replace = addfield_new(PRIORITY_FIELDNAME, priority, replace);
	if( replace == NULL )
		return RET_ERROR_OOM;

	replace = override_addreplacefields(override,replace);
	if( replace == NULL )
		return RET_ERROR_OOM;

	newchunk  = chunk_replacefields(pkg->control,replace,"Description");
	addfield_free(replace);
	if( newchunk == NULL ) {
		return RET_ERROR_OOM;
	}

583
	*newcontrol = newchunk;
584 585 586 587

	return RET_OK;
}

588
retvalue binaries_adddeb(const struct deb_headers *deb, struct database *database, architecture_t forcearchitecture, packagetype_t packagetype, struct distribution *distribution, struct strlist *dereferencedfilekeys, struct trackingdata *trackingdata, component_t component, const struct strlist *filekeys, bool *usedmarker, const char *control) {
589 590 591
	retvalue r,result;
	int i;

Bernhard Link's avatar
Bernhard Link committed
592 593
	assert( logger_isprepared(distribution->logger) );

594 595 596 597
	/* finally put it into one or more architectures of the distribution */

	result = RET_NOTHING;

598
	if( deb->architecture_atom != architecture_all ) {
599
		struct target *t = distribution_getpart(distribution,
600
				component, deb->architecture_atom,
601
				packagetype);
602
		r = target_initpackagesdb(t, database, READWRITE);
603 604 605
		if( !RET_WAS_ERROR(r) ) {
			retvalue r2;
			if( interrupted() )
606
				r = RET_ERROR_INTERRUPTED;
607
			else
Bernhard Link's avatar
Bernhard Link committed
608
				r = target_addpackage(t, distribution->logger,
609 610
						database,
						deb->name, deb->version,
611
						control,
612 613
						filekeys, usedmarker,
						false,
614
						dereferencedfilekeys,
615 616
						trackingdata,
						deb->architecture_atom);
617 618 619 620
			r2 = target_closepackagesdb(t);
			RET_ENDUPDATE(r,r2);
		}
		RET_UPDATE(result,r);
621
	} else if( atom_defined(forcearchitecture) && forcearchitecture != architecture_all ) {
622 623 624
		struct target *t = distribution_getpart(distribution,
				component, forcearchitecture,
				packagetype);
625
		r = target_initpackagesdb(t, database, READWRITE);
626 627 628
		if( !RET_WAS_ERROR(r) ) {
			retvalue r2;
			if( interrupted() )
629
				r = RET_ERROR_INTERRUPTED;
630
			else
Bernhard Link's avatar
Bernhard Link committed
631
				r = target_addpackage(t, distribution->logger,
632 633
						database,
						deb->name, deb->version,
634
						control,
635 636
						filekeys, usedmarker,
						false,
637
						dereferencedfilekeys,
638 639
						trackingdata,
						deb->architecture_atom);
640 641 642 643 644 645
			r2 = target_closepackagesdb(t);
			RET_ENDUPDATE(r,r2);
		}
		RET_UPDATE(result,r);
	} else for( i = 0 ; i < distribution->architectures.count ; i++ ) {
		/*@dependent@*/struct target *t;
646 647 648
		architecture_t a = distribution->architectures.atoms[i];

		if( a == architecture_source )
649
			continue;
650 651
		t = distribution_getpart(distribution,
				component, a, packagetype);
652
		r = target_initpackagesdb(t, database, READWRITE);
653 654 655
		if( !RET_WAS_ERROR(r) ) {
			retvalue r2;
			if( interrupted() )
656
				r = RET_ERROR_INTERRUPTED;
657
			else
Bernhard Link's avatar
Bernhard Link committed
658
				r = target_addpackage(t, distribution->logger,
659 660
						database,
						deb->name, deb->version,
661
						control,
662 663
						filekeys, usedmarker,
						false,
664
						dereferencedfilekeys,
665 666
						trackingdata,
						deb->architecture_atom);
667 668 669 670 671 672 673 674 675 676
			r2 = target_closepackagesdb(t);
			RET_ENDUPDATE(r,r2);
		}
		RET_UPDATE(result,r);
	}
	RET_UPDATE(distribution->status, result);

	return result;
}

677
static inline retvalue checkadddeb(struct database *database, struct distribution *distribution, component_t component, architecture_t architecture, packagetype_t packagetype, bool tracking, const struct deb_headers *deb, bool permitnewerold) {
678 679 680 681 682 683
	retvalue r;
	struct target *t;

	t = distribution_getpart(distribution,
			component, architecture, packagetype);
	assert( t != NULL );
684
	r = target_initpackagesdb(t, database, READONLY);
685 686 687
	if( !RET_WAS_ERROR(r) ) {
		retvalue r2;
		if( interrupted() )
688
			r = RET_ERROR_INTERRUPTED;
689 690
		else
			r = target_checkaddpackage(t,
691 692
					deb->name, deb->version,
					tracking,
693 694 695 696 697 698 699
					permitnewerold);
		r2 = target_closepackagesdb(t);
		RET_ENDUPDATE(r,r2);
	}
	return r;
}

700
retvalue binaries_checkadddeb(const struct deb_headers *deb, struct database *database, architecture_t forcearchitecture, packagetype_t packagetype, struct distribution *distribution, bool tracking, component_t component, bool permitnewerold) {
701 702 703 704 705 706
	retvalue r,result;
	int i;

	/* finally put it into one or more architectures of the distribution */
	result = RET_NOTHING;

707
	if( deb->architecture_atom != architecture_all ) {
708
		r = checkadddeb(database, distribution,
709
				component, deb->architecture_atom, packagetype,
710 711 712
				tracking, deb,
				permitnewerold);
		RET_UPDATE(result,r);
713
	} else if( atom_defined(forcearchitecture) && forcearchitecture != architecture_all ) {
714
		r = checkadddeb(database, distribution,
715 716 717 718 719
				component, forcearchitecture, packagetype,
				tracking, deb,
				permitnewerold);
		RET_UPDATE(result,r);
	} else for( i = 0 ; i < distribution->architectures.count ; i++ ) {
720 721
		architecture_t a = distribution->architectures.atoms[i];
		if( a == architecture_source )
722
			continue;
723
		r = checkadddeb(database, distribution,
724 725 726 727 728 729 730 731
				component, a, packagetype,
				tracking, deb,
				permitnewerold);
		RET_UPDATE(result,r);
	}
	return result;
}

732
retvalue binaries_calcfilekeys(component_t component, const struct deb_headers *deb, packagetype_t packagetype, struct strlist *filekeys) {
733
	retvalue r;
734
	char *basefilename;
735

736
	basefilename = calc_binary_basename(deb->name, deb->version,
737
			deb->architecture_atom, packagetype);
738
	if( FAILEDTOALLOC(basefilename) )
739 740
		return RET_ERROR_OOM;

741 742
	r = calcfilekeys(component, deb->source, basefilename, filekeys);
	free(basefilename);
743 744
	return r;
}
745