| Filename | /usr/libdata/perl5/OpenBSD/Ustar.pm |
| Statements | Executed 165 statements in 6.71ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1.55ms | 1.74ms | OpenBSD::Ustar::BEGIN@48 |
| 1 | 1 | 1 | 541µs | 581µs | OpenBSD::Ustar::BEGIN@49 |
| 2 | 1 | 1 | 214µs | 642µs | OpenBSD::Ustar::next |
| 2 | 1 | 1 | 44µs | 124µs | OpenBSD::Ustar::File::contents |
| 2 | 1 | 1 | 38µs | 38µs | OpenBSD::Ustar::new |
| 2 | 1 | 1 | 37µs | 127µs | OpenBSD::Ustar::CORE:read (opcode) |
| 8 | 4 | 1 | 28µs | 28µs | OpenBSD::Ustar::CORE:subst (opcode) |
| 1 | 1 | 1 | 26µs | 577µs | OpenBSD::Ustar::BEGIN@25 |
| 2 | 1 | 1 | 23µs | 23µs | OpenBSD::Ustar::new_object |
| 1 | 1 | 1 | 21µs | 27µs | OpenBSD::PackageLocation::BEGIN@20 |
| 4 | 2 | 1 | 20µs | 20µs | OpenBSD::Ustar::CORE:unpack (opcode) |
| 1 | 1 | 1 | 16µs | 28µs | OpenBSD::PackageLocation::BEGIN@21 |
| 1 | 1 | 1 | 14µs | 36µs | OpenBSD::Ustar::BEGIN@221 |
| 1 | 1 | 1 | 14µs | 166µs | OpenBSD::CompactWriter::BEGIN@738 |
| 1 | 1 | 1 | 11µs | 11µs | OpenBSD::Ustar::BEGIN@50 |
| 2 | 1 | 1 | 10µs | 10µs | OpenBSD::Ustar::set_description |
| 2 | 1 | 1 | 10µs | 80µs | OpenBSD::Ustar::File::CORE:read (opcode) |
| 2 | 1 | 1 | 9µs | 9µs | OpenBSD::Ustar::skip |
| 2 | 1 | 1 | 6µs | 6µs | OpenBSD::Ustar::CORE:match (opcode) |
| 4 | 2 | 2 | 4µs | 4µs | OpenBSD::Ustar::File::isFile (xsub) |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::CompactWriter::close |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::CompactWriter::new |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::CompactWriter::write |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::UStar::Device::create |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Dir::create |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Fifo::create |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::File::copy_contents |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::File::create |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::File::write_contents |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::HardLink::create |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::HardLink::resolve_links |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::alias |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::copy |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::copy_contents |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::ensure_dir |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::errsay |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::fatal |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::left_todo |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::make_basedir |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::name |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::recheck_owner |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::resolve_links |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::set_modes |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::set_name |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::system |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::write |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::Object::write_contents |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::SoftLink::create |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::close |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::destdir |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::extended_record |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::fatal |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::fh |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::mkheader |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::pack_header |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::pad |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::parse_records |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::prepare |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::read_records |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::set_callback |
| 0 | 0 | 0 | 0s | 0s | OpenBSD::Ustar::split_name |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | # ex:ts=8 sw=4: | ||||
| 2 | # $OpenBSD: Ustar.pm,v 1.88 2017/07/23 10:34:44 espie Exp $ | ||||
| 3 | # | ||||
| 4 | # Copyright (c) 2002-2014 Marc Espie <espie@openbsd.org> | ||||
| 5 | # | ||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||
| 9 | # | ||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||
| 17 | |||||
| 18 | # Handle utar archives | ||||
| 19 | |||||
| 20 | 2 | 55µs | 2 | 32µs | # spent 27µs (21+6) within OpenBSD::PackageLocation::BEGIN@20 which was called:
# once (21µs+6µs) by OpenBSD::PackageLocation::_opened at line 20 # spent 27µs making 1 call to OpenBSD::PackageLocation::BEGIN@20
# spent 6µs making 1 call to strict::import |
| 21 | 2 | 108µs | 2 | 41µs | # spent 28µs (16+12) within OpenBSD::PackageLocation::BEGIN@21 which was called:
# once (16µs+12µs) by OpenBSD::PackageLocation::_opened at line 21 # spent 28µs making 1 call to OpenBSD::PackageLocation::BEGIN@21
# spent 12µs making 1 call to warnings::import |
| 22 | |||||
| 23 | package OpenBSD::Ustar; | ||||
| 24 | |||||
| 25 | # spent 577µs (26+551) within OpenBSD::Ustar::BEGIN@25 which was called:
# once (26µs+551µs) by OpenBSD::PackageLocation::_opened at line 46 | ||||
| 26 | 1 | 1µs | FILE => "\0", | ||
| 27 | FILE1 => '0', | ||||
| 28 | HARDLINK => '1', | ||||
| 29 | SOFTLINK => '2', | ||||
| 30 | CHARDEVICE => '3', | ||||
| 31 | BLOCKDEVICE => '4', | ||||
| 32 | DIR => '5', | ||||
| 33 | FIFO => '6', | ||||
| 34 | CONTFILE => '7', | ||||
| 35 | USTAR_HEADER => 'a100a8a8a8a12a12a8aa100a6a2a32a32a8a8a155a12', | ||||
| 36 | MAXFILENAME => 100, | ||||
| 37 | MAXLINKNAME => 100, | ||||
| 38 | MAXPREFIX => 155, | ||||
| 39 | MAXUSERNAME => 32, | ||||
| 40 | MAXGROUPNAME => 32, | ||||
| 41 | XHDR => 'x', | ||||
| 42 | # XXX those are NOT supported, just recognized | ||||
| 43 | GHDR => 'g', | ||||
| 44 | LONGLINK => 'K', | ||||
| 45 | LONGNAME => 'L', | ||||
| 46 | 1 | 55µs | 2 | 1.13ms | }; # spent 577µs making 1 call to OpenBSD::Ustar::BEGIN@25
# spent 551µs making 1 call to constant::import |
| 47 | |||||
| 48 | 2 | 193µs | 1 | 1.74ms | # spent 1.74ms (1.55+184µs) within OpenBSD::Ustar::BEGIN@48 which was called:
# once (1.55ms+184µs) by OpenBSD::PackageLocation::_opened at line 48 # spent 1.74ms making 1 call to OpenBSD::Ustar::BEGIN@48 |
| 49 | 2 | 127µs | 1 | 581µs | # spent 581µs (541+40) within OpenBSD::Ustar::BEGIN@49 which was called:
# once (541µs+40µs) by OpenBSD::PackageLocation::_opened at line 49 # spent 581µs making 1 call to OpenBSD::Ustar::BEGIN@49 |
| 50 | 2 | 1.93ms | 1 | 11µs | # spent 11µs within OpenBSD::Ustar::BEGIN@50 which was called:
# once (11µs+0s) by OpenBSD::PackageLocation::_opened at line 50 # spent 11µs making 1 call to OpenBSD::Ustar::BEGIN@50 |
| 51 | |||||
| 52 | 1 | 6µs | 1 | 4µs | our $uidcache = new OpenBSD::UidCache; # spent 4µs making 1 call to OpenBSD::SimpleIdCache::new |
| 53 | 1 | 4µs | 1 | 3µs | our $gidcache = new OpenBSD::GidCache; # spent 3µs making 1 call to OpenBSD::SimpleIdCache::new |
| 54 | 1 | 3µs | 1 | 2µs | our $unamecache = new OpenBSD::UnameCache; # spent 2µs making 1 call to OpenBSD::SimpleIdCache::new |
| 55 | 1 | 3µs | 1 | 2µs | our $gnamecache = new OpenBSD::GnameCache; # spent 2µs making 1 call to OpenBSD::SimpleIdCache::new |
| 56 | |||||
| 57 | # This is a multiple of st_blksize everywhere.... | ||||
| 58 | 1 | 600ns | my $buffsize = 2 * 1024 * 1024; | ||
| 59 | |||||
| 60 | sub new | ||||
| 61 | # spent 38µs within OpenBSD::Ustar::new which was called 2 times, avg 19µs/call:
# 2 times (38µs+0s) by OpenBSD::PackageLocation::_opened at line 87 of OpenBSD/PackageLocation.pm, avg 19µs/call | ||||
| 62 | 2 | 4µs | my ($class, $fh, $state, $destdir) = @_; | ||
| 63 | |||||
| 64 | 2 | 2µs | $destdir = '' unless defined $destdir; | ||
| 65 | |||||
| 66 | 2 | 30µs | return bless { | ||
| 67 | fh => $fh, | ||||
| 68 | swallow => 0, | ||||
| 69 | state => $state, | ||||
| 70 | key => {}, | ||||
| 71 | destdir => $destdir} , $class; | ||||
| 72 | } | ||||
| 73 | |||||
| 74 | sub set_description | ||||
| 75 | # spent 10µs within OpenBSD::Ustar::set_description which was called 2 times, avg 5µs/call:
# 2 times (10µs+0s) by OpenBSD::PackageLocation::_opened at line 88 of OpenBSD/PackageLocation.pm, avg 5µs/call | ||||
| 76 | 2 | 2µs | my ($self, $d) = @_; | ||
| 77 | 2 | 13µs | $self->{description} = $d; | ||
| 78 | } | ||||
| 79 | |||||
| 80 | sub set_callback | ||||
| 81 | { | ||||
| 82 | my ($self, $code) = @_; | ||||
| 83 | $self->{callback} = $code; | ||||
| 84 | } | ||||
| 85 | |||||
| 86 | sub fatal | ||||
| 87 | { | ||||
| 88 | my ($self, $msg, @args) = @_; | ||||
| 89 | $self->{state}->fatal("Ustar [#1][#2]: #3", | ||||
| 90 | $self->{description} // '?', $self->{lastname} // '?', | ||||
| 91 | $self->{state}->f($msg, @args)); | ||||
| 92 | } | ||||
| 93 | |||||
| 94 | sub new_object | ||||
| 95 | # spent 23µs within OpenBSD::Ustar::new_object which was called 2 times, avg 12µs/call:
# 2 times (23µs+0s) by OpenBSD::Ustar::next at line 256, avg 12µs/call | ||||
| 96 | 2 | 2µs | my ($self, $h, $class) = @_; | ||
| 97 | 2 | 3µs | $h->{archive} = $self; | ||
| 98 | 2 | 2µs | $h->{destdir} = $self->{destdir}; | ||
| 99 | 2 | 8µs | bless $h, $class; | ||
| 100 | 2 | 8µs | return $h; | ||
| 101 | } | ||||
| 102 | |||||
| 103 | sub skip | ||||
| 104 | # spent 9µs within OpenBSD::Ustar::skip which was called 2 times, avg 5µs/call:
# 2 times (9µs+0s) by OpenBSD::Ustar::next at line 186, avg 5µs/call | ||||
| 105 | 2 | 2µs | my $self = shift; | ||
| 106 | 2 | 2µs | my $temp; | ||
| 107 | |||||
| 108 | 2 | 14µs | while ($self->{swallow} > 0) { | ||
| 109 | my $toread = $self->{swallow}; | ||||
| 110 | if ($toread >$buffsize) { | ||||
| 111 | $toread = $buffsize; | ||||
| 112 | } | ||||
| 113 | my $actual = read($self->{fh}, $temp, $toread); | ||||
| 114 | if (!defined $actual) { | ||||
| 115 | $self->fatal("Error while skipping archive: #1", $!); | ||||
| 116 | } | ||||
| 117 | if ($actual == 0) { | ||||
| 118 | $self->fatal("Premature end of archive in header: #1", $!); | ||||
| 119 | } | ||||
| 120 | $self->{swallow} -= $actual; | ||||
| 121 | } | ||||
| 122 | } | ||||
| 123 | |||||
| 124 | 1 | 5µs | my $types = { | ||
| 125 | DIR , 'OpenBSD::Ustar::Dir', | ||||
| 126 | HARDLINK , 'OpenBSD::Ustar::HardLink', | ||||
| 127 | SOFTLINK , 'OpenBSD::Ustar::SoftLink', | ||||
| 128 | FILE , 'OpenBSD::Ustar::File', | ||||
| 129 | FILE1 , 'OpenBSD::Ustar::File', | ||||
| 130 | FIFO , 'OpenBSD::Ustar::Fifo', | ||||
| 131 | CHARDEVICE , 'OpenBSD::Ustar::CharDevice', | ||||
| 132 | BLOCKDEVICE , 'OpenBSD::Ustar::BlockDevice', | ||||
| 133 | }; | ||||
| 134 | |||||
| 135 | 1 | 2µs | my $unsupported = { | ||
| 136 | XHDR => 'Extended header', | ||||
| 137 | GHDR => 'GNU header', | ||||
| 138 | LONGLINK => 'Long symlink', | ||||
| 139 | LONGNAME => 'Long file', | ||||
| 140 | }; | ||||
| 141 | |||||
| 142 | sub read_records | ||||
| 143 | { | ||||
| 144 | my ($self, $size) = @_; | ||||
| 145 | my $toread = $self->{swallow}; | ||||
| 146 | my $result = ''; | ||||
| 147 | while ($toread > 0) { | ||||
| 148 | my $buffer; | ||||
| 149 | my $maxread = $buffsize; | ||||
| 150 | $maxread = $toread if $maxread > $toread; | ||||
| 151 | my $actual = read($self->{fh}, $buffer, $maxread); | ||||
| 152 | if (!defined $actual) { | ||||
| 153 | $self->fatal("Error reading from archive: #1", $!); | ||||
| 154 | } | ||||
| 155 | if ($actual == 0) { | ||||
| 156 | $self->fatal("Premature end of archive"); | ||||
| 157 | } | ||||
| 158 | $self->{swallow} -= $actual; | ||||
| 159 | $toread -= $actual; | ||||
| 160 | $result .= $buffer; | ||||
| 161 | } | ||||
| 162 | return substr($result, 0, $size); | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | sub parse_records | ||||
| 166 | { | ||||
| 167 | my ($self, $result, $h) = @_; | ||||
| 168 | open(my $fh, '<', \$h); | ||||
| 169 | while (<$fh>) { | ||||
| 170 | chomp; | ||||
| 171 | if (m/^(\d+)\s+(\w+?)\=(.*)$/) { | ||||
| 172 | my ($k, $v) = ($2, $3); | ||||
| 173 | if ($k eq 'path') { | ||||
| 174 | $result->{name} = $v; | ||||
| 175 | } elsif ($k eq 'linkpath') { | ||||
| 176 | $result->{linkname} = $v; | ||||
| 177 | } | ||||
| 178 | } | ||||
| 179 | } | ||||
| 180 | } | ||||
| 181 | |||||
| 182 | sub next | ||||
| 183 | # spent 642µs (214+429) within OpenBSD::Ustar::next which was called 2 times, avg 321µs/call:
# 2 times (214µs+429µs) by OpenBSD::PackageLocation::getNext at line 319 of OpenBSD/PackageLocation.pm, avg 321µs/call | ||||
| 184 | 2 | 2µs | my $self = shift; | ||
| 185 | # get rid of the current object | ||||
| 186 | 2 | 6µs | 2 | 9µs | $self->skip; # spent 9µs making 2 calls to OpenBSD::Ustar::skip, avg 5µs/call |
| 187 | 2 | 2µs | my $header; | ||
| 188 | 2 | 41µs | 4 | 217µs | my $n = read($self->{fh}, $header, 512); # spent 127µs making 2 calls to OpenBSD::Ustar::CORE:read, avg 63µs/call
# spent 90µs making 2 calls to IO::Uncompress::Base::read, avg 45µs/call |
| 189 | 2 | 2µs | return if (defined $n) and $n == 0; | ||
| 190 | 2 | 2µs | $self->fatal("Error while reading header") | ||
| 191 | unless defined $n and $n == 512; | ||||
| 192 | 2 | 2µs | if ($header eq "\0"x512) { | ||
| 193 | return $self->next; | ||||
| 194 | } | ||||
| 195 | # decode header | ||||
| 196 | 2 | 31µs | 2 | 15µs | my ($name, $mode, $uid, $gid, $size, $mtime, $chksum, $type, # spent 15µs making 2 calls to OpenBSD::Ustar::CORE:unpack, avg 7µs/call |
| 197 | $linkname, $magic, $version, $uname, $gname, $major, $minor, | ||||
| 198 | $prefix, $pad) = unpack(USTAR_HEADER, $header); | ||||
| 199 | 2 | 2µs | if ($magic ne "ustar\0" || $version ne '00') { | ||
| 200 | $self->fatal("Not an ustar archive header"); | ||||
| 201 | } | ||||
| 202 | # verify checksum | ||||
| 203 | 2 | 4µs | my $value = $header; | ||
| 204 | 2 | 2µs | substr($value, 148, 8) = " "x8; | ||
| 205 | 2 | 11µs | 2 | 5µs | my $ck2 = unpack("%C*", $value); # spent 5µs making 2 calls to OpenBSD::Ustar::CORE:unpack, avg 3µs/call |
| 206 | 2 | 2µs | if ($ck2 != oct($chksum)) { | ||
| 207 | $self->fatal("Bad archive checksum"); | ||||
| 208 | } | ||||
| 209 | 2 | 26µs | 2 | 16µs | $name =~ s/\0*$//o; # spent 16µs making 2 calls to OpenBSD::Ustar::CORE:subst, avg 8µs/call |
| 210 | 2 | 2µs | $mode = oct($mode) & 0xfff; | ||
| 211 | 2 | 11µs | 2 | 6µs | $uname =~ s/\0*$//o; # spent 6µs making 2 calls to OpenBSD::Ustar::CORE:subst, avg 3µs/call |
| 212 | 2 | 9µs | 2 | 4µs | $gname =~ s/\0*$//o; # spent 4µs making 2 calls to OpenBSD::Ustar::CORE:subst, avg 2µs/call |
| 213 | 2 | 8µs | 2 | 3µs | $linkname =~ s/\0*$//o; # spent 3µs making 2 calls to OpenBSD::Ustar::CORE:subst, avg 1µs/call |
| 214 | 2 | 2µs | $major = oct($major); | ||
| 215 | 2 | 2µs | $minor = oct($minor); | ||
| 216 | 2 | 1µs | $uid = oct($uid); | ||
| 217 | 2 | 1µs | $gid = oct($gid); | ||
| 218 | 2 | 13µs | 2 | 120µs | $uid = $uidcache->lookup($uname, $uid); # spent 120µs making 2 calls to OpenBSD::IdCache::lookup, avg 60µs/call |
| 219 | 2 | 7µs | 2 | 92µs | $gid = $gidcache->lookup($gname, $gid); # spent 92µs making 2 calls to OpenBSD::IdCache::lookup, avg 46µs/call |
| 220 | { | ||||
| 221 | 4 | 2.72ms | 2 | 57µs | # spent 36µs (14+21) within OpenBSD::Ustar::BEGIN@221 which was called:
# once (14µs+21µs) by OpenBSD::PackageLocation::_opened at line 221 # spent 36µs making 1 call to OpenBSD::Ustar::BEGIN@221
# spent 21µs making 1 call to warnings::unimport |
| 222 | 2 | 2µs | $mtime = oct($mtime); | ||
| 223 | } | ||||
| 224 | 2 | 14µs | 2 | 6µs | unless ($prefix =~ m/^\0/o) { # spent 6µs making 2 calls to OpenBSD::Ustar::CORE:match, avg 3µs/call |
| 225 | $prefix =~ s/\0*$//o; | ||||
| 226 | $name = "$prefix/$name"; | ||||
| 227 | } | ||||
| 228 | |||||
| 229 | 2 | 5µs | $self->{lastname} = $name; | ||
| 230 | 2 | 2µs | $size = oct($size); | ||
| 231 | 2 | 30µs | my $result= { | ||
| 232 | name => $name, | ||||
| 233 | mode => $mode, | ||||
| 234 | atime => $mtime, | ||||
| 235 | mtime => $mtime, | ||||
| 236 | linkname=> $linkname, | ||||
| 237 | uname => $uname, | ||||
| 238 | uid => $uid, | ||||
| 239 | gname => $gname, | ||||
| 240 | gid => $gid, | ||||
| 241 | size => $size, | ||||
| 242 | major => $major, | ||||
| 243 | minor => $minor, | ||||
| 244 | }; | ||||
| 245 | # adjust swallow | ||||
| 246 | 2 | 2µs | $self->{swallow} = $size; | ||
| 247 | 2 | 4µs | if ($size % 512) { | ||
| 248 | $self->{swallow} += 512 - $size % 512; | ||||
| 249 | } | ||||
| 250 | 2 | 2µs | if ($type eq XHDR) { | ||
| 251 | my $h = $self->read_records($size); | ||||
| 252 | $result = $self->next; | ||||
| 253 | $self->parse_records($result, $h); | ||||
| 254 | return $result; | ||||
| 255 | } | ||||
| 256 | 2 | 11µs | 2 | 23µs | if (defined $types->{$type}) { # spent 23µs making 2 calls to OpenBSD::Ustar::new_object, avg 12µs/call |
| 257 | $self->new_object($result, $types->{$type}); | ||||
| 258 | } else { | ||||
| 259 | $self->fatal("Unsupported type #1 (#2)", $type, | ||||
| 260 | $unsupported->{$type} // "unknown"); | ||||
| 261 | } | ||||
| 262 | 2 | 12µs | 2 | 3µs | if (!$result->isFile && $result->{size} != 0) { # spent 3µs making 2 calls to OpenBSD::Ustar::File::isFile, avg 1µs/call |
| 263 | $self->fatal("Bad archive: non null size for #1 (#2)", | ||||
| 264 | $types->{$type}, $result->{name}); | ||||
| 265 | } | ||||
| 266 | |||||
| 267 | 2 | 7µs | $self->{cachename} = $name; | ||
| 268 | 2 | 9µs | return $result; | ||
| 269 | } | ||||
| 270 | |||||
| 271 | sub split_name | ||||
| 272 | { | ||||
| 273 | my $name = shift; | ||||
| 274 | my $prefix = ''; | ||||
| 275 | |||||
| 276 | my $l = length $name; | ||||
| 277 | if ($l > MAXFILENAME && $l <= MAXFILENAME+MAXPREFIX+1) { | ||||
| 278 | while (length($name) > MAXFILENAME && | ||||
| 279 | $name =~ m/^(.*?\/)(.*)$/o) { | ||||
| 280 | $prefix .= $1; | ||||
| 281 | $name = $2; | ||||
| 282 | } | ||||
| 283 | $prefix =~ s|/$||; | ||||
| 284 | } | ||||
| 285 | return ($prefix, $name); | ||||
| 286 | } | ||||
| 287 | |||||
| 288 | sub extended_record | ||||
| 289 | { | ||||
| 290 | my ($k, $v) = @_; | ||||
| 291 | my $string = " $k=$v\n"; | ||||
| 292 | my $len = length($string); | ||||
| 293 | if ($len < 995) { | ||||
| 294 | return sprintf("%3d", $len+3).$string; | ||||
| 295 | } elsif ($len < 9995) { | ||||
| 296 | return sprintf("%04d", $len+4).$string; | ||||
| 297 | } else { | ||||
| 298 | return sprintf("%05d", $len+5).$string; | ||||
| 299 | } | ||||
| 300 | } | ||||
| 301 | |||||
| 302 | sub pack_header | ||||
| 303 | { | ||||
| 304 | my ($archive, $type, $size, $entry, $prefix, $name, $linkname, | ||||
| 305 | $uname, $gname, $major, $minor) = @_; | ||||
| 306 | |||||
| 307 | my $header; | ||||
| 308 | my $cksum = ' 'x8; | ||||
| 309 | for (1 .. 2) { | ||||
| 310 | $header = pack(USTAR_HEADER, | ||||
| 311 | $name, | ||||
| 312 | sprintf("%07o", $entry->{mode}), | ||||
| 313 | sprintf("%07o", $entry->{uid} // 0), | ||||
| 314 | sprintf("%07o", $entry->{gid} // 0), | ||||
| 315 | sprintf("%011o", $size), | ||||
| 316 | sprintf("%011o", $entry->{mtime} // 0), | ||||
| 317 | $cksum, | ||||
| 318 | $type, | ||||
| 319 | $linkname, | ||||
| 320 | 'ustar', '00', | ||||
| 321 | $uname, | ||||
| 322 | $gname, | ||||
| 323 | sprintf("%07o", $major), | ||||
| 324 | sprintf("%07o", $minor), | ||||
| 325 | $prefix, "\0"); | ||||
| 326 | $cksum = sprintf("%07o", unpack("%C*", $header)); | ||||
| 327 | } | ||||
| 328 | return $header; | ||||
| 329 | } | ||||
| 330 | |||||
| 331 | 1 | 700ns | my $whatever = "usualSuspect000"; | ||
| 332 | |||||
| 333 | sub mkheader | ||||
| 334 | { | ||||
| 335 | my ($archive, $entry, $type) = @_; | ||||
| 336 | my ($prefix, $name) = split_name($entry->name); | ||||
| 337 | my ($extendedname, $extendedlink); | ||||
| 338 | my $linkname = $entry->{linkname}; | ||||
| 339 | my $size = $entry->{size}; | ||||
| 340 | my ($major, $minor); | ||||
| 341 | if ($entry->isDevice) { | ||||
| 342 | $major = $entry->{major}; | ||||
| 343 | $minor = $entry->{minor}; | ||||
| 344 | } else { | ||||
| 345 | $major = 0; | ||||
| 346 | $minor = 0; | ||||
| 347 | } | ||||
| 348 | my ($uname, $gname); | ||||
| 349 | if (defined $entry->{uname}) { | ||||
| 350 | $uname = $entry->{uname}; | ||||
| 351 | } else { | ||||
| 352 | $uname = $entry->{uid}; | ||||
| 353 | } | ||||
| 354 | if (defined $entry->{gname}) { | ||||
| 355 | $gname = $entry->{gname}; | ||||
| 356 | } else { | ||||
| 357 | $gname = $entry->{gid}; | ||||
| 358 | } | ||||
| 359 | |||||
| 360 | if (defined $entry->{cwd}) { | ||||
| 361 | my $cwd = $entry->{cwd}; | ||||
| 362 | $cwd.='/' unless $cwd =~ m/\/$/o; | ||||
| 363 | $linkname =~ s/^\Q$cwd\E//; | ||||
| 364 | } | ||||
| 365 | if (!defined $linkname) { | ||||
| 366 | $linkname = ''; | ||||
| 367 | } | ||||
| 368 | if (length $prefix > MAXPREFIX) { | ||||
| 369 | $prefix = substr($prefix, 0, MAXPREFIX); | ||||
| 370 | $extendedname = 1; | ||||
| 371 | } | ||||
| 372 | if (length $name > MAXFILENAME) { | ||||
| 373 | $name = substr($name, 0, MAXPREFIX); | ||||
| 374 | $extendedname = 1; | ||||
| 375 | } | ||||
| 376 | if (length $linkname > MAXLINKNAME) { | ||||
| 377 | $linkname = substr($linkname, 0, MAXLINKNAME); | ||||
| 378 | $extendedlink = 1; | ||||
| 379 | } | ||||
| 380 | if (length $uname > MAXUSERNAME) { | ||||
| 381 | $archive->fatal("Username too long #1", $uname); | ||||
| 382 | } | ||||
| 383 | if (length $gname > MAXGROUPNAME) { | ||||
| 384 | $archive->fatal("Groupname too long #1", $gname); | ||||
| 385 | } | ||||
| 386 | my $header = $archive->pack_header($type, $size, $entry, | ||||
| 387 | $prefix, $name, $linkname, $uname, $gname, $major, $minor); | ||||
| 388 | my $x; | ||||
| 389 | if ($extendedname) { | ||||
| 390 | $x .= extended_record("path", $entry->name); | ||||
| 391 | } | ||||
| 392 | if ($extendedlink) { | ||||
| 393 | $x .= extended_record("linkpath",$entry->{linkname}); | ||||
| 394 | } | ||||
| 395 | if ($x) { | ||||
| 396 | my $extended = $archive->pack_header(XHDR, length($x), $entry, | ||||
| 397 | '', $whatever, '', $uname, $gname, $major, $minor); | ||||
| 398 | $whatever++; | ||||
| 399 | if ((length $x) % 512) { | ||||
| 400 | $x .= "\0" x (512 - ((length $x) % 512)); | ||||
| 401 | } | ||||
| 402 | return $extended.$x.$header; | ||||
| 403 | } | ||||
| 404 | return $header; | ||||
| 405 | } | ||||
| 406 | |||||
| 407 | sub prepare | ||||
| 408 | { | ||||
| 409 | my ($self, $filename, $destdir) = @_; | ||||
| 410 | |||||
| 411 | $destdir //= $self->{destdir}; | ||||
| 412 | my $realname = "$destdir/$filename"; | ||||
| 413 | |||||
| 414 | my ($dev, $ino, $mode, $uid, $gid, $rdev, $size, $mtime) = | ||||
| 415 | (lstat $realname)[0,1,2, 4,5,6,7, 9]; | ||||
| 416 | |||||
| 417 | my $entry = { | ||||
| 418 | key => "$dev/$ino", | ||||
| 419 | name => $filename, | ||||
| 420 | realname => $realname, | ||||
| 421 | mode => $mode, | ||||
| 422 | uid => $uid, | ||||
| 423 | gid => $gid, | ||||
| 424 | size => $size, | ||||
| 425 | mtime => $mtime, | ||||
| 426 | uname => $unamecache->lookup($uid), | ||||
| 427 | gname => $gnamecache->lookup($gid), | ||||
| 428 | major => $rdev/256, | ||||
| 429 | minor => $rdev%256, | ||||
| 430 | }; | ||||
| 431 | my $k = $entry->{key}; | ||||
| 432 | my $class = "OpenBSD::Ustar::File"; # default | ||||
| 433 | if (defined $self->{key}{$k}) { | ||||
| 434 | $entry->{linkname} = $self->{key}{$k}; | ||||
| 435 | $class = "OpenBSD::Ustar::HardLink"; | ||||
| 436 | } elsif (-l $realname) { | ||||
| 437 | $entry->{linkname} = readlink($realname); | ||||
| 438 | $class = "OpenBSD::Ustar::SoftLink"; | ||||
| 439 | } elsif (-p _) { | ||||
| 440 | $class = "OpenBSD::Ustar::Fifo"; | ||||
| 441 | } elsif (-c _) { | ||||
| 442 | $class = "OpenBSD::Ustar::CharDevice"; | ||||
| 443 | } elsif (-b _) { | ||||
| 444 | $class ="OpenBSD::Ustar::BlockDevice"; | ||||
| 445 | } elsif (-d _) { | ||||
| 446 | $class = "OpenBSD::Ustar::Dir"; | ||||
| 447 | } | ||||
| 448 | $self->new_object($entry, $class); | ||||
| 449 | if (!$entry->isFile) { | ||||
| 450 | $entry->{size} = 0; | ||||
| 451 | } | ||||
| 452 | return $entry; | ||||
| 453 | } | ||||
| 454 | |||||
| 455 | sub pad | ||||
| 456 | { | ||||
| 457 | my $self = shift; | ||||
| 458 | my $fh = $self->{fh}; | ||||
| 459 | print $fh "\0"x1024 or $self->fatal("Error writing to archive: #1", $!); | ||||
| 460 | } | ||||
| 461 | |||||
| 462 | sub close | ||||
| 463 | { | ||||
| 464 | my $self = shift; | ||||
| 465 | if (defined $self->{padout}) { | ||||
| 466 | $self->pad; | ||||
| 467 | } | ||||
| 468 | close($self->{fh}); | ||||
| 469 | } | ||||
| 470 | |||||
| 471 | sub destdir | ||||
| 472 | { | ||||
| 473 | my $self = shift; | ||||
| 474 | if (@_ > 0) { | ||||
| 475 | $self->{destdir} = shift; | ||||
| 476 | } else { | ||||
| 477 | return $self->{destdir}; | ||||
| 478 | } | ||||
| 479 | } | ||||
| 480 | |||||
| 481 | sub fh | ||||
| 482 | { | ||||
| 483 | return $_[0]->{fh}; | ||||
| 484 | } | ||||
| 485 | |||||
| 486 | package OpenBSD::Ustar::Object; | ||||
| 487 | |||||
| 488 | sub recheck_owner | ||||
| 489 | { | ||||
| 490 | my $entry = shift; | ||||
| 491 | # XXX weird format to prevent cvs from expanding OpenBSD id | ||||
| 492 | $entry->{uid} //= $OpenBSD::Ustar::uidcache | ||||
| 493 | ->lookup($entry->{uname}); | ||||
| 494 | $entry->{gid} //= $OpenBSD::Ustar::gidcache | ||||
| 495 | ->lookup($entry->{gname}); | ||||
| 496 | } | ||||
| 497 | |||||
| 498 | sub fatal | ||||
| 499 | { | ||||
| 500 | my ($self, @args) = @_; | ||||
| 501 | $self->{archive}->fatal(@args); | ||||
| 502 | } | ||||
| 503 | |||||
| 504 | sub system | ||||
| 505 | { | ||||
| 506 | my ($self, @args) = @_; | ||||
| 507 | $self->{archive}{state}->system(@args); | ||||
| 508 | } | ||||
| 509 | |||||
| 510 | sub errsay | ||||
| 511 | { | ||||
| 512 | my ($self, @args) = @_; | ||||
| 513 | $self->{archive}{state}->errsay(@args); | ||||
| 514 | } | ||||
| 515 | sub left_todo | ||||
| 516 | { | ||||
| 517 | my ($self, $toread) = @_; | ||||
| 518 | return if $toread == 0; | ||||
| 519 | return unless defined $self->{archive}{callback}; | ||||
| 520 | &{$self->{archive}{callback}}($self->{size} - $toread); | ||||
| 521 | } | ||||
| 522 | |||||
| 523 | sub name | ||||
| 524 | { | ||||
| 525 | my $self = shift; | ||||
| 526 | return $self->{name}; | ||||
| 527 | } | ||||
| 528 | |||||
| 529 | sub set_name | ||||
| 530 | { | ||||
| 531 | my ($self, $v) = @_; | ||||
| 532 | $self->{name} = $v; | ||||
| 533 | } | ||||
| 534 | |||||
| 535 | sub set_modes | ||||
| 536 | { | ||||
| 537 | my $self = shift; | ||||
| 538 | chown $self->{uid}, $self->{gid}, $self->{destdir}.$self->name; | ||||
| 539 | chmod $self->{mode}, $self->{destdir}.$self->name; | ||||
| 540 | if (defined $self->{mtime} || defined $self->{atime}) { | ||||
| 541 | utime $self->{atime} // time, $self->{mtime} // time, | ||||
| 542 | $self->{destdir}.$self->name; | ||||
| 543 | } | ||||
| 544 | } | ||||
| 545 | |||||
| 546 | sub ensure_dir | ||||
| 547 | { | ||||
| 548 | my ($self, $dir) = @_; | ||||
| 549 | return if -d $dir; | ||||
| 550 | $self->ensure_dir(File::Basename::dirname($dir)); | ||||
| 551 | if (mkdir($dir)) { | ||||
| 552 | return; | ||||
| 553 | } | ||||
| 554 | $self->fatal("Error making directory #1: #2", $dir, $!); | ||||
| 555 | } | ||||
| 556 | |||||
| 557 | sub make_basedir | ||||
| 558 | { | ||||
| 559 | my $self = shift; | ||||
| 560 | my $dir = $self->{destdir}.File::Basename::dirname($self->name); | ||||
| 561 | $self->ensure_dir($dir); | ||||
| 562 | } | ||||
| 563 | |||||
| 564 | sub write | ||||
| 565 | { | ||||
| 566 | my $self = shift; | ||||
| 567 | my $arc = $self->{archive}; | ||||
| 568 | my $out = $arc->{fh}; | ||||
| 569 | |||||
| 570 | $arc->{padout} = 1; | ||||
| 571 | my $header = $arc->mkheader($self, $self->type); | ||||
| 572 | print $out $header or $self->fatal("Error writing to archive: #1", $!); | ||||
| 573 | $self->write_contents($arc); | ||||
| 574 | my $k = $self->{key}; | ||||
| 575 | if (!defined $arc->{key}{$k}) { | ||||
| 576 | $arc->{key}{$k} = $self->name; | ||||
| 577 | } | ||||
| 578 | } | ||||
| 579 | |||||
| 580 | sub alias | ||||
| 581 | { | ||||
| 582 | my ($self, $arc, $alias) = @_; | ||||
| 583 | |||||
| 584 | my $k = $self->{archive}.":".$self->{archive}{cachename}; | ||||
| 585 | if (!defined $arc->{key}{$k}) { | ||||
| 586 | $arc->{key}{$k} = $alias; | ||||
| 587 | } | ||||
| 588 | } | ||||
| 589 | |||||
| 590 | sub write_contents | ||||
| 591 | { | ||||
| 592 | # only files have anything to write | ||||
| 593 | } | ||||
| 594 | |||||
| 595 | sub resolve_links | ||||
| 596 | { | ||||
| 597 | # only hard links must cheat | ||||
| 598 | } | ||||
| 599 | |||||
| 600 | sub copy_contents | ||||
| 601 | { | ||||
| 602 | # only files need copying | ||||
| 603 | } | ||||
| 604 | |||||
| 605 | sub copy | ||||
| 606 | { | ||||
| 607 | my ($self, $wrarc) = @_; | ||||
| 608 | my $out = $wrarc->{fh}; | ||||
| 609 | $self->resolve_links($wrarc); | ||||
| 610 | $wrarc->{padout} = 1; | ||||
| 611 | my $header = $wrarc->mkheader($self, $self->type); | ||||
| 612 | print $out $header or $self->fatal("Error writing to archive: #1", $!); | ||||
| 613 | |||||
| 614 | $self->copy_contents($wrarc); | ||||
| 615 | } | ||||
| 616 | |||||
| 617 | sub isDir() { 0 } | ||||
| 618 | sub isFile() { 0 } | ||||
| 619 | sub isDevice() { 0 } | ||||
| 620 | sub isFifo() { 0 } | ||||
| 621 | sub isLink() { 0 } | ||||
| 622 | sub isSymLink() { 0 } | ||||
| 623 | sub isHardLink() { 0 } | ||||
| 624 | |||||
| 625 | package OpenBSD::Ustar::Dir; | ||||
| 626 | 1 | 6µs | our @ISA=qw(OpenBSD::Ustar::Object); | ||
| 627 | |||||
| 628 | sub create | ||||
| 629 | { | ||||
| 630 | my $self = shift; | ||||
| 631 | $self->ensure_dir($self->{destdir}.$self->name); | ||||
| 632 | $self->set_modes; | ||||
| 633 | } | ||||
| 634 | |||||
| 635 | sub isDir() { 1 } | ||||
| 636 | |||||
| 637 | sub type() { OpenBSD::Ustar::DIR } | ||||
| 638 | |||||
| 639 | package OpenBSD::Ustar::HardLink; | ||||
| 640 | 1 | 3µs | our @ISA=qw(OpenBSD::Ustar::Object); | ||
| 641 | |||||
| 642 | sub create | ||||
| 643 | { | ||||
| 644 | my $self = shift; | ||||
| 645 | $self->make_basedir; | ||||
| 646 | my $linkname = $self->{linkname}; | ||||
| 647 | if (defined $self->{cwd}) { | ||||
| 648 | $linkname=$self->{cwd}.'/'.$linkname; | ||||
| 649 | } | ||||
| 650 | link $self->{destdir}.$linkname, $self->{destdir}.$self->name or | ||||
| 651 | $self->fatal("Can't link #1#2 to #1#3: #4", | ||||
| 652 | $self->{destdir}, $linkname, $self->name, $!); | ||||
| 653 | } | ||||
| 654 | |||||
| 655 | sub resolve_links | ||||
| 656 | { | ||||
| 657 | my ($self, $arc) = @_; | ||||
| 658 | |||||
| 659 | my $k = $self->{archive}.":".$self->{linkname}; | ||||
| 660 | if (defined $arc->{key}{$k}) { | ||||
| 661 | $self->{linkname} = $arc->{key}{$k}; | ||||
| 662 | } else { | ||||
| 663 | print join("\n", keys(%{$arc->{key}})), "\n"; | ||||
| 664 | $self->fatal("Can't copy link over: original for #1 NOT available", $k); | ||||
| 665 | } | ||||
| 666 | } | ||||
| 667 | |||||
| 668 | sub isLink() { 1 } | ||||
| 669 | sub isHardLink() { 1 } | ||||
| 670 | |||||
| 671 | sub type() { OpenBSD::Ustar::HARDLINK } | ||||
| 672 | |||||
| 673 | package OpenBSD::Ustar::SoftLink; | ||||
| 674 | 1 | 3µs | our @ISA=qw(OpenBSD::Ustar::Object); | ||
| 675 | |||||
| 676 | sub create | ||||
| 677 | { | ||||
| 678 | my $self = shift; | ||||
| 679 | $self->make_basedir; | ||||
| 680 | symlink $self->{linkname}, $self->{destdir}.$self->name or | ||||
| 681 | $self->fatal("Can't symlink #1 to #2#3: #4", | ||||
| 682 | $self->{linkname}, $self->{destdir}, $self->name, $!); | ||||
| 683 | require POSIX; | ||||
| 684 | POSIX::lchown($self->{uid}, $self->{gid}, $self->{destdir}.$self->name); | ||||
| 685 | } | ||||
| 686 | |||||
| 687 | sub isLink() { 1 } | ||||
| 688 | sub isSymLink() { 1 } | ||||
| 689 | |||||
| 690 | sub type() { OpenBSD::Ustar::SOFTLINK } | ||||
| 691 | |||||
| 692 | package OpenBSD::Ustar::Fifo; | ||||
| 693 | 1 | 3µs | our @ISA=qw(OpenBSD::Ustar::Object); | ||
| 694 | |||||
| 695 | sub create | ||||
| 696 | { | ||||
| 697 | my $self = shift; | ||||
| 698 | $self->make_basedir; | ||||
| 699 | require POSIX; | ||||
| 700 | POSIX::mkfifo($self->{destdir}.$self->name, $self->{mode}) or | ||||
| 701 | $self->fatal("Can't create fifo #1#2: #3", $self->{destdir}, | ||||
| 702 | $self->name, $!); | ||||
| 703 | $self->set_modes; | ||||
| 704 | } | ||||
| 705 | |||||
| 706 | sub isFifo() { 1 } | ||||
| 707 | sub type() { OpenBSD::Ustar::FIFO } | ||||
| 708 | |||||
| 709 | package OpenBSD::UStar::Device; | ||||
| 710 | 1 | 2µs | our @ISA=qw(OpenBSD::Ustar::Object); | ||
| 711 | |||||
| 712 | sub create | ||||
| 713 | { | ||||
| 714 | my $self = shift; | ||||
| 715 | $self->make_basedir; | ||||
| 716 | $self->system(OpenBSD::Paths->mknod, | ||||
| 717 | '-m', $self->{mode}, '--', $self->{destdir}.$self->name, | ||||
| 718 | $self->devicetype, $self->{major}, $self->{minor}); | ||||
| 719 | $self->set_modes; | ||||
| 720 | } | ||||
| 721 | |||||
| 722 | sub isDevice() { 1 } | ||||
| 723 | |||||
| 724 | package OpenBSD::Ustar::BlockDevice; | ||||
| 725 | 1 | 4µs | our @ISA=qw(OpenBSD::Ustar::Device); | ||
| 726 | |||||
| 727 | sub type() { OpenBSD::Ustar::BLOCKDEVICE } | ||||
| 728 | sub devicetype() { 'b' } | ||||
| 729 | |||||
| 730 | package OpenBSD::Ustar::CharDevice; | ||||
| 731 | 1 | 3µs | our @ISA=qw(OpenBSD::Ustar::Device); | ||
| 732 | |||||
| 733 | sub type() { OpenBSD::Ustar::BLOCKDEVICE } | ||||
| 734 | sub devicetype() { 'c' } | ||||
| 735 | |||||
| 736 | package OpenBSD::CompactWriter; | ||||
| 737 | |||||
| 738 | # spent 166µs (14+153) within OpenBSD::CompactWriter::BEGIN@738 which was called:
# once (14µs+153µs) by OpenBSD::PackageLocation::_opened at line 743 | ||||
| 739 | 1 | 1µs | FH => 0, | ||
| 740 | BS => 1, | ||||
| 741 | ZEROES => 2, | ||||
| 742 | UNFINISHED => 3, | ||||
| 743 | 1 | 1.03ms | 2 | 319µs | }; # spent 166µs making 1 call to OpenBSD::CompactWriter::BEGIN@738
# spent 153µs making 1 call to constant::import |
| 744 | |||||
| 745 | sub new | ||||
| 746 | { | ||||
| 747 | my ($class, $fname) = @_; | ||||
| 748 | open (my $out, '>', $fname) or return; | ||||
| 749 | my $bs = (stat $out)[11]; | ||||
| 750 | my $zeroes; | ||||
| 751 | if (defined $bs) { | ||||
| 752 | $zeroes = "\x00"x$bs; | ||||
| 753 | } | ||||
| 754 | bless [ $out, $bs, $zeroes, 0 ], $class; | ||||
| 755 | } | ||||
| 756 | |||||
| 757 | sub write | ||||
| 758 | { | ||||
| 759 | my ($self, $buffer) = @_; | ||||
| 760 | my ($fh, $bs, $zeroes, $e) = @$self; | ||||
| 761 | START: | ||||
| 762 | if (defined $bs) { | ||||
| 763 | for (my $i = 0; $i + $bs <= length($buffer); $i+= $bs) { | ||||
| 764 | if (substr($buffer, $i, $bs) eq $zeroes) { | ||||
| 765 | my $r = syswrite($fh, $buffer, $i); | ||||
| 766 | unless (defined $r && $r == $i) { | ||||
| 767 | return 0; | ||||
| 768 | } | ||||
| 769 | $i+=$bs; | ||||
| 770 | my $seek_forward = $bs; | ||||
| 771 | while (substr($buffer, $i, $bs) eq $zeroes) { | ||||
| 772 | $i += $bs; | ||||
| 773 | $seek_forward += $bs; | ||||
| 774 | } | ||||
| 775 | defined(sysseek($fh, $seek_forward, 1)) | ||||
| 776 | or return 0; | ||||
| 777 | $buffer = substr($buffer, $i); | ||||
| 778 | if (length $buffer == 0) { | ||||
| 779 | $self->[UNFINISHED] = 1; | ||||
| 780 | return 1; | ||||
| 781 | } | ||||
| 782 | goto START; | ||||
| 783 | } | ||||
| 784 | } | ||||
| 785 | } | ||||
| 786 | $self->[UNFINISHED] = 0; | ||||
| 787 | my $r = syswrite($fh, $buffer); | ||||
| 788 | if (defined $r && $r == length $buffer) { | ||||
| 789 | return 1; | ||||
| 790 | } else { | ||||
| 791 | return 0; | ||||
| 792 | } | ||||
| 793 | } | ||||
| 794 | |||||
| 795 | sub close | ||||
| 796 | { | ||||
| 797 | my ($self) = @_; | ||||
| 798 | if ($self->[UNFINISHED]) { | ||||
| 799 | defined(sysseek($self->[FH], -1, 1)) or return 0; | ||||
| 800 | defined(syswrite($self->[FH], "\0")) or return 0; | ||||
| 801 | } | ||||
| 802 | return 1; | ||||
| 803 | } | ||||
| 804 | |||||
| 805 | package OpenBSD::Ustar::File; | ||||
| 806 | 1 | 2µs | our @ISA=qw(OpenBSD::Ustar::Object); | ||
| 807 | |||||
| 808 | sub create | ||||
| 809 | { | ||||
| 810 | my $self = shift; | ||||
| 811 | $self->make_basedir; | ||||
| 812 | my $buffer; | ||||
| 813 | my $out = OpenBSD::CompactWriter->new($self->{destdir}.$self->name); | ||||
| 814 | if (!defined $out) { | ||||
| 815 | $self->fatal("Can't write to #1#2: #3", $self->{destdir}, | ||||
| 816 | $self->name, $!); | ||||
| 817 | } | ||||
| 818 | my $toread = $self->{size}; | ||||
| 819 | if ($self->{partial}) { | ||||
| 820 | $toread -= length($self->{partial}); | ||||
| 821 | unless ($out->write($self->{partial})) { | ||||
| 822 | $self->fatal("Error writing to #1#2: #3", | ||||
| 823 | $self->{destdir}, $self->name, $!); | ||||
| 824 | } | ||||
| 825 | } | ||||
| 826 | while ($toread > 0) { | ||||
| 827 | my $maxread = $buffsize; | ||||
| 828 | $maxread = $toread if $maxread > $toread; | ||||
| 829 | my $actual = read($self->{archive}{fh}, $buffer, $maxread); | ||||
| 830 | if (!defined $actual) { | ||||
| 831 | $self->fatal("Error reading from archive: #1", $!); | ||||
| 832 | } | ||||
| 833 | if ($actual == 0) { | ||||
| 834 | $self->fatal("Premature end of archive"); | ||||
| 835 | } | ||||
| 836 | $self->{archive}{swallow} -= $actual; | ||||
| 837 | unless ($out->write($buffer)) { | ||||
| 838 | $self->fatal("Error writing to #1#2: #3", | ||||
| 839 | $self->{destdir}, $self->name, $!); | ||||
| 840 | } | ||||
| 841 | |||||
| 842 | $toread -= $actual; | ||||
| 843 | $self->left_todo($toread); | ||||
| 844 | } | ||||
| 845 | $out->close or $self->fatal("Error closing #1#2: #3", | ||||
| 846 | $self->{destdir}, $self->name, $!); | ||||
| 847 | $self->set_modes; | ||||
| 848 | } | ||||
| 849 | |||||
| 850 | sub contents | ||||
| 851 | # spent 124µs (44+80) within OpenBSD::Ustar::File::contents which was called 2 times, avg 62µs/call:
# 2 times (44µs+80µs) by OpenBSD::PackageLocation::find_contents at line 119 of OpenBSD/PackageLocation.pm, avg 62µs/call | ||||
| 852 | 2 | 2µs | my ($self, $lookfor) = @_; | ||
| 853 | 2 | 2µs | my $toread = $self->{size}; | ||
| 854 | 2 | 2µs | my $buffer; | ||
| 855 | 2 | 1µs | my $offset = 0; | ||
| 856 | 2 | 2µs | if ($self->{partial}) { | ||
| 857 | $buffer = $self->{partial}; | ||||
| 858 | $offset = length($self->{partial}); | ||||
| 859 | $toread -= $offset; | ||||
| 860 | } | ||||
| 861 | |||||
| 862 | 2 | 3µs | while ($toread != 0) { | ||
| 863 | 2 | 1µs | my $sz = $toread; | ||
| 864 | 2 | 2µs | if (defined $lookfor) { | ||
| 865 | last if (defined $buffer) and &$lookfor($buffer); | ||||
| 866 | $sz = 1024 if $sz > 1024; | ||||
| 867 | } | ||||
| 868 | 2 | 16µs | 4 | 150µs | my $actual = read($self->{archive}{fh}, $buffer, $sz, $offset); # spent 80µs making 2 calls to OpenBSD::Ustar::File::CORE:read, avg 40µs/call
# spent 70µs making 2 calls to IO::Uncompress::Base::read, avg 35µs/call |
| 869 | 2 | 1µs | if (!defined $actual) { | ||
| 870 | $self->fatal("Error reading from archive: #1", $!); | ||||
| 871 | } | ||||
| 872 | 2 | 2µs | if ($actual != $sz) { | ||
| 873 | $self->fatal("Error: short read from archive"); | ||||
| 874 | } | ||||
| 875 | 2 | 2µs | $self->{archive}{swallow} -= $actual; | ||
| 876 | 2 | 2µs | $toread -= $actual; | ||
| 877 | 2 | 3µs | $offset += $actual; | ||
| 878 | } | ||||
| 879 | |||||
| 880 | 2 | 2µs | $self->{partial} = $buffer; | ||
| 881 | 2 | 15µs | return $buffer; | ||
| 882 | } | ||||
| 883 | |||||
| 884 | sub write_contents | ||||
| 885 | { | ||||
| 886 | my ($self, $arc) = @_; | ||||
| 887 | my $filename = $self->{realname}; | ||||
| 888 | my $size = $self->{size}; | ||||
| 889 | my $out = $arc->{fh}; | ||||
| 890 | open my $fh, "<", $filename or $self->fatal("Can't read file #1: #2", | ||||
| 891 | $filename, $!); | ||||
| 892 | |||||
| 893 | my $buffer; | ||||
| 894 | my $toread = $size; | ||||
| 895 | while ($toread > 0) { | ||||
| 896 | my $maxread = $buffsize; | ||||
| 897 | $maxread = $toread if $maxread > $toread; | ||||
| 898 | my $actual = read($fh, $buffer, $maxread); | ||||
| 899 | if (!defined $actual) { | ||||
| 900 | $self->fatal("Error reading from file: #1", $!); | ||||
| 901 | } | ||||
| 902 | if ($actual == 0) { | ||||
| 903 | $self->fatal("Premature end of file"); | ||||
| 904 | } | ||||
| 905 | unless (print $out $buffer) { | ||||
| 906 | $self->fatal("Error writing to archive: #1", $!); | ||||
| 907 | } | ||||
| 908 | |||||
| 909 | $toread -= $actual; | ||||
| 910 | $self->left_todo($toread); | ||||
| 911 | } | ||||
| 912 | if ($size % 512) { | ||||
| 913 | print $out "\0" x (512 - $size % 512) or | ||||
| 914 | $self->fatal("Error writing to archive: #1", $!); | ||||
| 915 | } | ||||
| 916 | } | ||||
| 917 | |||||
| 918 | sub copy_contents | ||||
| 919 | { | ||||
| 920 | my ($self, $arc) = @_; | ||||
| 921 | my $out = $arc->{fh}; | ||||
| 922 | my $buffer; | ||||
| 923 | my $size = $self->{size}; | ||||
| 924 | my $toread = $size; | ||||
| 925 | while ($toread > 0) { | ||||
| 926 | my $maxread = $buffsize; | ||||
| 927 | $maxread = $toread if $maxread > $toread; | ||||
| 928 | my $actual = read($self->{archive}{fh}, $buffer, $maxread); | ||||
| 929 | if (!defined $actual) { | ||||
| 930 | $self->fatal("Error reading from archive: #1", $!); | ||||
| 931 | } | ||||
| 932 | if ($actual == 0) { | ||||
| 933 | $self->fatal("Premature end of archive"); | ||||
| 934 | } | ||||
| 935 | $self->{archive}{swallow} -= $actual; | ||||
| 936 | unless (print $out $buffer) { | ||||
| 937 | $self->fatal("Error writing to archive #1", $!); | ||||
| 938 | } | ||||
| 939 | |||||
| 940 | $toread -= $actual; | ||||
| 941 | } | ||||
| 942 | if ($size % 512) { | ||||
| 943 | print $out "\0" x (512 - $size % 512) or | ||||
| 944 | $self->fatal("Error writing to archive: #1", $!); | ||||
| 945 | } | ||||
| 946 | $self->alias($arc, $self->name); | ||||
| 947 | } | ||||
| 948 | |||||
| 949 | sub isFile() { 1 } | ||||
| 950 | |||||
| 951 | sub type() { OpenBSD::Ustar::FILE1 } | ||||
| 952 | |||||
| 953 | 1 | 10µs | 1; | ||
# spent 6µs within OpenBSD::Ustar::CORE:match which was called 2 times, avg 3µs/call:
# 2 times (6µs+0s) by OpenBSD::Ustar::next at line 224, avg 3µs/call | |||||
# spent 127µs (37+90) within OpenBSD::Ustar::CORE:read which was called 2 times, avg 63µs/call:
# 2 times (37µs+90µs) by OpenBSD::Ustar::next at line 188, avg 63µs/call | |||||
# spent 28µs within OpenBSD::Ustar::CORE:subst which was called 8 times, avg 4µs/call:
# 2 times (16µs+0s) by OpenBSD::Ustar::next at line 209, avg 8µs/call
# 2 times (6µs+0s) by OpenBSD::Ustar::next at line 211, avg 3µs/call
# 2 times (4µs+0s) by OpenBSD::Ustar::next at line 212, avg 2µs/call
# 2 times (3µs+0s) by OpenBSD::Ustar::next at line 213, avg 1µs/call | |||||
sub OpenBSD::Ustar::CORE:unpack; # opcode | |||||
# spent 80µs (10+70) within OpenBSD::Ustar::File::CORE:read which was called 2 times, avg 40µs/call:
# 2 times (10µs+70µs) by OpenBSD::Ustar::File::contents at line 868, avg 40µs/call | |||||
# spent 4µs within OpenBSD::Ustar::File::isFile which was called 4 times, avg 1µs/call:
# 2 times (3µs+0s) by OpenBSD::Ustar::next at line 262, avg 1µs/call
# 2 times (2µs+0s) by OpenBSD::PackageLocation::find_contents at line 116 of OpenBSD/PackageLocation.pm, avg 950ns/call |