summaryrefslogtreecommitdiffstats
path: root/pull.c
blob: 3c5673df507cbefcfb243d2c57b4d3f7bc3ba07f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
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
829
830
831
832
833
834
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
/*  This file is part of "reprepro"
 *  Copyright (C) 2006,2007,2016 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 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"
#include "ignore.h"
#include "mprintf.h"
#include "strlist.h"
#include "names.h"
#include "pull.h"
#include "upgradelist.h"
#include "distribution.h"
#include "tracking.h"
#include "termdecide.h"
#include "filterlist.h"
#include "log.h"
#include "configparser.h"
#include "package.h"

/***************************************************************************
 * 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;
	//e.g. "Architectures: i386 sparc mips" (not set means all)
	struct atomlist architectures_from;
	struct atomlist architectures_into;
	bool architectures_set;
	//e.g. "Components: main contrib" (not set means all)
	struct atomlist components;
	bool components_set;
	//e.g. "UDebComponents: main" // (not set means all)
	struct atomlist udebcomponents;
	bool udebcomponents_set;
	// We don't have equivalents for ddebs yet since we don't know
	// what the Debian archive layout is going to look like
	// NULL means no condition
	/*@null@*/term *includecondition;
	struct filterlist filterlist;
	struct filterlist filtersrclist;
	/*----only set after _addsourcedistribution----*/
	/*@NULL@*/ struct distribution *distribution;
	bool used;
};

static void pull_rule_free(/*@only@*/struct pull_rule *pull) {
	if (pull == NULL)
		return;
	free(pull->name);
	free(pull->from);
	atomlist_done(&pull->architectures_from);
	atomlist_done(&pull->architectures_into);
	atomlist_done(&pull->components);
	atomlist_done(&pull->udebcomponents);
	term_free(pull->includecondition);
	filterlist_release(&pull->filterlist);
	filterlist_release(&pull->filtersrclist);
	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);
	}
}

CFlinkedlistinit(pull_rule)
CFvalueSETPROC(pull_rule, name)
CFvalueSETPROC(pull_rule, from)
CFatomlistSETPROC(pull_rule, components, at_component)
CFatomlistSETPROC(pull_rule, udebcomponents, at_component)
CFfilterlistSETPROC(pull_rule, filterlist)
CFfilterlistSETPROC(pull_rule, filtersrclist)
CFtermSETPROC(pull_rule, includecondition)

CFUSETPROC(pull_rule, architectures) {
	CFSETPROCVAR(pull_rule, this);
	retvalue r;

	this->architectures_set = true;
	r = config_getsplitatoms(iter, "Architectures",
			at_architecture,
			&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;
}

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("FilterSrcList", pull_rule, filtersrclist),
	CF("FilterList", pull_rule, filterlist)
};

retvalue pull_getrules(struct pull_rule **rules) {
	struct pull_rule *pull = NULL;
	retvalue r;

	r = configfile_parse("pulls", IGNORABLE(unknownfield),
			configparser_pull_rule_init, linkedlistfinish,
			"pull rule",
			pullconfigfields, ARRAYCOUNT(pullconfigfields), &pull);
	if (RET_IS_OK(r))
		*rules = pull;
	else if (r == RET_NOTHING) {
		assert (pull == NULL);
		*rules = NULL;
		r = RET_OK;
	} else {
		// TODO special handle unknownfield
		pull_freerules(pull);
	}
	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 (FAILEDTOALLOC(p))
		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,
"Error: Unknown pull rule '%s' in distribution '%s'!\n",
						name, distribution->codename);
				free(p);
				return RET_ERROR_MISSING;
			}
			p->rules[i] = rule;
			rule->used = true;
		}
	}
	*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) {
		if (!d->selected)
			continue;
		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             *
 **************************************************************************/

static retvalue pull_loadsourcedistributions(struct distribution *alldistributions, struct pull_rule *rules) {
	struct pull_rule *rule;
	struct distribution *d;

	for (rule = rules ; rule != NULL ; rule = rule->next) {
		if (rule->used && rule->distribution == NULL) {
			for (d = alldistributions ; d != NULL ; d = d->next) {
				if (strcmp(d->codename, rule->from) == 0) {
					rule->distribution = d;
					break;
				}
			}
			if (d == NULL) {
				fprintf(stderr,
"Error: Unknown distribution '%s' referenced in pull rule '%s'\n",
						rule->from, rule->name);
				return RET_ERROR_MISSING;
			}
		}
	}
	return RET_OK;
}

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

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) {
	const struct atomlist *c;
	const struct atomlist *a_from, *a_into;
	int ai;

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

	if (rule->architectures_set) {
		a_from = &rule->architectures_from;
		a_into = &rule->architectures_into;
	} else {
		a_from = &rule->distribution->architectures;
		a_into = &rule->distribution->architectures;
	}
	if (target->packagetype == pt_udeb)  {
		if (rule->udebcomponents_set)
			c = &rule->udebcomponents;
		else
			c = &rule->distribution->udebcomponents;
	} else {
		if (rule->components_set)
			c = &rule->components;
		else
			c = &rule->distribution->components;
	}

	if (!atomlist_in(c, target->component))
		return RET_NOTHING;

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

		if (a_into->atoms[ai] != target->architecture)
			continue;

		source = NEW(struct pull_source);
		if (FAILEDTOALLOC(source))
			return RET_ERROR_OOM;

		source->next = NULL;
		source->rule = rule;
		source->source = distribution_getpart(rule->distribution,
				target->component,
				a_from->atoms[ai],
				target->packagetype);
		**s = source;
		*s = &source->next;
	}
	return RET_OK;
}

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

	source = NEW(struct pull_source);
	if (FAILEDTOALLOC(source))
		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 = NEW(struct pull_target);
	if (FAILEDTOALLOC(pt))
		return RET_ERROR_OOM;
	pt->target = target;
	pt->next = pd->targets;
	pt->upgradelist = NULL;
	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, const struct atomlist *components, const struct atomlist *architectures, const struct atomlist *packagetypes) {
	struct pull_distribution *pd;
	struct target *target;
	retvalue r;

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

			if (!target_matches(target, components, architectures, packagetypes))
				continue;

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

/***************************************************************************
 * Some checking to be able to warn against typos                          *
 **************************************************************************/

static bool *preparefoundlist(const struct atomlist *list) {
	bool *found;
	int i, j;

	found = nzNEW(list->count, bool);
	if (FAILEDTOALLOC(found))
		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) {
	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++) {
			o = atomlist_ofs(needed, have->atoms[j]);
			if (o >= 0)
				found[o] = true;
		}
	}
}

static void checkifarchitectureisused(const struct atomlist *architectures, const struct distribution *alldistributions, const struct pull_rule *rule, const char *action) {
	bool *found;
	const struct distribution *d;
	int i;

	assert (rule != NULL);
	if (architectures->count == 0)
		return;
	found = preparefoundlist(architectures);
	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,
				atoms_architectures[architectures->atoms[i]]);
	}
	free(found);
	return;
}

static void checkifcomponentisused(const struct atomlist *components, const struct distribution *alldistributions, const struct pull_rule *rule, const char *action) {
	bool *found;
	const struct distribution *d;
	int i;

	assert (rule != NULL);
	if (components->count == 0)
		return;
	found = preparefoundlist(components);
	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,
				atoms_components[components->atoms[i]]);
	}
	free(found);
	return;
}

static void checkifudebcomponentisused(const struct atomlist *udebcomponents, const struct distribution *alldistributions, const struct pull_rule *rule, const char *action) {
	bool *found;
	const struct distribution *d;
	int i;

	assert (rule != NULL);
	if (udebcomponents->count == 0)
		return;
	found = preparefoundlist(udebcomponents);
	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,
				atoms_components[udebcomponents->atoms[i]]);
	}
	free(found);
	return;
}

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

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

		for (j = 0 ; j < i ; j++) {
			if (value == needed->atoms[j])
				break;
		}
		if (j < i)
			continue;

		if (!atomlist_in(have, value)) {
			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",
					rulename, what,
					atoms[value], what, from);
		}
	}
}

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,
				rule->name, rule->from, "architecture",
				atoms_architectures);
		checksubset(&rule->components,
				&rule->distribution->components,
				rule->name, rule->from, "component",
				atoms_components);
		checksubset(&rule->udebcomponents,
				&rule->distribution->udebcomponents,
				rule->name, rule->from, "udeb component",
				atoms_components);
	}

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

/***************************************************************************
 * combination of the steps two, three and four                            *
 **************************************************************************/

retvalue pull_prepare(struct distribution *alldistributions, struct pull_rule *rules, bool fast, const struct atomlist *components, const struct atomlist *architectures, const struct atomlist *types, struct pull_distribution **pd) {
	struct pull_distribution *pulls;
	retvalue r;

	r = pull_init(&pulls, rules, alldistributions);
	if (RET_WAS_ERROR(r))
		return r;

	r = pull_loadsourcedistributions(alldistributions, rules);
	if (RET_WAS_ERROR(r)) {
		pull_freedistributions(pulls);
		return r;
	}
	if (!fast)
		pull_searchunused(alldistributions, rules);

	r = pull_generatetargets(pulls, components, architectures, types);
	if (RET_WAS_ERROR(r)) {
		pull_freedistributions(pulls);
		return r;
	}
	*pd = pulls;
	return RET_OK;
}

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

static upgrade_decision ud_decide_by_rule(void *privdata, struct target *target, struct package *new, /*@null@*/const char *old_version) {
	struct pull_rule *rule = privdata;
	upgrade_decision decision = UD_UPGRADE;
	retvalue r;
	struct filterlist *fl;
	const char *n, *v;
	bool cmdline_still_undecided;

	if (target->packagetype == pt_dsc) {
		assert (strcmp(new->name, new->source) == 0);
		assert (strcmp(new->version, new->sourceversion) == 0);
		if (rule->filtersrclist.set)
			fl = &rule->filtersrclist;
		else
			fl = &rule->filterlist;
		n = new->name;
		v = new->version;
	} else {
		if (rule->filterlist.set) {
			fl = &rule->filterlist;
			n = new->name;
			v = new->version;
		} else {
			fl = &rule->filtersrclist;
			n = new->source;
			v = new->sourceversion;
		}
	}

	switch (filterlist_find(n, v, fl)) {
		case flt_deinstall:
		case flt_purge:
			return UD_NO;
		case flt_warning:
			return UD_LOUDNO;
		case flt_supersede:
			decision = UD_SUPERSEDE;
			break;
		case flt_hold:
			decision = UD_HOLD;
			break;
		case flt_error:
			/* cannot yet be handled! */
			fprintf(stderr,
"Package name marked to be unexpected('error'): '%s'!\n", new->name);
			return UD_ERROR;
		case flt_upgradeonly:
			if (old_version == NULL)
				return UD_NO;
			break;
		case flt_install:
			break;
		case flt_unchanged:
		case flt_auto_hold:
			assert (false);
			break;
	}

	cmdline_still_undecided = false;
	switch (filterlist_find(new->source, new->sourceversion,
				&cmdline_src_filter)) {
		case flt_deinstall:
		case flt_purge:
			return UD_NO;
		case flt_warning:
			return UD_LOUDNO;
		case flt_auto_hold:
			cmdline_still_undecided = true;
			decision = UD_HOLD;
			break;
		case flt_hold:
			decision = UD_HOLD;
			break;
		case flt_supersede:
			decision = UD_SUPERSEDE;
			break;
		case flt_error:
			/* cannot yet be handled! */
			fprintf(stderr,
"Package name marked to be unexpected('error'): '%s'!\n", new->name);
			return UD_ERROR;
		case flt_upgradeonly:
			if (old_version == NULL)
				return UD_NO;
			break;
		case flt_install:
			decision = UD_UPGRADE;
			break;
		case flt_unchanged:
			cmdline_still_undecided = true;
			break;
	}


	if (target->packagetype != pt_dsc) {
		switch (filterlist_find(new->name, new->version,
					&cmdline_bin_filter)) {
			case flt_deinstall:
			case flt_purge:
				return UD_NO;
			case flt_warning:
				return UD_LOUDNO;
			case flt_hold:
				decision = UD_HOLD;
				break;
			case flt_supersede:
				decision = UD_SUPERSEDE;
				break;
			case flt_error:
				/* cannot yet be handled! */
				fprintf(stderr,
"Package name marked to be unexpected('error'): '%s'!\n", new->name);
				return UD_ERROR;
			case flt_upgradeonly:
				if (old_version == NULL)
					return UD_NO;
				break;
			case flt_install:
				decision = UD_UPGRADE;
				break;
			case flt_unchanged:
				break;
			case flt_auto_hold:
				/* hold only if it was not in the src-filter */
				if (cmdline_still_undecided)
					decision = UD_HOLD;
				break;
		}
	} else if (cmdline_bin_filter.defaulttype == flt_auto_hold) {
		if (cmdline_still_undecided)
			decision = UD_HOLD;
	}

	/* formula tested last as it is the most expensive */
	if (rule->includecondition != NULL) {
		r = term_decidepackage(rule->includecondition, new, target);
		if (RET_WAS_ERROR(r))
			return UD_ERROR;
		if (r == RET_NOTHING) {
			return UD_NO;
		}
	}

	return decision;
}

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

	if (verbose > 2 && out != NULL)
		fprintf(out, "  pulling into '%s'\n", p->target->identifier);
	assert(p->upgradelist == NULL);
	r = upgradelist_initialize(&p->upgradelist, p->target);
	if (RET_WAS_ERROR(r))
		return r;

	result = RET_NOTHING;

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

		if (source->rule == NULL) {
			if (verbose > 4 && out != NULL)
				fprintf(out,
"  marking everything to be deleted\n");
			r = upgradelist_deleteall(p->upgradelist);
			RET_UPDATE(result, r);
			if (RET_WAS_ERROR(r))
				return result;
			continue;
		}

		if (verbose > 4 && out != NULL)
			fprintf(out, "  looking what to get from '%s'\n",
					source->source->identifier);
		r = upgradelist_pull(p->upgradelist, source->source,
				ud_decide_by_rule, source->rule, source);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			return result;
	}

	return result;
}

static retvalue pull_search(/*@null@*/FILE *out, struct pull_distribution *d) {
	retvalue result, r;
	struct pull_target *u;

	result = RET_NOTHING;
	for (u=d->targets ; u != NULL ; u=u->next) {
		r = pull_searchformissing(out, u);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			break;
	}
	return result;
}

static bool pull_isbigdelete(struct pull_distribution *d) {
	struct pull_target *u, *v;

	for (u = d->targets ; u != NULL ; u=u->next) {
		if (upgradelist_isbigdelete(u->upgradelist)) {
			d->distribution->omitted = true;
			for (v = d->targets ; v != NULL ; v = v->next) {
				upgradelist_free(v->upgradelist);
				v->upgradelist = NULL;
			}
			return true;
		}
	}
	return false;
}


static void pull_from_callback(void *privdata, const char **rule_p, const char **from_p) {
	struct pull_source *source = privdata;

	*rule_p = source->rule->name;
	*from_p = source->rule->from;
}

static retvalue pull_install(struct pull_distribution *distribution) {
	retvalue result, r;
	struct pull_target *u;
	struct distribution *d = distribution->distribution;

	assert (logger_isprepared(d->logger));

	result = RET_NOTHING;
	for (u=distribution->targets ; u != NULL ; u=u->next) {
		r = upgradelist_install(u->upgradelist, d->logger,
				false, pull_from_callback);
		RET_UPDATE(d->status, r);
		RET_UPDATE(result, r);
		upgradelist_free(u->upgradelist);
		u->upgradelist = NULL;
		if (RET_WAS_ERROR(r))
			break;
	}
	if (RET_IS_OK(result) && d->tracking != dt_NONE) {
		r = tracking_retrack(d, false);
		RET_ENDUPDATE(result, r);
	}
	return result;
}

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 or superseded)\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');
	}
}

static void pull_dump(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_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);
		upgradelist_free(u->upgradelist);
		u->upgradelist = NULL;
	}
}

retvalue pull_update(struct pull_distribution *distributions) {
	retvalue result, r;
	struct pull_distribution *d;

	for (d=distributions ; d != NULL ; d=d->next) {
		r = distribution_prepareforwriting(d->distribution);
		if (RET_WAS_ERROR(r))
			return r;
		r = distribution_loadalloverrides(d->distribution);
		if (RET_WAS_ERROR(r))
			return r;
	}

	if (verbose >= 0)
		printf("Calculating packages to pull...\n");

	result = RET_NOTHING;

	for (d=distributions ; d != NULL ; d=d->next) {
		r = pull_search(stdout, d);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			break;
		// TODO: make already here sure the files are ready?
	}
	if (RET_WAS_ERROR(result)) {
		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;
			}
		}
		return result;
	}
	if (verbose >= 0)
		printf("Installing (and possibly deleting) packages...\n");

	for (d=distributions ; d != NULL ; d=d->next) {
		if (global.onlysmalldeletes) {
			if (pull_isbigdelete(d)) {
				fprintf(stderr,
"Not processing '%s' because of --onlysmalldeletes\n",
						d->distribution->codename);
				continue;
			}
		}
		r = pull_install(d);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			break;
	}
	logger_wait();

	return result;
}

retvalue pull_checkupdate(struct pull_distribution *distributions) {
	struct pull_distribution *d;
	retvalue result, r;

	for (d=distributions ; d != NULL ; d=d->next) {
		r = distribution_loadalloverrides(d->distribution);
		if (RET_WAS_ERROR(r))
			return r;
	}

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

	result = RET_NOTHING;

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

	return result;
}

retvalue pull_dumpupdate(struct pull_distribution *distributions) {
	struct pull_distribution *d;
	retvalue result, r;

	for (d=distributions ; d != NULL ; d=d->next) {
		r = distribution_loadalloverrides(d->distribution);
		if (RET_WAS_ERROR(r))
			return r;
	}

	result = RET_NOTHING;

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

	return result;
}