From: espie Date: Mon, 29 May 2017 12:28:54 +0000 (+0000) Subject: Refactor locator code so it's a proper object. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=01fd8ee4ca1f1d26f6a7f0ee358cd24091228ea7;p=openbsd Refactor locator code so it's a proper object. allow fw_update to short-circuit entirely, thus the specific treatment of paths is actually less of a special case, and the code that reaches into /etc/installurl is no longer used at all in that case. problem noticed by mlarkin@ --- diff --git a/usr.sbin/pkg_add/OpenBSD/FwUpdate.pm b/usr.sbin/pkg_add/OpenBSD/FwUpdate.pm index 2507a68b816..5a74efcd06c 100644 --- a/usr.sbin/pkg_add/OpenBSD/FwUpdate.pm +++ b/usr.sbin/pkg_add/OpenBSD/FwUpdate.pm @@ -1,7 +1,7 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: FwUpdate.pm,v 1.21 2017/03/25 22:26:44 sthen Exp $ +# $OpenBSD: FwUpdate.pm,v 1.22 2017/05/29 12:28:54 espie Exp $ # # Copyright (c) 2014 Marc Espie # @@ -21,19 +21,31 @@ use strict; use warnings; use OpenBSD::PkgAdd; use OpenBSD::PackageRepository; +use OpenBSD::PackageLocator; + +package OpenBSD::FwUpdate::Locator; +our @ISA = qw(OpenBSD::PackageLocator); + +sub add_default +{ + my ($self, $state, $p) = @_; + my $path = $state->opt('p'); + if (!$path) { + my $dir = OpenBSD::Paths->os_directory; + if (!defined $dir) { + $state->fatal("Couldn't find/parse OS version"); + } + $path = "http://firmware.openbsd.org/firmware/$dir/"; + } + $p->add(OpenBSD::PackageRepository->new($path, $state)); +} package OpenBSD::FwUpdate::State; our @ISA = qw(OpenBSD::PkgAdd::State); -sub find_path +sub locator { - my $state = shift; - my $dir = OpenBSD::Paths->os_directory; - if (defined $dir) { - $state->{path} = "http://firmware.openbsd.org/firmware/$dir/"; - } else { - $state->fatal("Couldn't find/parse OS version"); - } + return "OpenBSD::FwUpdate::Locator"; } sub handle_options @@ -42,11 +54,6 @@ sub handle_options $state->OpenBSD::State::handle_options('adinp:', '[-adinv] [-D keyword] [-p path] [driver...]'); $state->{not} = $state->opt('n'); - if ($state->opt('p')) { - $state->{path} = $state->opt('p'); - } else { - $state->find_path; - } if ($state->opt('i')) { $state->{not} = 1; } @@ -60,14 +67,13 @@ sub handle_options if ($state->opt('a') && @ARGV != 0) { $state->usage; } - $state->{fw_repository} = - OpenBSD::PackageRepository->new($state->{path}, $state); $state->{fw_verbose} = $state->{v}; if ($state->{v}) { $state->{v}--; } if ($state->{fw_verbose}) { - $state->say("Path to firmware: #1", $state->{path}); + $state->say("Path to firmware: #1", + $state->locator->printable_default_path($state)); } $state->{subst}->add('NO_SCP', 1); } @@ -211,7 +217,6 @@ sub find_handle } else { $set = $state->updateset->add_hints($pkgname); } - $set->add_repositories($state->{fw_repository}); return $set; } diff --git a/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm b/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm index 7ef8ce48e1e..977de465082 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackageLocator.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackageLocator.pm,v 1.109 2017/05/18 12:24:15 espie Exp $ +# $OpenBSD: PackageLocator.pm,v 1.110 2017/05/29 12:28:54 espie Exp $ # # Copyright (c) 2003-2010 Marc Espie # @@ -25,11 +25,9 @@ use OpenBSD::PackageRepository; my $default_path; -sub build_default_path +sub add_default { - my ($self, $state) = @_; - $default_path = OpenBSD::PackageRepositoryList->new($state); - + my ($self, $state, $p) = @_; my $w; if (defined $ENV{TRUSTED_PKG_PATH}) { @@ -38,7 +36,7 @@ sub build_default_path $v =~ s/\:+$//o; while (my $o = OpenBSD::PackageRepository->parse(\$v, $state)) { $o->{trusted} = 1; - $default_path->add($o); + $p->add($o); } } else { $w = "./:installpath"; @@ -50,19 +48,35 @@ sub build_default_path $w =~ s/^\:+//o; $w =~ s/\:+$//o; while (my $o = OpenBSD::PackageRepository->parse(\$w, $state)) { - $default_path->add($o); + $p->add($o); } } } +sub build_default_path +{ + my ($self, $state) = @_; + $default_path = OpenBSD::PackageRepositoryList->new($state); + + $self->add_default($state, $default_path); +} + sub default_path { + my ($self, $state) = @_; if (!defined $default_path) { - &build_default_path; + $self->build_default_path($state); } return $default_path; } +sub printable_default_path +{ + my ($self, $state) = @_; + + return join(':', $self->default_path($state)->do_something('url')); +} + sub path_parse { my ($self, $pkgname, $state, $path) = (@_, './'); diff --git a/usr.sbin/pkg_add/OpenBSD/PackageRepositoryList.pm b/usr.sbin/pkg_add/OpenBSD/PackageRepositoryList.pm index 1727989c635..8dd5483295c 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackageRepositoryList.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackageRepositoryList.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackageRepositoryList.pm,v 1.30 2015/07/09 12:57:55 espie Exp $ +# $OpenBSD: PackageRepositoryList.pm,v 1.31 2017/05/29 12:28:54 espie Exp $ # # Copyright (c) 2003-2006 Marc Espie # @@ -54,7 +54,7 @@ sub prepend sub do_something { my ($self, $do, $pkgname, @args) = @_; - if ($pkgname eq '-') { + if (defined $pkgname && $pkgname eq '-') { return OpenBSD::PackageRepository->pipe->new($self->{state})->$do($pkgname, @args); } for my $repo (@{$self->{l}}) { diff --git a/usr.sbin/pkg_add/OpenBSD/State.pm b/usr.sbin/pkg_add/OpenBSD/State.pm index 3327a3a31d4..6a10962659c 100644 --- a/usr.sbin/pkg_add/OpenBSD/State.pm +++ b/usr.sbin/pkg_add/OpenBSD/State.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: State.pm,v 1.45 2017/03/09 14:33:32 espie Exp $ +# $OpenBSD: State.pm,v 1.46 2017/05/29 12:28:54 espie Exp $ # # Copyright (c) 2007-2014 Marc Espie # @@ -26,6 +26,12 @@ sub new bless {state => $state}, $class; } +sub locator +{ + my $self = shift; + return $self->{state}->locator; +} + sub installed { my ($self, $all) = @_; @@ -37,17 +43,15 @@ sub installed sub path_parse { my ($self, $pkgname) = @_; - require OpenBSD::PackageLocator; - return OpenBSD::PackageLocator->path_parse($pkgname, $self->{state}); + return $self->locator->path_parse($pkgname, $self->{state}); } sub find { my ($self, $pkg) = @_; - require OpenBSD::PackageLocator; - return OpenBSD::PackageLocator->find($pkg, $self->{state}); + return $self->locator->find($pkg, $self->{state}); } sub reinitialize @@ -57,17 +61,15 @@ sub reinitialize sub match_locations { my $self = shift; - require OpenBSD::PackageLocator; - return OpenBSD::PackageLocator->match_locations(@_, $self->{state}); + return $self->locator->match_locations(@_, $self->{state}); } sub grabPlist { my ($self, $url, $code) = @_; - require OpenBSD::PackageLocator; - return OpenBSD::PackageLocator->grabPlist($url, $code, $self->{state}); + return $self->locator->grabPlist($url, $code, $self->{state}); } sub path @@ -88,6 +90,12 @@ require Exporter; our @ISA = qw(Exporter); our @EXPORT = (); +sub locator +{ + require OpenBSD::PackageLocator; + return "OpenBSD::PackageLocator"; +} + sub new { my $class = shift;