summaryrefslogtreecommitdiff
path: root/pkgtools/pkg_install/files/info/perform.c
blob: 318e3183ac6805bffa365e3498684976af576aec (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
/*	$NetBSD: perform.c,v 1.46.2.5 2008/08/10 22:09:38 joerg Exp $	*/

#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <nbcompat.h>
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if HAVE_SYS_QUEUE_H
#include <sys/queue.h>
#endif
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifndef lint
#if 0
static const char *rcsid = "from FreeBSD Id: perform.c,v 1.23 1997/10/13 15:03:53 jkh Exp";
#else
__RCSID("$NetBSD: perform.c,v 1.46.2.5 2008/08/10 22:09:38 joerg Exp $");
#endif
#endif

/*-
 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * FreeBSD install - a package for the installation and maintainance
 * of non-core utilities.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * Jordan K. Hubbard
 * 23 Aug 1993
 *
 * This is the main body of the info module.
 *
 */

#include "lib.h"
#include "info.h"

#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifndef BOOTSTRAP
#include <archive.h>
#include <archive_entry.h>
#endif
#if HAVE_ERR_H
#include <err.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#if HAVE_DIRENT_H
#include <dirent.h>
#endif
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#include <stddef.h>

#define	LOAD_CONTENTS		(1 << 0)
#define	LOAD_COMMENT		(1 << 1)
#define	LOAD_DESC		(1 << 2)
#define	LOAD_INSTALL		(1 << 3)
#define	LOAD_DEINSTALL		(1 << 4)
#define	LOAD_DISPLAY		(1 << 5)
#define	LOAD_MTREE		(1 << 6)
#define	LOAD_BUILD_VERSION	(1 << 7)
#define	LOAD_BUILD_INFO		(1 << 8)
#define	LOAD_SIZE_PKG		(1 << 9)
#define	LOAD_SIZE_ALL		(1 << 10)
#define	LOAD_PRESERVE		(1 << 11)
#define	LOAD_VIEWS		(1 << 12)
#define	LOAD_REQUIRED_BY	(1 << 13)
#define	LOAD_INSTALLED_INFO	(1 << 14)

static const struct pkg_meta_desc {
	size_t entry_offset;
	const char *entry_filename;
	int entry_mask;
	int required_file;
} pkg_meta_descriptors[] = {
	{ offsetof(struct pkg_meta, meta_contents), CONTENTS_FNAME,
	    LOAD_CONTENTS, 1},
	{ offsetof(struct pkg_meta, meta_comment), COMMENT_FNAME,
	    LOAD_COMMENT, 1 },
	{ offsetof(struct pkg_meta, meta_desc), DESC_FNAME,
	    LOAD_DESC, 1 },
	{ offsetof(struct pkg_meta, meta_install), INSTALL_FNAME,
	    LOAD_INSTALL, 0 },
	{ offsetof(struct pkg_meta, meta_deinstall), DEINSTALL_FNAME,
	    LOAD_DEINSTALL, 0 },
	{ offsetof(struct pkg_meta, meta_display), DISPLAY_FNAME,
	    LOAD_DISPLAY, 0 },
	{ offsetof(struct pkg_meta, meta_mtree), MTREE_FNAME,
	    LOAD_MTREE, 0 },
	{ offsetof(struct pkg_meta, meta_build_version), BUILD_VERSION_FNAME,
	    LOAD_BUILD_VERSION, 0 },
	{ offsetof(struct pkg_meta, meta_build_info), BUILD_INFO_FNAME,
	    LOAD_BUILD_INFO, 0 },
	{ offsetof(struct pkg_meta, meta_size_pkg), SIZE_PKG_FNAME,
	    LOAD_SIZE_PKG, 0 },
	{ offsetof(struct pkg_meta, meta_size_all), SIZE_ALL_FNAME,
	    LOAD_SIZE_ALL, 0 },
	{ offsetof(struct pkg_meta, meta_preserve), PRESERVE_FNAME,
	    LOAD_PRESERVE, 0 },
	{ offsetof(struct pkg_meta, meta_views), VIEWS_FNAME,
	    LOAD_VIEWS, 0 },
	{ offsetof(struct pkg_meta, meta_required_by), REQUIRED_BY_FNAME,
	    LOAD_REQUIRED_BY, 0 },
	{ offsetof(struct pkg_meta, meta_installed_info), INSTALLED_INFO_FNAME,
	    LOAD_INSTALLED_INFO, 0 },
	{ 0, NULL, 0, 0 },
};

static int desired_meta_data;

static void
free_pkg_meta(struct pkg_meta *meta)
{
	const struct pkg_meta_desc *descr;

	for (descr = pkg_meta_descriptors; descr->entry_filename; ++descr)
		free(*(char **)((char *)meta + descr->entry_offset));

	free(meta);
}

#ifndef BOOTSTRAP
static struct pkg_meta *
read_meta_data_from_archive(struct archive *archive,
    struct archive_entry *entry)
{
	struct pkg_meta *meta;
	const char *fname;
	const struct pkg_meta_desc *descr, *last_descr;
	char **target;
	int64_t size;
	int r, found_required;

	found_required = 0;

	meta = xcalloc(1, sizeof(*meta));

	last_descr = 0;
	if (entry != NULL)
		goto has_entry;

	while ((r = archive_read_next_header(archive, &entry)) == ARCHIVE_OK) {
has_entry:
		fname = archive_entry_pathname(entry);

		for (descr = pkg_meta_descriptors; descr->entry_filename;
		     ++descr) {
			if (strcmp(descr->entry_filename, fname) == 0)
				break;
		}
		if (descr->entry_filename == NULL)
			break;

		if (descr->required_file)
			++found_required;

		target = (char **)((char *)meta + descr->entry_offset);
		if (*target)
			errx(2, "duplicate entry, package corrupt");
		if (descr < last_descr)
			warnx("misordered package, continuing");
		else
			last_descr = descr;

		if ((descr->entry_mask & desired_meta_data) == 0) {
			if (archive_read_data_skip(archive))
				errx(2, "cannot read package meta data");
			continue;
		}

		size = archive_entry_size(entry);
		if (size > SSIZE_MAX - 1)
			errx(2, "package meta data too large to process");
		*target = xmalloc(size + 1);
		if (archive_read_data(archive, *target, size) != size)
			errx(2, "cannot read package meta data");
		(*target)[size] = '\0';
	}

	for (descr = pkg_meta_descriptors; descr->entry_filename; ++descr) {
		if (descr->required_file)
			--found_required;
	}
	if (found_required != 0) {
		free_pkg_meta(meta);
		meta = NULL;
	}

	archive_read_finish(archive);

	return meta;
}
#endif

static struct pkg_meta *
read_meta_data_from_pkgdb(const char *pkg)
{
	struct pkg_meta *meta;
	const struct pkg_meta_desc *descr;
	char **target;
	char *fname;
	int fd;
	struct stat st;

	meta = xcalloc(1, sizeof(*meta));

	for (descr = pkg_meta_descriptors; descr->entry_filename; ++descr) {
		if ((descr->entry_mask & desired_meta_data) == 0)
			continue;

		fname = pkgdb_pkg_file(pkg, descr->entry_filename);
		fd = open(fname, O_RDONLY, 0);
		free(fname);
		if (fd == -1) {
			if (errno == ENOENT && descr->required_file == 0)
				continue;
			err(2, "cannot read meta data file %s of package %s",
			    descr->entry_filename, pkg);
		}
		target = (char **)((char *)meta + descr->entry_offset);

		if (fstat(fd, &st) == -1)
			err(2, "cannot stat meta data");
		if ((st.st_mode & S_IFMT) != S_IFREG)
			errx(1, "meta data is not regular file");
		if (st.st_size > SSIZE_MAX - 1)
			err(2, "meta data file too large to process");
		*target = xmalloc(st.st_size + 1);
		if (read(fd, *target, st.st_size) != st.st_size)
			err(2, "cannot read meta data");
		(*target)[st.st_size] = '\0';
		close(fd);
	}

	return meta;
}

static lfile_head_t files;

static int
pkg_do(const char *pkg)
{
	struct pkg_meta *meta;
	char    log_dir[MaxPathSize];
	int     code = 0;
	const char   *binpkgfile = NULL;

	if (IS_URL(pkg) || (fexists(pkg) && isfile(pkg))) {
#ifdef BOOTSTRAP
		errx(2, "Binary packages not supported during bootstrap");
#else
		struct archive *archive;
		void *archive_cookie;
#  ifdef HAVE_SSL
		void *signature_cookie;
#  endif
		struct archive_entry *entry;
		char *pkgname;

		archive = open_archive(pkg, &archive_cookie);
		if (archive == NULL) {
			warnx("can't find package `%s', skipped", pkg);
			return -1;
		}
		pkgname = NULL;
		entry = NULL;
#  ifdef HAVE_SSL
		pkg_verify_signature(&archive, &entry, &pkgname,
		    &signature_cookie);
#  endif
		free(pkgname);

		meta = read_meta_data_from_archive(archive, entry);
		close_archive(archive_cookie);
#  ifdef HAVE_SSL
		pkg_free_signature(signature_cookie);
#  endif
		if (!IS_URL(pkg))
			binpkgfile = pkg;
#endif
	} else {
		/*
	         * It's not an uninstalled package, try and find it among the
	         * installed
	         */
		(void) snprintf(log_dir, sizeof(log_dir), "%s/%s",
		    _pkgdb_getPKGDB_DIR(), pkg);
		if (!fexists(log_dir) || !(isdir(log_dir) || islinktodir(log_dir))) {
			switch (add_installed_pkgs_by_basename(pkg, &pkgs)) {
			case 1:
				return 0;
			case 0:
				/* No match */
				warnx("can't find package `%s'", pkg);
				return 1;
			case -1:
				errx(EXIT_FAILURE, "Error during search in pkgdb for %s", pkg);
			}
		}
		meta = read_meta_data_from_pkgdb(pkg);
	}

	if (meta == NULL) {
		warnx("invalid package `%s' skipped", pkg);
		return 1;	
	}

	/*
         * Index is special info type that has to override all others to make
         * any sense.
         */
	if (Flags & SHOW_INDEX) {
		char    tmp[MaxPathSize];

		(void) snprintf(tmp, sizeof(tmp), "%-19s ", pkg);
		show_index(meta->meta_comment, tmp);
	} else if (Flags & SHOW_BI_VAR) {
		if (strcspn(BuildInfoVariable, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
		    == strlen(BuildInfoVariable))
			show_var(meta->meta_installed_info, BuildInfoVariable);
		else
			show_var(meta->meta_build_info, BuildInfoVariable);
	} else {
		package_t plist;
		
		/* Read the contents list */
		parse_plist(&plist, meta->meta_contents);

		/* Start showing the package contents */
		if (!Quiet && !(Flags & SHOW_SUMMARY)) {
			printf("%sInformation for %s:\n\n", InfoPrefix, pkg);
			if (meta->meta_preserve) {
				printf("*** PACKAGE MAY NOT BE DELETED ***\n");
			}
		}
		if (Flags & SHOW_SUMMARY) {
			show_summary(meta, &plist, binpkgfile);
		}
		if (Flags & SHOW_COMMENT) {
			show_file(meta->meta_comment, "Comment:\n", TRUE);
		}
		if (Flags & SHOW_DEPENDS) {
			show_depends("Requires:\n", &plist);
		}
		if (Flags & SHOW_BLD_DEPENDS) {
			show_bld_depends("Built using:\n", &plist);
		}
		if ((Flags & SHOW_REQBY) && meta->meta_required_by) {
			show_file(meta->meta_required_by, "Required by:\n", TRUE);
		}
		if (Flags & SHOW_DESC) {
			show_file(meta->meta_desc, "Description:\n", TRUE);
		}
		if ((Flags & SHOW_DISPLAY) && meta->meta_display) {
			show_file(meta->meta_display, "Install notice:\n",
				  TRUE);
		}
		if (Flags & SHOW_PLIST) {
			show_plist("Packing list:\n", &plist, PLIST_SHOW_ALL);
		}
		if ((Flags & SHOW_INSTALL) && meta->meta_install) {
			show_file(meta->meta_install, "Install script:\n",
				  TRUE);
		}
		if ((Flags & SHOW_DEINSTALL) && meta->meta_deinstall) {
			show_file(meta->meta_deinstall, "De-Install script:\n",
				  TRUE);
		}
		if ((Flags & SHOW_MTREE) && meta->meta_mtree) {
			show_file(meta->meta_mtree, "mtree file:\n", TRUE);
		}
		if (Flags & SHOW_PREFIX) {
			show_plist("Prefix(s):\n", &plist, PLIST_CWD);
		}
		if (Flags & SHOW_FILES) {
			show_files("Files:\n", &plist);
		}
		if ((Flags & SHOW_BUILD_VERSION) && meta->meta_build_version) {
			show_file(meta->meta_build_version, "Build version:\n",
				  TRUE);
		}
		if (Flags & SHOW_BUILD_INFO) {
			if (meta->meta_build_info) {
				show_file(meta->meta_build_info, "Build information:\n",
					  TRUE);
			}
			if (meta->meta_installed_info) {
				show_file(meta->meta_installed_info, "Installed information:\n",
					  TRUE);
			}
		}
		if ((Flags & SHOW_PKG_SIZE) && meta->meta_size_pkg) {
			show_file(meta->meta_size_pkg, "Size of this package in bytes: ",
				  TRUE);
		}
		if ((Flags & SHOW_ALL_SIZE) && meta->meta_size_all) {
			show_file(meta->meta_size_all, "Size in bytes including required pkgs: ",
				  TRUE);
		}
		if (!Quiet && !(Flags & SHOW_SUMMARY)) {
			if (meta->meta_preserve) {
				printf("*** PACKAGE MAY NOT BE DELETED ***\n\n");
			}
			puts(InfoPrefix);
		}
		free_plist(&plist);
	}
	free_pkg_meta(meta);
	return code;
}

struct print_matching_arg {
	const char *pattern;
	int got_match;
};

static int
print_matching_pkg(const char *pkgname, void *cookie)
{
	struct print_matching_arg *arg= cookie;

	if (pkg_match(arg->pattern, pkgname)) {
		if (!Quiet)
			puts(pkgname);
		arg->got_match = 1;
	}

	return 0;
}

/*
 * Returns 0 if at least one package matching pkgname.
 * Returns 1 otherwise.
 *
 * If -q was not specified, print all matching packages to stdout.
 */
int
CheckForPkg(const char *pkgname)
{
	struct print_matching_arg arg;

	arg.pattern = pkgname;
	arg.got_match = 0;

	if (iterate_pkg_db(print_matching_pkg, &arg) == -1) {
		warnx("cannot iterate pkgdb");
		return 1;
	}

	if (arg.got_match == 0 && !ispkgpattern(pkgname)) {
		char *pattern;

		pattern = xasprintf("%s-[0-9]*", pkgname);

		arg.pattern = pattern;
		arg.got_match = 0;

		if (iterate_pkg_db(print_matching_pkg, &arg) == -1) {
			free(pattern);
			warnx("cannot iterate pkgdb");
			return 1;
		}
		free(pattern);
	}

	if (arg.got_match)
		return 0;
	else
		return 1;
}

/*
 * Returns 0 if at least one package matching pkgname.
 * Returns 1 otherwise.
 *
 * If -q was not specified, print best match to stdout.
 */
int
CheckForBestPkg(const char *pkgname)
{
	char *pattern, *best_match;

	best_match = find_best_matching_installed_pkg(pkgname);
	if (best_match == NULL) {
		if (ispkgpattern(pkgname))
			return 1;

		pattern = xasprintf("%s-[0-9]*", pkgname);
		best_match = find_best_matching_installed_pkg(pattern);
		free(pattern);
	}

	if (best_match == NULL)
		return 1;
	if (!Quiet)
		puts(best_match);
	free(best_match);
	return 0;
}

void
cleanup(int sig)
{
	exit(1);
}

static int
perform_single_pkg(const char *pkg, void *cookie)
{
	int *err_cnt = cookie;

	if (Which == WHICH_ALL || !is_automatic_installed(pkg))
		*err_cnt += pkg_do(pkg);

	return 0;
}

int
pkg_perform(lpkg_head_t *pkghead)
{
	int     err_cnt = 0;

	signal(SIGINT, cleanup);

	TAILQ_INIT(&files);

	desired_meta_data = 0;
	if ((Flags & (SHOW_INDEX | SHOW_BI_VAR)) == 0)
		desired_meta_data |= LOAD_PRESERVE;
	if ((Flags & (SHOW_INDEX | SHOW_BI_VAR)) == 0)
		desired_meta_data |= LOAD_CONTENTS;
	if (Flags & (SHOW_COMMENT | SHOW_INDEX | SHOW_SUMMARY))
		desired_meta_data |= LOAD_COMMENT;
	if (Flags & (SHOW_BI_VAR | SHOW_BUILD_INFO | SHOW_SUMMARY))
		desired_meta_data |= LOAD_BUILD_INFO | LOAD_INSTALLED_INFO;
	if (Flags & (SHOW_SUMMARY | SHOW_PKG_SIZE))
		desired_meta_data |= LOAD_SIZE_PKG;
	if (Flags & SHOW_ALL_SIZE)
		desired_meta_data |= LOAD_SIZE_ALL;
	if (Flags & (SHOW_SUMMARY | SHOW_DESC))
		desired_meta_data |= LOAD_DESC;
	if (Flags & SHOW_REQBY)
		desired_meta_data |= LOAD_REQUIRED_BY;
	if (Flags & SHOW_DISPLAY)
		desired_meta_data |= LOAD_DISPLAY;
	if (Flags & SHOW_INSTALL)
		desired_meta_data |= LOAD_INSTALL;
	if (Flags & SHOW_DEINSTALL)
		desired_meta_data |= LOAD_DEINSTALL;
	if (Flags & SHOW_MTREE)
		desired_meta_data |= LOAD_MTREE;
	if (Flags & SHOW_BUILD_VERSION)
		desired_meta_data |= LOAD_BUILD_VERSION;


	if (Which != WHICH_LIST) {
		if (File2Pkg) {
			/* Show all files with the package they belong to */
			if (pkgdb_dump() == -1)
				err_cnt = 1;
		} else {
			if (iterate_pkg_db(perform_single_pkg, &err_cnt) == -1)
				err_cnt = 1;
		}
	} else {
		/* Show info on individual pkg(s) */
		lpkg_t *lpp;

		while ((lpp = TAILQ_FIRST(pkghead))) {
			TAILQ_REMOVE(pkghead, lpp, lp_link);
			err_cnt += pkg_do(lpp->lp_name);
			free_lpkg(lpp);
		}
	}
	return err_cnt;
}