| 1 | # Copyright © 2023-2024 Guillem Jover <guillem@debian.org> | ||
| 2 | # | ||
| 3 | # This program is free software; you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 15 | |||
| 16 | =encoding utf8 | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Dpkg::Archive::Ar - Unix ar archive support | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This module provides a class to handle Unix ar archives. | ||
| 25 | It support the common format, with no GNU or BSD extensions. | ||
| 26 | |||
| 27 | B<Note>: This is a private module, its API can change at any time. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | package Dpkg::Archive::Ar 0.01; | ||
| 32 | |||
| 33 | use v5.36; | ||
| 34 | |||
| 35 | use Carp; | ||
| 36 | use Fcntl qw(:seek); | ||
| 37 | use IO::File; | ||
| 38 | |||
| 39 | use Dpkg::ErrorHandling; | ||
| 40 | use Dpkg::Gettext; | ||
| 41 | |||
| 42 | my $AR_MAGIC = "!<arch>\n"; | ||
| 43 | my $AR_MAGIC_LEN = 8; | ||
| 44 | my $AR_FMAG = "\`\n"; | ||
| 45 | my $AR_HDR_LEN = 60; | ||
| 46 | |||
| 47 | =head1 METHODS | ||
| 48 | |||
| 49 | =over 8 | ||
| 50 | |||
| 51 | =item $ar = Dpkg::Archive::Ar->new(%opts) | ||
| 52 | |||
| 53 | Create a new object to handle Unix ar archives. | ||
| 54 | |||
| 55 | Options: | ||
| 56 | |||
| 57 | =over 8 | ||
| 58 | |||
| 59 | =item B<filename> | ||
| 60 | |||
| 61 | The filename for the archive to open or create. | ||
| 62 | |||
| 63 | =item B<create> | ||
| 64 | |||
| 65 | A boolean denoting whether the archive should be created, | ||
| 66 | otherwise if it does not exist the constructor will not open, create or | ||
| 67 | scan it. | ||
| 68 | |||
| 69 | =back | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub new { | ||
| 74 | my ($this, %opts) = @_; | ||
| 75 | my $class = ref($this) || $this; | ||
| 76 | my $self = { | ||
| 77 | filename => undef, | ||
| 78 | fh => undef, | ||
| 79 | # XXX: If we promote this out from internal use, we should make this | ||
| 80 | # default to the archive mtime, or be overridable like in libdpkg, | ||
| 81 | # so that it can be initialized from SOURCE_DATE_EPOCH for example. | ||
| 82 | time => 0, | ||
| 83 | size => 0, | ||
| 84 | members => [], | ||
| 85 | }; | ||
| 86 | bless $self, $class; | ||
| 87 | |||
| 88 | if ($opts{filename}) { | ||
| 89 | if ($opts{create}) { | ||
| 90 | $self->create_archive($opts{filename}); | ||
| 91 | } elsif (-e $opts{filename}) { | ||
| 92 | $self->open_archive($opts{filename}); | ||
| 93 | } | ||
| 94 | if (-e $opts{filename}) { | ||
| 95 | $self->scan_archive(); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub init_archive { | ||
| 103 | my $self = shift; | ||
| 104 | |||
| 105 | $self->{fh}->binmode(); | ||
| 106 | $self->{fh}->stat | ||
| 107 | or syserr(g_('cannot get archive %s size'), $self->{filename}); | ||
| 108 | $self->{size} = -s _; | ||
| 109 | if ($self->{size} % 2) { | ||
| 110 | error(g_('archive %s is truncated or corrupt, ' . | ||
| 111 | 'it has a non-even byte size'), $self->{filename}); | ||
| 112 | } | ||
| 113 | |||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | =item $ar->create_archive($filename) | ||
| 118 | |||
| 119 | Create the archive. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub create_archive { | ||
| 124 | my ($self, $filename) = @_; | ||
| 125 | |||
| 126 | if (defined $self->{fh}) { | ||
| 127 | croak 'the object has already been initialized with another file'; | ||
| 128 | } | ||
| 129 | |||
| 130 | $self->{filename} = $filename; | ||
| 131 | $self->{fh} = IO::File->new($filename, '+>') | ||
| 132 | or syserr(g_('cannot open or create archive %s'), $filename); | ||
| 133 | $self->init_archive(); | ||
| 134 | $self->{fh}->write($AR_MAGIC, $AR_MAGIC_LEN) | ||
| 135 | or syserr(g_('cannot write magic into archive %s'), $filename); | ||
| 136 | |||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | =item $ar->open_archive($filename) | ||
| 141 | |||
| 142 | Open the archive. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub open_archive { | ||
| 147 | my ($self, $filename) = @_; | ||
| 148 | |||
| 149 | if (defined $self->{fh}) { | ||
| 150 | croak 'the object has already been initialized with another file'; | ||
| 151 | } | ||
| 152 | |||
| 153 | $self->{filename} = $filename; | ||
| 154 | $self->{fh} = IO::File->new($filename, '+<') | ||
| 155 | or syserr(g_('cannot open or create archive %s'), $filename); | ||
| 156 | $self->init_archive(); | ||
| 157 | |||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _read_buf { | ||
| 162 | my ($self, $subject, $size) = @_; | ||
| 163 | |||
| 164 | my $buf; | ||
| 165 | my $offs = $self->{fh}->tell(); | ||
| 166 | my $n = $self->{fh}->read($buf, $size); | ||
| 167 | if (not defined $n) { | ||
| 168 | # TRANSLATORS: The first %s string is either "the archive magic" or | ||
| 169 | # "a file header". | ||
| 170 | syserr(g_('cannot read %s; archive %s at offset %d'), | ||
| 171 | $subject, $offs, $self->{filename}); | ||
| 172 | } elsif ($n == 0) { | ||
| 173 | return; | ||
| 174 | } elsif ($n != $size) { | ||
| 175 | # TRANSLATORS: The first %s string is either "the archive magic" or | ||
| 176 | # "a file header". | ||
| 177 | error(g_('cannot read %s; archive %s is truncated at offset %d'), | ||
| 178 | $subject, $offs, $self->{filename}); | ||
| 179 | } | ||
| 180 | |||
| 181 | return $buf; | ||
| 182 | } | ||
| 183 | |||
| 184 | =item $ar->parse_magic() | ||
| 185 | |||
| 186 | Reads and parses the archive magic string, and validates it. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub parse_magic { | ||
| 191 | my $self = shift; | ||
| 192 | |||
| 193 | my $magic = $self->_read_buf(g_('the archive magic'), $AR_MAGIC_LEN) | ||
| 194 | or error(g_('archive %s contains no magic'), $self->{filename}); | ||
| 195 | |||
| 196 | if ($magic ne $AR_MAGIC) { | ||
| 197 | error(g_('archive %s contains bad magic'), $self->{filename}); | ||
| 198 | } | ||
| 199 | |||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | =item $ar->parse_member() | ||
| 204 | |||
| 205 | Reads and parses the archive member and tracks it for later handling. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub parse_member { | ||
| 210 | my $self = shift; | ||
| 211 | |||
| 212 | my $offs = $self->{fh}->tell(); | ||
| 213 | |||
| 214 | my $hdr = $self->_read_buf(g_('a file header'), $AR_HDR_LEN) | ||
| 215 | or return; | ||
| 216 | |||
| 217 | my %hdr; | ||
| 218 | my $hdr_fmt = 'A16A12A6A6A8A10a2'; | ||
| 219 | @hdr{qw(name time uid gid mode size fmag)} = unpack $hdr_fmt, $hdr; | ||
| 220 | |||
| 221 | if ($hdr{fmag} ne $AR_FMAG) { | ||
| 222 | error(g_('file header at offset %d in archive %s contains bad magic'), | ||
| 223 | $offs, $self->{filename}); | ||
| 224 | } | ||
| 225 | |||
| 226 | # Remove trailing spaces from the member name. | ||
| 227 | $hdr{name} =~ s{ *$}{}; | ||
| 228 | |||
| 229 | # Remove optional slash terminator (on GNU-style archives). | ||
| 230 | $hdr{name} =~ s{/$}{}; | ||
| 231 | |||
| 232 | foreach my $f (qw(name time uid gid mode size fmag)) { | ||
| 233 | if (not length $hdr{$f}) { | ||
| 234 | error(g_('file member %s in archive %s contains empty %s field'), | ||
| 235 | $hdr{name}, $self->{filename}, $f); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | my $member = { | ||
| 240 | name => $hdr{name}, | ||
| 241 | time => int $hdr{time}, | ||
| 242 | uid => int $hdr{uid}, | ||
| 243 | gid => int $hdr{gid}, | ||
| 244 | mode => oct $hdr{mode}, | ||
| 245 | size => int $hdr{size}, | ||
| 246 | offs => $offs, | ||
| 247 | }; | ||
| 248 | push @{$self->{members}}, $member; | ||
| 249 | |||
| 250 | return $member; | ||
| 251 | } | ||
| 252 | |||
| 253 | =item $ar->skip_member($member) | ||
| 254 | |||
| 255 | Skip this member to the next one. | ||
| 256 | Get the value of a given substitution. | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub skip_member { | ||
| 261 | my ($self, $member) = @_; | ||
| 262 | |||
| 263 | my $size = $member->{size}; | ||
| 264 | my $offs = $member->{offs} + $AR_HDR_LEN + $size + ($size & 1); | ||
| 265 | |||
| 266 | $self->{fh}->seek($offs, SEEK_SET) | ||
| 267 | or syserr(g_('cannot seek into next file header at offset %d from archive %s'), | ||
| 268 | $offs, $self->{filename}); | ||
| 269 | |||
| 270 | return; | ||
| 271 | } | ||
| 272 | |||
| 273 | =item $ar->scan_archive() | ||
| 274 | |||
| 275 | Scan the archive for all its member files and metadata. | ||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | sub scan_archive { | ||
| 280 | my $self = shift; | ||
| 281 | |||
| 282 | $self->{fh}->seek(0, SEEK_SET) | ||
| 283 | or syserr(g_('cannot seek into beginning of archive %s'), | ||
| 284 | $self->{filename}); | ||
| 285 | |||
| 286 | $self->parse_magic(); | ||
| 287 | |||
| 288 | while (my $member = $self->parse_member()) { | ||
| 289 | $self->skip_member($member); | ||
| 290 | } | ||
| 291 | |||
| 292 | return; | ||
| 293 | } | ||
| 294 | |||
| 295 | =item $ar->get_members() | ||
| 296 | |||
| 297 | Get the list of members in the archive. | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | sub get_members { | ||
| 302 | my $self = shift; | ||
| 303 | |||
| 304 | return $self->{members}; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub _copy_fh_fh { | ||
| 308 | my ($self, $if, $of, $size) = @_; | ||
| 309 | |||
| 310 | while ($size > 0) { | ||
| 311 | my $buf; | ||
| 312 | my $buflen = $size > 4096 ? 4096 : $size; | ||
| 313 | |||
| 314 | my $n = $if->{fh}->read($buf, $buflen) | ||
| 315 | or syserr(g_('cannot read file %s'), $if->{name}); | ||
| 316 | |||
| 317 | $of->{fh}->write($buf, $n) | ||
| 318 | or syserr(g_('cannot write file %s'), $of->{name}); | ||
| 319 | |||
| 320 | $size -= $n; | ||
| 321 | } | ||
| 322 | |||
| 323 | return; | ||
| 324 | } | ||
| 325 | |||
| 326 | =item $ar->extract_member($member) | ||
| 327 | |||
| 328 | Extract the specified member to the current directory. | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | sub extract_member { | ||
| 333 | my ($self, $member) = @_; | ||
| 334 | |||
| 335 | $self->{fh}->seek($member->{offs} + $AR_HDR_LEN, SEEK_SET); | ||
| 336 | |||
| 337 | my $ofh = IO::File->new($member->{name}, '+>') | ||
| 338 | or syserr(g_('cannot create file %s to extract from archive %s'), | ||
| 339 | $member->{name}, $self->{filename}); | ||
| 340 | |||
| 341 | $self->_copy_fh_fh({ fh => $self->{fh}, name => $self->{filename} }, | ||
| 342 | { fh => $ofh, name => $member->{name} }, | ||
| 343 | $member->{size}); | ||
| 344 | |||
| 345 | $ofh->close() | ||
| 346 | or syserr(g_('cannot write file %s to the filesystem'), | ||
| 347 | $member->{name}); | ||
| 348 | |||
| 349 | return; | ||
| 350 | } | ||
| 351 | |||
| 352 | =item $ar->write_member($member) | ||
| 353 | |||
| 354 | Write the provided $member into the archive. | ||
| 355 | |||
| 356 | =cut | ||
| 357 | |||
| 358 | sub write_member { | ||
| 359 | my ($self, $member) = @_; | ||
| 360 | |||
| 361 | my $size = $member->{size}; | ||
| 362 | my $mode = sprintf '%o', $member->{mode}; | ||
| 363 | |||
| 364 | my $hdr_fmt = 'A16A12A6A6A8A10A2'; | ||
| 365 | my $data = pack $hdr_fmt, @{$member}{qw(name time uid gid)}, $mode, $size, $AR_FMAG; | ||
| 366 | |||
| 367 | $self->{fh}->write($data, $AR_HDR_LEN, $member->{offs}) | ||
| 368 | or syserr(g_('cannot write file header into archive %s'), | ||
| 369 | $self->{filename}); | ||
| 370 | |||
| 371 | return; | ||
| 372 | } | ||
| 373 | |||
| 374 | =item $ar->add_file($filename) | ||
| 375 | |||
| 376 | Append the specified $filename into the archive. | ||
| 377 | |||
| 378 | =cut | ||
| 379 | |||
| 380 | sub add_file { | ||
| 381 | my ($self, $filename) = @_; | ||
| 382 | |||
| 383 | if (length $filename > 15) { | ||
| 384 | error(g_('filename %s is too long'), $filename); | ||
| 385 | } | ||
| 386 | |||
| 387 | my $fh = IO::File->new($filename, '<') | ||
| 388 | or syserr(g_('cannot open file %s to append to archive %s'), | ||
| 389 | $filename, $self->{filename}); | ||
| 390 | $fh->stat() | ||
| 391 | or syserr(g_('cannot get file %s size'), $filename); | ||
| 392 | my $size = -s _; | ||
| 393 | |||
| 394 | my %member = ( | ||
| 395 | name => $filename, | ||
| 396 | size => $size, | ||
| 397 | time => $self->{time}, | ||
| 398 | mode => 0o100644, | ||
| 399 | uid => 0, | ||
| 400 | gid => 0, | ||
| 401 | ); | ||
| 402 | |||
| 403 | $self->write_member(\%member); | ||
| 404 | $self->_copy_fh_fh({ fh => $fh, name => $filename }, | ||
| 405 | { fh => $self->{fh}, name => $self->{filename} }, | ||
| 406 | $size); | ||
| 407 | if ($size & 1) { | ||
| 408 | $self->{fh}->write("\n", 1) | ||
| 409 | or syserr(g_('cannot write file %s padding to archive %s'), | ||
| 410 | $filename, $self->{filename}); | ||
| 411 | } | ||
| 412 | |||
| 413 | return; | ||
| 414 | } | ||
| 415 | |||
| 416 | =item $ar->close_archive() | ||
| 417 | |||
| 418 | Close the archive and release any allocated resource. | ||
| 419 | |||
| 420 | =cut | ||
| 421 | |||
| 422 | sub close_archive { | ||
| 423 | my $self = shift; | ||
| 424 | |||
| 425 | $self->{fh}->close() if defined $self->{fh}; | ||
| 426 | $self->{fh} = undef; | ||
| 427 | $self->{size} = 0; | ||
| 428 | $self->{members} = []; | ||
| 429 | |||
| 430 | return; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub DESTROY { | ||
| 434 | my $self = shift; | ||
| 435 | |||
| 436 | $self->close_archive(); | ||
| 437 | |||
| 438 | return; | ||
| 439 | } | ||
| 440 | |||
| 441 | =back | ||
| 442 | |||
| 443 | =head1 CHANGES | ||
| 444 | |||
| 445 | =head2 Version 0.xx | ||
| 446 | |||
| 447 | This is a private module. | ||
| 448 | |||
| 449 | =cut | ||
| 450 | |||
| 451 | 1; |