From 1b04b994dfc5a6779c4682a7401eaca9be3138a5 Mon Sep 17 00:00:00 2001 From: espie Date: Mon, 9 Aug 2021 16:40:20 +0000 Subject: [PATCH] add $self->fullname to simplify the cumbersome usual name add $self->extract_to_fh, only for IsFile objects, so that they can be written to a temporary file without needing to adjust the file object's name --- usr.sbin/pkg_add/OpenBSD/Ustar.pm | 75 ++++++++++++++++++------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/usr.sbin/pkg_add/OpenBSD/Ustar.pm b/usr.sbin/pkg_add/OpenBSD/Ustar.pm index 8783627ff5d..aef9740d87d 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.88 2017/07/23 10:34:44 espie Exp $ +# $OpenBSD: Ustar.pm,v 1.89 2021/08/09 16:40:20 espie Exp $ # # Copyright (c) 2002-2014 Marc Espie # @@ -526,23 +526,34 @@ sub name return $self->{name}; } +sub fullname +{ + my $self = shift; + return $self->{destdir}.$self->{name}; +} + sub set_name { my ($self, $v) = @_; $self->{name} = $v; } -sub set_modes +sub set_modes_on_object { - my $self = shift; - chown $self->{uid}, $self->{gid}, $self->{destdir}.$self->name; - chmod $self->{mode}, $self->{destdir}.$self->name; + my ($self, $o) = @_; + chown $self->{uid}, $self->{gid}, $o; + chmod $self->{mode}, $o; if (defined $self->{mtime} || defined $self->{atime}) { - utime $self->{atime} // time, $self->{mtime} // time, - $self->{destdir}.$self->name; + utime $self->{atime} // time, $self->{mtime} // time, $o; } } +sub set_modes +{ + my $self = shift; + $self->set_modes_on_object($self->fullname); +} + sub ensure_dir { my ($self, $dir) = @_; @@ -628,7 +639,7 @@ our @ISA=qw(OpenBSD::Ustar::Object); sub create { my $self = shift; - $self->ensure_dir($self->{destdir}.$self->name); + $self->ensure_dir($self->fullname); $self->set_modes; } @@ -647,7 +658,7 @@ sub create if (defined $self->{cwd}) { $linkname=$self->{cwd}.'/'.$linkname; } - link $self->{destdir}.$linkname, $self->{destdir}.$self->name or + link $self->{destdir}.$linkname, $self->fullname or $self->fatal("Can't link #1#2 to #1#3: #4", $self->{destdir}, $linkname, $self->name, $!); } @@ -677,11 +688,11 @@ sub create { my $self = shift; $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, $!); + symlink $self->{linkname}, $self->fullname or + $self->fatal("Can't symlink #1 to #2: #3", + $self->{linkname}, $self->fullname, $!); require POSIX; - POSIX::lchown($self->{uid}, $self->{gid}, $self->{destdir}.$self->name); + POSIX::lchown($self->{uid}, $self->{gid}, $self->fullname); } sub isLink() { 1 } @@ -697,9 +708,8 @@ sub create my $self = shift; $self->make_basedir; require POSIX; - POSIX::mkfifo($self->{destdir}.$self->name, $self->{mode}) or - $self->fatal("Can't create fifo #1#2: #3", $self->{destdir}, - $self->name, $!); + POSIX::mkfifo($self->fullname, $self->{mode}) or + $self->fatal("Can't create fifo #1: #2", $self->fullname, $!); $self->set_modes; } @@ -714,7 +724,7 @@ sub create my $self = shift; $self->make_basedir; $self->system(OpenBSD::Paths->mknod, - '-m', $self->{mode}, '--', $self->{destdir}.$self->name, + '-m', $self->{mode}, '--', $self->fullname, $self->devicetype, $self->{major}, $self->{minor}); $self->set_modes; } @@ -744,8 +754,7 @@ use constant { sub new { - my ($class, $fname) = @_; - open (my $out, '>', $fname) or return; + my ($class, $out) = @_; my $bs = (stat $out)[11]; my $zeroes; if (defined $bs) { @@ -809,18 +818,22 @@ sub create { my $self = shift; $self->make_basedir; + open(my $fh, '>', $self->fullname) or + $self->fatal("Can't write to #1: #2", $self->fullname, $!); + $self->extract_to_fh($fh); +} + +sub extract_to_fh +{ + my ($self, $fh) = @_; my $buffer; - my $out = OpenBSD::CompactWriter->new($self->{destdir}.$self->name); - if (!defined $out) { - $self->fatal("Can't write to #1#2: #3", $self->{destdir}, - $self->name, $!); - } + my $out = OpenBSD::CompactWriter->new($fh); my $toread = $self->{size}; if ($self->{partial}) { $toread -= length($self->{partial}); unless ($out->write($self->{partial})) { - $self->fatal("Error writing to #1#2: #3", - $self->{destdir}, $self->name, $!); + $self->fatal("Error writing to #1: #2", + $self->fullname, $!); } } while ($toread > 0) { @@ -835,16 +848,16 @@ sub create } $self->{archive}{swallow} -= $actual; unless ($out->write($buffer)) { - $self->fatal("Error writing to #1#2: #3", - $self->{destdir}, $self->name, $!); + $self->fatal("Error writing to #1: #2", + $self->fullname, $!); } $toread -= $actual; $self->left_todo($toread); } - $out->close or $self->fatal("Error closing #1#2: #3", - $self->{destdir}, $self->name, $!); - $self->set_modes; + $self->set_modes_on_object($fh); + $out->close or $self->fatal("Error closing #1: #2", + $self->fullname, $!); } sub contents -- 2.20.1