xref: /openbsd/usr.sbin/pkg_add/pkg_mklocatedb (revision 8e722a89)
158dbc7adSespie#! /usr/bin/perl
2a4f5498fSespie# Copyright (c) 2005-2010 Marc Espie <espie@openbsd.org>
3*8e722a89Sespie# $OpenBSD: pkg_mklocatedb,v 1.47 2021/05/04 08:41:03 espie Exp $
458dbc7adSespie#
558dbc7adSespie# Permission to use, copy, modify, and distribute this software for any
658dbc7adSespie# purpose with or without fee is hereby granted, provided that the above
758dbc7adSespie# copyright notice and this permission notice appear in all copies.
858dbc7adSespie#
958dbc7adSespie# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1058dbc7adSespie# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1158dbc7adSespie# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1258dbc7adSespie# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1358dbc7adSespie# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1458dbc7adSespie# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1558dbc7adSespie# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1658dbc7adSespie
1758dbc7adSespieuse strict;
180fbefeddSespieuse warnings;
19*8e722a89Sespieno lib ('/usr/local/libdata/perl5/site_perl');
200fbefeddSespie
2158dbc7adSespieuse OpenBSD::PackageInfo;
2258dbc7adSespieuse OpenBSD::PackingList;
2358dbc7adSespieuse OpenBSD::Getopt;
2458dbc7adSespieuse OpenBSD::Error;
2573c16894Sespieuse OpenBSD::Paths;
2669f8af35Sespieuse OpenBSD::AddCreateDelete;
2758dbc7adSespie
2862d979acSespiepackage OpenBSD::Pkgmklocatedb::State;
2962d979acSespieour @ISA = qw(OpenBSD::AddCreateDelete::State);
3062d979acSespie
3162d979acSespiesub handle_options
3262d979acSespie{
3362d979acSespie	my $state = shift;
3462d979acSespie	$state->{no_exports} = 1;
352d4ad278Sespie	$state->SUPER::handle_options('ad:Knqp:Pu',
362d4ad278Sespie	    '[-aKnPqu] [-d repository] [-p portsdir] [pkg-name ...]');
3762d979acSespie	$state->{portsdir} = $state->opt('p');
3862d979acSespie	$state->{pkgdir} = $state->opt('d');
3962d979acSespie	$state->{quiet} = $state->opt('q');
4062d979acSespie	$state->{pkgpath} = $state->opt('P');
4162d979acSespie	$state->{allinfo} = $state->opt('a');
4262d979acSespie	$state->{nopipe} = $state->opt('n');
433107f8b4Sespie	$state->{full} = $state->opt('K');
443107f8b4Sespie	$state->{update} = $state->opt('u');
45df0fe0c2Sespie}
4662d979acSespie
4758dbc7adSespiepackage OpenBSD::PackingElement;
4858dbc7adSespiesub print_name {}
49b0e520b0Sespiesub set_header {}
5058dbc7adSespie
5162d979acSespiepackage OpenBSD::PackingElement::Name;
52b0e520b0Sespiesub set_header
5362d979acSespie{
5462d979acSespie	my ($self, $state) = @_;
5562d979acSespie	$state->{currentheader} = $self->{name}.':';
5662d979acSespie}
5762d979acSespie
5862d979acSespiepackage OpenBSD::PackingElement::ExtraInfo;
59b0e520b0Sespiesub set_header
6062d979acSespie{
6162d979acSespie	my ($self, $state) = @_;
6262d979acSespie	if ($state->{allinfo}) {
6362d979acSespie		$state->{currentheader} .=  $self->{subdir}.':';
6462d979acSespie	} elsif ($state->{pkgpath}) {
6562d979acSespie		$state->{currentheader} = $self->{subdir}.':';
6662d979acSespie	}
673107f8b4Sespie	$state->{done}{$self->{subdir}} = 1;
6862d979acSespie	$state->errsay($state->{currentheader}) unless $state->{quiet};
6962d979acSespie}
7062d979acSespie
7158dbc7adSespiepackage OpenBSD::PackingElement::FileObject;
723107f8b4Sespiesub object_name
733107f8b4Sespie{
743107f8b4Sespie	my ($self, $state) = @_;
753107f8b4Sespie	if ($state->{full}) {
763107f8b4Sespie		if ($self->needs_keyword) {
773107f8b4Sespie			return "\@".$self->keyword." ".$self->fullname;
783107f8b4Sespie		}
793107f8b4Sespie	}
803107f8b4Sespie	return $self->fullname;
813107f8b4Sespie}
823107f8b4Sespie
8358dbc7adSespiesub print_name
8458dbc7adSespie{
8562d979acSespie	my ($self, $state) = @_;
863107f8b4Sespie	print {$state->{out}} $state->{currentheader},
873107f8b4Sespie	    $self->object_name($state), "\n";
8858dbc7adSespie}
8958dbc7adSespie
90bbaa4218Sespiepackage OpenBSD::PackingElement::Action;
91bbaa4218Sespiesub print_name
92bbaa4218Sespie{
93bbaa4218Sespie	my ($self, $state) = @_;
94b0e520b0Sespie	print {$state->{out}} $state->{currentheader}, $self->fullstring, "\n";
95bbaa4218Sespie}
96bbaa4218Sespie
97b0e520b0Sespiepackage OpenBSD::PackingElement::ExeclikeAction;
98bbaa4218Sespiesub print_name
99bbaa4218Sespie{
100bbaa4218Sespie	my ($self, $state) = @_;
101b0e520b0Sespie	print {$state->{out}} $state->{currentheader}, "\@".
102b0e520b0Sespie	    $self->keyword, " ", $self->{expanded}, "\n";
103bbaa4218Sespie}
104bbaa4218Sespie
10528ca0f9aSespiepackage OpenBSD::PackingElement::Conflict;
10628ca0f9aSespiesub print_name
10728ca0f9aSespie{
10828ca0f9aSespie	&OpenBSD::PackingElement::Action::print_name;
10928ca0f9aSespie}
11028ca0f9aSespie
11128ca0f9aSespiepackage OpenBSD::PackingElement::NoDefaultConflict;
11228ca0f9aSespiesub print_name
11328ca0f9aSespie{
11428ca0f9aSespie	&OpenBSD::PackingElement::Action::print_name;
11528ca0f9aSespie}
11628ca0f9aSespie
117a970ee5aSespiepackage OpenBSD::PackingElement::TagBase;
118a970ee5aSespiesub print_name
119a970ee5aSespie{
120a970ee5aSespie	my ($self, $state) = @_;
121a970ee5aSespie	print {$state->{out}} $state->{currentheader}, "\@".
122a970ee5aSespie	    join(' ', $self->keyword, $self->name, $self->{params}), "\n";
123a970ee5aSespie}
124a970ee5aSespie
125a970ee5aSespiepackage OpenBSD::PackingElement::Tag;
126a970ee5aSespiesub print_name
127a970ee5aSespie{
128a970ee5aSespie	my ($self, $state) = @_;
129a970ee5aSespie	print {$state->{out}} $state->{currentheader}, "\@".
130a970ee5aSespie	    join(' ', $self->keyword, $self->name, $self->{expanded}), "\n";
131a970ee5aSespie}
132a970ee5aSespie
133e51225d9Sespiepackage OpenBSD::PackingElement::DirBase;
134e51225d9Sespiesub print_name
135e51225d9Sespie{
136e51225d9Sespie	my ($self, $state) = @_;
1373107f8b4Sespie	print {$state->{out}} $state->{currentheader},
1383107f8b4Sespie	    $self->object_name($state), "/\n";
139e51225d9Sespie}
14058dbc7adSespie
141965def97Sespiepackage main;
14262d979acSespie
14362d979acSespiesub open_output
14462d979acSespie{
14562d979acSespie	my $state = shift;
14662d979acSespie
14762d979acSespie
14862d979acSespie	if ($state->{nopipe} or -t STDOUT) {
14962d979acSespie		$state->{out} = \*STDOUT;
15062d979acSespie	} else {
15162d979acSespie		my $MKLOCATEDB = OpenBSD::Paths->mklocatedb;
15262d979acSespie
15362d979acSespie		open $state->{out}, "|-", $MKLOCATEDB, $MKLOCATEDB or
15462d979acSespie		    $state->fatal("couldn't open pipe to mklocatedb: #1", $!);
15562d979acSespie	}
15662d979acSespie}
15762d979acSespie
158efcf1a09Sespiesub print_out
159efcf1a09Sespie{
160efcf1a09Sespie	my ($plist, $state) = @_;
161efcf1a09Sespie
162efcf1a09Sespie	$plist->set_header($state);
163efcf1a09Sespie	$plist->print_name($state);
164efcf1a09Sespie}
165efcf1a09Sespie
16662d979acSespiesub do_portsdir
16762d979acSespie{
16862d979acSespie	my $state = shift;
1696baa78d2Sespie	my $make = $ENV{MAKE} || 'make';
1704f52b46eSespie	my $target = defined $ENV{SUBDIRLIST} ?
1714f52b46eSespie	    'print-plist' : 'print-plist-all';
17288d40ff8Sespie	delete $ENV{FLAVOR};
17388d40ff8Sespie	delete $ENV{SUBPACKAGE};
1744f52b46eSespie	open my $in, "cd $state->{portsdir} && $make $target |";
1756baa78d2Sespie	my $done = 0;
1766baa78d2Sespie	while (!$done) {
1776baa78d2Sespie		my $plist = OpenBSD::PackingList->read($in,
1786baa78d2Sespie		    sub {
1796baa78d2Sespie			my ($fh, $cont) = @_;
1806baa78d2Sespie			while (<$fh>) {
1816baa78d2Sespie				return if m/^\=\=\=\> /o;
1826baa78d2Sespie				&$cont($_);
1836baa78d2Sespie			}
1846baa78d2Sespie			$done = 1;
1856baa78d2Sespie		    });
186cc24e6f2Sespie		if (defined $plist && defined $plist->pkgname) {
187efcf1a09Sespie			print_out($plist, $state);
1886baa78d2Sespie		}
1896baa78d2Sespie	}
1906baa78d2Sespie	close($in);
1916baa78d2Sespie}
19224bc8510Sespie
19362d979acSespiesub do_pkgdir
19462d979acSespie{
19562d979acSespie	my $state = shift;
19662d979acSespie
19799ebf4daSespie	require File::Find;
198a4f5498fSespie	no warnings qw(once);
19962d979acSespie	$state->fatal("Bad argument: #1 is not a directory", $state->{pkgdir})
20062d979acSespie	    unless -d $state->{pkgdir};
20199ebf4daSespie	File::Find::find(
20299ebf4daSespie	    sub {
20399ebf4daSespie		return unless -f $_;
20469f8af35Sespie		my $plist = $state->repo->grabPlist($File::Find::name);
20599ebf4daSespie		return unless defined $plist;
206efcf1a09Sespie		print_out($plist, $state);
20762d979acSespie	    }, $state->{pkgdir});
20862d979acSespie}
20962d979acSespie
2103107f8b4Sespiesub copy_stdin
2113107f8b4Sespie{
2123107f8b4Sespie	my $state = shift;
2133107f8b4Sespie	while (<STDIN>) {
2143107f8b4Sespie		# if we find something that looks like a pkgpath we've done
2153107f8b4Sespie		# assume we were updating it
2163107f8b4Sespie		if (m,([^:]*/[^:]*)\:,) {
2173107f8b4Sespie			next if defined $state->{done}{$1};
2183107f8b4Sespie		}
2193107f8b4Sespie		print {$state->{out}} $_;
2203107f8b4Sespie	}
2213107f8b4Sespie}
2223107f8b4Sespie
22341c1847dSespiemy $state = OpenBSD::Pkgmklocatedb::State->new;
22462d979acSespie$state->handle_options;
22562d979acSespie
22662d979acSespieopen_output($state);
22762d979acSespie
228df0fe0c2Sespieif ($state->{fatals}) {
229df0fe0c2Sespie	$state->fatal("Files not found, can't continue");
230df0fe0c2Sespie}
231df0fe0c2Sespie
23262d979acSespieif ($state->{portsdir}) {
23362d979acSespie	do_portsdir($state);
23462d979acSespie} elsif ($state->{pkgdir}) {
23562d979acSespie	do_pkgdir($state);
23699ebf4daSespie} elsif (@ARGV == 0) {
2373107f8b4Sespie	if (!$state->{update}) {
23869f8af35Sespie		$state->progress->for_list("Scanning installation",
23969f8af35Sespie		    [installed_packages()], sub {
24069f8af35Sespie			my $pkgname = shift;
2413107f8b4Sespie			my $plist =
2423107f8b4Sespie			    OpenBSD::PackingList->from_installation($pkgname);
24369f8af35Sespie			return unless defined $plist;
244efcf1a09Sespie			print_out($plist, $state);
24569f8af35Sespie		    });
2463107f8b4Sespie	}
24758dbc7adSespie} else {
24869f8af35Sespie	$state->progress->for_list("Scanning packages", \@ARGV,
24969f8af35Sespie	    sub {
25069f8af35Sespie	    	my $pkgname = shift;
25169f8af35Sespie		my $plist = $state->repo->grabPlist($pkgname);
252d5a96aa3Sespie		next unless $plist;
253efcf1a09Sespie		print_out($plist, $state);
25469f8af35Sespie	    });
25558dbc7adSespie}
2563107f8b4Sespieif ($state->{update}) {
2573107f8b4Sespie	copy_stdin($state);
2583107f8b4Sespie}
259