From 0c511f15abba3fecd8523f7d60c1fbb06356ffea Mon Sep 17 00:00:00 2001 From: John McQuah Date: Fri, 27 May 2022 14:08:07 -0400 Subject: [PATCH] prtsweep.pl: refactored handling of invalid port directories --- scripts/prtsweep.pl | 54 +++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/scripts/prtsweep.pl b/scripts/prtsweep.pl index 0d5313e..4fc4535 100755 --- a/scripts/prtsweep.pl +++ b/scripts/prtsweep.pl @@ -70,7 +70,7 @@ sub list_subdirs { opendir(DIR, $path) or return; foreach my $entry(sort(readdir(DIR))) { 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); return @list; @@ -80,7 +80,7 @@ sub parse_signature { my @signed = ("Pkgfile",".footprint",".signature","README","README.md", "pre-install","post-install",".32bit",".nostrip"); my $sigfile = shift; - open (FILE, $sigfile) or return ; + open (FILE, $sigfile) or return @signed; while () { if (/^SHA256 \(.+\) =.*$/) { $_ =~ s/^SHA256 \((.+)\) =.*$/$1/; @@ -92,9 +92,14 @@ sub parse_signature { } sub rm_emptydir { - my $portpath = shift; - my $okremove = shift; - rmdir $portpath if ($okremove == 1); + my $port = shift; my $nf = shift; + my $msg = ($options{rmdir}==1) ? "\n": + " 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 { @@ -102,20 +107,16 @@ sub sweep { while ($port =~ s/\/\//\//g) {} $port =~ s/\/$//; my @path = split /\//, $port; - my $builtpkg; print "=======> $port\n" unless $options{quiet}==1; my @wanted = parse_signature ("$port/.signature"); - - $builtpkg=($options{pkgtoo} != 1) ? $path[-1].'#.*pkg\.tar.*' : + my $builtpkg=($options{pkgtoo} != 1) ? $path[-1].'#.*pkg\.tar.*' : 'ReallyLongStringThatWouldNeverMatchAnyBuiltPackage'; $builtpkg =~ s/\+/\\\+/; opendir (DIR, $port) or return; - my $nf = 0; foreach my $f (sort(readdir(DIR))) { next if ( $f eq "." or $f eq ".." ); - $nf += 1; $f =~ s/\+/\\\+/; # plus sign in filenames interferes with regex search if ((grep /$f/, @wanted) >= 1 or ($f =~ /$builtpkg/)) { print "... keeping file $port/$f.\n" unless $options{quiet} == 1; @@ -125,7 +126,6 @@ sub sweep { } } closedir (DIR); - return (1-$options{dryrun})*($options{rmdir}-$nf); } sub remove_notify { @@ -137,8 +137,7 @@ sub remove_notify { } sub remove_forreal { - my $path = shift; - my $type = shift; + my $path = shift; my $type = shift; if ($type eq "file") { unlink "$path" or return; } else { @@ -147,22 +146,19 @@ sub remove_forreal { } sub do_sweep { - my $port=shift; + my $port = shift; my $nf = 0; if (! -f "$port/.signature") { - print "Warning: no signature found.\n"; - print "Merciless destruction will be unleashed on $port.\n"; - print "Press Ctrl+C within 3 seconds to abort.\n"; - sleep 3; + opendir (PORTDIR,$port) or die "cannot open $port for reading\n"; + foreach my $f (readdir PORTDIR) { + next if ($f eq '.' or $f eq '..'); + $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 { @@ -201,11 +197,11 @@ if ($options{auto} == 1) { foreach my $collection (@basedirs) { print "====> Sweeping port collection $collection\n"; foreach my $port (list_subdirs($collection)) { - do_sweep($port) + do_sweep($port); } } } else { foreach my $port (@portdirs) { - do_sweep($port) + do_sweep($port); } }