File: distribution.c

package info (click to toggle)
reprepro 5.3.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,960 kB
  • sloc: ansic: 52,311; sh: 1,875; python: 1,625; makefile: 167
file content (1145 lines) | stat: -rw-r--r-- 33,478 bytes parent folder | download | duplicates (2)
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
/*  This file is part of "reprepro"
 *  Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010,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 <limits.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "error.h"
#include "mprintf.h"
#include "atoms.h"
#include "sources.h"
#include "dirs.h"
#include "names.h"
#include "release.h"
#include "tracking.h"
#include "override.h"
#include "log.h"
#include "ignore.h"
#include "uploaderslist.h"
#include "configparser.h"
#include "byhandhook.h"
#include "package.h"
#include "distribution.h"

static retvalue distribution_free(struct distribution *distribution) {
	retvalue result, r;
	bool needsretrack = false;

	if (distribution != NULL) {
		free(distribution->suite);
		free(distribution->fakecomponentprefix);
		free(distribution->version);
		free(distribution->origin);
		free(distribution->notautomatic);
		free(distribution->butautomaticupgrades);
		free(distribution->label);
		free(distribution->description);
		free(distribution->signed_by);
		free(distribution->deb_override);
		free(distribution->udeb_override);
		free(distribution->dsc_override);
		free(distribution->uploaders);
		atomlist_done(&distribution->udebcomponents);
		atomlist_done(&distribution->architectures);
		atomlist_done(&distribution->components);
		strlist_done(&distribution->signwith);
		strlist_done(&distribution->updates);
		strlist_done(&distribution->pulls);
		strlist_done(&distribution->alsoaccept);
		exportmode_done(&distribution->dsc);
		exportmode_done(&distribution->deb);
		exportmode_done(&distribution->udeb);
		atomlist_done(&distribution->contents_architectures);
		atomlist_done(&distribution->contents_components);
		atomlist_done(&distribution->contents_ucomponents);
		override_free(distribution->overrides.deb);
		override_free(distribution->overrides.udeb);
		override_free(distribution->overrides.dsc);
		logger_free(distribution->logger);
		if (distribution->uploaderslist != NULL) {
			uploaders_unlock(distribution->uploaderslist);
		}
		byhandhooks_free(distribution->byhandhooks);
		result = RET_OK;

		while (distribution->targets != NULL) {
			struct target *next = distribution->targets->next;

			if (distribution->targets->staletracking)
				needsretrack = true;

			r = target_free(distribution->targets);
			RET_UPDATE(result, r);
			distribution->targets = next;
		}
		if (distribution->tracking != dt_NONE && needsretrack) {
			fprintf(stderr,
"WARNING: Tracking data of '%s' might have become out of date.\n"
"Consider running retrack to avoid getting funny effects.\n",
					distribution->codename);
		}
		free(distribution->codename);
		free(distribution);
		return result;
	} else
		return RET_OK;
}

/* allow premature free'ing of overrides to save some memory */
void distribution_unloadoverrides(struct distribution *distribution) {
	override_free(distribution->overrides.deb);
	override_free(distribution->overrides.udeb);
	override_free(distribution->overrides.dsc);
	distribution->overrides.deb = NULL;
	distribution->overrides.udeb = NULL;
	distribution->overrides.dsc = NULL;
}

/* create all contained targets... */
static retvalue createtargets(struct distribution *distribution) {
	retvalue r;
	int i, j;
	struct target *t;
	struct target *last = NULL;
	bool has_source = false;

	for (i = 0 ; i < distribution->components.count ; i++) {
		component_t c = distribution->components.atoms[i];
		for (j = 0 ; j < distribution->architectures.count ; j++) {
			architecture_t a = distribution->architectures.atoms[j];

			if (a == architecture_source) {
				has_source = true;
				continue;
			}
			if (a == architecture_all) {
				fprintf(stderr,
"Error: Distribution %s contains an architecture called 'all'.\n",
						distribution->codename);
				return RET_ERROR;
			}
			if (strcmp(atoms_architectures[a], "any") == 0) {
				fprintf(stderr,
"Error: Distribution %s contains an architecture called 'any'.\n",
						distribution->codename);
				return RET_ERROR;
			}

			r = target_initialize_binary(
					distribution,
					c, a,
					&distribution->deb,
					distribution->readonly,
					distribution->exportoptions[deo_noexport],
					distribution->fakecomponentprefix,
					&t);
			if (RET_IS_OK(r)) {
				if (last != NULL) {
					last->next = t;
				} else {
					distribution->targets = t;
				}
				last = t;
			}
			if (RET_WAS_ERROR(r))
				return r;
			if (atomlist_in(&distribution->udebcomponents, c)) {
				r = target_initialize_ubinary(
						distribution,
						c, a,
						&distribution->udeb,
						distribution->readonly,
						distribution->exportoptions
							[deo_noexport],
						distribution->fakecomponentprefix,
						&t);
				if (RET_IS_OK(r)) {
					if (last != NULL) {
						last->next = t;
					} else {
						distribution->targets = t;
					}
					last = t;
				}
				if (RET_WAS_ERROR(r))
					return r;

			}
		}
		/* check if this distribution contains source
		 * (yes, yes, source is not really an architecture, but
		 *  the .changes files started with this...) */
		if (has_source) {
			r = target_initialize_source(distribution,
					c, &distribution->dsc,
					distribution->readonly,
					distribution->exportoptions
							[deo_noexport],
					distribution->fakecomponentprefix, &t);
			if (last != NULL) {
				last->next = t;
			} else {
				distribution->targets = t;
			}
			last = t;
			if (RET_WAS_ERROR(r))
				return r;
		}
	}
	return RET_OK;
}

struct read_distribution_data {
	struct distribution *distributions;
};

CFstartparse(distribution) {
	CFstartparseVAR(distribution, result_p);
	struct distribution *n;
	retvalue r;

	n = zNEW(struct distribution);
	if (FAILEDTOALLOC(n))
		return RET_ERROR_OOM;
	/* set some default value: */
	r = exportmode_init(&n->udeb, true, NULL, "Packages");
	if (RET_WAS_ERROR(r)) {
		(void)distribution_free(n);
		return r;
	}
	r = exportmode_init(&n->deb, true, "Release", "Packages");
	if (RET_WAS_ERROR(r)) {
		(void)distribution_free(n);
		return r;
	}
	r = exportmode_init(&n->dsc, false, "Release", "Sources");
	if (RET_WAS_ERROR(r)) {
		(void)distribution_free(n);
		return r;
	}
	*result_p = n;
	return RET_OK;
}

static bool notpropersuperset(const struct atomlist *allowed, const char *allowedname, const struct atomlist *check, const char *checkname, const char **atoms, const struct distribution *d) {
	atom_t missing;

	if (!atomlist_subset(allowed, check, &missing)) {
		fprintf(stderr,
"In distribution description of '%s' (line %u to %u in %s):\n"
"%s contains '%s' not found in %s!\n",
				d->codename,
				d->firstline, d->lastline, d->filename,
				checkname, atoms[missing], allowedname);
		return true;
	}
	return false;
}

static inline retvalue checkcomponentsequalduetofake(const struct distribution *d) {
	size_t l;
	int i, j;

	if (d->fakecomponentprefix == NULL)
		return RET_OK;

	l = strlen(d->fakecomponentprefix);

	for (i = 0 ; i < d->components.count ; i++) {
		const char *c1 = atoms_components[d->components.atoms[i]];

		if (strncmp(c1, d->fakecomponentprefix, l) != 0)
			continue;
		if (d->fakecomponentprefix[l] != '/')
			continue;

		for (j = 0 ; i < d->components.count ; j++) {
			const char *c2;

			if (j == i)
				continue;

			c2 = atoms_components[d->components.atoms[j]];

			if (strcmp(c1 + l + 1, c2) == 0) {
				fprintf(stderr,
"ERROR: distribution '%s' has components '%s' and '%s',\n"
"which would be output to the same place due to FakeComponentPrefix '%s'.\n",
					d->codename, c1, c2,
					d->fakecomponentprefix);
				return RET_ERROR;
			}
		}
	}
	return RET_OK;
}

CFfinishparse(distribution) {
	CFfinishparseVARS(distribution, n, last_p, mydata);
	struct distribution *d;
	retvalue r;

	if (!complete) {
		distribution_free(n);
		return RET_NOTHING;
	}
	n->filename = config_filename(iter);
	n->firstline = config_firstline(iter);
	n->lastline = config_line(iter) - 1;

	/* Do some consitency checks */
	for (d = mydata->distributions; d != NULL; d = d->next) {
		if (strcmp(d->codename, n->codename) == 0) {
			fprintf(stderr,
"Multiple distributions with the common codename: '%s'!\n"
"First was in %s line %u to %u,\n"
"now another in lines %u to %u of %s.\n",
				n->codename, d->filename,
				d->firstline, d->lastline,
				n->firstline, n->lastline,
				n->filename);
			distribution_free(n);
			return RET_ERROR;
		}
	}

	if (notpropersuperset(&n->architectures, "Architectures",
			    &n->contents_architectures, "ContentsArchitectures",
			    atoms_architectures, n) ||
	    notpropersuperset(&n->components, "Components",
			    &n->contents_components, "ContentsComponents",
			    atoms_components, n) ||
	    notpropersuperset(&n->udebcomponents, "UDebComponents",
			    &n->contents_ucomponents, "ContentsUComponents",
			    atoms_components, n) ||
	    // TODO: instead of checking here make sure it can have more
	    // in the rest of the code...:
	    notpropersuperset(&n->components, "Components",
			    &n->udebcomponents, "UDebComponents",
			    atoms_components, n)) {
		(void)distribution_free(n);
		return RET_ERROR;
	}
	/* overwrite creation of contents files based on given lists: */
	if (n->contents_components_set) {
		if (n->contents_components.count > 0) {
			n->contents.flags.enabled = true;
			n->contents.flags.nodebs = false;
		} else {
			n->contents.flags.nodebs = true;
		}
	}
	if (n->contents_ucomponents_set) {
		if (n->contents_ucomponents.count > 0) {
			n->contents.flags.enabled = true;
			n->contents.flags.udebs = true;
		} else {
			n->contents.flags.udebs = false;
		}
	}
	if (n->contents_architectures_set) {
		if (n->contents_architectures.count > 0)
			n->contents.flags.enabled = true;
		else
			n->contents.flags.enabled = false;
	}

	r = checkcomponentsequalduetofake(n);
	if (RET_WAS_ERROR(r)) {
		(void)distribution_free(n);
		return r;
	}

	/* prepare substructures */

	r = createtargets(n);
	if (RET_WAS_ERROR(r)) {
		(void)distribution_free(n);
		return r;
	}
	n->status = RET_NOTHING;
	n->lookedat = false;
	n->selected = false;

	/* put in linked list */
	if (*last_p == NULL)
		mydata->distributions = n;
	else
		(*last_p)->next = n;
	*last_p = n;
	return RET_OK;
}

CFallSETPROC(distribution, suite)
CFallSETPROC(distribution, version)
CFallSETPROC(distribution, origin)
CFallSETPROC(distribution, notautomatic)
CFallSETPROC(distribution, butautomaticupgrades)
CFtruthSETPROC2(distribution, readonly, readonly)
CFallSETPROC(distribution, label)
CFallSETPROC(distribution, description)
CFallSETPROC(distribution, signed_by)
CFsignwithSETPROC(distribution, signwith)
CFfileSETPROC(distribution, deb_override)
CFfileSETPROC(distribution, udeb_override)
CFfileSETPROC(distribution, dsc_override)
CFfileSETPROC(distribution, uploaders)
CFuniqstrlistSETPROC(distribution, alsoaccept)
CFstrlistSETPROC(distribution, updates)
CFstrlistSETPROC(distribution, pulls)
CFinternatomsSETPROC(distribution, components, checkforcomponent, at_component)
CFinternatomsSETPROC(distribution, architectures, checkforarchitecture, at_architecture)
CFatomsublistSETPROC(distribution, contents_architectures, at_architecture, architectures, "Architectures")
CFatomsublistSETPROC(distribution, contents_components, at_component, components, "Components")
CFatomsublistSETPROC(distribution, udebcomponents, at_component, components, "Components")
CFatomsublistSETPROC(distribution, contents_ucomponents, at_component, udebcomponents, "UDebComponents")
CFexportmodeSETPROC(distribution, udeb)
CFexportmodeSETPROC(distribution, deb)
CFexportmodeSETPROC(distribution, dsc)
CFcheckvalueSETPROC(distribution, codename, checkforcodename)
CFcheckvalueSETPROC(distribution, fakecomponentprefix, checkfordirectoryandidentifier)
CFtimespanSETPROC(distribution, validfor)

CFUSETPROC(distribution, Contents) {
	CFSETPROCVAR(distribution, d);
	return contentsoptions_parse(d, iter);
}
CFUSETPROC(distribution, logger) {
	CFSETPROCVAR(distribution, d);
	return logger_init(iter, &d->logger);
}
CFUSETPROC(distribution, Tracking) {
	CFSETPROCVAR(distribution, d);
	return tracking_parse(d, iter);
}

CFUSETPROC(distribution, byhandhooks) {
	CFSETPROCVAR(distribution, d);

	return byhandhooks_parse(iter, &d->byhandhooks);
}

static const struct constant exportnames[deo_COUNT+1] = {
	{"noexport", deo_noexport},
	{"keepunknown", deo_keepunknown},
	{NULL, 0}
};

CFUSETPROC(distribution, exportoptions) {
	CFSETPROCVAR(distribution, d);
	return config_getflags(iter, name, exportnames, d->exportoptions,
			IGNORABLE(unknownfield),
			"(allowed values: noexport, keepunknown)");
}

static const struct configfield distributionconfigfields[] = {
	CF("AlsoAcceptFor",	distribution,	alsoaccept),
	CFr("Architectures",	distribution,	architectures),
	CF("ByHandHooks",	distribution,	byhandhooks),
	CFr("Codename",		distribution,	codename),
	CFr("Components",	distribution,	components),
	CF("ContentsArchitectures", distribution, contents_architectures),
	CF("ContentsComponents", distribution,	contents_components),
	CF("Contents",		distribution,	Contents),
	CF("ContentsUComponents", distribution,	contents_ucomponents),
	CF("DebIndices",	distribution,	deb),
	CF("DebOverride",	distribution,	deb_override),
	CF("Description",	distribution,	description),
	CF("Signed-By",		distribution,	signed_by),
	CF("DscIndices",	distribution,	dsc),
	CF("DscOverride",	distribution,	dsc_override),
	CF("FakeComponentPrefix", distribution,	fakecomponentprefix),
	CF("Label",		distribution,	label),
	CF("Log",		distribution,	logger),
	CF("NotAutomatic",	distribution,	notautomatic),
	CF("ButAutomaticUpgrades", distribution, butautomaticupgrades),
	CF("Origin",		distribution,	origin),
	CF("Pull",		distribution,	pulls),
	CF("ReadOnly",		distribution,	readonly),
	CF("ExportOptions",	distribution,	exportoptions),
	CF("SignWith",		distribution,	signwith),
	CF("Suite",		distribution,	suite),
	CF("Tracking",		distribution,	Tracking),
	CF("UDebComponents",	distribution,	udebcomponents),
	CF("UDebIndices",	distribution,	udeb),
	CF("UDebOverride",	distribution,	udeb_override),
	CF("Update",		distribution,	updates),
	CF("Uploaders",		distribution,	uploaders),
	CF("ValidFor",		distribution,	validfor),
	CF("Version",		distribution,	version)
};

/* read specification of all distributions */
retvalue distribution_readall(struct distribution **distributions) {
	struct read_distribution_data mydata;
	retvalue result;

	mydata.distributions = NULL;

	// TODO: readd some way to tell about -b or --confdir here?
	/*
	result = regularfileexists(fn);
	if (RET_WAS_ERROR(result)) {
		fprintf(stderr, "Could not find '%s'!\n"
"(Have you forgotten to specify a basedir by -b?\n"
"To only set the conf/ dir use --confdir)\n", fn);
		free(mydata.filter.found);
		free(fn);
		return RET_ERROR_MISSING;
	}
	*/

	result = configfile_parse("distributions",
			IGNORABLE(unknownfield),
			startparsedistribution, finishparsedistribution,
			"distribution definition",
			distributionconfigfields,
			ARRAYCOUNT(distributionconfigfields),
			&mydata);
	if (result == RET_ERROR_UNKNOWNFIELD)
		fprintf(stderr,
"Use --ignore=unknownfield to ignore unknown fields\n");
	if (RET_WAS_ERROR(result)) {
		distribution_freelist(mydata.distributions);
		return result;
	}
	if (mydata.distributions == NULL) {
		fprintf(stderr,
"No distribution definitions found in %s/distributions!\n",
				global.confdir);
		distribution_freelist(mydata.distributions);
		return RET_ERROR_MISSING;
	}
	*distributions = mydata.distributions;
	return RET_OK;
}

/* call <action> for each package */
retvalue package_foreach(struct distribution *distribution, const struct atomlist *components, const struct atomlist *architectures, const struct atomlist *packagetypes, action_each_package action, action_each_target target_action, void *data) {
	retvalue result, r;
	struct target *t;
	struct package_cursor iterator;

	result = RET_NOTHING;
	for (t = distribution->targets ; t != NULL ; t = t->next) {
		if (!target_matches(t, components, architectures, packagetypes))
			continue;
		if (target_action != NULL) {
			r = target_action(t, data);
			if (RET_WAS_ERROR(r))
				return result;
			if (r == RET_NOTHING)
				continue;
		}
		r = package_openiterator(t, READONLY, &iterator);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			return result;
		while (package_next(&iterator)) {
			r = action(&iterator.current, data);
			RET_UPDATE(result, r);
			if (RET_WAS_ERROR(r))
				break;
		}
		r = package_closeiterator(&iterator);
		RET_ENDUPDATE(result, r);
		if (RET_WAS_ERROR(result))
			return result;
	}
	return result;
}

retvalue package_foreach_c(struct distribution *distribution, const struct atomlist *components, architecture_t architecture, packagetype_t packagetype, action_each_package action, void *data) {
	retvalue result, r;
	struct target *t;
	struct package_cursor iterator;

	result = RET_NOTHING;
	for (t = distribution->targets ; t != NULL ; t = t->next) {
		if (components != NULL &&
				!atomlist_in(components, t->component))
			continue;
		if (limitation_missed(architecture, t->architecture))
			continue;
		if (limitation_missed(packagetype, t->packagetype))
			continue;
		r = package_openiterator(t, READONLY, &iterator);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			return result;
		while (package_next(&iterator)) {
			r = action(&iterator.current, data);
			RET_UPDATE(result, r);
			if (RET_WAS_ERROR(r))
				break;
		}
		r = package_closeiterator(&iterator);
		RET_ENDUPDATE(result, r);
		if (RET_WAS_ERROR(result))
			return result;
	}
	return result;
}

struct target *distribution_gettarget(const struct distribution *distribution, component_t component, architecture_t architecture, packagetype_t packagetype) {
	struct target *t = distribution->targets;

	assert (atom_defined(component));
	assert (atom_defined(architecture));
	assert (atom_defined(packagetype));

	// TODO: think about making read only access and only alowing readwrite when lookedat is set

	while (t != NULL &&
			(t->component != component ||
			  t->architecture != architecture ||
			  t->packagetype != packagetype)) {
		t = t->next;
	}
	return t;
}

struct target *distribution_getpart(const struct distribution *distribution, component_t component, architecture_t architecture, packagetype_t packagetype) {
	struct target *t = distribution->targets;

	assert (atom_defined(component));
	assert (atom_defined(architecture));
	assert (atom_defined(packagetype));

	while (t != NULL &&
			(t->component != component ||
			  t->architecture != architecture ||
			  t->packagetype != packagetype)) {
		t = t->next;
	}
	if (t == NULL) {
		fprintf(stderr,
"Internal error in distribution_getpart: Bogus request for c='%s' a='%s' t='%s' in '%s'!\n",
				atoms_components[component],
				atoms_architectures[architecture],
				atoms_packagetypes[packagetype],
				distribution->codename);
		abort();
	}
	return t;
}

/* mark all distributions matching one of the first argc argv */
retvalue distribution_match(struct distribution *alldistributions, int argc, const char *argv[], bool lookedat, bool allowreadonly) {
	struct distribution *d;
	bool found[argc], unusable_as_suite[argc];
	struct distribution *has_suite[argc];
	int i;

	assert (alldistributions != NULL);

	if (argc <= 0) {
		for (d = alldistributions ; d != NULL ; d = d->next) {
			if (!allowreadonly && d->readonly)
				continue;
			d->selected = true;
			d->lookedat = lookedat;
		}
		return RET_OK;
	}
	memset(found, 0, sizeof(found));
	memset(unusable_as_suite, 0, sizeof(unusable_as_suite));
	memset(has_suite, 0, sizeof(has_suite));

	for (d = alldistributions ; d != NULL ; d = d->next) {
		for (i = 0 ; i < argc ; i++) {
			if (strcmp(argv[i], d->codename) == 0) {
				assert (!found[i]);
				found[i] = true;
				d->selected = true;
				if (lookedat)
					d->lookedat = lookedat;
				if (!allowreadonly && d->readonly) {
					fprintf(stderr,
"Error: %s is readonly, so operation not allowed!\n",
							d->codename);
					return RET_ERROR;
				}
			} else if (d->suite != NULL &&
					strcmp(argv[i], d->suite) == 0) {
				if (has_suite[i] != NULL)
					unusable_as_suite[i] = true;
				has_suite[i] = d;
			}
		}
	}
	for (i = 0 ; i < argc ; i++) {
		if (!found[i]) {
			if (has_suite[i] != NULL && !unusable_as_suite[i]) {
				if (!allowreadonly && has_suite[i]->readonly) {
					fprintf(stderr,
"Error: %s is readonly, so operation not allowed!\n",
							has_suite[i]->codename);
					return RET_ERROR;
				}
				has_suite[i]->selected = true;
				if (lookedat)
					has_suite[i]->lookedat = lookedat;
				continue;
			}
			fprintf(stderr,
"No distribution definition of '%s' found in '%s/distributions'!\n",
				argv[i], global.confdir);
			if (unusable_as_suite[i])
				fprintf(stderr,
"(It is not the codename of any distribution and there are multiple\n"
"distributions with this as suite name.)\n");
			return RET_ERROR_MISSING;
		}
	}
	return RET_OK;
}

retvalue distribution_get(struct distribution *alldistributions, const char *name, bool lookedat, struct distribution **distribution) {
	struct distribution *d, *d2;

	d = alldistributions;
	while (d != NULL && strcmp(name, d->codename) != 0)
		d = d->next;
	if (d == NULL) {
		for (d2 = alldistributions; d2 != NULL ; d2 = d2->next) {
			if (d2->suite == NULL)
				continue;
			if (strcmp(name, d2->suite) != 0)
				continue;
			if (d != NULL) {
				fprintf(stderr,
"No distribution has '%s' as codename, but multiple as suite name,\n"
"thus it cannot be used to determine a distribution.\n", name);
				return RET_ERROR_MISSING;
			}
			d = d2;
		}
	}
	if (d == NULL) {
		fprintf(stderr,
"Cannot find definition of distribution '%s'!\n",
				name);
		return RET_ERROR_MISSING;
	}
	d->selected = true;
	if (lookedat)
		d->lookedat = true;
	*distribution = d;
	return RET_OK;
}

retvalue distribution_snapshot(struct distribution *distribution, const char *name) {
	struct target *target;
	retvalue result, r;
	struct release *release;
	char *id;

	assert (distribution != NULL);

	r = release_initsnapshot(distribution->codename, name, &release);
	if (RET_WAS_ERROR(r))
		return r;

	result = RET_NOTHING;
	for (target=distribution->targets; target != NULL ;
	                                   target = target->next) {
		r = release_mkdir(release, target->relativedirectory);
		RET_ENDUPDATE(result, r);
		if (RET_WAS_ERROR(r))
			break;
		r = target_export(target, false, true, release);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			break;
		if (target->exportmode->release != NULL) {
			r = release_directorydescription(release, distribution,
					target, target->exportmode->release,
					false);
			RET_UPDATE(result, r);
			if (RET_WAS_ERROR(r))
				break;
		}
	}
	if (!RET_WAS_ERROR(result)) {
		result = release_prepare(release, distribution, false);
		assert (result != RET_NOTHING);
	}
	if (RET_WAS_ERROR(result)) {
		release_free(release);
		return result;
	}
	result = release_finish(release, distribution);
	if (RET_WAS_ERROR(result))
		return r;
	id = mprintf("s=%s=%s", distribution->codename, name);
	if (FAILEDTOALLOC(id))
		return RET_ERROR_OOM;
	r = package_foreach(distribution,
			atom_unknown, atom_unknown, atom_unknown,
			package_referenceforsnapshot, NULL, id);
	free(id);
	RET_UPDATE(result, r);
	return result;
}

static retvalue export(struct distribution *distribution, bool onlyneeded) {
	struct target *target;
	retvalue result, r;
	struct release *release;

	assert (distribution != NULL);

	if (distribution->exportoptions[deo_noexport])
		return RET_NOTHING;

	if (distribution->readonly) {
		fprintf(stderr,
"Error: trying to re-export read-only distribution %s\n",
				distribution->codename);
		return RET_ERROR;
	}

	r = release_init(&release, distribution->codename, distribution->suite,
			distribution->fakecomponentprefix);
	if (RET_WAS_ERROR(r))
		return r;

	result = RET_NOTHING;
	for (target=distribution->targets; target != NULL ;
	                                   target = target->next) {
		r = release_mkdir(release, target->relativedirectory);
		RET_ENDUPDATE(result, r);
		if (RET_WAS_ERROR(r))
			break;
		r = target_export(target, onlyneeded, false, release);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			break;
		if (target->exportmode->release != NULL) {
			r = release_directorydescription(release, distribution,
					target, target->exportmode->release,
					onlyneeded);
			RET_UPDATE(result, r);
			if (RET_WAS_ERROR(r))
				break;
		}
	}
	if (!RET_WAS_ERROR(result) && distribution->contents.flags.enabled) {
		r = contents_generate(distribution, release, onlyneeded);
	}
	if (!RET_WAS_ERROR(result)) {
		result = release_prepare(release, distribution, onlyneeded);
		if (result == RET_NOTHING) {
			release_free(release);
			return result;
		}
	}
	if (RET_WAS_ERROR(result)) {
		bool workleft = false;
		release_free(release);
		fprintf(stderr, "ERROR: Could not finish exporting '%s'!\n",
				distribution->codename);
		for (target=distribution->targets; target != NULL ;
		                                   target = target->next) {
			workleft |= target->saved_wasmodified;
		}
		if (workleft) {
			(void)fputs(
"This means that from outside your repository will still look like before (and\n"
"should still work if this old state worked), but the changes intended with this\n"
"call will not be visible until you call export directly (via reprepro export)\n"
"Changes will also get visible when something else changes the same file and\n"
"thus creates a new export of that file, but even changes to other parts of the\n"
"same distribution will not!\n",
					stderr);
		}
	} else {
		r = release_finish(release, distribution);
		RET_UPDATE(result, r);
	}
	if (RET_IS_OK(result))
		distribution->status = RET_NOTHING;
	return result;
}

retvalue distribution_fullexport(struct distribution *distribution) {
	return export(distribution, false);
}

retvalue distribution_freelist(struct distribution *distributions) {
	retvalue result, r;

	result = RET_NOTHING;
	while (distributions != NULL) {
		struct distribution *d = distributions->next;
		r = distribution_free(distributions);
		RET_UPDATE(result, r);
		distributions = d;
	}
	return result;
}

retvalue distribution_exportlist(enum exportwhen when, struct distribution *distributions) {
	retvalue result, r;
	bool todo = false;
	struct distribution *d;

	if (when == EXPORT_SILENT_NEVER) {
		for (d = distributions ; d != NULL ; d = d->next) {
			struct target *t;

			for (t = d->targets ; t != NULL ; t = t->next)
				t->wasmodified = false;
		}
		return RET_NOTHING;
	}
	if (when == EXPORT_NEVER) {
		if (verbose > 10)
			fprintf(stderr,
"Not exporting anything as --export=never specified\n");
		return RET_NOTHING;
	}

	for (d=distributions; d != NULL; d = d->next) {
		if (d->omitted || !d->selected || d->exportoptions[deo_noexport])
			continue;
		if (d->lookedat && (RET_IS_OK(d->status) ||
			(d->status == RET_NOTHING && when != EXPORT_CHANGED) ||
			when == EXPORT_FORCE)) {
			todo = true;
		}
	}

	if (verbose >= 0 && todo)
		printf("Exporting indices...\n");

	result = RET_NOTHING;
	for (d=distributions; d != NULL; d = d->next) {
		if (d->exportoptions[deo_noexport])
			continue;
		if (d->omitted || !d->selected)
			continue;
		if (!d->lookedat) {
			if (verbose >= 30)
				printf(
" Not exporting %s because not looked at.\n", d->codename);
		} else if ((RET_WAS_ERROR(d->status)||interrupted()) &&
		           when != EXPORT_FORCE) {
			if (verbose >= 10)
				fprintf(stderr,
" Not exporting %s because there have been errors and no --export=force.\n",
						d->codename);
		} else if (d->status==RET_NOTHING && when==EXPORT_CHANGED) {
			struct target *t;

			if (verbose >= 10)
				printf(
" Not exporting %s because of no recorded changes and --export=changed.\n",
						d->codename);

			/* some paranoid check */

			for (t = d->targets ; t != NULL ; t = t->next) {
				if (t->wasmodified) {
					fprintf(stderr,
"A paranoid check found distribution %s would not have been exported,\n"
"despite having parts that are marked changed by deeper code.\n"
"Please report this and how you got this message as bugreport. Thanks.\n"
"Doing a export despite --export=changed....\n",
						d->codename);
					r = export(d, true);
					RET_UPDATE(result, r);
					break;
				}
			}
		} else {
			assert (RET_IS_OK(d->status) ||
					(d->status == RET_NOTHING &&
					  when != EXPORT_CHANGED) ||
					when == EXPORT_FORCE);
			r = export(d, true);
			RET_UPDATE(result, r);
		}
	}
	return result;
}


/* get a pointer to the apropiate part of the linked list */
struct distribution *distribution_find(struct distribution *distributions, const char *name) {
	struct distribution *d = distributions, *r;

	while (d != NULL && strcmp(d->codename, name) != 0)
		d = d->next;
	if (d != NULL)
		return d;
	d = distributions;
	while (d != NULL && !strlist_in(&d->alsoaccept, name))
		d = d->next;
	r = d;
	if (r != NULL) {
		d = d->next;
		while (d != NULL && ! strlist_in(&d->alsoaccept, name))
			d = d->next;
		if (d == NULL)
			return r;
		fprintf(stderr,
"No distribution has codename '%s' and multiple have it in AlsoAcceptFor!\n",
				name);
		return NULL;
	}
	d = distributions;
	while (d != NULL && (d->suite == NULL || strcmp(d->suite, name) != 0))
		d = d->next;
	r = d;
	if (r == NULL) {
		fprintf(stderr, "No distribution named '%s' found!\n", name);
		return NULL;
	}
	d = d->next;
	while (d != NULL && (d->suite == NULL || strcmp(d->suite, name) != 0))
		d = d->next;
	if (d == NULL)
		return r;
	fprintf(stderr,
"No distribution has codename '%s' and multiple have it as suite-name!\n",
			name);
	return NULL;
}

retvalue distribution_loadalloverrides(struct distribution *distribution) {
	retvalue r;

	if (distribution->overrides.deb == NULL) {
		r = override_read(distribution->deb_override,
				&distribution->overrides.deb, false);
		if (RET_WAS_ERROR(r)) {
			distribution->overrides.deb = NULL;
			return r;
		}
	}
	if (distribution->overrides.udeb == NULL) {
		r = override_read(distribution->udeb_override,
				&distribution->overrides.udeb, false);
		if (RET_WAS_ERROR(r)) {
			distribution->overrides.udeb = NULL;
			return r;
		}
	}
	if (distribution->overrides.dsc == NULL) {
		r = override_read(distribution->dsc_override,
				&distribution->overrides.dsc, true);
		if (RET_WAS_ERROR(r)) {
			distribution->overrides.dsc = NULL;
			return r;
		}
	}
	if (distribution->overrides.deb != NULL ||
	    distribution->overrides.udeb != NULL ||
	    distribution->overrides.dsc != NULL)
		return RET_OK;
	else
		return RET_NOTHING;
}

retvalue distribution_loaduploaders(struct distribution *distribution) {
	if (distribution->uploaders != NULL) {
		if (distribution->uploaderslist != NULL)
			return RET_OK;
		return uploaders_get(&distribution->uploaderslist,
				distribution->uploaders);
	} else {
		distribution->uploaderslist = NULL;
		return RET_NOTHING;
	}
}

void distribution_unloaduploaders(struct distribution *distribution) {
	if (distribution->uploaderslist != NULL) {
		uploaders_unlock(distribution->uploaderslist);
		distribution->uploaderslist = NULL;
	}
}

retvalue distribution_prepareforwriting(struct distribution *distribution) {
	retvalue r;

	if (distribution->readonly) {
		fprintf(stderr,
"Error: distribution %s is read-only.\n"
"Current operation not possible because it needs write access.\n",
				distribution->codename);
		return RET_ERROR;
	}

	if (distribution->logger != NULL) {
		r = logger_prepare(distribution->logger);
		if (RET_WAS_ERROR(r))
			return r;
	}
	distribution->lookedat = true;
	return RET_OK;
}

/* delete every package decider returns RET_OK for */
retvalue package_remove_each(struct distribution *distribution, const struct atomlist *components, const struct atomlist *architectures, const struct atomlist *packagetypes, action_each_package decider, struct trackingdata *trackingdata, void *data) {
	retvalue result, r;
	struct target *t;
	struct package_cursor iterator;

	if (distribution->readonly) {
		fprintf(stderr,
"Error: trying to delete packages in read-only distribution %s.\n",
				distribution->codename);
		return RET_ERROR;
	}

	result = RET_NOTHING;
	for (t = distribution->targets ; t != NULL ; t = t->next) {
		if (!target_matches(t, components, architectures, packagetypes))
			continue;
		r = package_openiterator(t, READWRITE, &iterator);
		RET_UPDATE(result, r);
		if (RET_WAS_ERROR(r))
			return result;
		while (package_next(&iterator)) {
			r = decider(&iterator.current, data);
			RET_UPDATE(result, r);
			if (RET_WAS_ERROR(r))
				break;
			if (RET_IS_OK(r)) {
				r = package_remove_by_cursor(&iterator,
					distribution->logger, trackingdata);
				RET_UPDATE(result, r);
				RET_UPDATE(distribution->status, r);
			}
		}
		r = package_closeiterator(&iterator);
		RET_ENDUPDATE(result, r);
		if (RET_WAS_ERROR(result))
			return result;
	}
	return result;
}