From 274f883c2156d4c295b9b04b1fa6534c23026b56 Mon Sep 17 00:00:00 2001 From: John McQuah Date: Fri, 27 May 2022 09:31:43 -0400 Subject: [PATCH] prtsweep.pl: improved handling of empty directories --- scripts/prtsweep.pl | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/scripts/prtsweep.pl b/scripts/prtsweep.pl index c2c5433..0d5313e 100755 --- a/scripts/prtsweep.pl +++ b/scripts/prtsweep.pl @@ -10,6 +10,7 @@ # Partial Changelog: # 1.2.1 - First rewrite in perl # 1.2 - Added support for renamed sources +# 1.1.3 - Replace .md5sum with .signature in the KEEP_FILES list # ######################################################################## @@ -31,7 +32,7 @@ Usage: prtsweep [OPTION]... [PORTDIRS]... -p delete any built packages too -q quiet mode -Report bugs to . +Report bugs on libera.chat #crux-devel EOT exit(0); } @@ -68,7 +69,7 @@ sub list_subdirs { opendir(DIR, $path) or return; foreach my $entry(sort(readdir(DIR))) { - next if ( $entry eq "." or $entry eq ".." ); + next if ( substr($entry,0,1) eq "." ); push (@list, "$path/$entry") if -d "$path/$entry" } closedir(DIR); @@ -92,17 +93,8 @@ sub parse_signature { sub rm_emptydir { my $portpath = shift; - my $postmsg = ($options{rmdir}==1) ? "\n" : "use '-d' to remove empty directories.\n"; - print "$portpath: no Pkgfile found.$postmsg"; - return unless $options{rmdir}==1; - my $nf = 0; - opendir (DIR,$portpath) or die "cannot open $portpath for directory listing\n"; - foreach (readdir DIR) { - next if (($_ eq '.') or ($_ eq '..')); - $nf += 1; - } - closedir (DIR); - if ($nf == 0) { rmdir $portpath; } + my $okremove = shift; + rmdir $portpath if ($okremove == 1); } sub sweep { @@ -120,8 +112,10 @@ sub sweep { $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; @@ -131,6 +125,7 @@ sub sweep { } } closedir (DIR); + return (1-$options{dryrun})*($options{rmdir}-$nf); } sub remove_notify { @@ -159,11 +154,15 @@ sub do_sweep { print "Press Ctrl+C within 3 seconds to abort.\n"; sleep 3; } - if (-f "$port/Pkgfile") { - sweep($port); - } else { - rm_emptydir($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 {