summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/OpenBSD/LibSpec.pm
blob: 7a46e7333df9b6ca122f66621429f2be090b9c66 (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
# ex:ts=8 sw=4:
# $OpenBSD: LibSpec.pm,v 1.21 2023/10/08 12:45:31 espie Exp $
#
# Copyright (c) 2010 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 v5.36;

package OpenBSD::LibObject;

sub systemlibraryclass($self)
{
	return ref($self);
}

sub key($self)
{
	if (defined $self->{dir}) {
		return "$self->{dir}/$self->{stem}";
	} else {
		return $self->{stem};
	}
}

sub major($self)
{
	return $self->{major};
}

sub minor($self)
{
	return $self->{minor};
}

sub version($self)
{
	return ".".$self->major.".".$self->minor;
}

sub is_static($) { 0 }

sub is_valid($) { 1 }

sub stem($self)
{
	return $self->{stem};
}

sub badclass($self)
{
	"OpenBSD::BadLib";
}

sub lookup($spec, $repo, $base)
{
	my $approx = $spec->lookup_stem($repo);
	if (!defined $approx) {
		return undef;
	}
	my $r = [];
	for my $c (@$approx) {
		if ($spec->match($c, $base)) {
			push(@$r, $c);
		}
	}
	return $r;
}

sub compare($a, $b)
{
	if ($a->key ne $b->key) {
		return $a->key cmp $b->key;
	}
	if ($a->major != $b->major) {
		return $a->major <=> $b->major;
	}
	return $a->minor <=> $b->minor;
}

package OpenBSD::BadLib;
our @ISA=qw(OpenBSD::LibObject);

sub to_string($self)
{
	return $$self;
}

sub new($class, $string)
{
	bless \$string, $class;
}

sub is_valid($)
{
	return 0;
}

sub lookup_stem($, $)
{
	return undef;
}

# $spec->match($library, $base)
sub match($, $, $)
{
	return 0;
}

package OpenBSD::LibRepo;

sub new($class)
{
	bless {}, $class;
}

sub register($repo, $lib, $origin)
{
	$lib->set_origin($origin);
	push @{$repo->{$lib->stem}}, $lib;
}

sub find_best($repo, $stem)
{
	my $best;

	if (exists $repo->{$stem}) {
		for my $lib (@{$repo->{$stem}}) {
			if (!defined $best || $lib->is_better($best)) {
				$best = $lib;
			}
		}
	} 
	return $best;
}

package OpenBSD::Library;
our @ISA = qw(OpenBSD::LibObject);

sub systemlibraryclass($)
{
	"OpenBSD::Library::System";
}

sub from_string($class, $filename)
{
	if (my ($dir, $stem, $major, $minor) = $filename =~ m/^(.*)\/lib([^\/]+)\.so\.(\d+)\.(\d+)$/o) {
		bless { dir => $dir, stem => $stem, major => $major,
		    minor => $minor }, $class;
	} else {
		return $class->badclass->new($filename);
	}
}

sub to_string($self)
{
	return "$self->{dir}/lib$self->{stem}.so.$self->{major}.$self->{minor}";
}

sub set_origin($self, $origin)
{
	$self->{origin} = $origin;
	if ($origin eq 'system') {
		bless $self, $self->systemlibraryclass;
	}
	return $self;
}

sub origin($self)
{
	return $self->{origin};
}

sub no_match_dispatch($library, $spec, $base)
{
	return $spec->no_match_shared($library, $base);
}

sub is_better($self, $other)
{
	if ($other->is_static) {
		return 1;
	}
	if ($self->major > $other->major) {
		return 1;
	}
	if ($self->major == $other->major && $self->minor > $other->minor) {
		return 1;
    	}
	return 0;
}

# could be used for better reporting
# is used for regression testing
package OpenBSD::Library::System;
our @ISA = qw(OpenBSD::Library);

package OpenBSD::LibSpec;
our @ISA = qw(OpenBSD::LibObject);

sub new($class, $dir, $stem, $major, $minor)
{
	bless {
		dir => $dir, stem => $stem,
		major => $major, minor => $minor
	    }, $class;
}

my $cached = {};

sub from_string($class, $s)
{
	return $cached->{$s} //= $class->new_from_string($s);
}

sub new_with_stem($class, $stem, $major, $minor)
{
	if ($stem =~ m/^(.*)\/([^\/]+)$/o) {
		return $class->new($1, $2, $major, $minor);
	} else {
		return $class->new(undef, $stem, $major, $minor);
	}
}

sub new_from_string($class, $string)
{
	if (my ($stem, $major, $minor) = $string =~ m/^(.*)\.(\d+)\.(\d+)$/o) {
		return $class->new_with_stem($stem, $major, $minor);
	} else {
		return $class->badclass->new($string);
	}
}

sub to_string($self)
{
	return join('.', $self->key, $self->major, $self->minor);

}

sub lookup_stem($spec, $repo)
{
	my $result = $repo->{$spec->stem};
	if (!defined $result) {
		return undef;
	} else {
		return $result;
	}
}

sub no_match_major($spec, $library)
{
	return $spec->major != $library->major;
}

sub no_match_name($spec, $library, $base)
{
	if (defined $spec->{dir}) {
		if ("$base/$spec->{dir}" eq $library->{dir}) {
			return undef;
		}
	} else {
		for my $d ($base, OpenBSD::Paths->library_dirs) {
			if ("$d/lib" eq $library->{dir}) {
				return undef;
			}
		}
	}
	return "bad directory";
}

sub no_match_shared($spec, $library, $base)
{
	if ($spec->no_match_major($library)) {
		return "bad major";
	}
	if ($spec->major == $library->major &&
	    $spec->minor > $library->minor) {
		return "minor is too small";
	}
	return $spec->no_match_name($library, $base);
}

# classic double dispatch pattern
sub no_match($spec, $library, $base)
{
	return $library->no_match_dispatch($spec, $base);
}

sub match($spec, $library, $base)
{
	return !$spec->no_match($library, $base);
}

1;