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
|
# debsigs: Package signing/verification system, arf module
# AR File manipulation libraries
# Copyright (C) 2000 Progeny Linux Systems, Inc. <jgoerzen@progeny.com>
# Copyright (C) 2009 Peter Pentchev <roam@ringlet.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package Debian::debsigs::arf;
use strict;
use warnings;
use Cwd;
use Fcntl qw/SEEK_CUR/;
use IO::Pipe;
use IO::Handle;
use Debian::debsigs::forktools ':all';
#my $CVSVERSION = '$Progeny: arf.pm,v 1.11 2001/05/09 11:21:52 epg Exp $'; #'
#my ($VERSION) = $CVSVERSION =~ /^\$Progeny: .+,v ([0-9.]+) /;
our $VERSION = '1.12';
my $ARMAG = "!<arch>\n";
my $SARMAG = length $ARMAG;
sub new {
my ($class, $filename) = @_;
my $self = {};
$self->{filename} = ($filename =~ m'^/') ? $filename :
getcwd() . "/$filename";
$self->{ar} = '/usr/bin/ar';
bless $self, $class;
return $self;
}
sub getfiles {
my ($self, @filenames) = @_;
return forkreader(undef, $self->{ar}, "-p", $self->{filename},
@filenames);
}
# If a data fd is specified, assume that someone already has an fd
# that would like to use for piping in data. Otherwise, generate
# a new one to let them print to.
sub setfile {
my ($self, $filename) = @_;
return(system($self->{ar}, "-r",
$self->{filename}, $filename));
}
# Delete the specified file.
sub delete {
my ($self, $filename) = @_;
return(system($self->{ar}, "-d",
$self->{filename}, $filename));
}
#
sub contents {
my ($self) = @_;
my ($fd, $arpid) =
forkreader(undef, $self->{ar}, "-t", $self->{filename});
my $line;
my @retval;
while (defined($line = <$fd>)) {
chomp $line;
push @retval, $line;
}
assertsuccess($arpid, 'ar -t');
return @retval;
}
sub fixup {
my ($self) = @_;
my ($fd, $buf, $len);
open($fd, '+<', $self->{'filename'}) or
die("Opening $self->{filename} for trailing slash fixup: $!\n");
$len = read($fd, $buf, $SARMAG);
if (!defined($len)) {
die("Reading the ar signature from $self->{filename}: $!\n");
}
if ($len != $SARMAG || $buf ne $ARMAG) {
die("Invalid ar signature at the start of $self->{filename}\n");
}
while (defined($len = read($fd, $buf, 60)) && $len == 60) {
my @fields = unpack("A16A12A6A6A8A10", $buf);
# Strip a slash, with loads and loads of precautions
if ($fields[0] =~ m{/$}) {
seek($fd, -(60 - length($fields[0]) + 1), SEEK_CUR) or
die("Seeking back to the slash for $fields[0] of $self->{filename}: $!\n");
$len = read($fd, $buf, 1);
if (!defined($len) || $len != 1 || $buf ne '/') {
die("Could not seek back to the trailing slash for $fields[0] of $self->{filename}: $!\n");
}
seek($fd, -1, SEEK_CUR) or
die("REALLY Seeking back to the slash for $fields[0] of $self->{filename}: $!\n");
print $fd ' ' or
die("Overwriting the trailing slash for $fields[0] of $self->{filename}: $!\n");
seek($fd, 60 - length($fields[0]), SEEK_CUR) or
die("Seeking to the end of the header for $fields[0] of $self->{filename}: $!\n");
}
# Skip the actual ar data
seek($fd, $fields[5] + ($fields[5] % 2), SEEK_CUR) or
die("Skipping $fields[5] for $fields[0] of $self->{filename}: $!\n");
}
if (!defined($len)) {
die("Could not read the whole archive $self->{filename}: $!\n");
}
close($fd);
return 1;
}
# if ($datafd) {
# return forktools::forkreader($datafd, $self->{ar}, "-r", $self->{filename},
# $filename);
# } else {
# return forktools::forkwriter(undef, $self->{ar}, "-r", $self->{filename},
# $filename);
# }
#}
1;
|