| 1 |
# debsigs: Package signing/verification system, arf module |
| 2 |
# AR File manipulation libraries |
| 3 |
# SPDX-FileCopyrightText: 2000 Progeny Linux Systems, Inc. <[email protected]> |
| 4 |
# SPDX-FileCopyrightText: 2009 Peter Pentchev <[email protected]> |
| 5 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
|
| 7 |
package Debian::debsigs::arf; |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
|
| 12 |
use Cwd; |
| 13 |
use Fcntl qw/SEEK_CUR/; |
| 14 |
use IO::Pipe; |
| 15 |
use IO::Handle; |
| 16 |
|
| 17 |
use Debian::debsigs::forktools ':all'; |
| 18 |
|
| 19 |
our $VERSION = '1.12'; |
| 20 |
|
| 21 |
my $ARMAG = "!<arch>\n"; |
| 22 |
my $SARMAG = length $ARMAG; |
| 23 |
|
| 24 |
sub new { |
| 25 |
my ($class, $filename) = @_; |
| 26 |
my $self = {}; |
| 27 |
|
| 28 |
$self->{filename} = ($filename =~ m'^/') ? $filename : |
| 29 |
getcwd() . "/$filename"; |
| 30 |
$self->{ar} = '/usr/bin/ar'; |
| 31 |
bless $self, $class; |
| 32 |
return $self; |
| 33 |
} |
| 34 |
|
| 35 |
sub getfiles { |
| 36 |
my ($self, @filenames) = @_; |
| 37 |
|
| 38 |
return forkreader(undef, $self->{ar}, "-p", $self->{filename}, |
| 39 |
@filenames); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
# If a data fd is specified, assume that someone already has an fd |
| 44 |
# that would like to use for piping in data. Otherwise, generate |
| 45 |
# a new one to let them print to. |
| 46 |
|
| 47 |
sub setfile { |
| 48 |
my ($self, $filename) = @_; |
| 49 |
|
| 50 |
return(system($self->{ar}, "-r", |
| 51 |
$self->{filename}, $filename)); |
| 52 |
} |
| 53 |
|
| 54 |
# Delete the specified file. |
| 55 |
|
| 56 |
sub delete { |
| 57 |
my ($self, $filename) = @_; |
| 58 |
|
| 59 |
return(system($self->{ar}, "-d", |
| 60 |
$self->{filename}, $filename)); |
| 61 |
} |
| 62 |
|
| 63 |
# |
| 64 |
|
| 65 |
sub contents { |
| 66 |
my ($self) = @_; |
| 67 |
my ($fd, $arpid) = |
| 68 |
forkreader(undef, $self->{ar}, "-t", $self->{filename}); |
| 69 |
|
| 70 |
my $line; |
| 71 |
my @retval; |
| 72 |
while (defined($line = <$fd>)) { |
| 73 |
chomp $line; |
| 74 |
push @retval, $line; |
| 75 |
} |
| 76 |
assertsuccess($arpid, 'ar -t'); |
| 77 |
return @retval; |
| 78 |
} |
| 79 |
|
| 80 |
sub fixup { |
| 81 |
my ($self) = @_; |
| 82 |
my ($fd, $buf, $len); |
| 83 |
|
| 84 |
open($fd, '+<', $self->{'filename'}) or |
| 85 |
die("Opening $self->{filename} for trailing slash fixup: $!\n"); |
| 86 |
|
| 87 |
$len = read($fd, $buf, $SARMAG); |
| 88 |
if (!defined($len)) { |
| 89 |
die("Reading the ar signature from $self->{filename}: $!\n"); |
| 90 |
} |
| 91 |
if ($len != $SARMAG || $buf ne $ARMAG) { |
| 92 |
die("Invalid ar signature at the start of $self->{filename}\n"); |
| 93 |
} |
| 94 |
|
| 95 |
while (defined($len = read($fd, $buf, 60)) && $len == 60) { |
| 96 |
my @fields = unpack("A16A12A6A6A8A10", $buf); |
| 97 |
|
| 98 |
# Strip a slash, with loads and loads of precautions |
| 99 |
if ($fields[0] =~ m{/$}) { |
| 100 |
seek($fd, -(60 - length($fields[0]) + 1), SEEK_CUR) or |
| 101 |
die("Seeking back to the slash for $fields[0] of $self->{filename}: $!\n"); |
| 102 |
$len = read($fd, $buf, 1); |
| 103 |
if (!defined($len) || $len != 1 || $buf ne '/') { |
| 104 |
die("Could not seek back to the trailing slash for $fields[0] of $self->{filename}: $!\n"); |
| 105 |
} |
| 106 |
seek($fd, -1, SEEK_CUR) or |
| 107 |
die("REALLY Seeking back to the slash for $fields[0] of $self->{filename}: $!\n"); |
| 108 |
print $fd ' ' or |
| 109 |
die("Overwriting the trailing slash for $fields[0] of $self->{filename}: $!\n"); |
| 110 |
seek($fd, 60 - length($fields[0]), SEEK_CUR) or |
| 111 |
die("Seeking to the end of the header for $fields[0] of $self->{filename}: $!\n"); |
| 112 |
} |
| 113 |
|
| 114 |
# Skip the actual ar data |
| 115 |
seek($fd, $fields[5] + ($fields[5] % 2), SEEK_CUR) or |
| 116 |
die("Skipping $fields[5] for $fields[0] of $self->{filename}: $!\n"); |
| 117 |
} |
| 118 |
|
| 119 |
if (!defined($len)) { |
| 120 |
die("Could not read the whole archive $self->{filename}: $!\n"); |
| 121 |
} |
| 122 |
close($fd); |
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
# if ($datafd) { |
| 127 |
# return forktools::forkreader($datafd, $self->{ar}, "-r", $self->{filename}, |
| 128 |
# $filename); |
| 129 |
# } else { |
| 130 |
# return forktools::forkwriter(undef, $self->{ar}, "-r", $self->{filename}, |
| 131 |
# $filename); |
| 132 |
# } |
| 133 |
#} |
| 134 |
|
| 135 |
1; |