diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2010-06-15 08:26:40 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2010-06-15 08:26:40 +0000 |
commit | e9543a25f760416cd896f98c7a8e9ebb72b0f956 (patch) | |
tree | 790547109c666da81ad863b3d1f2607918730f05 | |
parent | dc2af675bfd1f4eb14a47aa97b59c8b92d6e5419 (diff) |
more printing changes.
move log operations into their own file, since they're used outside of
pkg_add/pkg_delete by pkg_check.
move all system stuff to State, so that printing can be localized/controlled.
Create primitives _print, _errprint, _fatal for overriding output, without
localization.
Accordingly, move a lot of code out of Error. We should also move Unlink and
Copy eventually.
-rw-r--r-- | usr.sbin/pkg_add/Makefile | 3 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Add.pm | 12 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/AddDelete.pm | 46 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Error.pm | 127 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Log.pm | 119 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PackingElement.pm | 4 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PkgCheck.pm | 20 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/State.pm | 140 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/x509.pm | 6 |
9 files changed, 288 insertions, 189 deletions
diff --git a/usr.sbin/pkg_add/Makefile b/usr.sbin/pkg_add/Makefile index 37c1d7bd5fe..284e3d43da4 100644 --- a/usr.sbin/pkg_add/Makefile +++ b/usr.sbin/pkg_add/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.65 2010/06/09 07:26:01 espie Exp $ +# $OpenBSD: Makefile,v 1.66 2010/06/15 08:26:39 espie Exp $ .include <bsd.own.mk> @@ -27,6 +27,7 @@ PACKAGES= \ OpenBSD/Interactive.pm \ OpenBSD/LibSpec.pm \ OpenBSD/LibSpec/Build.pm \ + OpenBSD/Log.pm \ OpenBSD/Mtree.pm \ OpenBSD/OldLibs.pm \ OpenBSD/PackageInfo.pm \ diff --git a/usr.sbin/pkg_add/OpenBSD/Add.pm b/usr.sbin/pkg_add/OpenBSD/Add.pm index 8dae93f357b..3f5e082c67f 100644 --- a/usr.sbin/pkg_add/OpenBSD/Add.pm +++ b/usr.sbin/pkg_add/OpenBSD/Add.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Add.pm,v 1.110 2010/06/09 07:26:01 espie Exp $ +# $OpenBSD: Add.pm,v 1.111 2010/06/15 08:26:39 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -179,7 +179,7 @@ sub copy_info sub set_modes { - my ($self, $name) = @_; + my ($self, $state, $name) = @_; if (defined $self->{owner} || defined $self->{group}) { require OpenBSD::IdCache; @@ -207,7 +207,7 @@ sub set_modes if ($v =~ m/^\d+$/o) { chmod oct($v), $name; } else { - System(OpenBSD::Paths->chmod, $self->{mode}, $name); + $state->system(OpenBSD::Paths->chmod, $self->{mode}, $name); } } } @@ -404,7 +404,7 @@ sub install } } - $self->set_modes($destdir.$fullname); + $self->set_modes($state, $destdir.$fullname); } sub prepare_to_extract @@ -523,7 +523,7 @@ sub install if (!copy($origname, $filename)) { $state->errsay("File #1 could not be installed:\n\t#2", $filename, $!); } - $self->set_modes($filename); + $self->set_modes($state, $filename); if ($state->verbose >= 2) { $state->say("installed #1 from #2", $filename, $origname); @@ -605,7 +605,7 @@ sub install $state->say("new directory #1", $destdir.$fullname) if $state->verbose >= 5; return if $state->{not}; File::Path::mkpath($destdir.$fullname); - $self->set_modes($destdir.$fullname); + $self->set_modes($state, $destdir.$fullname); } package OpenBSD::PackingElement::Exec; diff --git a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm index 7c6b695ad4b..3f8f03991dc 100644 --- a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: AddDelete.pm,v 1.25 2010/06/09 08:13:19 espie Exp $ +# $OpenBSD: AddDelete.pm,v 1.26 2010/06/15 08:26:39 espie Exp $ # # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org> # @@ -152,30 +152,15 @@ sub cleanup OpenBSD::SharedItems::cleanup($self, $state); } -package OpenBSD::Log; -use OpenBSD::Error; -our @ISA = qw(OpenBSD::Error); - -sub set_context -{ - &OpenBSD::Error::set_pkgname; -} - -sub dump -{ - &OpenBSD::Error::delayed_output; -} - - package OpenBSD::AddDelete::State; -use OpenBSD::Error; use OpenBSD::Vstat; +use OpenBSD::Log; our @ISA = qw(OpenBSD::AddCreateDelete::State); sub init { my $self = shift; - $self->{l} = OpenBSD::Log->new; + $self->{l} = OpenBSD::Log->new($self); $self->{vstat} = OpenBSD::Vstat->new($self); $self->{status} = OpenBSD::Status->new; $self->{recorder} = OpenBSD::SharedItemsRecorder->new; @@ -209,29 +194,32 @@ sub log if (@_ == 0) { return $self->{l}; } else { - $self->{l}->print($self->f(@_), "\n"); + $self->{l}->say(@_); } } sub vsystem { my $self = shift; - $self->progress->clear; - OpenBSD::Error::VSystem($self->verbose >= 2, @_); + my $verbose = $self; + if ($self->verbose < 2) { + $self->system(@_); + } else { + $self->print("Running #1", join(' ', @_)); + my $r = CORE::system(@_); + if ($r != 0) { + $self->say("... failed: #1", $self->child_error); + } else { + $self->say; + } + } } sub system { my $self = shift; $self->progress->clear; - OpenBSD::Error::System(@_); -} - -sub unlink -{ - my $self = shift; - $self->progress->clear; - OpenBSD::Error::Unlink(@_); + $self->SUPER::system(@_); } sub check_root diff --git a/usr.sbin/pkg_add/OpenBSD/Error.pm b/usr.sbin/pkg_add/OpenBSD/Error.pm index 9d078c2c23c..edcd82ec4eb 100644 --- a/usr.sbin/pkg_add/OpenBSD/Error.pm +++ b/usr.sbin/pkg_add/OpenBSD/Error.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Error.pm,v 1.25 2010/05/10 09:17:55 espie Exp $ +# $OpenBSD: Error.pm,v 1.26 2010/06/15 08:26:39 espie Exp $ # # Copyright (c) 2004-2010 Marc Espie <espie@openbsd.org> # @@ -63,8 +63,7 @@ __PACKAGE__->reset; package OpenBSD::Error; require Exporter; our @ISA=qw(Exporter); -our @EXPORT=qw(System VSystem Copy Unlink Fatal Warn Usage set_usage - try throw catch catchall rethrow); +our @EXPORT=qw(Copy Unlink try throw catch catchall rethrow); our ($FileName, $Line, $FullMessage); @@ -128,32 +127,6 @@ sub child_error } } -sub System -{ - my $r = system(@_); - if ($r != 0) { - print "system(", join(", ", @_), ") failed: ", child_error(), - "\n"; - } - return $r; -} - -sub VSystem -{ - my $verbose = shift; - if (!$verbose) { - &System; - } else { - print "Running ", join(' ', @_); - my $r = system(@_); - if ($r != 0) { - print "... failed: ", child_error(), "\n"; - } else { - print "\n"; - } - } -} - sub Copy { require File::Copy; @@ -177,102 +150,6 @@ sub Unlink return $r; } -sub Fatal -{ - croak @_; -} - -sub Warn -{ - print STDERR @_; -} - -sub new -{ - my $class = shift; - bless {messages=>{}, dirs_okay=>{}}, $class; -} - -sub set_pkgname -{ - my ($self, $pkgname) = @_; - $self->{pkgname} = $pkgname; - $self->{output} = $self->{messages}->{$pkgname} //= []; -} - -sub warn -{ - &OpenBSD::Error::print; -} - -sub fatal -{ - my $self = shift; - if (defined $self->{pkgname}) { - unshift @_, $self->{pkgname}, ':'; - } - croak @_; -} - -sub print -{ - my $self = shift; - push(@{$self->{output}}, join('', @_)); -} - -sub delayed_output -{ - my $self = shift; - for my $pkg (sort keys %{$self->{messages}}) { - my $msgs = $self->{messages}->{$pkg}; - if (@$msgs > 0) { - print "--- $pkg -------------------\n"; - print @$msgs; - } - } - $self->{messages} = {}; -} - -sub system -{ - my $self = shift; - if (open(my $grab, "-|", @_)) { - my $_; - while (<$grab>) { - $self->print($_); - } - if (!close $grab) { - $self->print("system(", join(", ", @_), ") failed: $! ", - child_error(), "\n"); - } - return $?; - } else { - $self->print("system(", join(", ", @_), - ") was not run: $!", child_error(), "\n"); - } -} - -my @usage_line; - -sub set_usage -{ - @usage_line = @_; -} - -sub Usage -{ - my $code = 0; - if (@_) { - print STDERR "$0: ", @_, "\n"; - $code = 1; - } - print STDERR "Usage: ", shift(@usage_line), "\n"; - for my $l (@usage_line) { - print STDERR " $l\n"; - } - exit($code); -} - sub dienow { my ($error, $handler) = @_; diff --git a/usr.sbin/pkg_add/OpenBSD/Log.pm b/usr.sbin/pkg_add/OpenBSD/Log.pm new file mode 100644 index 00000000000..0cac688b5f4 --- /dev/null +++ b/usr.sbin/pkg_add/OpenBSD/Log.pm @@ -0,0 +1,119 @@ +# ex:ts=8 sw=4: +# $OpenBSD: Log.pm,v 1.1 2010/06/15 08:26:39 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; + +package OpenBSD::Log; + +sub new +{ + my ($class, $printer) = @_; + bless { p => $printer }, $class; +} + +sub set_context +{ + my ($self, $context) = @_; + $self->{context} = $context; + $self->{output} = $self->{messages}->{$context} //= []; + $self->{erroutput} = $self->{errmessages}->{$context} //= []; +} + +sub f +{ + my $self = shift; + $self->{p}->f(@_); +} + +sub print +{ + my $self = shift; + push(@{$self->{output}}, $self->f(@_)); +} + +sub say +{ + my $self = shift; + push(@{$self->{output}}, $self->f(@_)."\n"); +} + +sub errprint +{ + my $self = shift; + push(@{$self->{erroutput}}, $self->f(@_)); +} + +sub errsay +{ + my $self = shift; + push(@{$self->{erroutput}}, $self->f(@_)."\n"); +} + +sub dump +{ + my $self = shift; + for my $ctxt (sort keys %{$self->{errmessages}}) { + my $msgs = $self->{errmessages}->{$ctxt}; + if (@$msgs > 0) { + $self->{p}->errsay("--- #1 -------------------", $ctxt); + $self->{p}->_errprint(@$msgs); + } + } + $self->{errmessages} = {}; + for my $ctxt (sort keys %{$self->{messages}}) { + my $msgs = $self->{messages}->{$ctxt}; + if (@$msgs > 0) { + $self->{p}->say("--- #1 -------------------", $ctxt); + $self->{p}->_print(@$msgs); + } + } + $self->{messages} = {}; +} + +sub fatal +{ + my $self = shift; + if (defined $self->{context}) { + $self->{p}->_fatal($self->{context}, ":", $self->f(@_)); + } + + $self->{p}->_fatal($self->f(@_)); +} + +sub system +{ + my $self = shift; + if (open(my $grab, "-|", @_)) { + my $_; + while (<$grab>) { + $self->{p}->_print($_); + } + if (!close $grab) { + $self->{p}->say("system(#1) failed: #2 #3", + join(", ", @_), $!, + $self->{p}->child_error); + } + return $?; + } else { + $self->{p}->say("system(#1) was not run: #2 #3", + join(", ", @_), $!, $self->{p}->child_error); + } +} + +1; diff --git a/usr.sbin/pkg_add/OpenBSD/PackingElement.pm b/usr.sbin/pkg_add/OpenBSD/PackingElement.pm index fb7f90eaefa..dc7487a86e5 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackingElement.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackingElement.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackingElement.pm,v 1.179 2010/06/09 07:26:01 espie Exp $ +# $OpenBSD: PackingElement.pm,v 1.180 2010/06/15 08:26:39 espie Exp $ # # Copyright (c) 2003-2010 Marc Espie <espie@openbsd.org> # @@ -1578,7 +1578,7 @@ sub run chmod 0755, $name; return if $state->log->system($name, $pkgname, @args) == 0; if ($state->defines('scripts')) { - $state->log->warn($self->beautify, " script failed\n"); + $state->log->say($self->beautify." script failed"); } else { $state->log->fatal($self->beautify." script failed"); } diff --git a/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm b/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm index 1dbb673e298..48a7e76a8ab 100644 --- a/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm +++ b/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm @@ -1,7 +1,7 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: PkgCheck.pm,v 1.14 2010/06/14 08:16:14 espie Exp $ +# $OpenBSD: PkgCheck.pm,v 1.15 2010/06/15 08:26:39 espie Exp $ # # Copyright (c) 2003-2010 Marc Espie <espie@openbsd.org> # @@ -213,23 +213,11 @@ sub find_dependencies } } -package OpenBSD::Log; -use OpenBSD::Error; -our @ISA = qw(OpenBSD::Error); - -sub set_context -{ - &OpenBSD::Error::set_pkgname; -} - -sub dump -{ - &OpenBSD::Error::delayed_output; -} - package OpenBSD::PkgCheck::State; our @ISA = qw(OpenBSD::AddCreateDelete::State); +use OpenBSD::Log; + sub init { my $self = shift; @@ -243,7 +231,7 @@ sub log if (@_ == 0) { return $self->{l}; } else { - $self->{l}->print($self->f(@_), "\n"); + $self->{l}->say(@_); } } diff --git a/usr.sbin/pkg_add/OpenBSD/State.pm b/usr.sbin/pkg_add/OpenBSD/State.pm index 08b385d6678..3539378bb83 100644 --- a/usr.sbin/pkg_add/OpenBSD/State.pm +++ b/usr.sbin/pkg_add/OpenBSD/State.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: State.pm,v 1.2 2010/06/09 11:57:21 espie Exp $ +# $OpenBSD: State.pm,v 1.3 2010/06/15 08:26:39 espie Exp $ # # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org> # @@ -70,36 +70,54 @@ sub f return $_; } -sub fatal +sub _fatal { my $self = shift; # implementation note: to print "fatal errors" elsewhere, # the way is to eval { croak @_}; and decide what to do with $@. - croak "Fatal error: ", $self->f(@_), "\n"; + croak "Fatal error: ", @_, "\n"; +} + +sub fatal +{ + my $self = shift; + $self->_fatal($self->f(@_)); +} + +sub _print +{ + my $self = shift; + print @_; +} + +sub _errprint +{ + my $self = shift; + print STDERR @_; } sub print { my $self = shift; - print $self->f(@_); + $self->_print($self->f(@_)); } sub say { my $self = shift; - print $self->f(@_), "\n"; + $self-_print($self->f(@_), "\n"); } sub errprint { my $self = shift; - print STDERR $self->f(@_); + $self->_errprint($self->f(@_)); } sub errsay { my $self = shift; - print STDERR $self->f(@_), "\n"; + $self->_errprint($self->f(@_), "\n"); } sub do_options @@ -112,4 +130,112 @@ sub do_options bless sub { $state->usage("#1", $_)}, "OpenBSD::Error::catchall"); } +my @signal_name = (); +sub fillup_names +{ + { + # XXX force autoload + package verylocal; + + require POSIX; + POSIX->import(qw(signal_h)); + } + + for my $sym (keys %POSIX::) { + next unless $sym =~ /^SIG([A-Z].*)/; + $signal_name[eval "&POSIX::$sym()"] = $1; + } + # extra BSD signals + $signal_name[5] = 'TRAP'; + $signal_name[7] = 'IOT'; + $signal_name[10] = 'BUS'; + $signal_name[12] = 'SYS'; + $signal_name[16] = 'URG'; + $signal_name[23] = 'IO'; + $signal_name[24] = 'XCPU'; + $signal_name[25] = 'XFSZ'; + $signal_name[26] = 'VTALRM'; + $signal_name[27] = 'PROF'; + $signal_name[28] = 'WINCH'; + $signal_name[29] = 'INFO'; +} + +sub find_signal +{ + my $number = shift; + + if (@signal_name == 0) { + fillup_names(); + } + + return $signal_name[$number] || $number; +} + +sub child_error +{ + my $self = shift; + my $error = $?; + + my $extra = ""; + + if ($error & 128) { + $extra = $self->f(" (core dumped)"); + } + if ($error & 127) { + return $self->f("killed by signal #1#2", + find_signal($error & 127), $extra); + } else { + return $self->f("exit(#1)#2", ($error >> 8), $extra); + } +} + +sub system +{ + my $self = shift; + my $r = CORE::system(@_); + if ($r != 0) { + $self->say("system(#1) failed: #2", + join(", ", @_), $self->child_error); + } + return $r; +} + +sub copy_file +{ + my $self = shift; + require File::Copy; + + my $r = File::Copy::copy(@_); + if (!$r) { + $self->say("copy(#1) failed: #2", join(',', @_), $!); + } + return $r; +} + +sub unlink +{ + my $self = shift; + my $verbose = shift; + my $r = unlink @_; + if ($r != @_) { + $self->say("rm #1 failed: removed only #2 targets, #3", + join(' ', @_), $r, $1); + } elsif ($verbose) { + $self->say("rm #1", join(' ', @_)); + } + return $r; +} + +sub copy +{ + my $self = shift; + require File::Copy; + + my $r = File::Copy::copy(@_); + if (!$r) { + $self->say("copy(#1) failed: #2", join(',', @_), $!); + } + return $r; +} + 1; diff --git a/usr.sbin/pkg_add/OpenBSD/x509.pm b/usr.sbin/pkg_add/OpenBSD/x509.pm index c300ebefe60..a66c62efabb 100644 --- a/usr.sbin/pkg_add/OpenBSD/x509.pm +++ b/usr.sbin/pkg_add/OpenBSD/x509.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: x509.pm,v 1.5 2010/05/10 09:17:55 espie Exp $ +# $OpenBSD: x509.pm,v 1.6 2010/06/15 08:26:39 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -96,7 +96,7 @@ sub check_signature my ($plist, $state) = @_; my $sig = $plist->get('digital-signature'); if ($sig->{key} ne 'x509') { - $state->log->warn("Error: unknown signature style"); + $state->log("Error: unknown signature style"); return 0; } my ($fh, $fname) = mkstemp("/tmp/pkgcontent.XXXXXXXXX"); @@ -108,7 +108,7 @@ sub check_signature if (system_quiet (OpenBSD::Paths->openssl, "smime", "-verify", "-binary", "-inform", "DEM", "-in", $fname2, "-content", $fname, "-CAfile", OpenBSD::Paths->pkgca, "-out", "/dev/null") != 0) { - $state->log->warn("Bad signature"); + $state->log("Bad signature"); return 0; } if ($state->verbose >= 2) { |