prtsweep.pl: refactored handling of invalid port directories

This commit is contained in:
John McQuah 2022-05-27 14:08:07 -04:00
parent 274f883c21
commit 0c511f15ab

View File

@ -70,7 +70,7 @@ sub list_subdirs {
opendir(DIR, $path) or return; opendir(DIR, $path) or return;
foreach my $entry(sort(readdir(DIR))) { foreach my $entry(sort(readdir(DIR))) {
next if ( substr($entry,0,1) eq "." ); next if ( substr($entry,0,1) eq "." );
push (@list, "$path/$entry") if -d "$path/$entry" push (@list, "$path/$entry") if -d "$path/$entry";
} }
closedir(DIR); closedir(DIR);
return @list; return @list;
@ -80,7 +80,7 @@ sub parse_signature {
my @signed = ("Pkgfile",".footprint",".signature","README","README.md", my @signed = ("Pkgfile",".footprint",".signature","README","README.md",
"pre-install","post-install",".32bit",".nostrip"); "pre-install","post-install",".32bit",".nostrip");
my $sigfile = shift; my $sigfile = shift;
open (FILE, $sigfile) or return ; open (FILE, $sigfile) or return @signed;
while (<FILE>) { while (<FILE>) {
if (/^SHA256 \(.+\) =.*$/) { if (/^SHA256 \(.+\) =.*$/) {
$_ =~ s/^SHA256 \((.+)\) =.*$/$1/; $_ =~ s/^SHA256 \((.+)\) =.*$/$1/;
@ -92,9 +92,14 @@ sub parse_signature {
} }
sub rm_emptydir { sub rm_emptydir {
my $portpath = shift; my $port = shift; my $nf = shift;
my $okremove = shift; my $msg = ($options{rmdir}==1) ? "\n":
rmdir $portpath if ($okremove == 1); " use -d to remove empty directories.\n";
my $modal = ($options{dryrun}==1) ? "would be" : "";
$msg = ($nf == 0) ? "$msg $port: empty directory $modal deleted.\n" :
"$msg cannot remove $port: directory not empty\n";
print $msg;
rmdir ($port) if (($nf == 0) and ($options{dryrun} == 0));
} }
sub sweep { sub sweep {
@ -102,20 +107,16 @@ sub sweep {
while ($port =~ s/\/\//\//g) {} while ($port =~ s/\/\//\//g) {}
$port =~ s/\/$//; $port =~ s/\/$//;
my @path = split /\//, $port; my @path = split /\//, $port;
my $builtpkg;
print "=======> $port\n" unless $options{quiet}==1; print "=======> $port\n" unless $options{quiet}==1;
my @wanted = parse_signature ("$port/.signature"); my @wanted = parse_signature ("$port/.signature");
my $builtpkg=($options{pkgtoo} != 1) ? $path[-1].'#.*pkg\.tar.*' :
$builtpkg=($options{pkgtoo} != 1) ? $path[-1].'#.*pkg\.tar.*' :
'ReallyLongStringThatWouldNeverMatchAnyBuiltPackage'; 'ReallyLongStringThatWouldNeverMatchAnyBuiltPackage';
$builtpkg =~ s/\+/\\\+/; $builtpkg =~ s/\+/\\\+/;
opendir (DIR, $port) or return; opendir (DIR, $port) or return;
my $nf = 0;
foreach my $f (sort(readdir(DIR))) { foreach my $f (sort(readdir(DIR))) {
next if ( $f eq "." or $f eq ".." ); next if ( $f eq "." or $f eq ".." );
$nf += 1;
$f =~ s/\+/\\\+/; # plus sign in filenames interferes with regex search $f =~ s/\+/\\\+/; # plus sign in filenames interferes with regex search
if ((grep /$f/, @wanted) >= 1 or ($f =~ /$builtpkg/)) { if ((grep /$f/, @wanted) >= 1 or ($f =~ /$builtpkg/)) {
print "... keeping file $port/$f.\n" unless $options{quiet} == 1; print "... keeping file $port/$f.\n" unless $options{quiet} == 1;
@ -125,7 +126,6 @@ sub sweep {
} }
} }
closedir (DIR); closedir (DIR);
return (1-$options{dryrun})*($options{rmdir}-$nf);
} }
sub remove_notify { sub remove_notify {
@ -137,8 +137,7 @@ sub remove_notify {
} }
sub remove_forreal { sub remove_forreal {
my $path = shift; my $path = shift; my $type = shift;
my $type = shift;
if ($type eq "file") { if ($type eq "file") {
unlink "$path" or return; unlink "$path" or return;
} else { } else {
@ -147,22 +146,19 @@ sub remove_forreal {
} }
sub do_sweep { sub do_sweep {
my $port=shift; my $port = shift; my $nf = 0;
if (! -f "$port/.signature") { if (! -f "$port/.signature") {
print "Warning: no signature found.\n"; opendir (PORTDIR,$port) or die "cannot open $port for reading\n";
print "Merciless destruction will be unleashed on $port.\n"; foreach my $f (readdir PORTDIR) {
print "Press Ctrl+C within 3 seconds to abort.\n"; next if ($f eq '.' or $f eq '..');
sleep 3; $nf += 1;
}
closedir (PORTDIR);
print "no signature file, $port does not define a valid port.";
rm_emptydir($port,$nf);
} else {
sweep($port);
} }
my $premsg = ($options{rmdir}==1) ? " directory will be removed if empty.":
" use -d to remove empty directories.";
$premsg = (-f "$port/Pkgfile") ? "": "$port: no Pkgfile found, $premsg";
print $premsg if $premsg ne "";
my $okremove = sweep($port);
my $postmsg = (($premsg ne "")*($okremove != 1)) ?
"could not remove $port: directory not empty\n": "";
print $postmsg if $postmsg ne "";
rm_emptydir ($port, $okremove);
} }
sub getportdirs { sub getportdirs {
@ -201,11 +197,11 @@ if ($options{auto} == 1) {
foreach my $collection (@basedirs) { foreach my $collection (@basedirs) {
print "====> Sweeping port collection $collection\n"; print "====> Sweeping port collection $collection\n";
foreach my $port (list_subdirs($collection)) { foreach my $port (list_subdirs($collection)) {
do_sweep($port) do_sweep($port);
} }
} }
} else { } else {
foreach my $port (@portdirs) { foreach my $port (@portdirs) {
do_sweep($port) do_sweep($port);
} }
} }