prt-auf: improved the handling of locked ports

This commit is contained in:
John McQuah 2022-06-11 03:37:27 -04:00
parent 6356b9f98c
commit 6162bf44d9

View File

@ -14,7 +14,7 @@ use strict;
my $title="prt-auf"; my $version=0.5; my $title="prt-auf"; my $version=0.5;
my $CONFDIR = "/var/lib/pkg"; my $prtalias="/etc/prt-get.aliases"; my $CONFDIR = "/var/lib/pkg"; my $prtalias="/etc/prt-get.aliases";
my $pkgdb="$CONFDIR/db"; my $prtlocker="$CONFDIR/prt-get.locker"; my $pkgdb="$CONFDIR/db"; my $prtlocker="$CONFDIR/prt-get.locker";
my $prtcache="$CONFDIR/prt-get.cache"; my $LOCKED; my %ALIASES; my %DEPENDS; my $prtcache="$CONFDIR/prt-get.cache"; my @LOCKED; my %ALIASES; my %DEPENDS;
my @allports; my %V_REPO; my %V_INST; my %DESC; my @allports; my %V_REPO; my %V_INST; my %DESC;
my @results; my $strf; my $ind; my $hh; my $portpath; my $built_pkg; my @results; my $strf; my $ind; my $hh; my $portpath; my $built_pkg;
my %osearch = ( cache => 0, regex => 0, path => 0, exact => 0, verbose => 0 ); my %osearch = ( cache => 0, regex => 0, path => 0, exact => 0, verbose => 0 );
@ -43,7 +43,7 @@ if ($action !~ /^(quickdep|search|dsearch|fsearch|info|dup|readme|cat)$/) {
close (DB); close (DB);
} }
if ($action =~ /^(diff|quickdiff|listlocked|depends|deptree|install|update|depinst|sysup)$/) { if ($action =~ /^(diff|quickdiff|listlocked|depends|deptree|remove|install|update|depinst|sysup)$/) {
get_locked_and_aliased(); get_locked_and_aliased();
} }
@ -184,11 +184,11 @@ sub parse_args {
my @query; my @query;
$osearch{cache} = 1 if ($0 =~ /cache$/); $osearch{cache} = 1 if ($0 =~ /cache$/);
while (my $arg = shift) { while (my $arg = shift) {
if ($arg =~ /^(search|dsearch|fsearch|path|info|list|dup)$/) { $action = $1; if ($arg =~ /^(search|dsearch|fsearch|path|info|list|remove)$/) { $action = $1;
} elsif ($arg =~ /^(install|update|depinst|grpinst|sysup)$/) { $action = $1; } elsif ($arg =~ /^(install|update|depinst|grpinst|sysup)$/) { $action = $1;
} elsif ($arg =~ /^(lock|unlock|listlocked|current|isinst)$/) { $action = $1; } elsif ($arg =~ /^(lock|unlock|listlocked|current|isinst)$/) { $action = $1;
} elsif ($arg =~ /^(diff|quickdiff|listinst|listorphans)$/) { $action = $1; } elsif ($arg =~ /^(diff|quickdiff|listinst|listorphans)$/) { $action = $1;
} elsif ($arg =~ /^(depends|deptree|quickdep|dependent)$/) { $action = $1; } elsif ($arg =~ /^(depends|deptree|quickdep|dependent|dup)$/) { $action = $1;
} elsif ($arg =~ /^(readme|cat|edit|ls|help|version|cache)$/) { $action = $1; } elsif ($arg =~ /^(readme|cat|edit|ls|help|version|cache)$/) { $action = $1;
} elsif ($arg eq "--tree") { $odepends{tree} = 1; $odepends{recursive} = 1; } elsif ($arg eq "--tree") { $odepends{tree} = 1; $odepends{recursive} = 1;
} elsif ($arg eq "--all") { $odepends{all} = 1; } elsif ($arg eq "--all") { $odepends{all} = 1;
@ -200,14 +200,15 @@ sub parse_args {
} elsif ($arg eq "--cache") { $osearch{cache} = 1; } elsif ($arg eq "--cache") { $osearch{cache} = 1;
} elsif ($arg eq "--test") { $opkg{test} = "yes"; } elsif ($arg eq "--test") { $opkg{test} = "yes";
} elsif ($arg eq "-fr") { $opkg{margs} .= " -f"; } elsif ($arg eq "-fr") { $opkg{margs} .= " -f";
} elsif ($arg =~ /^(-uf|-if|-us|-is|-ns|-kw)$/) { $opkg{margs} .= $1; } elsif ($arg =~ /^(-uf|-if|-us|-is|-ns|-kw)$/) { $opkg{margs} .= " $1";
} elsif ($arg =~ /^--margs=(.*)/) { $opkg{margs} .= $1; } elsif ($arg =~ /^--margs=(.*)/) { $opkg{margs} .= $1;
} elsif ($arg =~ /^--aargs=(-r|--root)=(.*)/) { $opkg{aargs} .= "$1 $2"; } elsif ($arg =~ /^--aargs=(-r|--root)=(.*)/) { $opkg{aargs} .= "$1 $2";
} elsif ($arg =~ /^--rargs=(-r|--root)=(.*)/) { $opkg{rargs} .= "$1 $2"; } elsif ($arg =~ /^--rargs=(-r|--root)=(.*)/) { $opkg{rargs} .= "$1 $2";
} elsif ($arg =~ /^-/) { } elsif ($arg =~ /^-/) {
print "'$arg' is not a recognized option.\n"; exit 1; print "'$arg' is not a recognized option.\n";
} else { push (@query, $arg); } } else { push (@query, $arg); }
} }
if (! $action) { print_help(); }
if (($#query > -1) and if (($#query > -1) and
($action =~ /^(diff|quickdiff|cache|list|dup|sysup)/)) { ($action =~ /^(diff|quickdiff|cache|list|dup|sysup)/)) {
print "warning: $1 takes no arguments; ignoring those given.\n"; print "warning: $1 takes no arguments; ignoring those given.\n";
@ -281,12 +282,16 @@ sub find_dups {
sub get_locked_and_aliased { sub get_locked_and_aliased {
local $/ = "\n"; local $/ = "\n";
open (AL, $prtalias) or return; if (-f $prtalias) {
while (<AL>) { $ALIASES{$1} = $2 if m/^\s*(.*)\s*:\s*(.*)/; } open (AL, $prtalias);
close (AL); while (<AL>) { $ALIASES{$1} = $2 if m/^\s*(.*)\s*:\s*(.*)/; }
open (LK, $prtlocker) or return; close (AL);
while (<LK>) { $LOCKED .= " $_"; } }
close (LK); if (-f $prtlocker) {
open (LK, $prtlocker) or return;
while (<LK>) { push (@LOCKED, $_) unless /^\s*$/; }
close (LK);
}
} }
sub who_aliased_to { sub who_aliased_to {
@ -368,7 +373,7 @@ sub get_pkgfile_fields {
if (shift) { if (shift) {
return $Name, $portpath, $Version, $Release, $descrip, $Dependencies, $url, return $Name, $portpath, $Version, $Release, $descrip, $Dependencies, $url,
$packager, $maintainer, $readme, $preInstall, $postInstall, $built_pkg; $packager, $maintainer, $readme, $preInstall, $postInstall;
} else { return $Version, $Release, $descrip, $Dependencies; } } else { return $Version, $Release, $descrip, $Dependencies; }
} }
@ -411,7 +416,6 @@ sub find_port_by_name {
my $query = shift; my $exact=shift; my $fullpath=shift; my $exhaustive=shift; my $query = shift; my $exact=shift; my $fullpath=shift; my $exhaustive=shift;
$query =~ s/\+/\\\+/g unless ($osearch{regex}==1); $query =~ s/\+/\\\+/g unless ($osearch{regex}==1);
$query =~ s/\./\\\./g unless ($osearch{regex}==1); $query =~ s/\./\\\./g unless ($osearch{regex}==1);
# $query =~ s/\^/\// if (($osearch{regex}==1) and ($exact==0));
my $pattern = ($exact==1) ? qr/^$query$/s : qr/$query/is; my $pattern = ($exact==1) ? qr/^$query$/s : qr/$query/is;
my %names_only = map { ($_ => (split /\//, $_)[-1]) } @allports; my %names_only = map { ($_ => (split /\//, $_)[-1]) } @allports;
my @hits = grep { $names_only{$_} =~ $pattern } @allports; my @hits = grep { $names_only{$_} =~ $pattern } @allports;
@ -430,13 +434,14 @@ sub uninstall {
foreach my $t (@targets) { foreach my $t (@targets) {
($opkg{test} eq "no") ? system($PKGRM,$opkg{rargs},$t) : print "$PKGRM $opkg{rargs} $t\n"; ($opkg{test} eq "no") ? system($PKGRM,$opkg{rargs},$t) : print "$PKGRM $opkg{rargs} $t\n";
$removed{$t}=1 if ($?>>8 == 0); $removed{$t}=1 if ($?>>8 == 0);
if ((" $LOCKED " =~ / $t /) and ($opkg{test} eq "no")) { port_unlock($t); } if ((grep /^$t$/, @LOCKED) and ($opkg{test} eq "no")) { port_unlock($t); }
} }
return \%removed; return \%removed;
} }
sub port_lock { sub port_lock {
my @newlocks = grep { " $LOCKED " !~ / $_ / } @_; my %oldlocks = map { $_ => "L" } @LOCKED;
my @newlocks = grep { ! defined $oldlocks{$_} } @_;
if (@newlocks) { if (@newlocks) {
open (LK,'>>',$prtlocker) or die "cannot open $prtlocker for writing.\n"; open (LK,'>>',$prtlocker) or die "cannot open $prtlocker for writing.\n";
foreach my $lp (@newlocks) { foreach my $lp (@newlocks) {
@ -447,10 +452,10 @@ sub port_lock {
} }
sub port_unlock { sub port_unlock {
foreach my $ul (@_) { $LOCKED =~ s/ $ul / /; } my %unlocks = map { $_ => "U" } @_;
my @newlocks = split / /, $LOCKED; my @newlocks = grep { ! defined $unlocks{$_} } @LOCKED;
open (LL, $prtlocker."-tmp",">"); open (LL, $prtlocker."-tmp",">");
foreach my $nl (@newlocks) { print LL "$nl\n"; } foreach my $nl (@newlocks) { print LL "$nl\n" unless $nl =~ /^\s*$/; }
close (LL); close (LL);
system ("mv",$prtlocker."-tmp",$prtlocker); system ("mv",$prtlocker."-tmp",$prtlocker);
} }
@ -471,7 +476,7 @@ sub list_ports {
push (@found, $lp) if (-f "$lp/Pkgfile"); push (@found, $lp) if (-f "$lp/Pkgfile");
} }
} elsif ($filter eq "inst") { @found = keys %V_INST; } elsif ($filter eq "inst") { @found = keys %V_INST;
} elsif ($filter eq "locked") { @found=split / /, $LOCKED; } elsif ($filter eq "locked") { @found=@LOCKED;
} elsif ($filter =~ /^(orphans|dependent)$/) { } elsif ($filter =~ /^(orphans|dependent)$/) {
my $seed=shift; my $seed=shift;
if ( ((! $seed) and ($filter eq "dependent")) or if ( ((! $seed) and ($filter eq "dependent")) or
@ -538,7 +543,7 @@ sub port_diff { # returns a scalar indicating how many differences were found
if ($dtype !~ /^(current|isinst|utd)/) { if ($dtype !~ /^(current|isinst|utd)/) {
printf "$format", "Port", "Installed", "In Repository" if (! $dtype); printf "$format", "Port", "Installed", "In Repository" if (! $dtype);
foreach my $p (keys %V_INST) { foreach my $p (keys %V_INST) {
next if (($LOCKED =~ /$p/) and ($odepends{all}==0)); next if ((grep /$p/, @LOCKED) and ($odepends{all}==0));
$lastcol = ($V_REPO{$p}) ? $V_REPO{$p} : "MISSING!"; $lastcol = ($V_REPO{$p}) ? $V_REPO{$p} : "MISSING!";
if ($lastcol ne $V_INST{$p}) { if ($lastcol ne $V_INST{$p}) {
@ -619,17 +624,17 @@ sub deporder {
} }
sub up_inst { # returns scalar references to five arrays; sub up_inst { # returns scalar references to five arrays;
my $type=shift; my @requested=@_; my $type=shift; my @requested=@_; my %EXEMPT; my %WANTED;
my $PKGMK=$opkg{makecommand}; my $PKGADD=$opkg{addcommand}; my $PKGMK=$opkg{makecommand}; my $PKGADD=$opkg{addcommand};
my $SUDO="/usr/bin/doas"; my $FAKEROOT="/usr/bin/fakeroot"; my $SUDO="/usr/bin/doas"; my $FAKEROOT="/usr/bin/fakeroot";
$SUDO = (-x $SUDO) ? $SUDO : "/usr/bin/sudo"; $SUDO = (-x $SUDO) ? $SUDO : "/usr/bin/sudo";
$SUDO = (-x $SUDO) ? $SUDO : "/usr/bin/su -c"; $SUDO = (-x $SUDO) ? $SUDO : "/usr/bin/su -c";
$FAKEROOT = (-x $FAKEROOT) ? $FAKEROOT : "/usr/bin/su -c"; $FAKEROOT = (-x $FAKEROOT) ? $FAKEROOT : $SUDO;
# prepend commands with sudo/doas/fakeroot if the effective user id is not root # prepend commands with sudo/doas/fakeroot if the effective user id is not root
if ($> != 0) { $PKGADD = "$SUDO $PKGADD"; if ($> != 0) { $PKGADD = "$SUDO $PKGADD"; }
$PKGMK = "$FAKEROOT $PKGMK"; } # $PKGMK = "$FAKEROOT $PKGMK"; }
# resolve all dependencies unless the command was 'grpinst' # resolve all dependencies unless the command was 'grpinst'
my @targets=($type eq "grpinst") ? @_ : deporder("quickdep",@_); my @targets=($type eq "grpinst") ? @_ : deporder("quickdep",@_);
@ -638,9 +643,11 @@ sub up_inst { # returns scalar references to five arrays;
@targets = grep { (join " ", @allports) =~ / $_ / } @targets if ($type eq "grpinst"); @targets = grep { (join " ", @allports) =~ / $_ / } @targets if ($type eq "grpinst");
# exempt any locked ports from an sysup operation # exempt any locked ports from an sysup operation
if ($action eq "sysup") { @targets = grep {" $LOCKED " !~ / $_ /} @targets; %EXEMPT = map { $_ => 0 } @LOCKED;
%WANTED = map { $_ => 1 } @requested;
if ($action eq "sysup") { @targets = grep {defined $EXEMPT{$_}} @targets;
} else { } else {
@targets = grep {(" $LOCKED " !~ / $_ /) or (grep /$_/, @requested)} @targets; @targets = grep {(defined $EXEMPT{$_}) or (defined $WANTED{$_})} @targets;
} }
# first determine the directories from which pkgmk must be called # first determine the directories from which pkgmk must be called
@ -710,7 +717,7 @@ sub sysup {
sub find_built_pkg { sub find_built_pkg {
my $target = shift; my $CONF="/etc/pkgmk.conf"; local $/="\n"; my $target = shift; my $CONF="/etc/pkgmk.conf"; local $/="\n";
my $COMPRESSION; my $PKG_DIR; my $portpath = (find_port_by_name($target,1,1,0))[0]; my $COMPRESSION; my $PKG_DIR; my $portpath = (find_port_by_name($target,1,1,0));
my ($version, $release) = (get_pkgfile_fields($portpath))[0..1]; my ($version, $release) = (get_pkgfile_fields($portpath))[0..1];
open (CF,$CONF) or return; open (CF,$CONF) or return;