File: changes.c

package info (click to toggle)
reprepro 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,792 kB
  • sloc: ansic: 52,309; sh: 1,875; python: 1,625; makefile: 167
file content (333 lines) | stat: -rw-r--r-- 9,504 bytes parent folder | download | duplicates (5)
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
/*  This file is part of "reprepro"
 *  Copyright (C) 2003,2004,2005,2006,2008 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 <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include "error.h"
#include "names.h"
#include "uncompression.h"
#include "checksums.h"
#include "changes.h"

retvalue changes_parsefileline(const char *fileline, /*@out@*/filetype *result_type, /*@out@*/char **result_basename, /*@out@*/struct hash_data *hash_p, /*@out@*/struct hash_data *size_p, /*@out@*/char **result_section, /*@out@*/char **result_priority, /*@out@*/architecture_t *result_architecture, /*@out@*/char **result_name) {

	const char *p, *md5start, *md5end;
	const char *sizestart, *sizeend;
	const char *sectionstart, *sectionend;
	const char *priostart, *prioend;
	const char *filestart, *nameend, *fileend;
	const char *archstart, *archend;
	const char *versionstart;
	filetype type;
	char *section, *priority, *basefilename, *name;
	architecture_t architecture;
	size_t l;
	bool checkfilename = false;

	p = fileline;
	while (*p !='\0' && xisspace(*p))
		p++;
	md5start = p;
	while ((*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f'))
		p++;
	if (*p == '\0') {
		fprintf(stderr, "Missing md5sum in '%s'!\n", fileline);
		return RET_ERROR;
	}
	if (!xisspace(*p)) {
		fprintf(stderr, "Malformed md5 hash in '%s'!\n", fileline);
		return RET_ERROR;
	}
	md5end = p;
	while (*p !='\0' && xisspace(*p))
		p++;
	while (*p == '0' && p[1] >= '0' && p[1] <= '9')
		p++;
	sizestart = p;
	while (*p >= '0' && *p <= '9')
		p++;
	if (*p == '\0') {
		fprintf(stderr,
"Missing size (second argument) in '%s'!\n", fileline);
		return RET_ERROR;
	}
	if (!xisspace(*p)) {
		fprintf(stderr,
"Malformed size (second argument) in '%s'!\n", fileline);
		return RET_ERROR;
	}
	sizeend = p;
	while (*p !='\0' && xisspace(*p))
		p++;
	sectionstart = p;
	while (*p !='\0' && !xisspace(*p))
		p++;
	sectionend = p;
	while (*p !='\0' && xisspace(*p))
		p++;
	priostart = p;
	while (*p !='\0' && !xisspace(*p))
		p++;
	prioend = p;
	while (*p !='\0' && xisspace(*p))
		p++;
	filestart = p;
	while (*p !='\0' && !xisspace(*p))
		p++;
	fileend = p;
	while (*p !='\0' && xisspace(*p))
		p++;
	if (*p != '\0') {
		fprintf(stderr,
"Unexpected sixth argument in '%s'!\n", fileline);
		return RET_ERROR;
	}
	if (*md5start == '\0' || *sizestart == '\0' || *sectionstart == '\0'
			|| *priostart == '\0' || *filestart == '\0') {
		fprintf(stderr,
"Wrong number of arguments in '%s' (5 expected)!\n",
				fileline);
		return RET_ERROR;
	}
	if ((sectionend - sectionstart == 6 &&
				strncmp(sectionstart, "byhand", 6) == 0) ||
	    (sectionend - sectionstart > 4 &&
				strncmp(sectionstart, "raw-", 4) == 0)) {
		section = strndup(sectionstart, sectionend - sectionstart);
		priority = strndup(priostart, prioend - priostart);
		basefilename = strndup(filestart, fileend - filestart);
		if (FAILEDTOALLOC(section) || FAILEDTOALLOC(priority) ||
		    FAILEDTOALLOC(basefilename)) {
			free(section); free(priority);
			free(basefilename);
			return RET_ERROR_OOM;
		}
		hash_p->start = md5start;
		hash_p->len = md5end - md5start;
		size_p->start = sizestart;
		size_p->len = sizeend - sizestart;
		*result_section = section;
		*result_priority = priority;
		*result_basename = basefilename;
		*result_architecture = atom_unknown;
		*result_name = NULL;
		*result_type = fe_BYHAND;
		return RET_OK;
	}

	p = filestart;
	while (*p != '\0' && *p != '_' && !xisspace(*p))
		p++;
	if (*p != '_') {
		if (*p == '\0')
			fprintf(stderr,
"No underscore found in file name in '%s'!\n",
					fileline);
		else
			fprintf(stderr,
"Unexpected character '%c' in file name in '%s'!\n",
					*p, fileline);
		return RET_ERROR;
	}
	nameend = p;
	p++;
	versionstart = p;

	/* changing 3.0 format to now also allow _ in source files
	 * makes this parsing quite more ugly... */

	while (*p !='\0' && !xisspace(*p))
		p++;
	l = p - versionstart;

	/* identify the binary types (they have no compression
	 * and will need a _ */

	if (l >= 4 && memcmp(p-4, ".deb", 4) == 0)
		type = fe_DEB;
	else if (l >= 5 && memcmp(p-5, ".udeb", 5) == 0)
		type = fe_UDEB;
	else
		type = fe_UNKNOWN;

	if (type != fe_UNKNOWN) {
		/* a _ should separate the version from the rest */
		p = versionstart;
		names_overversion(&p, true);
		if (*p != '\0' && *p != '_') {
			fprintf(stderr,
"Unexpected character '%c' in file name within '%s'!\n", *p, fileline);
			return RET_ERROR;
		}
		if (*p != '_') {
			fprintf(stderr,
"Cannot cope with .[u]deb filename not containing an underscore (in '%s')!",
					fileline);
			return RET_ERROR;
		}
		p++;
		archstart = p;
		if (type == fe_DEB)
			archend = versionstart + l - 4;
		else {
			assert (type == fe_UDEB);
			archend = versionstart + l - 5;
		}
		if (archend - archstart == 6 &&
				strncmp(archstart, "source", 6) == 0) {
			fprintf(stderr,
"Architecture 'source' not allowed for .[u]debs ('%s')!\n", filestart);
			return RET_ERROR;
		}
	} else {
		enum compression c;
		const char *eoi;
		bool issignature = false;

		/* without those, it gets more complicated.
		 * It's not .deb or .udeb, so most likely a
		 * source file (or perhaps a log (reprepro extension)) */

		/* if it uses a known compression, things are easy,
		 * so try this first: */

		if (l > 4 && memcmp(versionstart + l - 4, ".asc", 4) == 0 ) {
			issignature = true;
			l -= 4;
		}
		c = compression_by_suffix(versionstart, &l);
		p = versionstart + l;

		archstart = "source";
		archend = archstart + 6;
		if (l > 9 && strncmp(p-9, ".orig.tar", 9) == 0) {
			type = fe_ORIG;
			eoi = p - 9;
		} else if (l > 4 && strncmp(p-4, ".tar", 4) == 0) {
			type = fe_TAR;
			eoi = p - 4;
		} else if (issignature) {
			/* only .tar.* files are allowed to have .asc files: */
			issignature = false;
		} else if (l > 5 && strncmp(p-5, ".diff", 5) == 0) {
			type = fe_DIFF;
			eoi = p - 5;
		} else if (l > 4 && strncmp(p-4, ".dsc", 4) == 0
				&& c == c_none) {
			type = fe_DSC;
			eoi = p - 4;
		} else if (l > 4 && strncmp(p-4, ".git", 4) == 0
				&& c == c_none) {
			type = fe_ALTSRC;
			eoi = p - 4;
		} else if (l > 4 && strncmp(p-4, ".log", 4) == 0) {
			type = fe_LOG;
			eoi = p - 4;
		} else if (l > 6 && strncmp(p-6, ".build", 6) == 0) {
			type = fe_LOG;
			eoi = p - 6;
		} else if (l > 10 && strncmp(p-10, ".buildinfo", 10) == 0) {
			type = fe_BUILDINFO;
			eoi = p - 10;
		}
		if (type != fe_UNKNOWN) {
			/* check for a proper version */
			p = versionstart;
			names_overversion(&p, true);
			if (p >= eoi) {
				/* all well */
			} else if (type == fe_TAR) {
				/* a tar might be a component with ugly
				 * data between .orig- and the .tar.c */
				const char *o = strstr(versionstart, ".orig-");
				if (o == NULL || o > eoi) {
					fprintf(stderr,
"Unexpected character '%c' in file name within '%s'!\n",
							*p, fileline);
					return RET_ERROR;
				}
				checkfilename = true;
			} else if (type == fe_LOG || type == fe_BUILDINFO) {
				if (*p == '_') {
					archstart = p + 1;
					archend = eoi;
					checkfilename = true;
				} else {
					fprintf(stderr,
"Unexpected character '%c' in file name within '%s'!\n",
						*p, fileline);
				}
			} else {
				fprintf(stderr,
"Unexpected character '%c' in file name within '%s'!\n",
						*p, fileline);
				return RET_ERROR;

			}
			if (issignature)
				type = fe_SIG;
		} else {
			/* everything else is assumed to be source */
			checkfilename = true;
			fprintf(stderr,
"Unknown file type: '%s', assuming source format...\n", fileline);
		}
	}
	section = strndup(sectionstart, sectionend - sectionstart);
	priority = strndup(priostart, prioend - priostart);
	basefilename = strndup(filestart, fileend - filestart);
	// TODO: this does not make much sense for log files, as they might
	// list multiple..
	architecture = architecture_find_l(archstart, archend - archstart);
	name = strndup(filestart, nameend - filestart);
	if (FAILEDTOALLOC(section) || FAILEDTOALLOC(priority) ||
	    FAILEDTOALLOC(basefilename) || FAILEDTOALLOC(name)) {
		free(section); free(priority);
		free(basefilename); free(name);
		return RET_ERROR_OOM;
	}
	if (checkfilename || !atom_defined(architecture)) {
		retvalue r;

		/* as we no longer run properversion over the whole
		 * rest of the string, at least make sure nothing evil
		 * is in this name */
		r = properfilename(basefilename);
		if (!RET_IS_OK(r)) {
			assert (r != RET_NOTHING);
			free(section); free(priority);
			free(basefilename); free(name);
			return r;
		}
	}
	hash_p->start = md5start;
	hash_p->len = md5end - md5start;
	size_p->start = sizestart;
	size_p->len = sizeend - sizestart;
	*result_section = section;
	*result_priority = priority;
	*result_basename = basefilename;
	*result_architecture = architecture;
	*result_name = name;
	*result_type = type;
	return RET_OK;
}