diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2005-06-13 18:39:00 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2005-06-13 18:39:00 +0000 |
commit | f55b42362f70def4b24a2dc0dedbf65832076d81 (patch) | |
tree | 0007191998a3ef9a83b9cf4f14f950e6c6b7f758 /usr.sbin | |
parent | b60b7ff20522855b42af46b3d08cddf58ef48edf (diff) |
add copy method, and document it.
Note that it doesn't do any magic with hard links so far.
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Ustar.pm | 59 | ||||
-rw-r--r-- | usr.sbin/pkg_add/pod/OpenBSD::Ustar.pod | 22 |
2 files changed, 71 insertions, 10 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/Ustar.pm b/usr.sbin/pkg_add/OpenBSD/Ustar.pm index 426bcb6074e..1012cfd031f 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.22 2005/06/13 18:11:44 espie Exp $ +# $OpenBSD: Ustar.pm,v 1.23 2005/06/13 18:38:59 espie Exp $ # # Copyright (c) 2002-2004 Marc Espie <espie@openbsd.org> # @@ -126,11 +126,6 @@ sub next archive => $self, destdir => $self->{destdir} }; - # adjust swallow - $self->{swallow} = $size; - if ($size % 512) { - $self->{swallow} += 512 - $size % 512; - } if ($type eq DIR) { bless $result, 'OpenBSD::Ustar::Dir'; } elsif ($type eq HARDLINK) { @@ -142,6 +137,16 @@ sub next } else { die "Unsupported type"; } +# if (!$result->isFile()) { +# if ($size != 0) { +# die "Bad archive: non null size for non file entry\n"; +# } +# } + # adjust swallow + $self->{swallow} = $size; + if ($size % 512) { + $self->{swallow} += 512 - $size % 512; + } return $result; } @@ -275,6 +280,21 @@ sub write_contents # only files have anything to write } +sub copy_contents +{ + # only files need copying +} + +sub copy +{ + my ($self, $wrarc) = @_; + my $out = $wrarc->{fh}; + my $header = OpenBSD::Ustar::mkheader($self, $self->type()); + print $out $header; + + $self->copy_contents($wrarc); +} + sub isDir() { 0 } sub isFile() { 0 } sub isLink() { 0 } @@ -435,7 +455,7 @@ sub write_contents open my $fh, "<", $filename or die "Can't read file $filename: $!"; my $buffer; - my $toread = $self->{size}; + my $toread = $size; while ($toread > 0) { my $maxread = $buffsize; $maxread = $toread if $maxread > $toread; @@ -453,6 +473,31 @@ sub write_contents } } +sub copy_contents +{ + my ($self, $arc) = @_; + my $out = $arc->{fh}; + my $buffer; + my $size = $self->{size}; + my $toread = $size; + while ($toread > 0) { + my $maxread = $buffsize; + $maxread = $toread if $maxread > $toread; + if (!defined read($self->{archive}->{fh}, $buffer, $maxread)) { + die "Error reading from archive: $!"; + } + $self->{archive}->{swallow} -= $maxread; + unless (print $out $buffer) { + die "Error writing to archive $!"; + } + + $toread -= $maxread; + } + if ($size % 512) { + print $out "\0" x (512 - $size % 512); + } +} + sub isFile() { 1 } sub type() { OpenBSD::Ustar::FILE1 } diff --git a/usr.sbin/pkg_add/pod/OpenBSD::Ustar.pod b/usr.sbin/pkg_add/pod/OpenBSD::Ustar.pod index 2695ee7ad1e..136394a6637 100644 --- a/usr.sbin/pkg_add/pod/OpenBSD::Ustar.pod +++ b/usr.sbin/pkg_add/pod/OpenBSD::Ustar.pod @@ -1,4 +1,4 @@ -$OpenBSD: OpenBSD::Ustar.pod,v 1.5 2005/06/13 14:24:06 espie Exp $ +$OpenBSD: OpenBSD::Ustar.pod,v 1.6 2005/06/13 18:38:59 espie Exp $ =head1 NAME @@ -28,10 +28,22 @@ OpenBSD::Ustar - simple access to Ustar C<tar(1)> archives $wrarc->pad(); close($out); + # for copying + open(my $in, "<", $arcname); + $rdarc = OpenBSD::Ustar->new($in, $destdir); + open(my $out, ">", $arcname); + $wrarc = OpenBSD::Ustar->new($fh, $destdir); + while (my $o = $rdarc->next()) { + $o->copy($wrarc); + } + $wrarc->pad(); + close($in); + close($out); + =head1 DESCRIPTION -C<OpenBSD::Ustar> provides an API to read or write archives compatible with -C<tar(1)> +C<OpenBSD::Ustar> provides an API to read, write and copy archives compatible +with C<tar(1)> For the time being, it can only handle the USTAR archive format. A filehandle C<$fh> is associated with an C<OpenBSD::Ustar> object through @@ -154,3 +166,7 @@ for creating the C<$arc> C<OpenBSD::Ustar> object. During writing, hard link status is determined according to already written archive entries: a name that references a file which has already been written will be granted hard link status. + +Archives can be copied by creating separate archives for reading and writing. +Calling C<$o = $rdarc-E<gt>next()> and C<$o-E<gt>copy($wrarc)> will copy +an entry obtained from C<$rdarc> to C<$wrarc>. |