pull.c 26.7 KB
Newer Older
1
/*  This file is part of "reprepro"
2
 *  Copyright (C) 2006,2007 Bernhard R. Link
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *  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 <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
Bernhard Link's avatar
Bernhard Link committed
25
#include "ignore.h"
26 27 28 29 30 31 32 33
#include "mprintf.h"
#include "strlist.h"
#include "names.h"
#include "pull.h"
#include "upgradelist.h"
#include "distribution.h"
#include "terms.h"
#include "filterlist.h"
Bernhard Link's avatar
Bernhard Link committed
34
#include "log.h"
Bernhard Link's avatar
Bernhard Link committed
35
#include "configparser.h"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/***************************************************************************
 * step one:                                                               *
 * parse CONFDIR/pull to get pull information saved in                     *
 * pull_rule structs                                                    *
 **************************************************************************/

/* the data for some upstream part to get pull from, some
 * some fields can be NULL or empty */
struct pull_rule {
	struct pull_rule *next;
	//e.g. "Name: woody"
	char *name;
	//e.g. "From: woody"
	char *from;
51
	//e.g. "Architectures: i386 sparc mips" (not set means all)
52 53
	struct atomlist architectures_from;
	struct atomlist architectures_into;
54 55
	bool architectures_set;
	//e.g. "Components: main contrib" (not set means all)
56
	struct atomlist components;
57 58
	bool components_set;
	//e.g. "UDebComponents: main" // (not set means all)
59
	struct atomlist udebcomponents;
60
	bool udebcomponents_set;
61 62 63 64 65
	// NULL means no condition
	/*@null@*/term *includecondition;
	struct filterlist filterlist;
	/*----only set after _addsourcedistribution----*/
	/*@NULL@*/ struct distribution *distribution;
66
	bool used;
67 68 69 70 71 72 73
};

static void pull_rule_free(/*@only@*/struct pull_rule *pull) {
	if( pull == NULL )
		return;
	free(pull->name);
	free(pull->from);
74 75 76 77
	atomlist_done(&pull->architectures_from);
	atomlist_done(&pull->architectures_into);
	atomlist_done(&pull->components);
	atomlist_done(&pull->udebcomponents);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	term_free(pull->includecondition);
	filterlist_release(&pull->filterlist);
	free(pull);
}

void pull_freerules(struct pull_rule *p) {
	while( p != NULL ) {
		struct pull_rule *rule;

		rule = p;
		p = rule->next;
		pull_rule_free(rule);
	}
}

Bernhard Link's avatar
Bernhard Link committed
93 94 95
CFlinkedlistinit(pull_rule)
CFvalueSETPROC(pull_rule, name)
CFvalueSETPROC(pull_rule, from)
96 97
CFatomlistSETPROC(pull_rule, components, at_component)
CFatomlistSETPROC(pull_rule, udebcomponents, at_component)
Bernhard Link's avatar
Bernhard Link committed
98 99 100
CFfilterlistSETPROC(pull_rule, filterlist)
CFtermSETPROC(pull_rule, includecondition)

101 102 103 104 105
CFUSETPROC(pull_rule, architectures) {
	CFSETPROCVAR(pull_rule, this);
	retvalue r;

	this->architectures_set = true;
106 107
	r = config_getsplitatoms(iter, "Architectures",
			at_architecture,
108 109 110 111 112 113 114 115 116 117 118 119
			&this->architectures_from,
			&this->architectures_into);
	if( r == RET_NOTHING ) {
		fprintf(stderr,
"Warning parsing %s, line %u: an empty Architectures field\n"
"causes the whole rule to do nothing.\n",
				config_filename(iter),
				config_markerline(iter));
	}
	return r;
}

Bernhard Link's avatar
Bernhard Link committed
120 121 122 123 124 125 126 127
static const struct configfield pullconfigfields[] = {
	CFr("Name", pull_rule, name),
	CFr("From", pull_rule, from),
	CF("Architectures", pull_rule, architectures),
	CF("Components", pull_rule, components),
	CF("UDebComponents", pull_rule, udebcomponents),
	CF("FilterFormula", pull_rule, includecondition),
	CF("FilterList", pull_rule, filterlist)
128 129
};

130
retvalue pull_getrules(struct pull_rule **rules) {
131 132 133
	struct pull_rule *pull = NULL;
	retvalue r;

134
	r = configfile_parse("pulls", IGNORABLE(unknownfield),
Bernhard Link's avatar
Bernhard Link committed
135 136
			configparser_pull_rule_init, linkedlistfinish,
			pullconfigfields, ARRAYCOUNT(pullconfigfields), &pull);
137 138 139
	if( RET_IS_OK(r) )
		*rules = pull;
	else if( r == RET_NOTHING ) {
Bernhard Link's avatar
Bernhard Link committed
140
		assert( pull == NULL );
141 142
		*rules = NULL;
		r = RET_OK;
Bernhard Link's avatar
Bernhard Link committed
143 144 145
	} else {
		// TODO special handle unknownfield
		pull_freerules(pull);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	}
	return r;
}

/***************************************************************************
 * step two:                                                               *
 * create pull_distribution structs to hold all additional information for *
 * a distribution                                                          *
 **************************************************************************/

struct pull_target;
static void pull_freetargets(struct pull_target *targets);

struct pull_distribution {
	struct pull_distribution *next;
	/*@dependant@*/struct distribution *distribution;
	struct pull_target *targets;
	/*@dependant@*/struct pull_rule *rules[];
};

void pull_freedistributions(struct pull_distribution *d) {
	while( d != NULL ) {
		struct pull_distribution *next;

		next = d->next;
		pull_freetargets(d->targets);
		free(d);
		d = next;
	}
}

static retvalue pull_initdistribution(struct pull_distribution **pp,
		struct distribution *distribution,
		struct pull_rule *rules) {
	struct pull_distribution *p;
	int i;

	assert(distribution != NULL);
	if( distribution->pulls.count == 0 )
		return RET_NOTHING;

	p = malloc(sizeof(struct pull_distribution)+
		sizeof(struct pull_rules *)*distribution->pulls.count);
	if( p == NULL )
		return RET_ERROR_OOM;
	p->next = NULL;
	p->distribution = distribution;
	p->targets = NULL;
	for( i = 0 ; i < distribution->pulls.count ; i++ ) {
		const char *name = distribution->pulls.values[i];
		if( strcmp(name,"-") == 0 ) {
			p->rules[i] = NULL;
		} else {
			struct pull_rule *rule = rules;
			while( rule && strcmp(rule->name,name) != 0 )
				rule = rule->next;
			if( rule == NULL ) {
				fprintf(stderr,
Bernhard Link's avatar
Bernhard Link committed
204
"Error: Unknown pull rule '%s' in distribution '%s'!\n",
205 206 207 208
						name, distribution->codename);
				return RET_ERROR_MISSING;
			}
			p->rules[i] = rule;
209
			rule->used = true;
210 211 212 213 214 215 216 217 218 219 220 221 222 223
		}
	}
	*pp = p;
	return RET_OK;
}

static retvalue pull_init(struct pull_distribution **pulls,
		struct pull_rule *rules,
		struct distribution *distributions) {
	struct pull_distribution *p = NULL, **pp = &p;
	struct distribution *d;
	retvalue r;

	for( d = distributions ; d != NULL ; d = d->next ) {
224 225
		if( !d->selected )
			continue;
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		r = pull_initdistribution(pp, d, rules);
		if( RET_WAS_ERROR(r) ) {
			pull_freedistributions(p);
			return r;
		}
		if( RET_IS_OK(r) ) {
			assert( *pp != NULL );
			pp = &(*pp)->next;
		}
	}
	*pulls = p;
	return RET_OK;
}

/***************************************************************************
 * step three:                                                             *
 * load the config of the distributions mentioned in the rules             *
 **************************************************************************/

245
static retvalue pull_loadsourcedistributions(struct distribution *alldistributions, struct pull_rule *rules) {
246 247 248 249 250
	struct pull_rule *rule;
	struct distribution *d;

	for( rule = rules ; rule != NULL ; rule = rule->next ) {
		if( rule->used && rule->distribution == NULL ) {
251 252 253
			for( d = alldistributions ; d != NULL ; d = d->next ) {
				if( strcmp(d->codename, rule->from) == 0 ) {
					rule->distribution = d;
254 255 256
					break;
				}
			}
257 258 259 260 261
			if( d == NULL ) {
				fprintf(stderr, "Error: Unknown distribution '%s' referenced in pull rule '%s'\n",
						rule->from, rule->name);
				return RET_ERROR_MISSING;
			}
262
		}
263
	}
264
	return RET_OK;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

/***************************************************************************
 * step four:                                                              *
 * create pull_targets and pull_sources                                    *
 **************************************************************************/

struct pull_source {
	struct pull_source *next;
	/* NULL, if this is a delete rule */
	struct target *source;
	struct pull_rule *rule;
};
struct pull_target {
	/*@null@*/struct pull_target *next;
	/*@null@*/struct pull_source *sources;
	/*@dependent@*/struct target *target;
	/*@null@*/struct upgradelist *upgradelist;
	/* Ignore delete marks (as some lists were missing) */
284
	bool ignoredelete;
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
};

static void pull_freetargets(struct pull_target *targets) {
	while( targets != NULL ) {
		struct pull_target *target = targets;
		targets = target->next;
		while( target->sources != NULL ) {
			struct pull_source *source = target->sources;
			target->sources = source->next;
			free(source);
		}
		free(target);
	}
}

static retvalue pull_createsource(struct pull_rule *rule,
		struct target *target,
		struct pull_source ***s) {
303 304
	const struct atomlist *c;
	const struct atomlist *a_from, *a_into;
305 306 307 308 309
	int ai;

	assert( rule != NULL );
	assert( rule->distribution != NULL );

310
	if( rule->architectures_set ) {
311 312 313 314 315 316
		a_from = &rule->architectures_from;
		a_into = &rule->architectures_into;
	} else {
		a_from = &rule->distribution->architectures;
		a_into = &rule->distribution->architectures;
	}
317
	if( target->packagetype_atom == pt_udeb )  {
318
		if( rule->udebcomponents_set )
319 320 321 322
			c = &rule->udebcomponents;
		else
			c = &rule->distribution->udebcomponents;
	} else {
323
		if( rule->components_set )
324 325 326 327 328
			c = &rule->components;
		else
			c = &rule->distribution->components;
	}

329
	if( !atomlist_in(c, target->component_atom) )
330 331 332 333
		return RET_NOTHING;

	for( ai = 0 ; ai < a_into->count ; ai++ ) {
		struct pull_source *source;
334 335

		if( a_into->atoms[ai] != target->architecture_atom )
336 337 338 339 340 341 342 343 344
			continue;

		source = malloc(sizeof(struct pull_source));
		if( source == NULL )
			return RET_ERROR_OOM;

		source->next = NULL;
		source->rule = rule;
		source->source = distribution_getpart(rule->distribution,
345 346 347
				target->component_atom,
				a_from->atoms[ai],
				target->packagetype_atom);
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
		**s = source;
		*s = &source->next;
	}
	return RET_OK;
}

static retvalue pull_createdelete(struct pull_source ***s) {
	struct pull_source *source;

	source = malloc(sizeof(struct pull_source));
	if( source == NULL )
		return RET_ERROR_OOM;

	source->next =  NULL;
	source->rule = NULL;
	source->source = NULL;
	**s = source;
	*s = &source->next;
	return RET_OK;
}

static retvalue generatepulltarget(struct pull_distribution *pd, struct target *target) {
	struct pull_source **s;
	struct pull_target *pt;
	retvalue r;
	int i;

	pt = malloc(sizeof(struct pull_target));
	if( pt == NULL ) {
		return RET_ERROR_OOM;
	}
	pt->target = target;
	pt->next = pd->targets;
	pt->upgradelist = NULL;
382
	pt->ignoredelete = false;
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
	pt->sources = NULL;
	s = &pt->sources;
	pd->targets = pt;

	for( i = 0 ; i < pd->distribution->pulls.count ; i++ ) {
		struct pull_rule *rule = pd->rules[i];

		if( rule == NULL)
			r = pull_createdelete(&s);
		else
			r = pull_createsource(rule, target, &s);
		if( RET_WAS_ERROR(r) )
			return r;
	}

	return RET_OK;
}

static retvalue pull_generatetargets(struct pull_distribution *pull_distributions) {
	struct pull_distribution *pd;
	struct target *target;
	struct pull_distribution *u_ds;
	retvalue r;

	u_ds = NULL;

	for( pd = pull_distributions ; pd != NULL ; pd = pd->next ) {
		for( target = pd->distribution->targets ; target != NULL ;
				target = target->next) {

			r = generatepulltarget(pd,target);
			if( RET_WAS_ERROR(r) )
				return r;
		}
	}
	return RET_OK;
}

421 422 423 424
/***************************************************************************
 * Some checking to be able to warn against typos                          *
 **************************************************************************/

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
static bool *preparefoundlist(const struct atomlist *list) {
	bool *found;
	int i, j;

	found = calloc(list->count, sizeof(bool));
	if( found == NULL )
		return found;
	for( i = 0 ; i < list->count ; i++ ) {
		if( found[i] )
			continue;
		for( j = i + 1 ; j < list->count ; j++ )
			if( list->atoms[i] == list->atoms[j] )
				found[j] = true;
	}
	return found;
}


static inline void markasused(const struct strlist *pulls, const char *rulename, const struct atomlist *needed, const struct atomlist *have, bool *found) {
444 445 446 447 448 449
	int i, j, o;

	for( i = 0 ; i < pulls->count ; i++ ) {
		if( strcmp(pulls->values[i], rulename) != 0 )
			continue;
		for( j = 0 ; j < have->count ; j++ ) {
450
			o = atomlist_ofs(needed, have->atoms[j]);
Bernhard Link's avatar
Bernhard Link committed
451
			if( o >= 0 )
452 453 454 455 456
				found[o] = true;
		}
	}
}

457
static void checkifarchitectureisused(const struct atomlist *architectures, const struct distribution *alldistributions, const struct pull_rule *rule, const char *action) {
458 459 460 461 462 463 464
	bool *found;
	const struct distribution *d;
	int i;

	assert( rule != NULL );
	if( architectures->count == 0 )
		return;
465
	found = preparefoundlist(architectures);
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	if( found == NULL )
		return;
	for( d = alldistributions ; d != NULL ; d = d->next ) {
		markasused(&d->pulls, rule->name,
				architectures, &d->architectures,
				found);
	}
	for( i = 0 ; i < architectures->count ; i++ ) {
		if( found[i] )
			continue;
		fprintf(stderr,
"Warning: pull rule '%s' wants to %s architecture '%s',\n"
"but no distribution using this has such an architecture.\n"
"(This will simply be ignored and is not even checked when using --fast).\n",
				rule->name, action,
481
				atoms_architectures[architectures->atoms[i]]);
482 483 484 485 486
	}
	free(found);
	return;
}

487
static void checkifcomponentisused(const struct atomlist *components, const struct distribution *alldistributions, const struct pull_rule *rule, const char *action) {
488 489 490 491 492 493 494
	bool *found;
	const struct distribution *d;
	int i;

	assert( rule != NULL );
	if( components->count == 0 )
		return;
495
	found = preparefoundlist(components);
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	if( found == NULL )
		return;
	for( d = alldistributions ; d != NULL ; d = d->next ) {
		markasused(&d->pulls, rule->name,
				components, &d->components,
				found);
	}
	for( i = 0 ; i < components->count ; i++ ) {
		if( found[i] )
			continue;
		fprintf(stderr,
"Warning: pull rule '%s' wants to %s component '%s',\n"
"but no distribution using this has such an component.\n"
"(This will simply be ignored and is not even checked when using --fast).\n",
				rule->name, action,
511
				atoms_components[components->atoms[i]]);
512 513 514 515 516
	}
	free(found);
	return;
}

517
static void checkifudebcomponentisused(const struct atomlist *udebcomponents, const struct distribution *alldistributions, const struct pull_rule *rule, const char *action) {
518 519 520 521 522 523 524
	bool *found;
	const struct distribution *d;
	int i;

	assert( rule != NULL );
	if( udebcomponents->count == 0 )
		return;
525
	found = preparefoundlist(udebcomponents);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	if( found == NULL )
		return;
	for( d = alldistributions ; d != NULL ; d = d->next ) {
		markasused(&d->pulls, rule->name,
				udebcomponents, &d->udebcomponents,
				found);
	}
	for( i = 0 ; i < udebcomponents->count ; i++ ) {
		if( found[i] )
			continue;
		fprintf(stderr,
"Warning: pull rule '%s' wants to %s udeb component '%s',\n"
"but no distribution using this has such an udeb component.\n"
"(This will simply be ignored and is not even checked when using --fast).\n",
				rule->name, action,
541
				atoms_components[udebcomponents->atoms[i]]);
542 543 544 545 546
	}
	free(found);
	return;
}

547
static void checksubset(const struct atomlist *needed, const struct atomlist *have, const char *rulename, const char *from, const char *what, const char **atoms) {
548 549 550
	int i, j;

	for( i = 0 ; i < needed->count ; i++ ) {
551
		atom_t value = needed->atoms[i];
552 553

		for( j = 0 ; j < i ; j++ ) {
554
			if( value == needed->atoms[j])
555 556 557 558 559
				break;
		}
		if( j < i )
			continue;

560
		if( !atomlist_in(have, value) ) {
561 562 563 564
			fprintf(stderr,
"Warning: pull rule '%s' wants to get something from %s '%s',\n"
"but there is no such %s in distribution '%s'.\n"
"(This will simply be ignored and is not even checked when using --fast).\n",
565 566
					rulename, what,
					atoms[value], what, from);
567 568 569 570 571 572 573 574 575
		}
	}
}

static void searchunused(const struct distribution *alldistributions, const struct pull_rule *rule) {
	if( rule->distribution != NULL ) {
		// TODO: move this part of the checks into parsing?
		checksubset(&rule->architectures_from,
				&rule->distribution->architectures,
576 577
				rule->name, rule->from, "architecture",
				atoms_architectures);
578 579
		checksubset(&rule->components,
				&rule->distribution->components,
580 581
				rule->name, rule->from, "component",
				atoms_components);
582 583
		checksubset(&rule->udebcomponents,
				&rule->distribution->udebcomponents,
584 585
				rule->name, rule->from, "udeb component",
				atoms_components);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	}

	if( rule->distribution == NULL ) {
		assert( strcmp(rule->from, "*") == 0 );
		checkifarchitectureisused(&rule->architectures_from,
				alldistributions, rule, "get something from");
		/* no need to check component and udebcomponent, as those
		 * are the same with the others */
	}
	checkifarchitectureisused(&rule->architectures_into,
			alldistributions, rule, "put something into");
	checkifcomponentisused(&rule->components,
			alldistributions, rule, "put something into");
	checkifudebcomponentisused(&rule->udebcomponents,
			alldistributions, rule, "put something into");
}

static void pull_searchunused(const struct distribution *alldistributions, struct pull_rule *pull_rules) {
	struct pull_rule *rule;

	for( rule = pull_rules ; rule != NULL ; rule = rule->next ) {
		if( !rule->used )
			continue;

		searchunused(alldistributions, rule);
	}
}

614 615 616 617
/***************************************************************************
 * combination of the steps two, three and four                            *
 **************************************************************************/

618
retvalue pull_prepare(struct distribution *alldistributions, struct pull_rule *rules, bool fast, struct pull_distribution **pd) {
619 620 621
	struct pull_distribution *pulls;
	retvalue r;

622
	r = pull_init(&pulls, rules, alldistributions);
623 624 625
	if( RET_WAS_ERROR(r) )
		return r;

626
	r = pull_loadsourcedistributions(alldistributions, rules);
627 628 629 630
	if( RET_WAS_ERROR(r) ) {
		pull_freedistributions(pulls);
		return r;
	}
631 632 633
	if( !fast )
		pull_searchunused(alldistributions, rules);

634 635 636 637 638 639 640 641 642 643 644 645 646 647
	r = pull_generatetargets(pulls);
	if( RET_WAS_ERROR(r) ) {
		pull_freedistributions(pulls);
		return r;
	}
	*pd = pulls;
	return RET_OK;
}

/***************************************************************************
 * step five:                                                              *
 * decide what gets pulled                                                 *
 **************************************************************************/

648
static upgrade_decision ud_decide_by_rule(void *privdata, const char *package, /*@null@*/const char *old_version, UNUSED(const char *new_version), const char *newcontrolchunk) {
649
	struct pull_rule *rule = privdata;
650
	upgrade_decision decision = UD_UPGRADE;
651 652 653 654 655 656
	retvalue r;

	switch( filterlist_find(package,&rule->filterlist) ) {
		case flt_deinstall:
		case flt_purge:
			return UD_NO;
657 658
		case flt_warning:
			return UD_LOUDNO;
659
		case flt_hold:
660 661
			decision = UD_HOLD;
			break;
662 663
		case flt_error:
			/* cannot yet be handled! */
Bernhard Link's avatar
Bernhard Link committed
664 665
			fprintf(stderr,
"Package name marked to be unexpected('error'): '%s'!\n", package);
666
			return UD_ERROR;
667 668 669 670
		case flt_upgradeonly:
			if( old_version == NULL )
				return UD_NO;
			break;
671 672 673 674 675 676 677 678 679 680 681 682 683
		case flt_install:
			break;
	}

	if( rule->includecondition != NULL ) {
		r = term_decidechunk(rule->includecondition,newcontrolchunk);
		if( RET_WAS_ERROR(r) )
			return UD_ERROR;
		if( r == RET_NOTHING ) {
			return UD_NO;
		}
	}

684
	return decision;
685 686
}

687
static inline retvalue pull_searchformissing(/*@null@*/FILE *out,struct database *database,struct pull_target *p) {
688 689 690
	struct pull_source *source;
	retvalue result,r;

691
	if( verbose > 2 && out != NULL )
692
		fprintf(out,"  pulling into '%s'\n",p->target->identifier);
693
	assert(p->upgradelist == NULL);
694
	r = upgradelist_initialize(&p->upgradelist, p->target, database);
695 696 697 698 699 700 701 702
	if( RET_WAS_ERROR(r) )
		return r;

	result = RET_NOTHING;

	for( source=p->sources ; source != NULL ; source=source->next ) {

		if( source->rule == NULL ) {
703
			if( verbose > 4 && out != NULL )
704
				fprintf(out,"  marking everything to be deleted\n");
705 706
			r = upgradelist_deleteall(p->upgradelist);
			RET_UPDATE(result,r);
Bernhard Link's avatar
Bernhard Link committed
707
			if( RET_WAS_ERROR(r) )
708
				return result;
709
			p->ignoredelete = false;
710 711 712
			continue;
		}

713
		if( verbose > 4 && out != NULL )
714
			fprintf(out,"  looking what to get from '%s'\n",
715 716 717 718
					source->source->identifier);
		r = upgradelist_pull(p->upgradelist,
				source->source,
				ud_decide_by_rule, source->rule,
719
				database, source);
720
		RET_UPDATE(result,r);
Bernhard Link's avatar
Bernhard Link committed
721
		if( RET_WAS_ERROR(r) )
722 723 724 725 726 727
			return result;
	}

	return result;
}

728
static retvalue pull_search(/*@null@*/FILE *out,struct database *database,struct pull_distribution *d) {
729 730 731 732 733 734 735 736
	retvalue result,r;
	struct pull_target *u;

	if( d->distribution->deb_override != NULL ||
			d->distribution->dsc_override != NULL ||
			d->distribution->udeb_override != NULL ) {
		if( verbose >= 0 )
			fprintf(stderr,
Bernhard Link's avatar
Bernhard Link committed
737
"Warning: Override files of '%s' ignored as not yet supported while updating!\n",
738 739 740 741
					d->distribution->codename);
	}
	if( d->distribution->tracking != dt_NONE ) {
		fprintf(stderr,
Bernhard Link's avatar
Bernhard Link committed
742
"WARNING: Pull does not yet update tracking data. Tracking data of %s will be outdated!\n",
743 744 745 746 747
					d->distribution->codename);
	}

	result = RET_NOTHING;
	for( u=d->targets ; u != NULL ; u=u->next ) {
748
		r = pull_searchformissing(out, database, u);
749
		RET_UPDATE(result,r);
Bernhard Link's avatar
Bernhard Link committed
750
		if( RET_WAS_ERROR(r) )
751 752 753 754 755
			break;
	}
	return result;
}

756
static retvalue pull_install(struct database *database, struct pull_distribution *distribution) {
757 758 759
	retvalue result,r;
	struct pull_target *u;

Bernhard Link's avatar
Bernhard Link committed
760 761
	assert( logger_isprepared(distribution->distribution->logger) );

762 763
	result = RET_NOTHING;
	for( u=distribution->targets ; u != NULL ; u=u->next ) {
Bernhard Link's avatar
Bernhard Link committed
764 765
		r = upgradelist_install(u->upgradelist,
				distribution->distribution->logger,
766
				database, u->ignoredelete);
767 768 769 770
		RET_UPDATE(distribution->distribution->status, r);
		RET_UPDATE(result,r);
		upgradelist_free(u->upgradelist);
		u->upgradelist = NULL;
Bernhard Link's avatar
Bernhard Link committed
771
		if( RET_WAS_ERROR(r) )
772 773 774 775 776
			break;
	}
	return result;
}

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
static void pull_dumppackage(const char *packagename, /*@null@*/const char *oldversion, /*@null@*/const char *newversion, /*@null@*/const char *bestcandidate, /*@null@*/const struct strlist *newfilekeys, /*@null@*/const char *newcontrol, void *privdata) {
	struct pull_source *source = privdata;

	if( newversion == NULL ) {
		if( oldversion != NULL && bestcandidate != NULL ) {
			printf("'%s': '%s' will be deleted"
					" (best new: '%s')\n",
					packagename, oldversion, bestcandidate);
		} else if( oldversion != NULL ) {
			printf("'%s': '%s' will be deleted"
					" (no longer available)\n",
					packagename, oldversion);
		} else {
			printf("'%s': will NOT be added as '%s'\n",
					packagename, bestcandidate);
		}
	} else if( newversion == oldversion ) {
		if( bestcandidate != NULL ) {
			if( verbose > 1 )
				printf("'%s': '%s' will be kept"
						" (best new: '%s')\n",
						packagename, oldversion,
						bestcandidate);
		} else {
			if( verbose > 0 )
				printf("'%s': '%s' will be kept"
						" (unavailable for reload)\n",
						packagename, oldversion);
		}
	} else {
		const char *via = source->rule->name;

		assert( newfilekeys != NULL );
		assert( newcontrol != NULL );
		if( oldversion != NULL )
			(void)printf("'%s': '%s' will be upgraded"
					" to '%s' (from '%s'):\n files needed: ",
					packagename, oldversion,
					newversion, via);
		else
			(void)printf("'%s': newly installed"
					" as '%s' (from '%s'):\n files needed: ",
					packagename, newversion, via);
		(void)strlist_fprint(stdout, newfilekeys);
		if( verbose > 2)
			(void)printf("\n installing as: '%s'\n",
					newcontrol);
		else
			(void)putchar('\n');
	}
}

829 830 831 832
static void pull_dump(struct pull_distribution *distribution) {
	struct pull_target *u;

	for( u=distribution->targets ; u != NULL ; u=u->next ) {
833 834
		if( u->upgradelist == NULL )
			continue;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
		printf("Updates needed for '%s':\n", u->target->identifier);
		upgradelist_dump(u->upgradelist, pull_dumppackage);
		upgradelist_free(u->upgradelist);
		u->upgradelist = NULL;
	}
}

static void pull_dumplistpackage(const char *packagename, /*@null@*/const char *oldversion, /*@null@*/const char *newversion, /*@null@*/const char *bestcandidate, /*@null@*/const struct strlist *newfilekeys, /*@null@*/const char *newcontrol, void *privdata) {
	struct pull_source *source = privdata;

	if( newversion == NULL ) {
		if( oldversion == NULL )
			return;
		printf("delete '%s' '%s'\n", packagename, oldversion);
	} else if( newversion == oldversion ) {
		if( bestcandidate != NULL )
			printf("keep '%s' '%s' '%s'\n", packagename,
					oldversion, bestcandidate);
		else
			printf("keep '%s' '%s' unavailable\n", packagename,
					oldversion);
	} else {
		const char *via = source->rule->name;

		assert( newfilekeys != NULL );
		assert( newcontrol != NULL );
		if( oldversion != NULL )
			(void)printf("update '%s' '%s' '%s' '%s'\n",
					packagename, oldversion,
					newversion, via);
		else
			(void)printf("add '%s' - '%s' '%s'\n",
					packagename, newversion, via);
	}
}

static void pull_dumplist(struct pull_distribution *distribution) {
	struct pull_target *u;

	for( u=distribution->targets ; u != NULL ; u=u->next ) {
		if( u->upgradelist == NULL )
			continue;
		printf("Updates needed for '%s':\n", u->target->identifier);
		upgradelist_dump(u->upgradelist, pull_dumplistpackage);
879 880
		upgradelist_free(u->upgradelist);
		u->upgradelist = NULL;
881 882 883
	}
}

884
retvalue pull_update(struct database *database, struct pull_distribution *distributions) {
885 886 887
	retvalue result,r;
	struct pull_distribution *d;

Bernhard Link's avatar
Bernhard Link committed
888 889 890 891 892 893
	for( d=distributions ; d != NULL ; d=d->next) {
		r = distribution_prepareforwriting(d->distribution);
		if( RET_WAS_ERROR(r) )
			return r;
	}

894
	if( verbose >= 0 )
895
		printf("Calculating packages to pull...\n");
896 897 898 899

	result = RET_NOTHING;

	for( d=distributions ; d != NULL ; d=d->next) {
900
		r = pull_search(stdout, database, d);
901
		RET_UPDATE(result,r);
Bernhard Link's avatar
Bernhard Link committed
902
		if( RET_WAS_ERROR(r) )
903 904 905
			break;
		// TODO: make already here sure the files are ready?
	}
Bernhard Link's avatar
Bernhard Link committed
906
	if( RET_WAS_ERROR(result) ) {
907 908 909 910 911 912 913
		for( d=distributions ; d != NULL ; d=d->next) {
			struct pull_target *u;
			for( u=d->targets ; u != NULL ; u=u->next ) {
				upgradelist_free(u->upgradelist);
				u->upgradelist = NULL;
			}
		}
914 915 916
		return result;
	}
	if( verbose >= 0 )
917
		printf("Installing (and possibly deleting) packages...\n");
918 919

	for( d=distributions ; d != NULL ; d=d->next) {
920
		r = pull_install(database, d);
921
		RET_UPDATE(result,r);
Bernhard Link's avatar
Bernhard Link committed
922
		if( RET_WAS_ERROR(r) )
923 924
			break;
	}
925
	logger_wait();
926 927 928 929

	return result;
}

930
retvalue pull_checkupdate(struct database *database, struct pull_distribution *distributions) {
931 932 933 934 935 936 937 938 939
	struct pull_distribution *d;
	retvalue result,r;

	if( verbose >= 0 )
		fprintf(stderr,"Calculating packages to get...\n");

	result = RET_NOTHING;

	for( d=distributions ; d != NULL ; d=d->next) {
940
		r = pull_search(stderr, database, d);
941
		RET_UPDATE(result,r);
Bernhard Link's avatar
Bernhard Link committed
942
		if( RET_WAS_ERROR(r) )
943 944 945 946 947 948 949
			break;
		pull_dump(d);
	}

	return result;
}

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
retvalue pull_dumpupdate(struct database *database, struct pull_distribution *distributions) {
	struct pull_distribution *d;
	retvalue result,r;

	result = RET_NOTHING;

	for( d=distributions ; d != NULL ; d=d->next) {
		r = pull_search(NULL, database, d);
		RET_UPDATE(result,r);
		if( RET_WAS_ERROR(r) )
			break;
		pull_dumplist(d);
	}

	return result;
}