summaryrefslogtreecommitdiffstats
path: root/sources.c
blob: 1e7efcb36756c5b1224f5f304aa62bdb310c61ce (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
/*  This file is part of "reprepro"
 *  Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010 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 <assert.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "error.h"
#include "mprintf.h"
#include "strlist.h"
#include "chunks.h"
#include "sources.h"
#include "names.h"
#include "dirs.h"
#include "dpkgversions.h"
#include "override.h"
#include "tracking.h"
#include "signature.h"
#include "package.h"

/* split a "<md5> <size> <filename>" into md5sum and filename */
static retvalue calc_parsefileline(const char *fileline, /*@out@*/char **filename) {
	const char *p, *fn, *fnend;
	char *filen;

	assert (fileline != NULL);
	if (*fileline == '\0')
		return RET_NOTHING;

	/* the md5sums begins after the (perhaps) heading spaces ...  */
	p = fileline;
	while (*p != '\0' && (*p == ' ' || *p == '\t'))
		p++;
	if (*p == '\0')
		return RET_NOTHING;
	/* ... and ends with the following spaces. */
	while (*p != '\0' && !(*p == ' ' || *p == '\t'))
		p++;
	if (*p == '\0') {
		fprintf(stderr, "Expecting more data after md5sum!\n");
		return RET_ERROR;
	}
	/* Then the size of the file is expected: */
	while ((*p == ' ' || *p == '\t'))
		p++;
	while (*p !='\0' && !(*p == ' ' || *p == '\t'))
		p++;
	if (*p == '\0') {
		fprintf(stderr, "Expecting more data after size!\n");
		return RET_ERROR;
	}
	/* Then the filename */
	fn = p;
	while ((*fn == ' ' || *fn == '\t'))
		fn++;
	fnend = fn;
	while (*fnend != '\0' && !(*fnend == ' ' || *fnend == '\t'))
		fnend++;

	filen = strndup(fn, fnend-fn);
	if (FAILEDTOALLOC(filen))
		return RET_ERROR_OOM;
	*filename = filen;
	return RET_OK;
}

static retvalue getBasenames(const struct strlist *filelines, /*@out@*/struct strlist *basenames) {
	int i;
	retvalue r;

	assert (filelines != NULL && basenames != NULL);

	r = strlist_init_n(filelines->count, basenames);
	if (RET_WAS_ERROR(r))
		return r;
	r = RET_NOTHING;
	for (i = 0 ; i < filelines->count ; i++) {
		char *basefilename;
		const char *fileline = filelines->values[i];

		r = calc_parsefileline(fileline, &basefilename);
		if (r == RET_NOTHING) {
			fprintf(stderr, "Malformed Files: line '%s'!\n",
					fileline);
			r = RET_ERROR;
		}
		if (RET_WAS_ERROR(r))
			break;

		r = strlist_add(basenames, basefilename);
		if (RET_WAS_ERROR(r)) {
			break;
		}
		r = RET_OK;
	}
	if (RET_WAS_ERROR(r)) {
		strlist_done(basenames);
	} else {
		assert (filelines->count == basenames->count);
	}
	return r;
}

retvalue sources_getversion(const char *control, char **version) {
	retvalue r;

	r = chunk_getvalue(control, "Version", version);
	if (RET_WAS_ERROR(r))
		return r;
	if (r == RET_NOTHING) {
		fprintf(stderr, "Missing 'Version' field in chunk:'%s'\n",
				control);
		return RET_ERROR;
	}
	return r;
}

retvalue sources_getarchitecture(UNUSED(const char *chunk), architecture_t *architecture_p) {
	*architecture_p = architecture_source;
	return RET_OK;
}

retvalue sources_getinstalldata(const struct target *t, struct package *package, char **control, struct strlist *filekeys, struct checksumsarray *origfiles) {
	retvalue r;
	char *origdirectory, *directory, *mychunk;
	struct strlist myfilekeys;
	struct strlist filelines[cs_hashCOUNT];
	struct checksumsarray files;
	enum checksumtype cs;
	bool gothash = false;
	const char *chunk = package->control;

	assert (package->architecture == architecture_source);

	for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++) {
		assert (source_checksum_names[cs] != NULL);
		r = chunk_getextralinelist(chunk, source_checksum_names[cs],
				&filelines[cs]);
		if (r == RET_NOTHING)
			strlist_init(&filelines[cs]);
		else if (RET_WAS_ERROR(r)) {
			while (cs-- > cs_md5sum) {
				strlist_done(&filelines[cs]);
			}
			return r;
		} else
			gothash = true;
	}
	if (!gothash) {
		fprintf(stderr,
"Missing 'Files' (or 'SHA1' or ...)  entry in '%s'!\n",
				chunk);
		for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++)
			strlist_done(&filelines[cs]);
		return RET_ERROR;
	}
	r = checksumsarray_parse(&files, filelines, package->name);
	for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++) {
		strlist_done(&filelines[cs]);
	}
	if (RET_WAS_ERROR(r))
		return r;

	r = chunk_getvalue(chunk, "Directory", &origdirectory);
	if (r == RET_NOTHING) {
/* Flat repositories can come without this, TODO: add warnings in other cases
		fprintf(stderr, "Missing 'Directory' entry in '%s'!\n", chunk);
		r = RET_ERROR;
*/
		origdirectory = strdup(".");
		if (FAILEDTOALLOC(origdirectory))
			r = RET_ERROR_OOM;
	}
	if (RET_WAS_ERROR(r)) {
		checksumsarray_done(&files);
		return r;
	}

	r = propersourcename(package->name);
	assert (r != RET_NOTHING);
	if (RET_IS_OK(r))
		r = properfilenames(&files.names);
	if (RET_WAS_ERROR(r)) {
		fprintf(stderr,
"Forbidden characters in source package '%s'!\n", package->name);
		free(origdirectory);
		checksumsarray_done(&files);
		return r;
	}

	directory = calc_sourcedir(t->component, package->name);
	if (FAILEDTOALLOC(directory))
		r = RET_ERROR_OOM;
	else
		r = calc_dirconcats(directory, &files.names, &myfilekeys);
	if (RET_WAS_ERROR(r)) {
		free(directory);
		free(origdirectory);
		checksumsarray_done(&files);
		return r;
	}
	r = calc_inplacedirconcats(origdirectory, &files.names);
	free(origdirectory);
	if (!RET_WAS_ERROR(r)) {
		char *n;

		n = chunk_normalize(chunk, "Package", package->name);
		if (FAILEDTOALLOC(n))
			mychunk = NULL;
		else
			mychunk = chunk_replacefield(n,
					"Directory", directory, true);
		free(n);
		if (FAILEDTOALLOC(mychunk))
			r = RET_ERROR_OOM;
	}
	free(directory);
	if (RET_WAS_ERROR(r)) {
		strlist_done(&myfilekeys);
		checksumsarray_done(&files);
		return r;
	}
	*control = mychunk;
	strlist_move(filekeys, &myfilekeys);
	checksumsarray_move(origfiles, &files);
	return RET_OK;
}

retvalue sources_getfilekeys(const char *chunk, struct strlist *filekeys) {
	char *origdirectory;
	struct strlist basenames;
	retvalue r;
	struct strlist filelines;


	/* Read the directory given there */
	r = chunk_getvalue(chunk, "Directory", &origdirectory);
	if (r == RET_NOTHING) {
		//TODO: check if it is even text and do not print
		//of looking binary??
		fprintf(stderr, "Does not look like source control: '%s'\n",
				chunk);
		return RET_ERROR;
	}
	if (RET_WAS_ERROR(r))
		return r;

	r = chunk_getextralinelist(chunk, "Files", &filelines);
	if (r == RET_NOTHING) {
		//TODO: check if it is even text and do not print
		//of looking binary??
		fprintf(stderr, "Does not look like source control: '%s'\n",
				chunk);
		r = RET_ERROR;
	}
	if (RET_WAS_ERROR(r)) {
		free(origdirectory);
		return r;
	}
	r = getBasenames(&filelines, &basenames);
	strlist_done(&filelines);
	if (RET_WAS_ERROR(r)) {
		free(origdirectory);
		return r;
	}

	r = calc_dirconcats(origdirectory, &basenames, filekeys);
	free(origdirectory);
	strlist_done(&basenames);
	return r;
}

retvalue sources_getchecksums(const char *chunk, struct checksumsarray *out) {
	char *origdirectory;
	struct checksumsarray a;
	retvalue r;
	struct strlist filelines[cs_hashCOUNT];
	enum checksumtype cs;

	/* Read the directory given there */
	r = chunk_getvalue(chunk, "Directory", &origdirectory);
	if (!RET_IS_OK(r))
		return r;

	for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++) {
		assert (source_checksum_names[cs] != NULL);
		r = chunk_getextralinelist(chunk, source_checksum_names[cs],
				&filelines[cs]);
		if (r == RET_NOTHING) {
			if (cs == cs_md5sum) {
				fprintf(stderr,
"Missing 'Files' entry in '%s'!\n",
						chunk);
				r = RET_ERROR;
			} else
				strlist_init(&filelines[cs]);
		}
		if (RET_WAS_ERROR(r)) {
			while (cs-- > cs_md5sum) {
				strlist_done(&filelines[cs]);
			}
			free(origdirectory);
			return r;
		}
	}
	r = checksumsarray_parse(&a, filelines, "source chunk");
	for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++) {
		strlist_done(&filelines[cs]);
	}
	if (RET_WAS_ERROR(r)) {
		free(origdirectory);
		return r;
	}

	r = calc_inplacedirconcats(origdirectory, &a.names);
	free(origdirectory);
	if (RET_WAS_ERROR(r)) {
		checksumsarray_done(&a);
		return r;
	}
	checksumsarray_move(out, &a);
	return RET_OK;
}

retvalue sources_doreoverride(const struct target *target, const char *packagename, const char *controlchunk, /*@out@*/char **newcontrolchunk) {
	const struct overridedata *o;
	struct fieldtoadd *fields;
	char *newchunk;
	retvalue r;

	if (interrupted())
		return RET_ERROR_INTERRUPTED;

	o = override_search(target->distribution->overrides.dsc, packagename);
	if (o == NULL)
		return RET_NOTHING;

	r = override_allreplacefields(o, &fields);
	if (!RET_IS_OK(r))
		return r;
	newchunk = chunk_replacefields(controlchunk, fields,
			"Directory", true);
	addfield_free(fields);
	if (FAILEDTOALLOC(newchunk))
		return RET_ERROR_OOM;
	*newcontrolchunk = newchunk;
	return RET_OK;
}

retvalue sources_retrack(const char *sourcename, const char *chunk, trackingdb tracks) {
	retvalue r;
	char *sourceversion;
	struct trackedpackage *pkg;
	struct strlist filekeys;
	int i;

	//TODO: eliminate duplicate code!
	assert(sourcename!=NULL);

	if (interrupted())
		return RET_ERROR_INTERRUPTED;

	r = chunk_getvalue(chunk, "Version", &sourceversion);
	if (r == RET_NOTHING) {
		fprintf(stderr, "Missing 'Version' field in chunk:'%s'\n",
				chunk);
		r = RET_ERROR;
	}
	if (RET_WAS_ERROR(r)) {
		return r;
	}

	r = sources_getfilekeys(chunk, &filekeys);
	if (r == RET_NOTHING) {
		fprintf(stderr, "Malformed source control:'%s'\n", chunk);
		r = RET_ERROR;
	}
	if (RET_WAS_ERROR(r)) {
		free(sourceversion);
		return r;
	}

	r = tracking_getornew(tracks, sourcename, sourceversion, &pkg);
	free(sourceversion);
	if (RET_WAS_ERROR(r)) {
		strlist_done(&filekeys);
		return r;
	}

	// TODO: error handling is suboptimal here.
	//  is there a way to again remove old additions (esp. references)
	//  where something fails?
	for (i = 0 ; i < filekeys.count ; i++) {
		r = trackedpackage_addfilekey(tracks, pkg,
				ft_SOURCE, filekeys.values[i], true);
		filekeys.values[i] = NULL;
		if (RET_WAS_ERROR(r)) {
			strlist_done(&filekeys);
			trackedpackage_free(pkg);
			return r;
		}
	}
	strlist_done(&filekeys);
	return tracking_save(tracks, pkg);
}

retvalue sources_getsourceandversion(const char *chunk, const char *packagename, char **source, char **version) {
	retvalue r;
	char *sourceversion;
	char *sourcename;

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

	r = chunk_getvalue(chunk, "Version", &sourceversion);
	if (r == RET_NOTHING) {
		fprintf(stderr, "Missing 'Version' field in chunk:'%s'\n",
				chunk);
		r = RET_ERROR;
	}
	if (RET_WAS_ERROR(r)) {
		return r;
	}
	sourcename = strdup(packagename);
	if (FAILEDTOALLOC(sourcename)) {
		free(sourceversion);
		return RET_ERROR_OOM;
	}
	*source = sourcename;
	*version = sourceversion;
	return RET_OK;
}

/****************************************************************/

static inline retvalue getvalue(const char *filename, const char *chunk, const char *field, char **value) {
	retvalue r;

	r = chunk_getvalue(chunk, field, value);
	if (r == RET_NOTHING) {
		fprintf(stderr, "Missing '%s' field in %s!\n",
				field, filename);
		r = RET_ERROR;
	}
	return r;
}

static inline retvalue checkvalue(const char *filename, const char *chunk, const char *field) {
	retvalue r;

	r = chunk_checkfield(chunk, field);
	if (r == RET_NOTHING) {
		fprintf(stderr, "Cannot find '%s' field in %s!\n",
				field, filename);
		r = RET_ERROR;
	}
	return r;
}

static inline retvalue getvalue_n(const char *chunk, const char *field, char **value) {
	retvalue r;

	r = chunk_getvalue(chunk, field, value);
	if (r == RET_NOTHING) {
		*value = NULL;
	}
	return r;
}

retvalue sources_readdsc(struct dsc_headers *dsc, const char *filename, const char *filenametoshow, bool *broken) {
	retvalue r;
	struct strlist filelines[cs_hashCOUNT];
	enum checksumtype cs;

	r = signature_readsignedchunk(filename, filenametoshow,
			&dsc->control, NULL, broken);
	if (RET_WAS_ERROR(r)) {
		return r;
	}
	if (verbose > 100) {
		fprintf(stderr, "Extracted control chunk from '%s': '%s'\n",
				filenametoshow, dsc->control);
	}

	/* first look for fields that should be there */

	r = chunk_getname(dsc->control, "Source", &dsc->name, false);
	if (r == RET_NOTHING) {
		fprintf(stderr, "Missing 'Source' field in %s!\n",
				filenametoshow);
		return RET_ERROR;
	}
	if (RET_WAS_ERROR(r))
		return r;

	/* This is needed and cannot be ignored unless
	 * sources_complete is changed to not need it */
	r = checkvalue(filenametoshow, dsc->control, "Format");
	if (RET_WAS_ERROR(r))
		return r;

	r = checkvalue(filenametoshow, dsc->control, "Maintainer");
	if (RET_WAS_ERROR(r))
		return r;

	r = getvalue(filenametoshow, dsc->control, "Version", &dsc->version);
	if (RET_WAS_ERROR(r))
		return r;

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

	for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++) {
		assert (source_checksum_names[cs] != NULL);
		r = chunk_getextralinelist(dsc->control,
				source_checksum_names[cs], &filelines[cs]);
		if (r == RET_NOTHING) {
			if (cs == cs_md5sum) {
				fprintf(stderr,
"Missing 'Files' field in '%s'!\n",
					filenametoshow);
				r = RET_ERROR;
			} else
				strlist_init(&filelines[cs]);
		}
		if (RET_WAS_ERROR(r)) {
			while (cs-- > cs_md5sum) {
				strlist_done(&filelines[cs]);
			}
			return r;
		}
	}
	r = checksumsarray_parse(&dsc->files, filelines, filenametoshow);
	for (cs = cs_md5sum ; cs < cs_hashCOUNT ; cs++) {
		strlist_done(&filelines[cs]);
	}
	return r;
}

void sources_done(struct dsc_headers *dsc) {
	free(dsc->name);
	free(dsc->version);
	free(dsc->control);
	checksumsarray_done(&dsc->files);
	free(dsc->section);
	free(dsc->priority);
}

retvalue sources_complete(const struct dsc_headers *dsc, const char *directory, const struct overridedata *override, const char *section, const char *priority, char **newcontrol) {
	retvalue r;
	struct fieldtoadd *replace;
	char *newchunk, *newchunk2;
	char *newfilelines, *newsha1lines, *newsha256lines;

	assert(section != NULL && priority != NULL);

	newchunk2 = chunk_normalize(dsc->control, "Package", dsc->name);
	if (FAILEDTOALLOC(newchunk2))
		return RET_ERROR_OOM;

	r = checksumsarray_genfilelist(&dsc->files,
			&newfilelines, &newsha1lines, &newsha256lines);
	if (RET_WAS_ERROR(r)) {
		free(newchunk2);
		return r;
	}
	assert (newfilelines != NULL);
	replace = aodfield_new("Checksums-Sha256", newsha256lines, NULL);
	if (!FAILEDTOALLOC(replace))
		replace = aodfield_new("Checksums-Sha1", newsha1lines, replace);
	if (!FAILEDTOALLOC(replace))
		replace = deletefield_new("Source", replace);
	if (!FAILEDTOALLOC(replace))
		replace = addfield_new("Files", newfilelines, replace);
	if (!FAILEDTOALLOC(replace))
		replace = addfield_new("Directory", directory, replace);
	if (!FAILEDTOALLOC(replace))
		replace = deletefield_new("Status", replace);
	if (!FAILEDTOALLOC(replace))
		replace = addfield_new(SECTION_FIELDNAME, section, replace);
	if (!FAILEDTOALLOC(replace))
		replace = addfield_new(PRIORITY_FIELDNAME, priority, replace);
	if (!FAILEDTOALLOC(replace))
		replace = override_addreplacefields(override, replace);
	if (FAILEDTOALLOC(replace)) {
		free(newsha256lines);
		free(newsha1lines);
		free(newfilelines);
		free(newchunk2);
		return RET_ERROR_OOM;
	}

	newchunk  = chunk_replacefields(newchunk2, replace, "Files", true);
	free(newsha256lines);
	free(newsha1lines);
	free(newfilelines);
	free(newchunk2);
	addfield_free(replace);
	if (FAILEDTOALLOC(newchunk)) {
		return RET_ERROR_OOM;
	}

	*newcontrol = newchunk;

	return RET_OK;
}

/* update Checksums */
retvalue sources_complete_checksums(const char *chunk, const struct strlist *filekeys, struct checksums **c, char **out) {
	struct fieldtoadd *replace;
	char *newchunk;
	char *newfilelines, *newsha1lines, *newsha256lines;
	struct checksumsarray checksums;
	retvalue r;
	int i;

	/* fake a checksumarray... */
	checksums.checksums = c;
	checksums.names.count = filekeys->count;
	checksums.names.values = nzNEW(filekeys->count, char *);
	if (FAILEDTOALLOC(checksums.names.values))
		return RET_ERROR_OOM;
	for (i = 0 ; i < filekeys->count ; i++) {
		checksums.names.values[i] = (char*)
			dirs_basename(filekeys->values[i]);
	}

	r = checksumsarray_genfilelist(&checksums,
			&newfilelines, &newsha1lines, &newsha256lines);
	free(checksums.names.values);
	if (RET_WAS_ERROR(r))
		return r;
	assert (newfilelines != NULL);
	replace = aodfield_new("Checksums-Sha256", newsha256lines, NULL);
	if (!FAILEDTOALLOC(replace))
		replace = aodfield_new("Checksums-Sha1", newsha1lines, replace);
	if (!FAILEDTOALLOC(replace))
		replace = addfield_new("Files", newfilelines, replace);
	if (FAILEDTOALLOC(replace)) {
		free(newsha256lines);
		free(newsha1lines);
		free(newfilelines);
		return RET_ERROR_OOM;
	}
	newchunk = chunk_replacefields(chunk, replace, "Files", true);
	free(newsha256lines);
	free(newsha1lines);
	free(newfilelines);
	addfield_free(replace);
	if (FAILEDTOALLOC(newchunk))
		return RET_ERROR_OOM;

	*out = newchunk;
	return RET_OK;
}

char *calc_source_basename(const char *name, const char *version) {
	const char *v = strchr(version, ':');
	if (v != NULL)
		v++;
	else
		v = version;
	return mprintf("%s_%s.dsc", name, v);
}

char *calc_sourcedir(component_t component, const char *sourcename) {

	assert (*sourcename != '\0');

	if (sourcename[0] == 'l' && sourcename[1] == 'i' &&
			sourcename[2] == 'b' && sourcename[3] != '\0')
		return mprintf("pool/%s/lib%c/%s",
				atoms_components[component],
				sourcename[3], sourcename);
	else if (*sourcename != '\0')
		return mprintf("pool/%s/%c/%s",
				atoms_components[component],
				sourcename[0], sourcename);
	else
		return NULL;
}

char *calc_filekey(component_t component, const char *sourcename, const char *filename) {
	if (sourcename[0] == 'l' && sourcename[1] == 'i' &&
			sourcename[2] == 'b' && sourcename[3] != '\0')
		return mprintf("pool/%s/lib%c/%s/%s",
				atoms_components[component],
				sourcename[3], sourcename, filename);
	else if (*sourcename != '\0')
		return mprintf("pool/%s/%c/%s/%s",
				atoms_components[component],
				sourcename[0], sourcename, filename);
	else
		return NULL;
}

char *calc_byhanddir(component_t component, const char *sourcename, const char *version) {
	if (sourcename[0] == 'l' && sourcename[1] == 'i' &&
			sourcename[2] == 'b' && sourcename[3] != '\0')
		return mprintf("pool/%s/lib%c/%s/%s_%s_byhand",
				atoms_components[component],
				sourcename[3], sourcename,
				sourcename, version);
	else if (*sourcename != '\0')
		return mprintf("pool/%s/%c/%s/%s_%s_byhand",
				atoms_components[component],
				sourcename[0], sourcename,
				sourcename, version);
	else
		return NULL;
}