summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/OpenBSD/PkgDelete.pm
blob: c70f145eda89eec48ba817c37941f75722d4c574 (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
#!/usr/bin/perl
# ex:ts=8 sw=4:
# $OpenBSD: PkgDelete.pm,v 1.8 2010/06/30 10:51:04 espie Exp $
#
# Copyright (c) 2003-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 strict;
use warnings;

use OpenBSD::AddDelete;

package OpenBSD::PkgDelete::State;
our @ISA = qw(OpenBSD::AddDelete::State);

sub handle_options
{
	my $state = shift;
	$state->SUPER::handle_options('',
	    '[-cIinqsvx] [-B pkg-destdir] [-D name[=value]] pkg-name [...]');

	my $base = $state->opt('B') // $ENV{'PKG_DESTDIR'} // '';
	if ($base ne '') {
		$base.='/' unless $base =~ m/\/$/o;
	}
	$ENV{'PKG_DESTDIR'} = $base;

	$state->{destdir} = $base;
	if ($base eq '') {
	    $state->{destdirname} = '';
	} else {
	    $state->{destdirname} = '${PKG_DESTDIR}';
	}
}

sub todo
{
	my ($state, $offset) = @_;
	return sprintf("%u/%u", $state->{done} - $offset, $state->{total});
}

package OpenBSD::PkgDelete;
our @ISA = qw(OpenBSD::AddDelete);

use OpenBSD::PackingList;
use OpenBSD::RequiredBy;
use OpenBSD::Delete;
use OpenBSD::PackageInfo;
use OpenBSD::UpdateSet;


my %done;
my $removed;

# Resolve pkg names
my @realnames;
my @todo;

sub process_parameters
{
	my ($self, $state) = @_;
	OpenBSD::PackageInfo::solve_installed_names(\@ARGV, \@realnames,
	    "(removing them all)", $state);

	@todo = OpenBSD::RequiredBy->compute_closure(@realnames);

	if (@todo > @realnames) {
		my $details = $state->verbose >= 2 ||
		    $state->defines('verbosedeps');
		my $show = sub {
			my ($p, $d) = @_;
			$state->say("Can't remove #1".
			    " without also removing:\n#2",
			    join(' ', @$p), join(' ', @$d));
		};
		if ($state->{interactive} || !$details) {
			my %deps = map {($_, 1)} @todo;
			for my $p (@realnames) {
				delete $deps{$p};
			}
			&$show([@realnames], [keys %deps]);
			if (@realnames > 1 && (keys %deps) > 1 &&
			    $state->confirm("Do you want details", 1)) {
				$details = 1;
			}
		}
		if ($details) {
			for my $pkg (@realnames) {
				my @deps = OpenBSD::RequiredBy->compute_closure($pkg);
				next unless @deps > 1;
				@deps = grep {$_ ne $pkg} @deps;
				&$show([$pkg], [@deps]);
			}
		}
		my $them = @todo > 1 ? 'them' : 'it';
		if ($state->defines('dependencies') or
		    $state->confirm("Do you want to remove $them as well", 0)) {
			$state->say("(removing #1 as well)", $them);
		} else {
			$state->{bad}++;
		}
	}
}

sub finish_display
{
}

sub main
{
	my ($self, $state) = @_;

	# and finally, handle the removal
	do {
		$removed = 0;
		if ($state->{not}) {
			$state->status->what("Pretending to delete");
		} else {
			$state->status->what("Deleting");
		}
		$state->{total} = scalar @todo;
		DELETE: for my $pkgname (@todo) {
			if ($done{$pkgname}) {
				next;
			}
			unless (is_installed($pkgname)) {
				$state->errsay("#1 was not installed", $pkgname);
				$done{$pkgname} = 1;
				$removed++;
				next;
			}
			my $r = OpenBSD::RequiredBy->new($pkgname);
			if ($r->list > 0) {
				if ($state->defines('baddepend')) {
					for my $p ($r->list) {
						if ($done{$p}) {
							$r->delete($p);
						} else {
							next DELETE;
						}
					}
				} else {
					next;
				}
			}
			my $info = sub {
			};

			$state->status->object($pkgname);
			if (!$state->progress->set_header($pkgname)) {
				$state->say($state->{not} ?
				    "Pretending to delete #1" :
				    "Deleting #1",
				    $pkgname) if $state->verbose;
			}
			$state->log->set_context('-'.$pkgname);
			OpenBSD::Delete::delete_package($pkgname, $state);
			$done{$pkgname} = 1;
			$removed++;
		}
	} while ($removed);
}

sub new_state
{
	my ($self, $cmd) = @_;
	return OpenBSD::PkgDelete::State->new($cmd);
}

1;