1# ex:ts=8 sw=4:
2# $OpenBSD: ForwardDependencies.pm,v 1.16 2020/02/19 10:53:53 espie Exp $
3#
4# Copyright (c) 2009 Marc Espie <espie@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
17# handling of forward dependency adjustments
18
19use strict;
20use warnings;
21
22package OpenBSD::ForwardDependencies;
23
24require OpenBSD::RequiredBy;
25
26sub find
27{
28	my ($class, $set) = @_;
29	my $forward = {};
30	for my $old ($set->older) {
31		for my $f (OpenBSD::RequiredBy->new($old->pkgname)->list) {
32			next if defined $set->{older}{$f};
33			$forward->{$f} = 1;
34		}
35	}
36	bless { forward => $forward, set => $set}, $class;
37}
38
39sub adjust
40{
41	my ($self, $state) = @_;
42	my $set = $self->{set};
43
44	for my $f (keys %{$self->{forward}}) {
45		my $deps_f = OpenBSD::Requiring->new($f);
46		for my $check ($deps_f->list) {
47			my $h = $set->{older}{$check};
48			next unless defined $h;
49			my $r = $h->{update_found};
50			if (!defined $r) {
51				$state->errsay("XXX #1", $check);
52				$deps_f->delete($check);
53				next;
54			}
55			# XXX is_real corresponds to actual package dependencies
56			# for stuff moving to the base system, we got
57			# a pseudo handle which shouldn't be recorded.
58			my $p = $r->pkgname;
59			$state->say("Adjusting #1 to #2 in #3", $check, $p, $f)
60				if $state->verbose >= 3;
61			if ($check ne $p) {
62				if ($r->is_real) {
63					$deps_f->delete($check)->add($p);
64				} else {
65					$deps_f->delete($check);
66				}
67			}
68			if ($r->is_real) {
69				OpenBSD::RequiredBy->new($p)->add($f);
70			}
71		}
72	}
73}
74
75sub dump
76{
77	my ($self, $result, $state) = @_;
78	$state->say("#1 forward dependencies:", $self->{set}->print);
79	while (my ($pkg, $l) = each %$result) {
80		if (@$l == 1) {
81			$state->say("| Dependency of #1 on #2 doesn't match",
82			    $pkg, $l->[0]{pattern});
83		} else {
84			my $deps = join(',', map {$_->{pattern}} @$l);
85			$state->say("| Dependencies of #1 on #2 don't match",
86			    $pkg, $deps);
87		}
88	}
89}
90
91sub check
92{
93	my ($self, $state) = @_;
94
95	my @r = keys %{$self->{forward}};
96	my $set = $self->{set};
97	my $result = {};
98	return $result if @r == 0;
99	$state->say("Verifying dependencies still match for #1",
100	    join(', ', @r)) if $state->verbose >= 2;
101
102	my @new = ($set->newer_names, $set->kept_names);
103	my @old = $set->older_names;
104
105	for my $f (@r) {
106		my $p2 = OpenBSD::PackingList->from_installation(
107		    $f, \&OpenBSD::PackingList::DependOnly);
108		if (!defined $p2) {
109			$state->errsay("Error: #1 missing from installation",
110			    $f);
111		} else {
112			$p2->check_forward_dependency($f, \@old, \@new,
113			    $result);
114		}
115	}
116	if (%$result) {
117		$self->dump($result, $state);
118	}
119	return $result;
120}
121
122package OpenBSD::PackingElement;
123sub check_forward_dependency
124{
125}
126
127package OpenBSD::PackingElement::Dependency;
128sub check_forward_dependency
129{
130	my ($self, $f, $old, $new, $r) = @_;
131
132	# nothing to validate if old dependency doesn't concern us.
133	return unless $self->spec->filter(@$old);
134	# nothing to do if new dependency just matches
135	return if $self->spec->filter(@$new);
136
137	push(@{$r->{$f}}, $self);
138}
139
1401;
141