summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/OpenBSD/Handle.pm
blob: 14f8e480c6c3526e66738b9069679135656ee756 (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
# ex:ts=8 sw=4:
# $OpenBSD: Handle.pm,v 1.6 2009/10/19 14:00:10 espie Exp $
#
# Copyright (c) 2007-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.


# fairly non-descriptive name. Used to store various package information
# during installs and updates.
package OpenBSD::Handle;

use OpenBSD::PackageInfo;

use constant {
	BAD_PACKAGE => 1,
	CANT_INSTALL => 2,
	ALREADY_INSTALLED => 3,
	NOT_FOUND => 4
};

sub cleanup
{
	my ($self, $error) = @_;
	$self->{error} //= $error;
	if (defined $self->location) {
		if ($self->{error} == ALREADY_INSTALLED) {
			$self->location->close_now;
		} elsif ($self->{error} == CANT_INSTALL) {
			$self->location->close_with_client_error;
		}
		$self->location->wipe_info;
	}
	delete $self->{plist};
}

sub new
{
	my $class = shift;
	return bless {}, $class;
}

sub pkgname
{
	my $self = shift;
	if (!defined $self->{pkgname}) {
		if (defined $self->{plist}) {
			$self->{pkgname} = $self->{plist}->pkgname;
		} elsif (defined $self->{location}) {
			$self->{pkgname} = $self->{location}->name;
		} elsif (defined $self->{name}) {
			require OpenBSD::PackageName;

			$self->{pkgname} = 
			    OpenBSD::PackageName::url2pkgname($self->{name});
		}
	}

	return $self->{pkgname};
}

sub location
{
	return shift->{location};
}

sub plist
{
	return shift->{plist};
}

sub set_error
{
	my ($self, $error) = @_;
	$self->{error} = $error;
}

sub has_error
{
	my ($self, $error) = @_;
	if (!defined $self->{error}) {
		return undef;
	}
	if (defined $error) {
		return $self->{error} eq $error;
	}
	return $self->{error};
}

sub create_old
{

	my ($class, $pkgname, $state) = @_;
	my $self= $class->new;
	$self->{name} = $pkgname;

	require OpenBSD::PackageRepository::Installed;

	my $location = OpenBSD::PackageRepository::Installed->new->find($pkgname, $state->{arch});
	if (!defined $location) {
		$self->set_error(NOT_FOUND);
    	} else {
		$self->{location} = $location;
		my $plist = $location->plist;
		if (!defined $plist) {
			$self->set_error(BAD_PACKAGE);
		} else {
			$self->{plist} = $plist;
		}
	}

	return $self;
}

sub create_new
{
	my ($class, $pkg) = @_;
	my $handle = $class->new;
	$handle->{name} = $pkg;
	$handle->{tweaked} = 0;
	return $handle;
}

sub from_location
{
	my ($class, $location) = @_;
	my $handle = $class->new;
	$handle->{location} = $location;
	$handle->{tweaked} = 0;
	return $handle;
}

sub get_plist
{
	my ($handle, $state) = @_;

	my $location = $handle->{location};
	my $pkg = $handle->pkgname;

	if ($state->{verbose}) {
		print $state->deptree_header($pkg);
		print "parsing $pkg\n";
	}
	my $plist = $location->grabPlist;
	unless (defined $plist) {
		print "Can't find CONTENTS from ", $location->url, "\n";
		$location->close_with_client_error;
		$location->wipe_info;
		$handle->set_error(BAD_PACKAGE);
		return;
	}
	if ($plist->localbase ne $state->{localbase}) {
		print "Localbase mismatch: package has: ", $plist->localbase, " , user wants: ", $state->{localbase}, "\n";
		$location->close_with_client_error;
		$location->wipe_info;
		$handle->set_error(BAD_PACKAGE);
		return;
	}
	my $pkgname = $handle->{pkgname} = $plist->pkgname;

	if (is_installed($pkgname) && 
	    (!$state->{allow_replacing} ||	
	      !$state->{defines}->{installed} &&
	      !$plist->has_new_sig($state) && 
	      !$plist->uses_old_libs)) {
		$handle->{tweaked} = 
		    OpenBSD::Add::tweak_package_status($pkgname, $state);
		print "Not reinstalling $pkgname\n" if $state->{verbose} and
		    !$handle->{tweaked};
		$state->{tracker}->{installed}->{$pkgname} = 1;
		$location->close_now;
		$location->wipe_info;
		$handle->set_error(ALREADY_INSTALLED);
		return;
	}
	if ($pkg ne '-') {
		if (!defined $pkgname or $pkg ne $pkgname) {
			print "Package name is not consistent ???\n";
			$location->close_with_client_error;
			$location->wipe_info;
			$handle->set_error(BAD_PACKAGE);
			return;
		}
	}
	$handle->{plist} = $plist;
}

sub get_location
{
	my ($handle, $state) = @_;

	my $name = $handle->{name};

	my $location = OpenBSD::PackageLocator->find($name, $state->{arch});
	if (!$location) {
		print $state->deptree_header($name);
		$handle->set_error(NOT_FOUND);
		$handle->{tweaked} = 
		    OpenBSD::Add::tweak_package_status($handle->pkgname, 
			$state);
		if (!$handle->{tweaked}) {
			print "Can't find $name\n";
		}
		return;
	}
	$handle->{location} = $location;
	$handle->{pkgname} = $location->name;
}

sub complete
{
	my ($handle, $state) = @_;

	return if $handle->has_error;

	if (!defined $handle->{location}) {
		$handle->get_location($state);
	}
	if (!defined $handle->{plist}) {
		$handle->get_plist($state);
	}
}

1;