summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/pkg_info
blob: 05fbfcdf77712b9f89db1c6259985cf1f83eab13 (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
#! /usr/bin/perl
# ex:ts=8 sw=4:
# $OpenBSD: pkg_info,v 1.76 2009/10/11 18:41:21 espie Exp $
#
# Copyright (c) 2003-2009 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use strict;
use warnings;
use OpenBSD::PackageInfo;
use OpenBSD::PackageName;
use OpenBSD::Getopt;
use OpenBSD::Error;

package OpenBSD::PackingElement;
sub dump_file
{
}

sub hunt_file
{
}

sub sum_up
{
	my ($self, $rsize) = @_;
	if (defined $self->{size}) {
		$$rsize += $self->{size};
	}
}

package OpenBSD::PackingElement::FileBase;
sub dump_file
{
	my ($item, $opt_K) = @_;
	if ($opt_K) {
		print '@', $item->keyword, " ";
	}
	print $item->fullname, "\n";
}

package OpenBSD::PackingElement::FileObject;
sub hunt_file
{
	my ($item, $h, $pkgname, $l) = @_;
	my $fname = $item->fullname;
	if (defined $h->{$fname}) {
		push(@{$h->{$fname}}, $pkgname);
		push(@$l, $pkgname);
	}
}

package main;

my $total_size = 0;
my $pkgs = 0;

sub find_pkg_in
{
	my ($repo, $pkgname, $code) = @_;

	if (OpenBSD::PackageName::is_stem($pkgname)) {
		require OpenBSD::Search;
		my $l = $repo->match_locations(OpenBSD::Search::Stem->new($pkgname));
		if (@$l != 0) {
			for my $pkg (sort {$a->name cmp $b->name} @$l) {
				&$code($pkg->name, $pkg);
				$pkg->close_now;
				$pkg->wipe_info;
			}
			return 1;
		}
	}
	my $pkg = $repo->find($pkgname);
	if (defined $pkg) {
		&$code($pkgname, $pkg);
		$pkg->close_now;
		$pkg->wipe_info;
		return 1;
	}
	return 0;
}

sub find_pkg
{
	my ($pkgname, $code) = @_;
	require OpenBSD::PackageRepository::Installed;

	if (find_pkg_in(OpenBSD::PackageRepository::Installed->new, $pkgname, 
	    $code)) {
		return;
	}
	require OpenBSD::PackageLocator;

	my ($repo, $path);

	if ($pkgname =~ m/\//o) {
		($repo, $path, $pkgname) = 
		    OpenBSD::PackageLocator::path_parse($pkgname);
	} else {
		$repo = 'OpenBSD::PackageLocator';
	}

	find_pkg_in($repo, $pkgname, $code);
}

sub printfile
{
	my $filename = shift;
	my $_;

	open my $fh, '<', $filename or return;
	while(<$fh>) {
		print;
	}
	close $fh;
}

sub print_description
{
	my $dir = shift;
	my $_;

	open my $fh, '<', $dir.DESC or return;
	$_ = <$fh> unless -f $dir.COMMENT;
	while(<$fh>) {
		print;
	}
	close $fh;
}

sub get_line
{
	open my $fh, '<', shift or return "";
	my $c = <$fh>;
	chomp($c);
	close $fh;
	return $c;
}

sub get_comment
{
	my $d = shift;
	return get_line(-f $d.COMMENT? $d.COMMENT : $d.DESC);
}

sub find_by_spec
{
	my $pat = shift;

	require OpenBSD::Search;
	require OpenBSD::PackageRepository::Installed;

	return sort(OpenBSD::PackageRepository::Installed->new->match(OpenBSD::Search::PkgSpec->new($pat)));
}

sub filter_files
{
	require OpenBSD::PackingList;

	my $search = shift;
	my @result = ();
	for my $arg (@_) {
		find_pkg($arg, 
		    sub {
		    	my ($pkgname, $handle) = @_;

			my $plist = $handle->plist(\&OpenBSD::PackingList::FilesOnly);
				
			$plist->hunt_file($search, $pkgname, \@result);
		    });
	}
	return @result;
}

sub manual_filter
{
	require OpenBSD::PackingList;

	my @result = ();
	for my $arg (@_) {
		find_pkg($arg, 
		    sub {
		    	my ($pkgname, $handle) = @_;

			my $plist = $handle->plist(\&OpenBSD::PackingList::ConflictOnly);
				
			push(@result, $pkgname) if $plist->has('manual-installation');
		    });
	}
	return @result;
}

my $path_info;

sub add_to_path_info
{
	my ($path, $pkgname) = @_;

	$path_info->{$path} = [] unless 
	    defined $path_info->{$path};
	push(@{$path_info->{$path}}, $pkgname);
}

sub find_by_path
{
	my $pat = shift;

	if (!defined $path_info) {
		require OpenBSD::PackingList;

		$path_info = {};
		for my $pkg (installed_packages(1)) {
			my $plist =
				OpenBSD::PackingList->from_installation($pkg,
				    \&OpenBSD::PackingList::ExtraInfoOnly);
			next if !defined $plist;
			add_to_path_info($plist->{extrainfo}->{subdir}, 
			    $plist->pkgname);
			if ($plist->has('pkgpath')) {
				for my $p (@{$plist->{pkgpath}}) {
					add_to_path_info($p->name, 
					    $plist->pkgname);
				}
			}
		}
	}
	if (defined $path_info->{$pat}) {
		return @{$path_info->{$pat}};
	} else {
		return ();
	}
}

our ($opt_c, $opt_C, $opt_D, $opt_d, $opt_f, $opt_I, $opt_i, $opt_k, $opt_K, 
    $opt_L, $opt_Q, $opt_q, $opt_R, $opt_r, $opt_s, $opt_v, $opt_h,
    $opt_l, $opt_a, $opt_m, $opt_M, $opt_U, $opt_A, $opt_S, $opt_P, $opt_t);
my $terse = 0;
my $exit_code = 0;
my $error_e = 0;
my @sought_files;

sub just_in_time_header
{
	my ($pkg, $handle, $rdone) = @_;
	return if $$rdone == 1;
	if (!$terse && !$opt_q) {
		print $opt_l, "Information for ", $handle->url, "\n\n";
	}
	$$rdone = 1;
}

sub print_info
{
	my ($pkg, $handle) = @_;
	unless (defined $handle) {
		print STDERR "Error printing info for $pkg: no info ?\n";
	}
	if ($opt_I) {
		my $l = 20 - length($pkg);
		$l = 1 if $l <= 0;
		print $pkg;
		print " "x$l, get_comment($handle->info) unless $opt_q;
		print "\n";
	} else {
		my $done = 0;
		if ($opt_c) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Comment:\n" unless $opt_q;
			print get_comment($handle->info), "\n";
			print "\n";
		}
		if ($opt_R && -f $handle->info.REQUIRED_BY) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Required by:\n" unless $opt_q;
			printfile($handle->info.REQUIRED_BY);
			print "\n";
		}
		if ($opt_d) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Description:\n" unless $opt_q;
			print_description($handle->info);
			print "\n";
		}
		if ($opt_M && -f $handle->info.DISPLAY) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Install notice:\n" unless $opt_q;
			printfile($handle->info.DISPLAY);
			print "\n";
		}
		if ($opt_U && -f $handle->info.UNDISPLAY) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Deinstall notice:\n" unless $opt_q;
			printfile($handle->info.UNDISPLAY);
			print "\n";
		}
		if ($opt_i && -f $handle->info.INSTALL) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Install script:\n" unless $opt_q;
			printfile($handle->info.INSTALL);
			print "\n";
		}
		if ($opt_k && -f $handle->info.DEINSTALL) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "De-Install script:\n" unless $opt_q;
			printfile($handle->info.DEINSTALL, \*STDOUT);
			print "\n";
		}
		if ($opt_r && -f $handle->info.REQUIRE) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Require script:\n" unless $opt_q;
			printfile($handle->info.REQUIRE, \*STDOUT);
			print "\n";
		}
		my $plist;
		if ($opt_f || $opt_L || $opt_s || $opt_S || $opt_C) {
			require OpenBSD::PackingList;

			if ($opt_f || $opt_s || $opt_S || $opt_C) {
				$plist = $handle->plist;
			} else {
				$plist = $handle->plist(\&OpenBSD::PackingList::FilesOnly);
			}
			Fatal "Bad packing list for", $handle->url 
			    unless defined $plist;
		}
		if ($opt_L) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Files:\n" unless $opt_q;
			$plist->dump_file($opt_K);
			print "\n";
		}
		if ($opt_C) {
			just_in_time_header($pkg, $handle ,\$done);
			if ($plist->is_signed) {

				require OpenBSD::x509;
				print $opt_l, "Certificate info:\n" unless $opt_q;
				OpenBSD::x509::print_certificate_info($plist);
			} else {
				print $opt_l, "No digital signature\n" unless $opt_q;
			}
		}
		if ($opt_s) {
			just_in_time_header($pkg, $handle ,\$done);
			my $size = 0;
			$plist->sum_up(\$size);
			print "Size: " unless $opt_q;
			print "$size\n";
			$total_size += $size;
			$pkgs++;
		}
		if ($opt_S) {
			just_in_time_header($pkg, $handle ,\$done);
			print "Signature: " unless $opt_q;
			print $plist->signature, "\n";
		}
		if ($opt_P) {
			require OpenBSD::PackingList;

			my $plist = $handle->plist(
			    \&OpenBSD::PackingList::ExtraInfoOnly);
			just_in_time_header($pkg, $handle ,\$done);
			print "Pkgpath:\n" unless $opt_q;
			print $plist->{extrainfo}->{subdir}, "\n";
		}

		if ($opt_f) {
			just_in_time_header($pkg, $handle ,\$done);
			print $opt_l, "Packing list:\n" unless $opt_q;
			$plist->write(\*STDOUT);
			print "\n";
		}
		return unless $done;
		print $opt_l, "\n" unless $opt_q || $terse;
	}
}

set_usage('pkg_info [-AacCDdfIiKkLmMPqRrSstUv] [-F opt] [-E filename] [-e pkg-name] [-l str] [-Q query] [pkg-name] [...]');

my %defines;
my $locked;
try { 
	getopts('cCDdfF:hIikKLmPQ:qRrsSUve:E:Ml:aAt',
	    {'e' =>
		    sub {
			    my $pat = shift;
			    my @list;
			    lock_db(1, $opt_q) unless $defines{nolock};
			    $locked = 1;
			    if ($pat =~ m/\//o) {
				    @list = find_by_path($pat);
			    } else {
				    @list = find_by_spec($pat);
			    }
			    if (@list == 0) {
				    $exit_code = 1;
				    $error_e = 1;
			    }
			    push(@ARGV, @list);
			    $terse = 1;
		    },
	     'F' => sub { 
			    for my $o (split /\,/o, shift) { 
				    $defines{$o} = 1;
			    }
		    },
	     'h' => sub {	Usage(); },
	     'E' =>
		    sub {
			    require File::Spec;

			    push(@sought_files, File::Spec->rel2abs(shift));

		    }
	}); 
} catchall {
	Usage($_);
};

lock_db(1, $opt_q) unless $locked or $defines{nolock};

if ($opt_D) {
	$opt_M = 1;
}

unless ($opt_c || $opt_M || $opt_U || $opt_d || $opt_f || $opt_I || $opt_i ||
	$opt_k || $opt_L || $opt_R || $opt_r || $opt_s || 
	$opt_S || $opt_P || $terse) {
	if (@ARGV == 0) {
		$opt_I = $opt_a = 1;
	} else {
		$opt_c = $opt_d = $opt_M = $opt_R = 1;
	}
}

if ($opt_Q) {
	require OpenBSD::PackageLocator;
	require OpenBSD::Search;

	print "PKG_PATH=$ENV{PKG_PATH}\n" if $opt_v;
	my $partial = OpenBSD::Search::PartialStem->new($opt_Q);

	for my $p (sort OpenBSD::PackageLocator->match($partial)) {
		print $p, is_installed($p) ? " (installed)" : "" , "\n";
	}

	exit 0;
}

if ($opt_v) {
	$opt_c = $opt_d = $opt_f = $opt_i = $opt_k = $opt_r = $opt_M =
	    $opt_U = $opt_R = $opt_s = $opt_S = 1;
}

if (!defined $opt_l) {
	$opt_l = "";
}

if ($opt_K && !$opt_L) {
	Usage "-K only makes sense with -L";
}

if (@ARGV == 0 && !$opt_a && !$opt_A) {
	Usage "Missing package name(s)" unless $terse || $opt_q;
}

if (@ARGV > 0 && ($opt_a || $opt_A)) {
	Usage "Can't specify package name(s) with -a";
}

if (@ARGV > 0 && $opt_t) {
	Usage "Can't specify package name(s) with -t";
}

if (@ARGV > 0 && $opt_m) {
	Usage "Can't specify package name(s) with -m";
}

if (@ARGV == 0 && !$error_e) {
	@ARGV = sort(installed_packages(defined $opt_A ? 0 : 1));
	if ($opt_t) {
		require OpenBSD::RequiredBy;
		@ARGV = grep { OpenBSD::RequiredBy->new($_)->list == 0 } @ARGV;
	}
}

if (@sought_files) {
	my %hash = map { ($_, []) }  @sought_files;
	@ARGV = filter_files(\%hash, @ARGV);
	for my $f (@sought_files) {
		my $l = $hash{$f};
		if (@$l) {
			print "$f: ", join(',', @$l), "\n" unless $opt_q;
		} else {
			$exit_code = 1;
		}
	}
}

if ($opt_m) {
	@ARGV = manual_filter(@ARGV);
}

for my $pkg (@ARGV) {
	if ($terse && !$opt_q) {
		print $opt_l, $pkg, "\n";
	}
	find_pkg($pkg, \&print_info);
}
if ($pkgs > 1) {
	print "Total size: $total_size\n";
}
exit($exit_code);