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
|
# ex:ts=8 sw=4:
# $OpenBSD: Vstat.pm,v 1.30 2007/05/30 14:04:51 espie Exp $
#
# Copyright (c) 2003-2007 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.
# Provides stat and statfs-like functions for package handling.
# allows user to add/remove files.
# uses mount and df directly for now.
use strict;
use warnings;
package OpenBSD::Vstat;
use File::Basename;
my $devinfo = {};
my $devinfo2 = {};
my $virtual = {};
my $giveup;
sub create_device($)
{
my $dev = shift;
my $n = $devinfo->{$dev};
if (!defined $n) {
$n = { dev => $dev, used => 0, delayed => 0, problems => 0 };
bless $n, "OpenBSD::Vstat::MountPoint";
$devinfo->{$dev} = $n;
}
return $n;
}
sub init_devices()
{
delete $ENV{'BLOCKSIZE'};
open(my $cmd1, "/sbin/mount|") or print STDERR "Can't run mount\n";
while (<$cmd1>) {
chomp;
if (m/^(.*?)\s+on\s+\/.*?\s+type\s+.*?(?:\s+\((.*?)\))?$/) {
my ($dev, $opts) = ($1, $2);
my $i = create_device($dev);
next unless defined $i;
next unless defined $opts;
for my $o (split /,\s*/, $opts) {
if ($o eq 'read-only') {
$i->{ro} = 1;
} elsif ($o eq 'nodev') {
$i->{nodev} = 1;
} elsif ($o eq 'nosuid') {
$i->{nosuid} = 1;
} elsif ($o eq 'noexec') {
$i->{noexec} = 1;
}
}
} else {
print STDERR "Can't parse mount line: $_\n";
}
}
close($cmd1) or print STDERR "Error running mount: $!\n";
$giveup = { used => 0, dev => '???' };
bless $giveup, "OpenBSD::Vstat::Failsafe";
}
sub ask_df($)
{
my $fname = shift;
my $info = $giveup;
open(my $cmd2, "-|", "/bin/df", $fname)
or print STDERR "Can't run df\n";
my $blocksize = 512;
while (<$cmd2>) {
chomp;
if (m/^Filesystem\s+(\d+)\-blocks/) {
$blocksize = $1;
} elsif (m/^(.*?)\s+\d+\s+\d+\s+(\-?\d+)\s+\d+\%\s+\/.*?$/) {
my ($dev, $avail) = ($1, $2);
$info = $devinfo->{$dev};
if (!defined $info) {
$info = create_device($dev);
}
$info->{avail} = $avail;
$info->{blocksize} = $blocksize;
}
}
close($cmd2) or print STDERR "Error running df: $!\n";
return $info;
}
init_devices();
sub filestat($);
sub filestat($)
{
my $fname = shift;
my $dev = (stat $fname)[0];
if (!defined $dev && $fname ne '/') {
return filestat(dirname($fname));
}
if (!defined $dev) {
return $giveup;
} else {
if (!defined $devinfo2->{$dev}) {
return $devinfo2->{$dev} = ask_df($fname);
} else {
return $devinfo2->{$dev};
}
}
}
sub vexists($)
{
my $name = shift;
if (defined $virtual->{$name}) {
return $virtual->{$name};
} else {
return -e $name;
}
}
sub account_for($$)
{
my ($name, $size) = @_;
my $e = filestat($name);
$e->{used} += $size;
return $e;
}
sub account_later($$)
{
my ($name, $size) = @_;
my $e = filestat($name);
$e->{delayed} += $size;
return $e;
}
sub synchronize
{
while (my ($k, $v) = each %$devinfo) {
$v->{used} += $v->{delayed};
$v->{delayed} = 0;
}
}
sub add($$;$)
{
my ($name, $size, $value) = @_;
if (defined $value) {
$virtual->{$name} = $value;
} else {
$virtual->{$name} = 1;
}
return defined($size) ? account_for($name, $size) : undef;
}
sub remove($$)
{
my ($name, $size) = @_;
$virtual->{$name} = 0;
return defined($size) ? account_later($name, -$size) : undef;
}
sub tally()
{
while (my ($device, $data) = each %$devinfo) {
if ($data->{used} != 0) {
print $device, ": ", $data->{used}, " bytes";
my $avail = $data->avail();
if ($avail < 0) {
print " (missing ", int(-$avail+1), " blocks)";
}
print "\n";
}
}
}
package OpenBSD::Vstat::MountPoint;
sub avail
{
my $self = shift;
return $self->{avail} - $self->{used}/$self->{blocksize};
}
sub report_ro
{
my ($s, $state, $fname) = @_;
if ($state->{very_verbose} or ++($s->{problems}) < 4) {
print STDERR "Error: ", $s->{dev},
" is read-only ($fname)\n";
} elsif ($s->{problems} == 4) {
print STDERR "Error: ... more files on ", $s->{dev}, "\n";
}
$state->{problems}++;
}
sub report_overflow
{
my ($s, $state, $fname) = @_;
if ($state->{very_verbose} or ++($s->{problems}) < 4) {
print STDERR "Error: ", $s->{dev},
" is not large enough ($fname)\n";
} elsif ($s->{problems} == 4) {
print STDERR "Error: ... more files do not fit on ",
$s->{dev}, "\n";
}
$state->{problems}++;
$state->{overflow} = 1;
}
sub report_noexec
{
my ($s, $state, $fname) = @_;
print STDERR "Error: ", $s->{dev}, " is noexec ($fname)\n";
$state->{problems}++;
}
package OpenBSD::Vstat::Failsafe;
our @ISA=(qw(OpenBSD::Vstat::MountPoint));
sub avail
{
return 1;
}
# Here we stuff things common to pkg_add and pkg_delete which do not warrant
# their own file yet.
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);
}
# fairly non-descriptive name. Used to store various package information
# during installs and updates.
package OpenBSD::Handle;
use constant {
BAD_PACKAGE => 1,
CANT_INSTALL => 2,
ALREADY_INSTALLED => 3,
NOT_FOUND => 4
};
sub new
{
my $class = shift;
return bless {}, $class;
}
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};
}
package OpenBSD::UpdateSet;
sub new
{
my $class = shift;
return bless {}, $class;
}
sub add_newer
{
my ($self, @handles) = @_;
push(@{$self->{newer}}, @handles);
}
sub add_older
{
my ($self, @handles) = @_;
push(@{$self->{older}}, @handles);
}
sub newer
{
my $self =shift;
return @{$self->{newer}};
}
sub older
{
my $self = shift;
# XXX in `combined' updates, some dependencies may remove extra
# packages, so we do a double-take on the list of packages we
# are actually replacing... for now, until we merge update sets.
require OpenBSD::PackageInfo;
my @l = ();
for my $h (@{$self->{older}}) {
if (OpenBSD::PackageInfo::is_installed($h->{pkgname})) {
push(@l, $h);
}
}
return @l;
}
sub print
{
my $self = shift;
my @l = ();
if (defined $self->{newer}) {
push(@l, "installing", map {$_->{pkgname}} $self->newer);
}
if (defined $self->{older} && @{$self->{older}} > 0) {
push(@l, "deinstalling", map {$_->{pkgname}} $self->older);
}
return join(' ', @l);
}
sub validate_plists
{
my ($self, $state) = @_;
$state->{problems} = 0;
for my $o ($self->older) {
require OpenBSD::Delete;
$o->{totsize} =
OpenBSD::Delete::validate_plist($o->{plist}, $state);
}
$state->{colliding} = [];
for my $n ($self->newer) {
require OpenBSD::Add;
$n->{totsize} =
OpenBSD::Add::validate_plist($n->{plist}, $state);
}
if (@{$state->{colliding}} > 0) {
require OpenBSD::CollisionReport;
OpenBSD::CollisionReport::collision_report($state->{colliding}, $state);
}
if (defined $state->{overflow}) {
OpenBSD::Vstat::tally();
}
if ($state->{problems}) {
require OpenBSD::Error;
OpenBSD::Error::Fatal "fatal issues in ", $self->print;
}
OpenBSD::Vstat::synchronize();
}
# temporary shortcut
sub handle
{
my $self = shift;
if (defined $self->{newer}) {
return $self->{newer}[0];
} else {
return undef;
}
}
package OpenBSD::PackingElement;
sub mark_progress
{
}
package OpenBSD::PackingElement::FileBase;
sub mark_progress
{
my ($self, $donesize, $totsize) = @_;
return unless defined $self->{size};
require OpenBSD::ProgressMeter;
$$donesize += $self->{size};
OpenBSD::ProgressMeter::show($$donesize, $totsize);
}
1;
|