diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2010-01-03 09:30:41 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2010-01-03 09:30:41 +0000 |
commit | ca53d7e3f314417f18e246b9ff8287527a3aa95e (patch) | |
tree | f7822d764ee6895cec555d625cc1977b2dbd2512 /usr.sbin/pkg_add/OpenBSD/Vstat.pm | |
parent | 31b1a8edf6a01da84153d2a24d882624071b39e8 (diff) |
separate code into two classes, cleaner.
Diffstat (limited to 'usr.sbin/pkg_add/OpenBSD/Vstat.pm')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Vstat.pm | 191 |
1 files changed, 108 insertions, 83 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/Vstat.pm b/usr.sbin/pkg_add/OpenBSD/Vstat.pm index 6717e60219b..e5762d692b4 100644 --- a/usr.sbin/pkg_add/OpenBSD/Vstat.pm +++ b/usr.sbin/pkg_add/OpenBSD/Vstat.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Vstat.pm,v 1.52 2010/01/03 00:59:31 espie Exp $ +# $OpenBSD: Vstat.pm,v 1.53 2010/01/03 09:30:40 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -36,7 +36,7 @@ sub stat if (!defined $dev && $fname ne '/') { return $self->stat(dirname($fname)); } - return OpenBSD::MountPoint->find($dev, $fname, $self->{state}); + return OpenBSD::Mounts->find($dev, $fname, $self->{state}); } sub account_for @@ -77,7 +77,7 @@ sub synchronize { my $self = shift; - OpenBSD::MountPoint->synchronize; + OpenBSD::Mounts->synchronize; if ($self->{state}->{not}) { # this is the actual stacking case: in pretend mode, # I have to put a second vfs on top @@ -97,7 +97,7 @@ sub drop_changes { my $self = shift; - OpenBSD::MountPoint->drop_changes; + OpenBSD::Mounts->drop_changes; # drop the top layer $self->{v}[0] = {}; } @@ -124,68 +124,21 @@ sub tally { my $self = shift; - OpenBSD::MountPoint->tally($self->{state}); + OpenBSD::Mounts->tally($self->{state}); } -package OpenBSD::MountPoint; +package OpenBSD::Mounts; my $devinfo; my $devinfo2; my $giveup; -sub parse_opts -{ - my ($self, $opts) = @_; - for my $o (split /\,\s*/o, $opts) { - if ($o eq 'read-only') { - $self->{ro} = 1; - } elsif ($o eq 'nodev') { - $self->{nodev} = 1; - } elsif ($o eq 'nosuid') { - $self->{nosuid} = 1; - } elsif ($o eq 'noexec') { - $self->{noexec} = 1; - } - } -} - -sub ro -{ - return shift->{ro}; -} - -sub nodev -{ - return shift->{nodev}; -} - -sub nosuid -{ - return shift->{nosuid}; -} - -sub noexec -{ - return shift->{noexec}; -} - -sub create -{ - my ($class, $dev, $opts) = @_; - my $n = bless { commited_use => 0, used => 0, delayed => 0, - hw => 0, dev => $dev }, $class; - if (defined $opts) { - $n->parse_opts($opts); - } - return $n; -} - sub new { my ($class, $dev, $opts) = @_; if (!defined $devinfo->{$dev}) { - $devinfo->{$dev} = $class->create($dev, $opts); + $devinfo->{$dev} = OpenBSD::MountPoint->new($dev, $opts); } return $devinfo->{$dev}; } @@ -212,7 +165,7 @@ sub run sub ask_mount { - my $state = shift; + my ($class, $state) = shift; $giveup = OpenBSD::MountPoint::Fail->new; delete $ENV{'BLOCKSIZE'}; @@ -221,7 +174,7 @@ sub ask_mount chomp; if (m/^(.*?)\s+on\s+\/.*?\s+type\s+.*?(?:\s+\((.*?)\))?$/o) { my ($dev, $opts) = ($1, $2); - OpenBSD::MountPoint->new($dev, $opts); + $class->new($dev, $opts); } else { $state->errsay("Can't parse mount line: $_"); } @@ -230,12 +183,12 @@ sub ask_mount sub ask_df { - my ($fname, $state) = @_; + my ($class, $fname, $state) = @_; my $info = $giveup; my $blocksize = 512; - ask_mount($state) if !defined $devinfo; + $class->ask_mount($state) if !defined $devinfo; run($state, OpenBSD::Paths->df, "--", $fname, sub { my $_ = shift; chomp; @@ -245,7 +198,7 @@ sub ask_df my ($dev, $avail) = ($1, $2); $info = $devinfo->{$dev}; if (!defined $info) { - $info = OpenBSD::MountPoint->new($dev); + $info = $class->new($dev); } $info->{avail} = $avail; $info->{blocksize} = $blocksize; @@ -262,11 +215,84 @@ sub find return $giveup; } if (!defined $devinfo2->{$dev}) { - $devinfo2->{$dev} = ask_df($fname, $state); + $devinfo2->{$dev} = $class->ask_df($fname, $state); } return $devinfo2->{$dev}; } +sub synchronize +{ + for my $v (values %$devinfo2) { + $v->synchronize; + } +} + +sub drop_changes +{ + for my $v (values %$devinfo2) { + $v->drop_changes; + } +} + +sub tally +{ + my ($self, $state) = @_; + + for my $v ((sort {$a->name cmp $b->name } values %$devinfo2), $giveup) { + $v->tally($state); + } +} + +package OpenBSD::MountPoint; + +sub parse_opts +{ + my ($self, $opts) = @_; + for my $o (split /\,\s*/o, $opts) { + if ($o eq 'read-only') { + $self->{ro} = 1; + } elsif ($o eq 'nodev') { + $self->{nodev} = 1; + } elsif ($o eq 'nosuid') { + $self->{nosuid} = 1; + } elsif ($o eq 'noexec') { + $self->{noexec} = 1; + } + } +} + +sub ro +{ + return shift->{ro}; +} + +sub nodev +{ + return shift->{nodev}; +} + +sub nosuid +{ + return shift->{nosuid}; +} + +sub noexec +{ + return shift->{noexec}; +} + +sub new +{ + my ($class, $dev, $opts) = @_; + my $n = bless { commited_use => 0, used => 0, delayed => 0, + hw => 0, dev => $dev }, $class; + if (defined $opts) { + $n->parse_opts($opts); + } + return $n; +} + + sub avail { my ($self, $used) = @_; @@ -316,38 +342,37 @@ sub report_noexec sub synchronize { - for my $v (values %$devinfo2) { - if ($v->{used} > $v->{hw}) { - $v->{hw} = $v->{used}; - } - $v->{used} += $v->{delayed}; - $v->{delayed} = 0; - $v->{commited_use} = $v->{used}; + my $v = shift; + + if ($v->{used} > $v->{hw}) { + $v->{hw} = $v->{used}; } + $v->{used} += $v->{delayed}; + $v->{delayed} = 0; + $v->{commited_use} = $v->{used}; } sub drop_changes { - for my $v (values %$devinfo2) { - $v->{used} = $v->{commited_use}; - $v->{delayed} = 0; - } + my $v = shift; + + $v->{used} = $v->{commited_use}; + $v->{delayed} = 0; } sub tally { - my ($self, $state) = @_; - for my $data (values %$devinfo2) { - next if $data->{used} == 0; - $state->print($data->name, ": ", $data->{used}, " bytes"); - my $avail = $data->avail; - if ($avail < 0) { - $state->print(" (missing ", int(-$avail+1), " blocks)"); - } elsif ($data->{hw} > $data->{used}) { - $state->print(" (highwater ", $data->{hw}, " bytes)"); - } - $state->say; + my ($data, $state) = @_; + + return if $data->{used} == 0; + $state->print($data->name, ": ", $data->{used}, " bytes"); + my $avail = $data->avail; + if ($avail < 0) { + $state->print(" (missing ", int(-$avail+1), " blocks)"); + } elsif ($data->{hw} >0 && $data->{hw} > $data->{used}) { + $state->print(" (highwater ", $data->{hw}, " bytes)"); } + $state->say; } package OpenBSD::MountPoint::Fail; @@ -361,7 +386,7 @@ sub avail sub new { my $class = shift; - my $n = $class->SUPER::create('???'); + my $n = $class->SUPER::new('???'); $n->{avail} = 0; return $n; } |