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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
# ex:ts=8 sw=4:
# $OpenBSD: AddDelete.pm,v 1.43 2010/12/24 09:09:54 espie Exp $
#
# Copyright (c) 2007-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;
# common framework, let's place most everything in there
package main;
our $not;
package OpenBSD::AddDelete;
use OpenBSD::Error;
use OpenBSD::Paths;
use OpenBSD::PackageInfo;
use OpenBSD::AddCreateDelete;
sub do_the_main_work
{
my ($self, $state) = @_;
if ($state->{bad}) {
return;
}
my $handler = sub { $state->fatal("Caught SIG#1", shift); };
local $SIG{'INT'} = $handler;
local $SIG{'QUIT'} = $handler;
local $SIG{'HUP'} = $handler;
local $SIG{'KILL'} = $handler;
local $SIG{'TERM'} = $handler;
if ($state->defines('debug')) {
$self->main($state);
} else {
eval { $self->main($state); };
}
my $dielater = $@;
return $dielater;
}
sub framework
{
my ($self, $state) = @_;
my $do = sub {
lock_db($state->{not}) unless $state->defines('nolock');
$state->check_root;
$self->process_parameters($state);
my $dielater = $self->do_the_main_work($state);
# cleanup various things
$state->{recorder}->cleanup($state);
$state->ldconfig->ensure;
OpenBSD::PackingElement::Fontdir::finish_fontdirs($state);
$state->progress->clear;
$state->log->dump;
$self->finish_display($state);
if ($state->verbose >= 2 || $state->{size_only} ||
$state->defines('tally')) {
$state->vstat->tally;
}
# show any error, and show why we died...
rethrow $dielater;
};
if ($state->defines('debug')) {
&$do;
} else {
try {
&$do;
} catch {
print STDERR "$0: $_\n";
OpenBSD::Handler->reset;
if ($_ =~ m/^Caught SIG(\w+)/o) {
kill $1, $$;
}
$state->{bad}++;
};
}
}
sub parse_and_run
{
my ($self, $cmd) = @_;
my $state = $self->new_state($cmd);
$state->handle_options;
local $SIG{'INFO'} = sub { $state->status->print($state); };
$self->framework($state);
return $state->{bad} != 0;
}
package OpenBSD::SharedItemsRecorder;
sub new
{
my $class = shift;
return bless {}, $class;
}
sub is_empty
{
my $self = shift;
return !(defined $self->{dirs} or defined $self->{users} or
defined $self->{groups});
}
sub cleanup
{
my ($self, $state) = @_;
return if $self->is_empty or $state->{not};
require OpenBSD::SharedItems;
OpenBSD::SharedItems::cleanup($self, $state);
}
package OpenBSD::AddDelete::State;
use OpenBSD::Vstat;
use OpenBSD::Log;
our @ISA = qw(OpenBSD::AddCreateDelete::State);
sub handle_options
{
my ($state, $opt_string, @usage) = @_;
# backward compatibility
$state->{opt}{F} = sub {
for my $o (split /\,/o, shift) {
$state->{subst}->add($o, 1);
}
};
$state->{no_exports} = 1;
$state->SUPER::handle_options($opt_string.'ciInqsB:F:', @usage);
if ($state->opt('s')) {
$state->{not} = 1;
}
# XXX RequiredBy
$main::not = $state->{not};
$state->{interactive} = $state->opt('i');
$state->{localbase} = $state->opt('L') // OpenBSD::Paths->localbase;
$state->{size_only} = $state->opt('s');
$state->{quick} = $state->opt('q') || $state->config->istrue("nochecksum");
$state->{extra} = $state->opt('c');
$state->{dont_run_scripts} = $state->opt('I');
$ENV{'PKG_DELETE_EXTRA'} = $state->{extra} ? "Yes" : "No";
}
sub init
{
my $self = shift;
$self->{l} = OpenBSD::Log->new($self);
$self->{vstat} = OpenBSD::Vstat->new($self);
$self->{status} = OpenBSD::Status->new;
$self->{recorder} = OpenBSD::SharedItemsRecorder->new;
$self->{v} = 0;
$self->{wantntogo} = $self->config->istrue("ntogo");
$self->SUPER::init(@_);
$self->{export_level}++;
}
sub ntogo
{
my ($self, $offset) = @_;
return $self->{wantntogo} ?
$self->progress->ntogo($self, $offset) :
$self->f("ok");
}
sub ntogo_string
{
my ($self, $offset) = @_;
return $self->todo($offset // 0);
}
sub vstat
{
my $self = shift;
return $self->{vstat};
}
sub log
{
my $self = shift;
if (@_ == 0) {
return $self->{l};
} else {
$self->{l}->say(@_);
}
}
sub vsystem
{
my $self = shift;
my $verbose = $self;
if ($self->verbose < 2) {
$self->system(@_);
} else {
$self->verbose_system(@_);
}
}
sub system
{
my $self = shift;
$self->progress->clear;
$self->SUPER::system(@_);
}
sub check_root
{
my $state = shift;
if ($< && !$state->defines('nonroot')) {
if ($state->{not}) {
$state->errsay("#1 should be run as root",
$state->{cmd}) if $state->verbose;
} else {
$state->fatal("#1 must be run as root", $state->{cmd});
}
}
}
sub choose_location
{
my ($state, $name, $list, $is_quirks) = @_;
if (@$list == 0) {
$state->errsay("Can't find #1", $name) unless $is_quirks;
return undef;
} elsif (@$list == 1) {
return $list->[0];
}
my %h = map {($_->name, $_)} @$list;
if ($state->{interactive}) {
require OpenBSD::Interactive;
$h{'<None>'} = undef;
$state->progress->clear;
my $result = $state->ask_list("Ambiguous: choose package for $name", 1, sort keys %h);
return $h{$result};
} else {
$state->errsay("Ambiguous: #1 could be #2",
$name, join(' ', keys %h));
return undef;
}
}
sub confirm
{
my ($state, $prompt, $default) = @_;
return 0 if !$state->{interactive};
require OpenBSD::Interactive;
return OpenBSD::Interactive::confirm($prompt, $default);
}
sub ask_list
{
my ($state, $prompt, $interactive, @values) = @_;
require OpenBSD::Interactive;
return OpenBSD::Interactive::ask_list($prompt, $interactive, @values);
}
sub status
{
my $self = shift;
return $self->{status};
}
sub updateset
{
my $self = shift;
require OpenBSD::UpdateSet;
return OpenBSD::UpdateSet->new($self);
}
sub updateset_with_new
{
my ($self, $pkgname) = @_;
return $self->updateset->add_newer(
OpenBSD::Handle->create_new($pkgname));
}
sub updateset_from_location
{
my ($self, $location) = @_;
return $self->updateset->add_newer(
OpenBSD::Handle->from_location($location));
}
# those are required for makewhatis integration
sub picky
{
return shift->{picky};
}
sub testmode
{
return shift->{testmode};
}
sub check_dir
{
my ($self, $dir) = @_;
unless (-d $dir) {
$self->fatal("#1: #2 is not a directory", $0, $dir);
}
}
OpenBSD::Auto::cache(ldconfig,
sub {
my $self = shift;
return OpenBSD::LdConfig->new($self);
});
# this is responsible for running ldconfig when needed
package OpenBSD::LdConfig;
sub new
{
my ($class, $state) = @_;
bless { state => $state, todo => 0 }, $class;
}
# called once to figure out which directories are actually used
sub init
{
my $self = shift;
my $state = $self->{state};
my $destdir = $state->{destdir};
$self->{ldconfig} = [OpenBSD::Paths->ldconfig];
$self->{path} = {};
if ($destdir ne '') {
unshift @{$self->{ldconfig}}, OpenBSD::Paths->chroot, '--',
$destdir;
}
open my $fh, "-|", @{$self->{ldconfig}}, "-r";
if (defined $fh) {
my $_;
while (<$fh>) {
if (m/^\s*search directories:\s*(.*?)\s*$/o) {
for my $d (split(/\:/o, $1)) {
$self->{path}{$d} = 1;
}
last;
}
}
close($fh);
} else {
$state->errsay("Can't find ldconfig");
}
}
# called from libs to figure out whether ldconfig should be rerun
sub mark_directory
{
my ($self, $name) = @_;
if (!defined $self->{path}) {
$self->init;
}
require File::Basename;
my $d = File::Basename::dirname($name);
if ($self->{path}{$d}) {
$self->{todo} = 1;
}
}
# call before running any command (or at end) to run ldconfig just in time
sub ensure
{
my $self = shift;
if ($self->{todo}) {
my $state = $self->{state};
$state->vsystem(@{$self->{ldconfig}}, "-R")
unless $state->{not};
$self->{todo} = 0;
}
}
# the object that gets displayed during status updates
package OpenBSD::Status;
sub print
{
my ($self, $state) = @_;
my $what = $self->{what};
$what //= "Processing";
my $object;
if (defined $self->{object}) {
$object = $self->{object};
} elsif (defined $self->{set}) {
$object = $self->{set}->print;
} else {
$object = "Parameters";
}
$state->say($what." #1 (#2)", $object, $state->ntogo_string);
if ($state->defines('carp')) {
require Carp;
Carp::cluck("currently here");
}
}
sub set
{
my ($self, $set) = @_;
delete $self->{object};
$self->{set} = $set;
return $self;
}
sub object
{
my ($self, $object) = @_;
delete $self->{set};
$self->{object} = $object;
return $self;
}
sub what
{
my ($self, $what) = @_;
$self->{what} = $what;
return $self;
}
sub new
{
my $class = shift;
bless {}, $class;
}
1;
|