/usr/share/perl5/Archive/Ar.pm is in libarchive-ar-perl 1.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | package Archive::Ar;
###########################################################
# Archive::Ar - Pure perl module to handle ar achives
#
# Copyright 2003 - Jay Bonci <jaybonci@cpan.org>
# Licensed under the same terms as perl itself
#
###########################################################
use strict;
use Exporter;
use File::Spec;
use Time::Local;
use vars qw($VERSION);
$VERSION = '1.15';
use constant ARMAG => "!<arch>\n";
use constant SARMAG => length(ARMAG);
use constant ARFMAG => "`\n";
sub new {
my ($class, $filenameorhandle, $debug) = @_;
my $this = {};
my $obj = bless $this, $class;
$obj->{_verbose} = 0;
$obj->_initValues();
if($debug)
{
$obj->DEBUG();
}
if($filenameorhandle){
unless($obj->read($filenameorhandle)){
$obj->_dowarn("new() failed on filename or filehandle read");
return;
}
}
return $obj;
}
sub read
{
my ($this, $filenameorhandle) = @_;
my $retval;
$this->_initValues();
if(ref $filenameorhandle eq "GLOB")
{
unless($retval = $this->_readFromFilehandle($filenameorhandle))
{
$this->_dowarn("Read from filehandle failed");
return;
}
}else
{
unless($retval = $this->_readFromFilename($filenameorhandle))
{
$this->_dowarn("Read from filename failed");
return;
}
}
unless($this->_parseData())
{
$this->_dowarn("read() failed on data structure analysis. Probable bad file");
return;
}
return $retval;
}
sub read_memory
{
my ($this, $data) = @_;
$this->_initValues();
unless($data)
{
$this->_dowarn("read_memory() can't continue because no data was given");
return;
}
$this->{_filedata} = $data;
unless($this->_parseData())
{
$this->_dowarn("read_memory() failed on data structure analysis. Probable bad file");
return;
}
return length($data);
}
sub remove
{
my($this, $filenameorarray, @otherfiles) = @_;
my $filelist;
if(ref $filenameorarray eq "ARRAY")
{
$filelist = $filenameorarray;
}else{
$filelist = [$filenameorarray];
if(@otherfiles)
{
push @$filelist, @otherfiles;
}
}
my $filecount = 0;
foreach my $file (@$filelist)
{
$filecount += $this->_remFile($file);
}
return $filecount;
}
sub list_files
{
my($this) = @_;
return wantarray ? @{$this->{_files}} : $this->{_files};
}
sub add_files
{
my($this, $filenameorarray, @otherfiles) = @_;
my $filelist;
if(ref $filenameorarray eq "ARRAY")
{
$filelist = $filenameorarray;
}else
{
$filelist = [$filenameorarray];
if(@otherfiles)
{
push @$filelist, @otherfiles;
}
}
my $filecount = 0;
foreach my $filename (@$filelist)
{
my @props = stat($filename);
unless(@props)
{
$this->_dowarn("Could not stat() filename. add_files() for this file failed");
next;
}
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = @props;
my $header = {
"date" => $mtime,
"uid" => $uid,
"gid" => $gid,
"mode" => $mode,
"size" => $size,
};
local $/ = undef;
unless(open HANDLE, $filename)
{
$this->_dowarn("Could not open filename. add_files() for this file failed");
next;
}
binmode HANDLE;
$header->{data} = <HANDLE>;
close HANDLE;
# fix the filename
(undef, undef, $filename) = File::Spec->splitpath($filename);
$header->{name} = $filename;
$this->_addFile($header);
$filecount++;
}
return $filecount;
}
sub add_data
{
my($this, $filename, $data, $params) = @_;
unless ($filename)
{
$this->_dowarn("No filename given; add_data() can't proceed");
return;
}
$params ||= {};
$data ||= "";
(undef, undef, $filename) = File::Spec->splitpath($filename);
$params->{name} = $filename;
$params->{size} = length($data);
$params->{data} = $data;
$params->{uid} ||= 0;
$params->{gid} ||= 0;
$params->{date} ||= timelocal(localtime());
$params->{mode} ||= 0100644;
unless($this->_addFile($params))
{
$this->_dowarn("add_data failed due to a failure in _addFile");
return;
}
return $params->{size};
}
sub write
{
my($this, $filename) = @_;
my $outstr;
$outstr= ARMAG;
foreach(@{$this->{_files}})
{
my $content = $this->get_content($_);
unless($content)
{
$this->_dowarn("Internal Error. $_ file in _files list but no filedata");
next;
}
# For whatever reason, the uids and gids get stripped
# if they are zero. We'll blank them here to emulate that
$content->{uid} ||= "";
$content->{gid} ||= "";
$outstr.= pack("A16A12A6A6A8A10",
@$content{qw/name date uid gid/},
sprintf('%o', $content->{mode}), # octal!
$content->{size});
$outstr.= ARFMAG;
$outstr.= $content->{data};
unless (((length($content->{data})) % 2) == 0) {
# Padding to make up an even number of bytes
$outstr.= "\n";
}
}
return $outstr unless $filename;
unless(open HANDLE, ">$filename")
{
$this->_dowarn("Can't open filename $filename");
return;
}
binmode HANDLE;
print HANDLE $outstr;
close HANDLE;
return length($outstr);
}
sub get_content
{
my ($this, $filename) = @_;
unless($filename)
{
$this->_dowarn("get_content can't continue without a filename");
return;
}
unless(exists($this->{_filehash}->{$filename}))
{
$this->_dowarn("get_content failed because there is not a file named $filename");
return;
}
return $this->{_filehash}->{$filename};
}
sub DEBUG
{
my($this, $verbose) = @_;
$verbose = 1 unless(defined($verbose) and int($verbose) == 0);
$this->{_verbose} = $verbose;
return;
}
sub _parseData
{
my($this) = @_;
unless($this->{_filedata})
{
$this->_dowarn("Cannot parse this archive. It appears to be blank");
return;
}
my $scratchdata = $this->{_filedata};
unless(substr($scratchdata, 0, SARMAG, "") eq ARMAG)
{
$this->_dowarn("Bad magic header token. Either this file is not an ar archive, or it is damaged. If you are sure of the file integrity, Archive::Ar may not support this type of ar archive currently. Please report this as a bug");
return "";
}
while($scratchdata =~ /\S/)
{
if($scratchdata =~ s/^(.{58})`\n//s)
{
my $headers = {};
@$headers{qw/name date uid gid mode size/} =
unpack("A16A12A6A6A8A10", $1);
for (values %$headers) {
$_ ||= "";
$_ =~ s/\s*$//;
}
$headers->{mode} = oct($headers->{mode});
$headers->{data} = substr($scratchdata, 0, $headers->{size}, "");
# delete padding, if any
substr($scratchdata, 0, $headers->{size} % 2, "");
$this->_addFile($headers);
}else{
$this->_dowarn("File format appears to be corrupt. The file header is not of the right size, or does not exist at all");
return;
}
}
return scalar($this->{_files});
}
sub _readFromFilename
{
my ($this, $filename) = @_;
my $handle;
open $handle, $filename or return;
binmode $handle;
return $this->_readFromFilehandle($handle);
}
sub _readFromFilehandle
{
my ($this, $filehandle) = @_;
return unless $filehandle;
#handle has to be open
return unless fileno $filehandle;
local $/ = undef;
$this->{_filedata} = <$filehandle>;
close $filehandle;
return length($this->{_filedata});
}
sub _addFile
{
my ($this, $file) = @_;
return unless $file;
foreach(qw/name date uid gid mode size data/)
{
unless(exists($file->{$_}))
{
$this->_dowarn("Can't _addFile because virtual file is missing $_ parameter");
return;
}
}
if(exists($this->{_filehash}->{$file->{name}}))
{
$this->_dowarn("Can't _addFile because virtual file already exists with that name in the archive");
return;
}
push @{$this->{_files}}, $file->{name};
$this->{_filehash}->{$file->{name}} = $file;
return $file->{name};
}
sub _remFile
{
my ($this, $filename) = @_;
return unless $filename;
if(exists($this->{_filehash}->{$filename}))
{
delete $this->{_filehash}->{$filename};
@{$this->{_files}} = grep(!/^$filename$/, @{$this->{_files}});
return 1;
}
$this->_dowarn("Can't remove file $filename, because it doesn't exist in the archive");
return 0;
}
sub _initValues
{
my ($this) = @_;
$this->{_files} = [];
$this->{_filehash} = {};
$this->{_filedata} ="";
return;
}
sub _dowarn
{
my ($this, $warning) = @_;
if($this->{_verbose})
{
warn "DEBUG: $warning";
}
return;
}
1;
__END__
=head1 NAME
Archive::Ar - Interface for manipulating ar archives
=head1 SYNOPSIS
use Archive::Ar;
my $ar = new Archive::Ar("./foo.ar");
$ar->add_data("newfile.txt","Some contents", $properties);
$ar->add_files("./bar.tar.gz", "bat.pl")
$ar->add_files(["./again.gz"]);
$ar->remove("file1", "file2");
$ar->remove(["file1", "file2");
my $filedata = $ar->get_content("bar.tar.gz");
my @files = $ar->list_files();
$ar->read("foo.deb");
$ar->write("outbound.ar");
$ar->DEBUG();
=head1 DESCRIPTION
Archive::Ar is a pure-perl way to handle standard ar archives.
This is useful if you have those types of old archives on the system, but it
is also useful because .deb packages for the Debian GNU/Linux distribution are
ar archives. This is one building block in a future chain of modules to build,
manipulate, extract, and test debian modules with no platform or architecture
dependence.
You may notice that the API to Archive::Ar is similar to Archive::Tar, and
this was done intentionally to keep similarity between the Archive::*
modules
=head2 Class Methods
=over 4
=item * C<new()>
=item * C<new(I<$filename>)>
=item * C<new(I<*GLOB>,I<$debug>)>
Returns a new Archive::Ar object. Without a filename or glob, it returns an
empty object. If passed a filename as a scalar or in a GLOB, it will attempt
to populate from either of those sources. If it fails, you will receive
undef, instead of an object reference.
This also can take a second optional debugging parameter. This acts exactly
as if C<DEBUG()> is called on the object before it is returned. If you have a
C<new()> that keeps failing, this should help.
=back
=over 4
=item * C<read(I<$filename>)>
=item * C<read(I<*GLOB>)>;
This reads a new file into the object, removing any ar archive already
represented in the object. Any calls to C<DEBUG()> are not lost by reading
in a new file. Returns the number of bytes read, undef on failure.
=back
=over 4
=item * C<read_memory(I<$data>)>
This read information from the first parameter, and attempts to parse and treat
it like an ar archive. Like C<read()>, it will wipe out whatever you have in the
object and replace it with the contents of the new archive, even if it fails.
Returns the number of bytes read (processed) if successful, undef otherwise.
=back
=over 4
=item * C<list_files()>
This lists the files contained inside of the archive by filename, as an array.
If called in a scalar context, returns a reference to an array.
=back
=over 4
=item * C<add_files(I<"filename1">, I<"filename2">)>
=item * C<add_files(I<["filename1", "filename2"]>)>
Takes an array or an arrayref of filenames to add to the ar archive, in order.
The filenames can be paths to files, in which case the path information is
stripped off. Filenames longer than 16 characters are truncated when written
to disk in the format, so keep that in mind when adding files.
Due to the nature of the ar archive format, C<add_files()> will store the uid,
gid, mode, size, and creation date of the file as returned by C<stat()>;
C<add_files()> returns the number of files successfully added, or undef on failure.
=back
=over 4
=item * C<add_data(I<"filename">, I<$filedata>)>
Takes an filename and a set of data to represent it. Unlike C<add_files>, C<add_data>
is a virtual add, and does not require data on disk to be present. The
data is a hash that looks like:
$filedata = {
"data" => $data,
"uid" => $uid, #defaults to zero
"gid" => $gid, #defaults to zero
"date" => $date, #date in epoch seconds. Defaults to now.
"mode" => $mode, #defaults to 0100644;
}
You cannot add_data over another file however. This returns the file length in
bytes if it is successful, undef otherwise.
=back
=over 4
=item * C<write()>
=item * C<write(I<"filename.ar">)>
This method will return the data as an .ar archive, or will write to the
filename present if specified. If given a filename, C<write()> will return the
length of the file written, in bytes, or undef on failure. If the filename
already exists, it will overwrite that file.
=back
=over 4
=item * C<get_content(I<"filename">)>
This returns a hash with the file content in it, including the data that the
file would naturally contain. If the file does not exist or no filename is
given, this returns undef. On success, a hash is returned with the following
keys:
name - The file name
date - The file date (in epoch seconds)
uid - The uid of the file
gid - The gid of the file
mode - The mode permissions
size - The size (in bytes) of the file
data - The contained data
=back
=over 4
=item * C<remove(I<"filename1">, I<"filename2">)>
=item * C<remove(I<["filename1", "filename2"]>)>
The remove method takes a filenames as a list or as an arrayref, and removes
them, one at a time, from the Archive::Ar object. This returns the number
of files successfully removed from the archive.
=back
=over 4
=item * C<DEBUG()>
This method turns on debugging. Optionally this can be done by passing in a
value as the second parameter to new. While verbosity is enabled,
Archive::Ar will toss a C<warn()> if there is a suspicious condition or other
problem while proceeding. This should help iron out any problems you have
while using the module.
=back
=head1 CHANGES
=over 4
=item * B<Version 1.15> - May 14, 2013
Use binmode for portability. Closes RT #81310 (thanks to Stanislav Meduna).
=item * B<Version 1.14> - October 14, 2009
Fix list_files to return a list in list context, to match doc.
Pad odd-size archives to an even number of bytes.
Closes RT #18383 (thanks to David Dick).
Fixed broken file perms (decimal mode stored as octal string).
Closes RT #49987 (thanks to Stephen Gran - debian bug #523515).
=item * B<Version 1.13b> - May 7th, 2003
Fixes to the Makefile.PL file. Ar.pm wasn't being put into /blib
Style fix to a line with non-standard unless parenthesis
=item * B<Version 1.13> - April 30th, 2003
Removed unneeded exports. Thanks to pudge for the pointer.
=item * B<Version 1.12> - April 14th, 2003
Found podchecker. CPAN HTML documentation should work right now.
=item * B<Version 1.11> - April 10th, 2003
Trying to get the HTML POD documentation to come out correctly
=item * B<Version 1.1> - April 10th, 2003
Documentation cleanups
Added a C<remove()> function
=item * B<Version 1.0> - April 7th, 2003
This is the initial public release for CPAN, so everything is new.
=back
=head1 TODO
A better unit test suite perhaps. I have a private one, but a public one would be
nice if there was good file faking module.
Fix / investigate stuff in the BUGS section.
=head1 BUGS
To be honest, I'm not sure of a couple of things. The first is that I know
of ar archives made on old AIX systems (pre 4.3?) that have a different header
with a different magic string, etc. This module perfectly (hopefully) handles
ar archives made with the modern ar command from the binutils distribution. If
anyone knows of anyway to produce these old-style AIX archives, or would like
to produce a few for testing, I would be much grateful.
There's no really good reason why this module I<shouldn't> run on Win32
platforms, but admittedly, this might change when we have a file exporting
function that supports owner and permission writing.
If you read in and write out a file, you get different md5sums, but it's still
a valid archive. I'm still investigating this, and consider it a minor bug.
=head1 COPYRIGHT
Archive::Ar is copyright 2003 Jay Bonci E<lt>jaybonci@cpan.orgE<gt>.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|