1# ex:ts=8 sw=4:
2# $OpenBSD: Installed.pm,v 1.32 2015/04/16 14:08:19 espie Exp $
3#
4# Copyright (c) 2007-2014 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# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18use strict;
19use warnings;
20
21# XXX: we want to be able to load PackageRepository::Installed stand-alone,
22# so we put the only common method into PackageRepositoryBase.
23#
24# later, when we load the base PackageRepository, we tweak the inheritance
25# of PackageRepository::Installed to have full access...
26
27package OpenBSD::PackageRepositoryBase;
28
29my ($version, $current);
30
31sub expand_locations
32{
33	my ($class, $string, $state) = @_;
34	require OpenBSD::Paths;
35	if ($string eq '%a') {
36		return OpenBSD::Paths->architecture;
37	} elsif ($string eq '%v') {
38		return OpenBSD::Paths->os_version;
39	} elsif ($string eq '%c') {
40		return OpenBSD::Paths->os_directory;
41	} elsif ($string eq '%m') {
42		return join('/',
43		    'pub/OpenBSD',
44		    OpenBSD::Paths->os_directory,
45		    'packages',
46		    OpenBSD::Paths->architecture);
47	}
48}
49
50sub parse_url
51{
52	my ($class, $r, $state) = @_;
53
54	my $path;
55
56	if ($$r =~ m/^(.*?)\:(.*)/) {
57		$path = $1;
58		$$r = $2;
59	} else {
60		$path = $$r;
61		$$r = '';
62	}
63
64	$path =~ s/\%[vacm]/$class->expand_locations($&, $state)/ge;
65	$path .= '/' unless $path =~ m/\/$/;
66	bless { path => $path, state => $state }, $class;
67}
68
69sub parse_fullurl
70{
71	my ($class, $r, $state) = @_;
72
73	$class->strip_urlscheme($r) or return undef;
74	return $class->parse_url($r, $state);
75}
76
77sub strip_urlscheme
78{
79	my ($class, $r) = @_;
80	if ($$r =~ m/^(.*?)\:(.*)$/) {
81		my $scheme = lc($1);
82		if ($scheme eq $class->urlscheme) {
83			$$r = $2;
84			return 1;
85	    	}
86	}
87	return 0;
88}
89
90sub match_locations
91{
92	my ($self, $search, @filters) = @_;
93	my $l = $search->match_locations($self);
94	while (my $filter = (shift @filters)) {
95		last if @$l == 0; # don't bother filtering empty list
96		$l = $filter->filter_locations($l);
97	}
98	return $l;
99}
100
101sub url
102{
103	my ($self, $name) = @_;
104	return $self->urlscheme.':'.$self->relative_url($name);
105}
106
107sub finish_and_close
108{
109	my ($self, $object) = @_;
110	$self->close($object);
111}
112
113sub close_now
114{
115	my ($self, $object) = @_;
116	$self->close($object, 0);
117}
118
119sub close_after_error
120{
121	my ($self, $object) = @_;
122	$self->close($object, 1);
123}
124
125sub close_with_client_error
126{
127	my ($self, $object) = @_;
128	$self->close($object, 1);
129}
130
131sub canonicalize
132{
133	my ($self, $name) = @_;
134
135	if (defined $name) {
136		$name =~ s/\.tgz$//o;
137	}
138	return $name;
139}
140
141sub new_location
142{
143	my ($self, @args) = @_;
144
145	return $self->locationClassName->new($self, @args);
146}
147
148sub locationClassName
149{ "OpenBSD::PackageLocation" }
150
151sub locations_list
152{
153	my $self = shift;
154	if (!defined $self->{locations}) {
155		my $l = [];
156		require OpenBSD::PackageLocation;
157
158		for my $name (@{$self->list}) {
159			push @$l, $self->new_location($name);
160		}
161		$self->{locations} = $l;
162	}
163	return $self->{locations};
164}
165
166sub reinitialize
167{
168}
169
170package OpenBSD::PackageRepository::Installed;
171
172our @ISA = (qw(OpenBSD::PackageRepositoryBase));
173
174sub urlscheme
175{
176	return 'inst';
177}
178
179use OpenBSD::PackageInfo (qw(is_installed installed_info
180    installed_packages installed_stems installed_name));
181
182sub new
183{
184	my ($class, $all, $state) = @_;
185	return bless { all => $all, state => $state }, $class;
186}
187
188sub relative_url
189{
190	my ($self, $name) = @_;
191	$name or '';
192}
193
194sub close
195{
196}
197
198sub canonicalize
199{
200	my ($self, $name) = @_;
201	return installed_name($name);
202}
203
204sub find
205{
206	my ($repository, $name, $arch) = @_;
207	my $self;
208
209	if (is_installed($name)) {
210		require OpenBSD::PackageLocation;
211
212		$self = $repository->new_location($name);
213		$self->{dir} = installed_info($name);
214	}
215	return $self;
216}
217
218sub locationClassName
219{ "OpenBSD::PackageLocation::Installed" }
220
221sub grabPlist
222{
223	my ($repository, $name, $arch, $code) = @_;
224	require OpenBSD::PackingList;
225	return  OpenBSD::PackingList->from_installation($name, $code);
226}
227
228sub available
229{
230	my $self = shift;
231	return installed_packages($self->{all});
232}
233
234sub list
235{
236	my $self = shift;
237	my @list = installed_packages($self->{all});
238	return \@list;
239}
240
241sub stemlist
242{
243	return installed_stems();
244}
245
246sub wipe_info
247{
248}
249
250sub may_exist
251{
252	my ($self, $name) = @_;
253	return is_installed($name);
254}
255
2561;
257