summaryrefslogtreecommitdiffstats
path: root/indexfile.c
blob: c13832de300f1aa38015993209d20b2453bea35f (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
/*  This file is part of "reprepro"
 *  Copyright (C) 2003,2004,2005,2007,2008,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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include "error.h"
#include "ignore.h"
#include "chunks.h"
#include "names.h"
#include "uncompression.h"
#include "package.h"
#include "indexfile.h"

/* the purpose of this code is to read index files, either from a snapshot
 * previously generated or downloaded while updating. */

struct indexfile {
	struct compressedfile *f;
	char *filename;
	int linenumber, startlinenumber;
	retvalue status;
	char *buffer;
	int size, ofs, content;
	bool failed;
};

retvalue indexfile_open(struct indexfile **file_p, const char *filename, enum compression compression) {
	struct indexfile *f = zNEW(struct indexfile);
	retvalue r;

	if (FAILEDTOALLOC(f))
		return RET_ERROR_OOM;
	f->filename = strdup(filename);
	if (FAILEDTOALLOC(f->filename)) {
		free(f);
		return RET_ERROR_OOM;
	}
	r = uncompress_open(&f->f, filename, compression);
	assert (r != RET_NOTHING);
	if (RET_WAS_ERROR(r)) {
		free(f->filename);
		free(f);
		return RET_ERRNO(errno);
	}
	f->linenumber = 0;
	f->startlinenumber = 0;
	f->status = RET_OK;
	f->size = 4*1024*1024;
	f->ofs = 0;
	f->content = 0;
	/* +1 for *d = '\0' in eof case */
	f->buffer = malloc(f->size + 1);
	if (FAILEDTOALLOC(f->buffer)) {
		uncompress_abort(f->f);
		free(f->filename);
		free(f);
		return RET_ERROR_OOM;
	}
	*file_p = f;
	return RET_OK;
}

retvalue indexfile_close(struct indexfile *f) {
	retvalue r;

	r = uncompress_close(f->f);

	free(f->filename);
	free(f->buffer);
	RET_UPDATE(r, f->status);
	free(f);

	return r;
}

static retvalue indexfile_get(struct indexfile *f) {
	char *p, *d, *e, *start;
	bool afternewline, nothingyet;
	int bytes_read;

	if (f->failed)
		return RET_ERROR;

	d = f->buffer;
	afternewline = true;
	nothingyet = true;
	do {
		start = f->buffer + f->ofs;
		p = start ;
		e = p + f->content;

		// TODO: if the chunk_get* are more tested with strange
		// input, this could be kept in-situ and only chunk_edit
		// beautifying this chunk...

		while (p < e) {
			/* just ignore '\r', even if not line-end... */
			if (*p == '\r') {
				p++;
				continue;
			}
			if (*p == '\n') {
				f->linenumber++;
				if (afternewline) {
					p++;
					f->content -= (p - start);
					f->ofs += (p - start);
					assert (f->ofs == (p - f->buffer));
					if (nothingyet)
						/* restart */
						return indexfile_get(f);
					if (d > f->buffer && *(d-1) == '\n')
						d--;
					*d = '\0';
					return RET_OK;
				}
				afternewline = true;
				nothingyet = false;
			} else
				afternewline = false;
			if (unlikely(*p == '\0')) {
				*(d++) = ' ';
				p++;
			} else
				*(d++) = *(p++);
		}
		/* ** out of data, read new ** */

		/* start at beginning of free space */
		f->ofs = (d - f->buffer);
		f->content = 0;

		if (f->size - f->ofs <= 2048) {
			/* Adding code to enlarge the buffer in this case
			 * is risky as hard to test properly.
			 *
			 * Also it is almost certainly caused by some
			 * mis-representation of the file or perhaps
			 * some attack. Requesting all existing memory in
			 * those cases does not sound very useful. */

			fprintf(stderr,
"Error parsing %s line %d: Ridiculous long (>= 256K) control chunk!\n",
					f->filename,
					f->startlinenumber);
			f->failed = true;
			return RET_ERROR;
		}

		bytes_read = uncompress_read(f->f, d, f->size - f->ofs);
		if (bytes_read < 0)
			return RET_ERROR;
		else if (bytes_read == 0)
			break;
		f->content = bytes_read;
	} while (true);

	if (d == f->buffer)
		return RET_NOTHING;

	/* end of file reached, return what we got so far */
	assert (f->content == 0);
	assert (d-f->buffer <= f->size);
	if (d > f->buffer && *(d-1) == '\n')
		d--;
	*d = '\0';
	return RET_OK;
}

bool indexfile_getnext(struct indexfile *f, struct package *pkgout, struct target *target, bool allowwrongarchitecture) {
	retvalue r;
	bool ignorecruft = false; // TODO
	char *packagename, *version;
	const char *control;
	architecture_t atom;

	packagename = NULL; version = NULL;
	do {
		free(packagename); packagename = NULL;
		free(version); version = NULL;
		f->startlinenumber = f->linenumber + 1;
		r = indexfile_get(f);
		if (!RET_IS_OK(r))
			break;
		control = f->buffer;
		r = chunk_getvalue(control, "Package", &packagename);
		if (r == RET_NOTHING) {
			fprintf(stderr,
"Error parsing %s line %d to %d: Chunk without 'Package:' field!\n",
					f->filename,
					f->startlinenumber, f->linenumber);
			if (!ignorecruft)
				r = RET_ERROR_MISSING;
			else
				continue;
		}
		if (RET_WAS_ERROR(r))
			break;

		r = chunk_getvalue(control, "Version", &version);
		if (r == RET_NOTHING) {
			fprintf(stderr,
"Error parsing %s line %d to %d: Chunk without 'Version:' field!\n",
					f->filename,
					f->startlinenumber, f->linenumber);
			if (!ignorecruft)
				r = RET_ERROR_MISSING;
			else
				continue;
		}
		if (RET_WAS_ERROR(r))
			break;
		if (target->packagetype == pt_dsc) {
			atom = architecture_source;
		} else {
			char *architecture;

			r = chunk_getvalue(control, "Architecture", &architecture);
			if (RET_WAS_ERROR(r))
				break;
			if (r == RET_NOTHING)
				architecture = NULL;

			/* check if architecture fits for target and error
			    out if not ignorewrongarchitecture */
			if (architecture == NULL) {
				fprintf(stderr,
"Error parsing %s line %d to %d: Chunk without 'Architecture:' field!\n",
						f->filename,
						f->startlinenumber, f->linenumber);
				if (!ignorecruft) {
					r = RET_ERROR_MISSING;
					break;
				} else
					continue;
			} else if (strcmp(architecture, "all") == 0) {
				atom = architecture_all;
			} else if (strcmp(architecture,
					   atoms_architectures[
						target->architecture
						]) == 0) {
				atom = target->architecture;
			} else if (!allowwrongarchitecture
					&& !ignore[IGN_wrongarchitecture]) {
				fprintf(stderr,
"Warning: ignoring package because of wrong 'Architecture:' field '%s'"
" (expected 'all' or '%s') in %s lines %d to %d!\n",
						architecture,
						atoms_architectures[
						target->architecture],
						f->filename,
						f->startlinenumber,
						f->linenumber);
				if (ignored[IGN_wrongarchitecture] == 0) {
					fprintf(stderr,
"This either mean the repository you get packages from is of an extremely\n"
"low quality, or something went wrong. Trying to ignore it now, though.\n"
"To no longer get this message use '--ignore=wrongarchitecture'.\n");
				}
				ignored[IGN_wrongarchitecture]++;
				free(architecture);
				continue;
			} else {
				/* just ignore this because of wrong
				 * architecture */
				free(architecture);
				continue;
			}
			free(architecture);
		}
		if (RET_WAS_ERROR(r))
			break;
		pkgout->target = target;
		pkgout->control = control;
		pkgout->pkgname = packagename;
		pkgout->name = pkgout->pkgname;
		pkgout->pkgversion = version;
		pkgout->version = pkgout->pkgversion;
		pkgout->architecture = atom;
		return true;
	} while (true);
	free(packagename);
	free(version);
	RET_UPDATE(f->status, r);
	return false;
}