summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2011-01-02 14:39:19 +0000
committerMarc Espie <espie@cvs.openbsd.org>2011-01-02 14:39:19 +0000
commit0989245aaae543bb61854f71ccc0331457315b75 (patch)
tree431ce68c2bda7030da068ff200e1b955c44782dc /usr.sbin/pkg_add
parentb40f3c265535bc3bcccd3119a5535800b245d683 (diff)
tidy things: use ->new more often.
use mkdir directly to be able to report errors in a sane way (as the File::Path interface is prone to change). allow prepare to specify its own destdir (for temp files). use state->system to report more errors. be consistent in destdir error printing.
Diffstat (limited to 'usr.sbin/pkg_add')
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Ustar.pm66
1 files changed, 42 insertions, 24 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/Ustar.pm b/usr.sbin/pkg_add/OpenBSD/Ustar.pm
index da70ae5be0a..14e52abf724 100644
--- a/usr.sbin/pkg_add/OpenBSD/Ustar.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Ustar.pm
@@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
-# $OpenBSD: Ustar.pm,v 1.66 2010/12/24 09:04:14 espie Exp $
+# $OpenBSD: Ustar.pm,v 1.67 2011/01/02 14:39:18 espie Exp $
#
# Copyright (c) 2002-2007 Marc Espie <espie@openbsd.org>
#
@@ -40,7 +40,6 @@ use constant {
MAXGROUPNAME => 32
};
-use File::Path ();
use File::Basename ();
use OpenBSD::IdCache;
use OpenBSD::Paths;
@@ -128,8 +127,8 @@ sub next
}
# decode header
my ($name, $mode, $uid, $gid, $size, $mtime, $chksum, $type,
- $linkname, $magic, $version, $uname, $gname, $major, $minor,
- $prefix, $pad) = unpack(USTAR_HEADER, $header);
+ $linkname, $magic, $version, $uname, $gname, $major, $minor,
+ $prefix, $pad) = unpack(USTAR_HEADER, $header);
if ($magic ne "ustar\0" || $version ne '00') {
$self->fatal("Not an ustar archive header");
}
@@ -281,9 +280,10 @@ sub mkheader
sub prepare
{
- my ($self, $filename) = @_;
+ my ($self, $filename, $destdir) = @_;
- my $realname = "$self->{destdir}/$filename";
+ $destdir //= $self->{destdir};
+ my $realname = "$destdir/$filename";
my ($dev, $ino, $mode, $uid, $gid, $rdev, $size, $mtime) =
(lstat $realname)[0,1,2,4,5,6, 7,9];
@@ -303,24 +303,23 @@ sub prepare
minor => $rdev%256,
});
my $k = $entry->{key};
+ my $class = "OpenBSD::Ustar::File"; # default
if (defined $self->{key}->{$k}) {
$entry->{linkname} = $self->{key}->{$k};
- bless $entry, "OpenBSD::Ustar::HardLink";
+ $class = "OpenBSD::Ustar::HardLink";
} elsif (-l $realname) {
$entry->{linkname} = readlink($realname);
- bless $entry, "OpenBSD::Ustar::SoftLink";
+ $class = "OpenBSD::Ustar::SoftLink";
} elsif (-p _) {
- bless $entry, "OpenBSD::Ustar::Fifo";
+ $class = "OpenBSD::Ustar::Fifo";
} elsif (-c _) {
- bless $entry, "OpenBSD::Ustar::CharDevice";
+ $class = "OpenBSD::Ustar::CharDevice";
} elsif (-b _) {
- bless $entry, "OpenBSD::Ustar::BlockDevice";
+ $class ="OpenBSD::Ustar::BlockDevice";
} elsif (-d _) {
- bless $entry, "OpenBSD::Ustar::Dir";
- } else {
- bless $entry, "OpenBSD::Ustar::File";
+ $class = "OpenBSD::Ustar::Dir";
}
- return $entry;
+ $class->new($entry);
}
sub pad
@@ -355,6 +354,7 @@ sub fh
}
package OpenBSD::Ustar::Object;
+
sub new
{
my ($class, $object) = @_;
@@ -372,6 +372,12 @@ sub fatal
$self->{archive}->fatal(@args);
}
+sub system
+{
+ my ($self, @args) = @_;
+ $self->{archive}{state}->system(@args);
+}
+
sub errsay
{
my ($self, @args) = @_;
@@ -408,11 +414,22 @@ sub set_modes
}
}
+sub ensure_dir
+{
+ my ($self, $dir) = @_;
+ return if -d $dir;
+ $self->ensure_dir(File::Basename::dirname($dir));
+ if (mkdir($dir)) {
+ return;
+ }
+ $self->fatal("Error making directory #1: #2", $dir, $!);
+}
+
sub make_basedir
{
my $self = shift;
my $dir = $self->{destdir}.File::Basename::dirname($self->name);
- File::Path::mkpath($dir) unless -d $dir;
+ $self->ensure_dir($dir);
}
sub write
@@ -482,7 +499,7 @@ our @ISA=qw(OpenBSD::Ustar::Object);
sub create
{
my $self = shift;
- File::Path::mkpath($self->{destdir}.$self->name);
+ $self->ensure_dir($self->{destdir}.$self->name);
$self->set_modes;
}
@@ -496,7 +513,7 @@ our @ISA=qw(OpenBSD::Ustar::Object);
sub create
{
my $self = shift;
- $self->make_basedir($self->name);
+ $self->make_basedir;
my $linkname = $self->{linkname};
if (defined $self->{cwd}) {
$linkname=$self->{cwd}.'/'.$linkname;
@@ -530,7 +547,7 @@ our @ISA=qw(OpenBSD::Ustar::Object);
sub create
{
my $self = shift;
- $self->make_basedir($self->name);
+ $self->make_basedir;
symlink $self->{linkname}, $self->{destdir}.$self->name or
$self->fatal("Can't symlink #1 to #2#3: #4",
$self->{linkname}, $self->{destdir}, $self->name, $!);
@@ -547,10 +564,11 @@ our @ISA=qw(OpenBSD::Ustar::Object);
sub create
{
my $self = shift;
- $self->make_basedir($self->name);
+ $self->make_basedir;
require POSIX;
POSIX::mkfifo($self->{destdir}.$self->name, $self->{mode}) or
- $self->fatal("Can't create fifo #1: #2", $self->name, $!);
+ $self->fatal("Can't create fifo #1#2: #3", $self->{destdir},
+ $self->name, $!);
$self->set_modes;
}
@@ -563,8 +581,8 @@ our @ISA=qw(OpenBSD::Ustar::Object);
sub create
{
my $self = shift;
- $self->make_basedir($self->name);
- system(OpenBSD::Paths->mknod,
+ $self->make_basedir;
+ $self->system(OpenBSD::Paths->mknod,
'-m', $self->{mode}, '--', $self->{destdir}.$self->name,
$self->devicetype, $self->{major}, $self->{minor});
$self->set_modes;
@@ -665,7 +683,7 @@ sub new
sub create
{
my $self = shift;
- $self->make_basedir($self->name);
+ $self->make_basedir;
my $buffer;
my $out = OpenBSD::CompactWriter->new($self->{destdir}.$self->name);
if (!defined $out) {