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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
# ex:ts=8 sw=4:
# $OpenBSD: AddDelete.pm,v 1.76 2017/03/01 10:35:24 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}, $state) 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->finish($state);
$state->progress->clear;
$state->log->dump;
$self->finish_display($state);
if ($state->verbose >= 2 || $state->{size_only} ||
$state->defines('tally')) {
$state->vstat->tally;
}
$state->say("Extracted #1 from #2",
$state->{stats}{donesize},
$state->{stats}{totsize})
if defined $state->{stats} and $state->verbose;
# show any error, and show why we died...
rethrow $dielater;
};
if ($state->defines('debug')) {
&$do;
} else {
try {
&$do;
} catch {
$state->errsay("#1: #2", $0, $_);
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;
}
# nothing to do
sub tweak_list
{
}
sub process_setlist
{
my ($self, $state) = @_;
$state->tracker->todo(@{$state->{setlist}});
# this is the actual very small loop that processes all sets
while (my $set = shift @{$state->{setlist}}) {
$state->status->what->set($set);
$set = $set->real_set;
next if $set->{finished};
$state->progress->set_header('Checking packages');
unshift(@{$state->{setlist}}, $self->process_set($set, $state));
$self->tweak_list($state);
}
}
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) = @_;
$state->{extra_stats} = 0;
$state->{opt}{V} = sub {
$state->{extra_stats}++;
};
$state->{no_exports} = 1;
$state->add_interactive_options;
$state->SUPER::handle_options($opt_string.'aciInqsVB:', @usage);
if ($state->opt('s')) {
$state->{not} = 1;
}
# XXX RequiredBy
$main::not = $state->{not};
$state->{localbase} = $state->opt('L') // OpenBSD::Paths->localbase;
$ENV{PATH} = join(':',
'/bin',
'/sbin',
'/usr/bin',
'/usr/sbin',
'/usr/X11R6/bin',
"$state->{localbase}/bin",
"$state->{localbase}/sbin");
$state->{size_only} = $state->opt('s');
$state->{quick} = $state->opt('q');
$state->{extra} = $state->opt('c');
$state->{automatic} = $state->opt('a') // 0;
$ENV{'PKG_DELETE_EXTRA'} = $state->{extra} ? "Yes" : "No";
if ($state->{not}) {
$state->{loglevel} = 0;
}
$state->{loglevel} //= 1;
if ($state->{loglevel}) {
require Sys::Syslog;
Sys::Syslog::openlog($state->{cmd}, "nofatal");
}
$state->{wantntogo} = $state->{extra_stats};
}
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->SUPER::init(@_);
$self->{export_level}++;
}
sub syslog
{
my $self = shift;
return unless $self->{loglevel};
Sys::Syslog::syslog('info', $self->f(@_));
}
sub ntodo
{
my ($state, $offset) = @_;
return $state->tracker->sets_todo($offset);
}
# one-level dependencies tree, for nicer printouts
sub build_deptree
{
my ($state, $set, @deps) = @_;
if (defined $state->{deptree}->{$set}) {
$set = $state->{deptree}->{$set};
}
for my $dep (@deps) {
$state->{deptree}->{$dep} = $set unless
defined $state->{deptree}->{$dep};
}
}
sub deptree_header
{
my ($state, $pkg) = @_;
if (defined $state->{deptree}->{$pkg}) {
my $s = $state->{deptree}->{$pkg}->real_set;
if ($s eq $pkg) {
delete $state->{deptree}->{$pkg};
} else {
return $s->short_print.':';
}
}
return '';
}
sub vstat
{
my $self = shift;
return $self->{vstat};
}
sub log
{
my $self = shift;
if (@_ == 0) {
return $self->{l};
} else {
$self->{l}->say(@_);
}
}
sub run_quirks
{
my ($state, $sub) = @_;
if (!exists $state->{quirks}) {
eval {
require OpenBSD::Quirks;
# interface version number.
$state->{quirks} = OpenBSD::Quirks->new(1);
};
if ($@) {
my $show = $state->verbose >= 2;
if (!$show) {
my $l = $state->repo->installed->match_locations(OpenBSD::Search::Stem->new('quirks'));
$show = @$l > 0;
}
$state->errsay("Can't load quirk: #1", $@) if $show;
# XXX cache that this didn't work
$state->{quirks} = undef;
}
}
if (defined $state->{quirks}) {
eval {
&$sub($state->{quirks});
};
if ($@) {
$state->errsay("Bad quirk: #1", $@);
}
}
}
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) {
if (!$is_quirks) {
$state->errsay("Can't find #1", $name);
$state->run_quirks(
sub {
my $quirks = shift;
$quirks->filter_obsolete([$name], $state);
});
}
return undef;
} elsif (@$list == 1) {
return $list->[0];
}
my %h = map {($_->name, $_)} @$list;
if ($state->is_interactive) {
$h{'<None>'} = undef;
$state->progress->clear;
my $result = $state->ask_list("Ambiguous: choose package for $name", sort keys %h);
return $h{$result};
} else {
$state->errsay("Ambiguous: #1 could be #2",
$name, join(' ', keys %h));
return undef;
}
}
sub status
{
my $self = shift;
return $self->{status};
}
sub replacing
{
my $self = shift;
return $self->{replacing};
}
OpenBSD::Auto::cache(ldconfig,
sub {
my $self = shift;
return OpenBSD::LdConfig->new($self);
});
# if we're not running as root, allow some stuff when not under /usr/local
sub allow_nonroot
{
my ($state, $path) = @_;
return $state->defines('nonroot') &&
$path !~ m,^\Q$state->{localbase}/\E,;
}
sub make_path
{
my ($state, $path, $fullname) = @_;
require File::Path;
if ($state->allow_nonroot($fullname)) {
eval {
File::Path::mkpath($path);
};
} else {
File::Path::mkpath($path);
}
}
# 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) {
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;
|