slightly clearer code: retrieve files through a FileSource, that abstracts

all details of archive extraction (or not).
This commit is contained in:
espie 2007-05-21 14:41:38 +00:00
parent 65787ed31c
commit ee3c5a28f4

View File

@ -1,6 +1,6 @@
#!/usr/bin/perl
# $OpenBSD: check-newlib-depends,v 1.27 2007/05/21 14:15:31 espie Exp $
# $OpenBSD: check-newlib-depends,v 1.28 2007/05/21 14:41:38 espie Exp $
# Copyright (c) 2004 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
@ -33,22 +33,72 @@ use Getopt::Std;
our ($opt_o, $opt_d, $opt_f, $opt_B);
my $errors = 0;
# fake archive handle, since the extraction does not happen...
package FakeHandle;
package FakeFileSource;
sub new
{
return bless {}, "FakeHandle";
my ($class, $location) = @_;
bless {location => $location }, $class
}
sub next
sub retrieve
{
return shift;
my ($self, $item) = @_;
return $self->{location}.$item->fullname;
}
sub create
sub skip
{
return 0;
}
sub clean
{
}
package RealFileSource;
sub new
{
my ($class, $handle, $location) = @_;
bless {handle => $handle, location => $location }, $class
}
sub prepare_to_extract
{
my ($self, $item) = @_;
require OpenBSD::ArcCheck;
my $o = $self->{handle}->next;
$o->{destdir} = $self->{location};
$o->{cwd} = $item->{cwd};
if (!$o->check_name($item)) {
die "Error checking name for $o->{name} vs. $item->{name}\n";
}
return $o;
}
sub extracted_name
{
my ($self, $item) = @_;
return $self->{location}.$item->fullname;
}
sub retrieve
{
my ($self, $item) = @_;
my $o = $self->prepare_to_extract($item);
$o->create;
return $self->extracted_name($item);
}
sub skip
{
my ($self, $item) = @_;
my $o = $self->prepare_to_extract($item);
$self->{handle}->skip;
}
sub clean
{
my ($self, $item) = @_;
unlink($self->extracted_name($item));
}
package OpenBSD::PackingList;
@ -96,12 +146,9 @@ sub shellquote
sub record_needed_libs
{
my ($item, $t, $where, $handle) = @_;
my $fullname = File::Spec->canonpath($item->fullname());
my $file = $handle->next();
$file->{destdir} = $where;
$file->{cwd} = $item->cwd();
$file->{name} = $fullname;
my ($item, $t, $source) = @_;
my $fullname = File::Spec->canonpath($item->fullname);
my $linux_bin = 0;
my $freebsd_bin = 0;
if ($fullname =~ m,^/usr/local/emul/redhat/,) {
@ -110,33 +157,32 @@ sub record_needed_libs
if ($fullname =~ m,^/usr/local/emul/freebsd/,) {
$freebsd_bin = 1;
}
if ($linux_bin || $freebsd_bin || $item->{symlink}) {
$source->skip($item);
return;
}
my $n = shellquote($source->retrieve($item));
# this will fail because of links, so we don't care.
eval { $file->create(); };
unless ($@ or defined $item->{symlink}) {
my $n = shellquote("$where$fullname");
my $cmd;
if ($main::opt_o) {
open($cmd, "ldd -f 'NEEDED lib%o.so.%m.%n\\n'|");
} else {
open($cmd, "objdump -p $n 2>/dev/null|");
}
local $_;
my @l;
while(<$cmd>) {
if (m/^\s+NEEDED\s+(.*?)\s*$/) {
my $lib = $1;
push(@l, $lib);
# detect linux binaries
if ($lib eq 'libc.so.6') {
$linux_bin = 1;
}
my $cmd;
if ($main::opt_o) {
open($cmd, "ldd -f 'NEEDED lib%o.so.%m.%n\\n'|");
} else {
open($cmd, "objdump -p $n 2>/dev/null|");
}
my @l;
while(my $line = <$cmd>) {
if ($line =~ m/^\s+NEEDED\s+(.*?)\s*$/) {
my $lib = $1;
push(@l, $lib);
# detect linux binaries
if ($lib eq 'libc.so.6') {
$linux_bin = 1;
}
}
close($cmd);
# okay, we are not OpenBSD, we don't have sensible names
if ($linux_bin or $freebsd_bin) {
return;
}
}
close($cmd);
# okay, we are not OpenBSD, we don't have sensible names
unless ($linux_bin or $freebsd_bin) {
for my $lib (@l) {
# don't look for modules
next if $lib =~ m/\.so$/;
@ -148,9 +194,7 @@ sub record_needed_libs
}
}
}
if (!defined $main::opt_B) {
unlink($where.$fullname);
}
$source->clean($item);
}
package OpenBSD::PackingElement::Dependency;
@ -316,11 +360,11 @@ sub print_list
sub analyze
{
my ($plist, $db, $where, @l) = @_;
my ($plist, $db, $source, @l) = @_;
my $pkgname = $plist->pkgname();
my $t = { haslib => {}, needlib => {} };
$plist->record_needed_libs($t->{needlib}, $where, @l);
$plist->record_needed_libs($t->{needlib}, $source, @l);
$plist->register_libs($t->{haslib});
if (!defined $dependencies->{$pkgname}) {
@ -365,15 +409,15 @@ sub do_pkg
return 0 unless -d $dir;
my $plist = OpenBSD::PackingList->fromfile($dir.CONTENTS);
if ($opt_B) {
analyze($plist, $db, $opt_B, FakeHandle->new());
analyze($plist, $db, FakeFileSource->new($opt_B));
} else {
my $where = File::Temp::mkdtemp("/tmp/zoinx.XXXXXXXXXX");
analyze($plist, $db, $where, $true_package);
rmtree($where);
my $temp = File::Temp::mkdtemp("/tmp/zoinx.XXXXXXXXXX");
analyze($plist, $db, RealFileSource->new($true_package, $temp));
rmtree($temp);
}
$true_package->close();
$true_package->wipe_info();
$plist->forget();
$true_package->close;
$true_package->wipe_info;
$plist->forget;
return 1;
}
@ -384,9 +428,9 @@ sub do_plist
print STDERR "Error reading plist\n";
$errors++;
} else {
my $pkgname = $plist->pkgname();
my $pkgname = $plist->pkgname;
print "\n$pkgname:\n";
analyze($plist, $db, $opt_B, FakeHandle->new());
analyze($plist, $db, FakeFileSource->new($opt_B));
}
}