File: debfile.c

package info (click to toggle)
reprepro 1.3.1%2B1-1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,660 kB
  • ctags: 1,621
  • sloc: ansic: 22,424; sh: 2,032; python: 138; makefile: 127
file content (368 lines) | stat: -rw-r--r-- 10,052 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
/*  This file is part of "reprepro"
 *  Copyright (C) 2006 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 <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <archive.h>
#include <archive_entry.h>
#include "error.h"
#include "ar.h"
#include "md5sum.h"
#include "chunks.h"
#include "debfile.h"

#ifndef HAVE_LIBARCHIVE
#error Why did this file got compiled instead of extractcontrol.c?
#endif

static retvalue read_control_file(char **control, const char *debfile, struct archive *tar, struct archive_entry *entry) {
	int64_t size;
	char *buffer, *startofchanges, *endofchanges, *afterchanges, *n;
	size_t len;
	ssize_t got;

	size = archive_entry_size(entry);
	if( size <= 0 ) {
		fprintf(stderr, "Error: Empty control file within %s!\n", debfile);
		return RET_ERROR;
	}
	if( size > 10*1024*1024 ) {
		fprintf(stderr, "Error: Ridiculous long control file within %s!\n", debfile);
		return RET_ERROR;
	}
	buffer = malloc(size+2);
	len = 0;
	while( (got = archive_read_data(tar, buffer+len, ((size_t)size+1)-len)) > 0
			&& !interrupted() ) {
		len += got;
		if( len > size ) {
			fprintf(stderr, "Internal Error: libarchive miscalculated length of the control file within '%s',\n"
			" perhaps the file is corrupt, perhaps libarchive!\n", debfile);
			free(buffer);
			return RET_ERROR;
		}
	}
	if( interrupted() ) {
		free(buffer);
		return RET_ERROR_INTERUPTED;
	}
	if( got < 0 ) {
		free(buffer);
		fprintf(stderr, "Error reading control file from %s\n", debfile);
		return RET_ERROR;
	}
	if( len < size ) 
		fprintf(stderr, "Warning: libarchive overcalculated length of the control file within '%s',\n"
			" perhaps the file is corrupt, perhaps libarchive!\n", debfile);
	buffer[len] = '\0';

	startofchanges = buffer;
	while( *startofchanges != '\0' && xisspace(*startofchanges)) {
		startofchanges++;
	}
	if( (size_t)(startofchanges-buffer) >= len) {
		fprintf(stderr,"Could only find spaces within contol file of '%s'!\n",
				debfile);
		free(buffer);
		return RET_ERROR;
	}
	len -= (startofchanges-buffer);
	memmove(buffer, startofchanges, len+1);
	endofchanges = buffer;
	while( *endofchanges != '\0' && 
		( *endofchanges != '\n' || *(endofchanges-1)!= '\n')) {
		endofchanges++;
	}
	afterchanges = endofchanges;
	while(  *afterchanges != '\0' && xisspace(*afterchanges)) {
		afterchanges++;
	}
	if( (size_t)(afterchanges - buffer) < len ) {
		if( *afterchanges == '\0' ) {
			fprintf(stderr,"Unexpected \\0 character within control file of '%s'!\n", debfile);
			free(buffer);
			return RET_ERROR;
		}
		fprintf(stderr,"Unexpected data after ending empty line in control file of '%s'!\n", debfile);
		free(buffer);
		return RET_ERROR;
	}
	*afterchanges = '\0';
	n = realloc(buffer, (afterchanges-buffer)+1);
	if( n == NULL ) {
		free(buffer);
		return RET_ERROR_OOM;
	}
	*control = n;
	return RET_OK;
}

static retvalue read_control_tar(char **control, const char *debfile, struct ar_archive *ar, struct archive *tar) {
	struct archive_entry *entry;
	int a;
	retvalue r;

	archive_read_support_format_tar(tar);
	a = archive_read_open(tar,ar,
			ar_archivemember_open,
			ar_archivemember_read,
			ar_archivemember_close);
	if( a != ARCHIVE_OK ) {
		fprintf(stderr,"open control.tar.gz within '%s' failed: %d:%d:%s\n",
				debfile, 
				a,archive_errno(tar),
				archive_error_string(tar));
		return RET_ERROR;
	}
	while( (a=archive_read_next_header(tar, &entry)) == ARCHIVE_OK ) {
		if( strcmp(archive_entry_pathname(entry),"./control") != 0 &&
		    strcmp(archive_entry_pathname(entry),"control") != 0 ) {
			a = archive_read_data_skip(tar);
			if( a != ARCHIVE_OK ) {
				int e = archive_errno(tar);
				printf("Error skipping %s within data.tar.gz from %s: %d=%s\n",
						archive_entry_pathname(entry),
						debfile,
						e, archive_error_string(tar));
				return (e!=0)?(RET_ERRNO(e)):RET_ERROR;
			}
			if( interrupted() ) 
				return RET_ERROR_INTERUPTED;
		} else {
			r = read_control_file(control, debfile, tar, entry);
			if( r != RET_NOTHING )
				return r;
		}
	}
	if( a != ARCHIVE_EOF ) {
		int e = archive_errno(tar);
		printf("Error reading control.tar.gz from %s: %d=%s\n",
				debfile,
				e, archive_error_string(tar));
		return (e!=0)?(RET_ERRNO(e)):RET_ERROR;
	}
	fprintf(stderr,"Could not find a control file within control.tar.gz within '%s'!\n",debfile);
	return RET_ERROR_MISSING;
}

retvalue extractcontrol(char **control,const char *debfile) {
	struct ar_archive *ar;
	retvalue r;

	r = ar_open(&ar,debfile);
	if( RET_WAS_ERROR(r) )
		return r;
	assert( r != RET_NOTHING);
	do {
		char *filename;

		r = ar_nextmember(ar, &filename);
		if( RET_IS_OK(r) ) {
			if( strcmp(filename,"control.tar.gz") == 0 ) {
				struct archive *tar;

				tar = archive_read_new();
				archive_read_support_compression_gzip(tar);
				r = read_control_tar(control, debfile, ar, tar);
				archive_read_finish(tar);
				if( r != RET_NOTHING ) {
					ar_close(ar);
					free(filename);
					return r;
				}
				
			}
/* see below for a discussion about this #ifdef */
#ifdef HAVE_LIBBZ2
			if( strcmp(filename,"control.tar.bz2") == 0 ) {
				struct archive *tar;

				tar = archive_read_new();
				archive_read_support_compression_gzip(tar);
				archive_read_support_compression_bzip2(tar);
				r = read_control_tar(control, debfile, ar, tar);
				archive_read_finish(tar);
				if( r != RET_NOTHING ) {
					ar_close(ar);
					free(filename);
					return r;
				}
				
			}
#endif
			free(filename);
		}
	} while( RET_IS_OK(r) );
	ar_close(ar);
	fprintf(stderr,"Could not find a control.tar.gz file within '%s'!\n",debfile);
	return RET_ERROR_MISSING;
}

static retvalue read_data_tar(/*@out@*/char **list, const char *debfile, struct ar_archive *ar, struct archive *tar) {
	struct archive_entry *entry;
	char *filelist;
	size_t size,len;
	int a;

	size = 2000; len = 0;
	filelist = malloc(size);
	if( filelist == NULL )
		return RET_ERROR_OOM;

	archive_read_support_format_tar(tar);
	a = archive_read_open(tar,ar,
			ar_archivemember_open,
			ar_archivemember_read,
			ar_archivemember_close);
	if( a != ARCHIVE_OK ) {
		free(filelist);
		fprintf(stderr,"open data.tar.gz within '%s' failed: %d:%d:%s\n",
				debfile, 
				a,archive_errno(tar),
				archive_error_string(tar));
		return RET_ERROR;
	}
	while( (a=archive_read_next_header(tar, &entry)) == ARCHIVE_OK ) {
		const char *name = archive_entry_pathname(entry);
		mode_t mode;

		if( name[0] == '.' )
			name++;
		if( name[0] == '/' )
			name++;
		if( name[0] == '\0' )
			continue;
		mode = archive_entry_mode(entry);
		if( !S_ISDIR(mode) ) {
			size_t n_len = strlen(name);

			if( len + n_len + 2 > size ) {
				char *n;

				if( size > 1024*1024*1024 ) {
					fprintf(stderr, "Ridicilous long filelist for %s!",debfile);
					free(filelist);
					return RET_ERROR;
				}
				size = len + n_len + 2048;
				n = realloc(filelist, size);
				if( n == NULL ) {
					free(filelist);
					return RET_ERROR_OOM;
				}
				filelist = n;

			}
			memcpy(filelist + len, name, n_len+1);
			len += n_len+1;
		}
		if( interrupted() ) {
			free(filelist);
			return RET_ERROR_INTERUPTED;
		}
		a = archive_read_data_skip(tar);
		if( a != ARCHIVE_OK ) {
			int e = archive_errno(tar);
			printf("Error skipping %s within data.tar.gz from %s: %d=%s\n",
					archive_entry_pathname(entry),
					debfile,
					e, archive_error_string(tar));
			free(filelist);
			return (e!=0)?(RET_ERRNO(e)):RET_ERROR;
		}
	}
	if( a != ARCHIVE_EOF ) {
		int e = archive_errno(tar);
		printf("Error reading data.tar.gz from %s: %d=%s\n",
				debfile,
				e, archive_error_string(tar));
		free(filelist);
		return (e!=0)?(RET_ERRNO(e)):RET_ERROR;
	}
	filelist[len] = '\0';
	*list = realloc(filelist, len+1);
	if( *list == NULL ) {
		free(filelist);
		return RET_ERROR_OOM;
	}
	return RET_OK;
}


retvalue getfilelist(/*@out@*/char **filelist, const char *debfile) {
	struct ar_archive *ar;
	retvalue r;

	r = ar_open(&ar,debfile);
	if( RET_WAS_ERROR(r) )
		return r;
	assert( r != RET_NOTHING);
	do {
		char *filename;

		r = ar_nextmember(ar, &filename);
		if( RET_IS_OK(r) ) {
			if( strcmp(filename,"data.tar.gz") == 0 ) {
				struct archive *tar;

				tar = archive_read_new();
				archive_read_support_compression_gzip(tar);
				r = read_data_tar(filelist, debfile, ar, tar);
				archive_read_finish(tar);
				if( r != RET_NOTHING ) {
					ar_close(ar);
					free(filename);
					return r;
				}
				
			}
/* TODO: here some better heuristic would be nice,
 * but when compiling against the static libarchive this is what needed,
 * and when libarchive is compiled locally it will have bz2 support
 * essentially when we have it, too.
 * Thus this ifdef is quite a good choice, especially as no offical
 * files should have this member, anyway */
#ifdef HAVE_LIBBZ2
			if( strcmp(filename,"data.tar.bz2") == 0 ) {
				struct archive *tar;

				tar = archive_read_new();
				archive_read_support_compression_gzip(tar);
				archive_read_support_compression_bzip2(tar);
				r = read_data_tar(filelist, debfile, ar, tar);
				archive_read_finish(tar);
				if( r != RET_NOTHING ) {
					ar_close(ar);
					free(filename);
					return r;
				}
				
			}
#endif
			free(filename);
		}
	} while( RET_IS_OK(r) );
	ar_close(ar);
	fprintf(stderr,"Could not find a data.tar.gz file within '%s'!\n",debfile);
	return RET_ERROR_MISSING;
}