summaryrefslogtreecommitdiffstats
path: root/src/opk/read.c
blob: e0f3af43913b0a023de85a9d3b6939866952482f (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
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opkg.
 *
 * opkg-opkg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opkg 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 opkg-opkg.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include "../defs.h"
#include "../gzip.h"
#include "../i18n.h"
#include "../opk.h"
#include "../ustar.h"
#include "opk.h"

static int
_opkg_opk_opk_read_file(void *user_data, char **buffer, size_t *size)
{
	struct opkg_opk_opk *opk = user_data;

	*buffer = opk->file_buffer;
	*size   = fread(opk->file_buffer, 1, sizeof(opk->file_buffer),
			opk->file);
	if (feof(opk->file)) {
		return OPKG_OPK_END;
	} else if (ferror(opk->file) || *size == 0) {
		return OPKG_OPK_ERROR;
	} else {
		return OPKG_OPK_OK;
	}
}

static int
_opkg_opk_opk_read_init_inner(struct opkg_opk_opk *opk)
{
	/* Initialize inner gzip decompressor. */
	opk->inner_gzip = opkg_opk_gzip_init_read(
			(opkg_opk_gzip_read_func *) &opkg_opk_ustar_read,
			opk->outer_ustar);
	if (opk->inner_gzip == NULL) {
		fputs(_("Error: Failed to initialize\n"), stderr);
		return OPKG_OPK_ERROR;
	}

	/* Initialize inner ustar unarchiver. */
	opk->inner_ustar = opkg_opk_ustar_init(opk->inner_gzip);
	if (opk->inner_ustar == NULL) {
		fputs(_("Error: Failed to initialize\n"), stderr);
		opkg_opk_gzip_free(opk->inner_gzip);
		return OPKG_OPK_ERROR;
	}

	return OPKG_OPK_OK;
}

static void
_opkg_opk_opk_read_free_inner(struct opkg_opk_opk *opk)
{
	opkg_opk_ustar_free(opk->inner_ustar);
	opkg_opk_gzip_free(opk->inner_gzip);
}

static int
_opkg_opk_opk_read_check_name(const char *member_name,
		struct _opkg_opk_opk_seek_name **head,
		struct _opkg_opk_opk_seek_name **tail)
{
	struct _opkg_opk_opk_seek_name *seek_name;

	if (member_name[0] == '.' && member_name[1] == '/') {
		member_name += 2;
	} else if (member_name[0] == '/') {
		member_name += 1;
	}

	/* Check each requested name. */
	for (seek_name = *head; seek_name != NULL; seek_name = seek_name->next)
	{
		if (strcmp(member_name, seek_name->name) == 0) {
			if (seek_name->prev != NULL) {
				seek_name->prev->next = seek_name->next;
			} else {
				/* This was the head.  Update. */
				*head = seek_name->next;
			}
			if (seek_name->next != NULL) {
				seek_name->next->prev = seek_name->prev;
			} else {
				/* This was the tail.  Update. */
				*tail = seek_name->prev;
			}
			free(seek_name);
			return OPKG_OPK_OK;
		}
	}

	return OPKG_OPK_ERROR;  /* Member not found */
}

static int
_opkg_opk_opk_read_control(struct opkg_opk_opk *opk)
{
	struct stat                     stat_buf;
	char                           *path;
	struct opkg_opk_ustar_member   *member;
	int                             ret_list;
	FILE                           *fp;
	int                             print;
	int                             ret_read;
	char                           *buffer;
	size_t                          size;
	struct _opkg_opk_opk_seek_name *seek_name;

	if (opk->control_dir != NULL) {
		if (stat(opk->control_dir, &stat_buf) == 0) {
			if (!S_ISDIR(stat_buf.st_mode)) {
				fputs(_("Error: Cannot create control directory"
							"\n"), stderr);
				return OPKG_OPK_ERROR;
			}
		} else if (mkdir(opk->control_dir, 0755) != 0) {
			fputs(_("Error: Cannot create control directory"
						"\n"), stderr);
			return OPKG_OPK_ERROR;
		}
		path = malloc(strlen(opk->control_dir) +
				OPKG_OPK_USTAR_NAME_SIZE + 1);
		if (path == NULL) {
			return OPKG_OPK_ERROR;
		}
	} else if (opk->list_control == 0 && opk->print_control_head == NULL) {
		/* Not listing or printing any control files. */
		return OPKG_OPK_OK;
	}

	if (_opkg_opk_opk_read_init_inner(opk) != OPKG_OPK_OK) {
		if (opk->control_dir != NULL) {
			free(path);
		}
		return OPKG_OPK_ERROR;
	}

	while ((ret_list = opkg_opk_ustar_list(opk->inner_ustar, &member)) ==
			OPKG_OPK_OK) {
		/* Remove leading "./" if present and skip if nothing's left. */
		buffer = member->name;
		if (buffer[0] == '.' && buffer[1] == '/') {
			buffer += 2;
		}
		if (buffer[0] == '\0') {
			free(member);
			continue;
		}

		/* Open file for extraction. */
		if (opk->control_dir != NULL) {
			if (sprintf(path, "%s/%s", opk->control_dir,
						member->name) <= 0) {
				free(member);
				_opkg_opk_opk_read_free_inner(opk);
				free(path);
				return OPKG_OPK_ERROR;
			}
			fp = fopen(path, "wb");
			if (fp == NULL) {
				fputs(_("Error: Failed to extract control file"
							"\n"), stderr);
				free(member);
				_opkg_opk_opk_read_free_inner(opk);
				free(path);
				return OPKG_OPK_ERROR;
			}
		}

		if (opk->list_control > 0) {
			puts(buffer);
			opk->previously_printed = 1;
		}

		if (opk->print_control_head != NULL &&
				_opkg_opk_opk_read_check_name(member->name,
					&opk->print_control_head,
					&opk->print_control_tail) ==
				OPKG_OPK_OK) {
			/* Name requested for printing. */
			print = 1;
		} else {
			print = 0;
		}
		free(member);
		if (print == 1 && opk->previously_printed == 1) {
			puts("");
		}
		while ((ret_read = opkg_opk_ustar_read(opk->inner_ustar,
						&buffer, &size)) == OPKG_OPK_OK)
		{
			if (print == 1 && fwrite(buffer, 1, size, stdout) !=
					size) {
				fputs(_("Error: Failed to print control file\n")
						, stderr);
				_opkg_opk_opk_read_free_inner(opk);
				if (opk->control_dir != NULL) {
					free(path);
				}
				return OPKG_OPK_ERROR;
			}
			if (opk->control_dir != NULL && fwrite(buffer, 1, size,
						fp) != size) {
				fputs(_("Error: Failed to write control file\n")
						, stderr);
				_opkg_opk_opk_read_free_inner(opk);
				free(path);
				return OPKG_OPK_ERROR;
			}
		}
		if (ret_read == OPKG_OPK_ERROR) {
			fputs(_("Error: Failed to read control file\n"),
					stderr);
			_opkg_opk_opk_read_free_inner(opk);
			if (opk->control_dir != NULL) {
				free(path);
				fclose(fp);
			}
			return OPKG_OPK_ERROR;
		}
		opk->previously_printed = 1;

		/* Close file for extraction. */
		if (opk->control_dir != NULL) {
			if (fclose(fp) != 0) {
				_opkg_opk_opk_read_free_inner(opk);
				free(path);
				return OPKG_OPK_ERROR;
			}
		}
	}
	if (opk->control_dir != NULL) {
		/* Done with path buffer. */
		free(path);
	}
	if (ret_list == OPKG_OPK_ERROR) {
		fputs(_("Error: Failed to list control files\n"), stderr);
		_opkg_opk_opk_read_free_inner(opk);
		return OPKG_OPK_ERROR;
	}

	/* Check for files not found. */
	if (opk->print_control_head != NULL) {
		for (seek_name = opk->print_control_head; seek_name != NULL;
				seek_name = seek_name->next) {
			fprintf(stderr, _("Error: Failed to find control file "
						"\"%s\"\n"), seek_name->name);
		}
		_opkg_opk_opk_read_free_inner(opk);
		return OPKG_OPK_ERROR;
	}

	_opkg_opk_opk_read_free_inner(opk);

	return OPKG_OPK_OK;
}

static int
_opkg_opk_opk_read_data(struct opkg_opk_opk *opk)
{
	struct opkg_opk_ustar_member *head;
	struct opkg_opk_ustar_member *tail;
	struct opkg_opk_ustar_member *member;
	int                           ret;
	size_t                        uname_len;
	size_t                        uname_len_max;
	size_t                        gname_len;
	size_t                        gname_len_max;
	uint64_t                      size_max;
	int                           ret_read;
	char                         *buffer;
	size_t                        size;
	long int                      size_len_max;
	char                          fmt[26];
	                              /* "%c%s %-32s/%-32s %11d %s " */
	int                           len;
	char                          mode[10];
	char                          mtime[20];

	if (opk->list_data == 0 && opk->print_data_head == NULL) {
		/* Not listing or printing any data files. */
		return OPKG_OPK_OK;
	}

	if (_opkg_opk_opk_read_init_inner(opk) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}

	/* Build singly-linked list and find maximum column widths. */
	head = NULL;
	tail = NULL;
	uname_len_max = 0;
	gname_len_max = 0;
	size_max      = 0;
	while ((ret = opkg_opk_ustar_list(opk->inner_ustar, &member)) ==
			OPKG_OPK_OK) {
		/* If listing, link members and update max column widths. */
		if (opk->list_data > 0) {
			if (head == NULL) {
				head = member;
			} else {
				tail->next = member;
			}
			tail = member;
			uname_len = strlen(member->uname);
			if (uname_len > uname_len_max) {
				uname_len_max = uname_len;
			}
			gname_len = strlen(member->gname);
			if (gname_len > gname_len_max) {
				gname_len_max = gname_len;
			}
			if (member->size > size_max) {
				size_max = member->size;
			}
		}

		if (opk->print_data_head == NULL) {
			if (opk->list_data == 0) {
				free(member);
			}
			continue;
		}
		if (_opkg_opk_opk_read_check_name(member->name,
					&opk->print_data_head,
					&opk->print_data_tail) !=
				OPKG_OPK_OK) {
			/* Name not requested for printing. */
			if (opk->list_data == 0) {
				free(member);
			}
			continue;
		}
		if (opk->list_data == 0) {
			free(member);
		}
		if (opk->previously_printed == 1) {
			puts("");
		}
		while ((ret_read = opkg_opk_ustar_read(opk->inner_ustar,
						&buffer, &size)) == OPKG_OPK_OK)
		{
			if (fwrite(buffer, 1, size, stdout) != size) {
				fputs(_("Error: Failed to print data file\n")
						, stderr);
				_opkg_opk_opk_read_free_inner(opk);
				return OPKG_OPK_ERROR;
			}
		}
		if (ret_read == OPKG_OPK_ERROR) {
			fputs(_("Error: Failed to read data file\n"),
					stderr);
			_opkg_opk_opk_read_free_inner(opk);
			return OPKG_OPK_ERROR;
		}
		opk->previously_printed = 1;
	}
	if (ret == OPKG_OPK_ERROR) {
		fputs(_("Error: Failed to list data files\n"), stderr);
		_opkg_opk_opk_read_free_inner(opk);
		return OPKG_OPK_ERROR;
	}
	if (tail != NULL) {
		tail->next = NULL;
	}

	if (opk->list_data == 0) {
		_opkg_opk_opk_read_free_inner(opk);
		return OPKG_OPK_OK;
	}

	/* Print and free members. */
	if (opk->previously_printed == 1) {
		puts("");
	}
	ret = OPKG_OPK_OK;
	size_len_max = lrint(ceil(log10(size_max)));
	snprintf(fmt, sizeof(fmt), "%%c%%s %%-%zus/%%-%zus %%%lid %%s ",
			uname_len_max, gname_len_max, size_len_max);
	len = 34 + uname_len_max + gname_len_max + size_len_max;
	for (member = head; member != NULL;) {
		if (member->mode & 00400) mode[0] = 'r'; else mode[0] = '-';
		if (member->mode & 00200) mode[1] = 'w'; else mode[1] = '-';
		if (member->mode & 00100) mode[2] = 'x'; else mode[2] = '-';
		if (member->mode & 04000) mode[2] = 's';
		if (member->mode & 00040) mode[3] = 'r'; else mode[3] = '-';
		if (member->mode & 00020) mode[4] = 'w'; else mode[4] = '-';
		if (member->mode & 00010) mode[5] = 'x'; else mode[5] = '-';
		if (member->mode & 02000) mode[5] = 's';
		if (member->mode & 00004) mode[6] = 'r'; else mode[6] = '-';
		if (member->mode & 00002) mode[7] = 'w'; else mode[7] = '-';
		if (member->mode & 00001) mode[8] = 'x'; else mode[8] = '-';
		if (member->mode & 01000) mode[8] = 't';
		mode[9] = '\0';
		strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S",
				localtime((const time_t *) &member->mtime));
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
		if (printf(fmt, member->type, mode,
				member->uname, member->gname,
				member->size, mtime) != len) {
			ret = OPKG_OPK_ERROR;
		}
#pragma GCC diagnostic pop
		/* Print name, ensuring it begins with "/". */
		if (member->name[0] == '.' && member->name[1] == '/') {
			if (fputs(member->name + 1, stdout) == EOF) {
				ret = OPKG_OPK_ERROR;
			}
		} else {
			if (member->name[0] != '/' && putchar('/') == EOF) {
				ret = OPKG_OPK_ERROR;
			}
			if (fputs(member->name, stdout) == EOF) {
				ret = OPKG_OPK_ERROR;
			}
		}
		/* Print link name, if any. */
		if (member->type == 'l' && printf(" -> %s", member->linkname) !=
				(int) (strlen(member->linkname) + 4)) {
			ret = OPKG_OPK_ERROR;
		}
		if (puts("") == EOF) {
			ret = OPKG_OPK_ERROR;
		}
		head   = member;
		member = member->next;
		free(head);
	}

	opk->previously_printed = 1;

	_opkg_opk_opk_read_free_inner(opk);

	return ret;
}

int
opkg_opk_opk_read(struct opkg_opk_opk *opk, const char *file_name)
{
	int                           ret;
	struct opkg_opk_ustar_member *member;
	char                         *version_buffer;
	size_t                        version_size;

	ret = OPKG_OPK_OK;

	/* Open outer archive. */
	opk->file = fopen(file_name, "rb");
	if (opk->file == NULL) {
		fprintf(stderr, _("Error: Failed to open file \"%s\"\n"),
				file_name);
		ret = OPKG_OPK_ERROR;
		goto out0;
	}

	/* Initialize outer gzip decompressor. */
	opk->outer_gzip = opkg_opk_gzip_init_read(&_opkg_opk_opk_read_file,
			opk);
	if (opk->outer_gzip == NULL) {
		fputs(_("Error: Failed to initialize\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out1;
	}

	/* Initialize outer ustar unarchiver. */
	opk->outer_ustar = opkg_opk_ustar_init(opk->outer_gzip);
	if (opk->outer_ustar == NULL) {
		fputs(_("Error: Failed to initialize\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out2;
	}

	/* Check package version. */
	if (opkg_opk_ustar_list(opk->outer_ustar, &member) != OPKG_OPK_OK) {
		fputs(_("Error: Failed to find \"debian-binary\" in archive\n"),
				stderr);
		ret = OPKG_OPK_ERROR;
		goto out3;
	}
	if (strcmp(member->name, "debian-binary") != 0) {
		free(member);
		fputs(_("Error: Failed to find \"debian-binary\" in archive\n"),
				stderr);
		ret = OPKG_OPK_ERROR;
		goto out3;
	}
	free(member);
	if (opkg_opk_ustar_read(opk->outer_ustar,
				&version_buffer, &version_size) !=
			OPKG_OPK_OK) {
		fputs(_("Error: Failed to read \"debian-binary\" in archive\n"),
				stderr);
		ret = OPKG_OPK_ERROR;
		goto out3;
	}
	if (version_size < 4 || strncmp(version_buffer, "2.", 2) != 0) {
		fputs(_("Error: Unsupported package version\n"), stderr);
		ret = OPKG_OPK_ERROR;
		goto out3;
	}

	/* Read control archive. */
	if (opkg_opk_ustar_list(opk->outer_ustar, &member) != OPKG_OPK_OK) {
		fputs(_("Error: Failed to find \"control.tar.gz\" in archive\n")
				, stderr);
		return OPKG_OPK_ERROR;
	}
	if (strcmp(member->name, "control.tar.gz") != 0) {
		free(member);
		fputs(_("Error: Failed to find \"control.tar.gz\" in archive\n")
				, stderr);
		return OPKG_OPK_ERROR;
	}
	free(member);
	if (_opkg_opk_opk_read_control(opk) != OPKG_OPK_OK) {
		ret = OPKG_OPK_ERROR;
		goto out3;
	}

	/* Read data archive. */
	if (opkg_opk_ustar_list(opk->outer_ustar, &member) != OPKG_OPK_OK) {
		fputs(_("Error: Failed to find \"data.tar.gz\" in archive\n"),
				stderr);
		return OPKG_OPK_ERROR;
	}
	if (strcmp(member->name, "data.tar.gz") != 0) {
		free(member);
		fputs(_("Error: Failed to find \"data.tar.gz\" in archive\n"),
				stderr);
		return OPKG_OPK_ERROR;
	}
	free(member);
	if (_opkg_opk_opk_read_data(opk) != OPKG_OPK_OK) {
		ret = OPKG_OPK_ERROR;
		goto out3;
	}

 out3:
	opkg_opk_ustar_free(opk->outer_ustar);
 out2:
	opkg_opk_gzip_free(opk->outer_gzip);
 out1:
	fclose(opk->file);
 out0:
	return ret;
}