summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2003-12-19 00:29:21 +0000
committerMarc Espie <espie@cvs.openbsd.org>2003-12-19 00:29:21 +0000
commit9b8c8bf773df62af4bda2d4bbfd0d965b4057c49 (patch)
treeddf588cd5c772da49c3c2f1aee44ee1a6a1a391e
parent5e003feba9b898fea84451fb88281e082b5c6f76 (diff)
Clean up PackageLocator so it looks somewhat more object-oriented.
Most importantly, put all the state information into the created object, so that the actual archive can be closed, later reopened, and scanned until the correct file is found. This will be used to allow retrieving packages through ftp without keeping loads of connections opened because of dependency resolving. Approved by fries and naddy.
-rw-r--r--usr.sbin/pkg_add/OpenBSD/PackageLocator.pm85
-rw-r--r--usr.sbin/pkg_add/OpenBSD/Ustar.pm15
-rw-r--r--usr.sbin/pkg_add/pkg_add7
3 files changed, 74 insertions, 33 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm b/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm
index 0689b9c9600..e5b8c0ef826 100644
--- a/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm
+++ b/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm
@@ -1,4 +1,4 @@
-# $OpenBSD: PackageLocator.pm,v 1.5 2003/11/06 17:59:23 espie Exp $
+# $OpenBSD: PackageLocator.pm,v 1.6 2003/12/19 00:29:20 espie Exp $
#
# Copyright (c) 2003 Marc Espie.
#
@@ -203,8 +203,7 @@ sub find
if ($_ eq '-') {
my $location = OpenBSD::PackageLocation->new('-');
- my $package = openAbsolute($location, '');
- bless $package, $class;
+ my $package = $class->openAbsolute($location, '');
return $package;
}
$_.=".tgz" unless m/\.tgz$/;
@@ -217,19 +216,18 @@ sub find
my ($pkgname, $path) = fileparse($_);
my $location = OpenBSD::PackageLocation->new($path);
- $package = openAbsolute($location, $pkgname);
+ $package = $class->openAbsolute($location, $pkgname);
if (defined $package) {
push(@pkgpath, $location);
}
} else {
for my $p (@pkgpath) {
- $package = openAbsolute($p, $_);
+ $package = $class->openAbsolute($p, $_);
last if defined $package;
}
}
- return $package unless defined $package;
- $packages{$_} = $package;
- bless $package, $class;
+ $packages{$_} = $package if defined($package);
+ return $package;
}
sub info
@@ -243,36 +241,91 @@ sub close
my $self = shift;
close($self->{fh}) if defined $self->{fh};
$self->{fh} = undef;
- $self->{archive} = undef;
+ $self->{_archive} = undef;
}
-sub openAbsolute
+sub _open
{
- my ($location, $name) = @_;
- my $fh = $location->open($name);
+ my $self = shift;
+
+ my $fh = $self->{location}->open($self->{name});
+ $self->{fh} = $fh;
if (!defined $fh) {
return undef;
}
my $archive = new OpenBSD::Ustar $fh;
+ $self->{_archive} = $archive;
+}
+
+sub openAbsolute
+{
+ my ($class, $location, $name) = @_;
+ my $self = { location => $location, name => $name};
+ bless $self, $class;
+
+ if (!$self->_open()) {
+ return undef;
+ }
my $dir = OpenBSD::Temp::dir();
+ $self->{dir} = $dir;
- my $self = { name => $_, fh => $fh, archive => $archive, dir => $dir };
# check that Open worked
- while (my $e = $archive->next()) {
+ while (my $e = $self->next()) {
if ($e->isFile() && is_info_name($e->{name})) {
$e->{name}=$dir.$e->{name};
$e->create();
} else {
- $archive->unput();
+ $self->unput();
last;
}
}
if (-f $dir.CONTENTS) {
+# $self->close();
return $self;
} else {
- CORE::close($fh);
+ $self->close();
+ return undef;
+ }
+}
+
+sub reopen
+{
+ my $self = shift;
+# print "Reopening ", $self->{name}, "\n";
+ if (!$self->_open()) {
return undef;
}
+ while (my $e = $self->{_archive}->next()) {
+# print "Scanning ", $e->{name}, "\n";
+ if ($e->{name} eq $self->{_current}->{name}) {
+ $self->{_current} = $e;
+ return $self;
+ }
+ }
+ return undef;
+}
+
+# proxy for archive operations
+sub next
+{
+ my $self = shift;
+
+ if (!defined $self->{fh}) {
+ if (!$self->reopen()) {
+ return undef;
+ }
+ }
+ if (!$self->{_unput}) {
+ $self->{_current} = $self->{_archive}->next();
+ }
+ $self->{_unput} = 0;
+ return $self->{_current};
+}
+
+sub unput
+{
+ my $self = shift;
+ $self->{_unput} = 1;
}
# allows the autoloader to work correctly
diff --git a/usr.sbin/pkg_add/OpenBSD/Ustar.pm b/usr.sbin/pkg_add/OpenBSD/Ustar.pm
index 0d021992913..78ea34dccd7 100644
--- a/usr.sbin/pkg_add/OpenBSD/Ustar.pm
+++ b/usr.sbin/pkg_add/OpenBSD/Ustar.pm
@@ -1,4 +1,4 @@
-# $OpenBSD: Ustar.pm,v 1.3 2003/12/10 17:45:11 espie Exp $
+# $OpenBSD: Ustar.pm,v 1.4 2003/12/19 00:29:20 espie Exp $
#
# Copyright (c) 2002 Marc Espie.
#
@@ -56,7 +56,7 @@ sub new
{
my ($class, $fh) = @_;
- return bless { fh => $fh, swallow => 0, unput => 0} , $class;
+ return bless { fh => $fh, swallow => 0} , $class;
}
@@ -98,19 +98,9 @@ sub skip
$self->{swallow} = 0;
}
-sub unput
-{
- my $self = shift;
- $self->{unput} = 1;
-}
-
sub next
{
my $self = shift;
- if ($self->{unput}) {
- $self->{unput} = 0;
- return $self->{current};
- }
# get rid of the current object
$self->skip();
my $header;
@@ -178,7 +168,6 @@ sub next
} else {
die "Unsupported type";
}
- $self->{current} = $result;
return $result;
}
diff --git a/usr.sbin/pkg_add/pkg_add b/usr.sbin/pkg_add/pkg_add
index 5fb8a5c89f7..9bc28c40acc 100644
--- a/usr.sbin/pkg_add/pkg_add
+++ b/usr.sbin/pkg_add/pkg_add
@@ -1,7 +1,7 @@
#! /usr/bin/perl
# ex:ts=8 sw=4:
-# $OpenBSD: pkg_add,v 1.17 2003/12/10 11:12:22 espie Exp $
+# $OpenBSD: pkg_add,v 1.18 2003/12/19 00:29:20 espie Exp $
#
# Copyright (c) 2003 Marc Espie.
#
@@ -344,9 +344,8 @@ sub really_add($)
system($dir.INSTALL, $pkgname, "PRE-INSTALL") == 0 or die "install script borked";
}
}
- my $archive=$handle->{archive};
- if (!defined $archive) {
+ if (!defined $handle) {
print STDERR "Archive in $pkgname broken\n";
$errors++;
return;
@@ -355,7 +354,7 @@ sub really_add($)
$plist->{done} = [];
for my $item (@{$plist->{items}}) {
- $item->install($archive, $opt_v, $opt_n);
+ $item->install($handle, $opt_v, $opt_n);
push(@{$plist->{done}}, $item);
last if $interrupted;
}