prtsweep.pl: improved handling of empty directories

This commit is contained in:
John McQuah 2022-05-27 09:31:43 -04:00
parent efbc69ff53
commit 274f883c21

View File

@ -10,6 +10,7 @@
# Partial Changelog: # Partial Changelog:
# 1.2.1 - First rewrite in perl # 1.2.1 - First rewrite in perl
# 1.2 - Added support for renamed sources # 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 -p delete any built packages too
-q quiet mode -q quiet mode
Report bugs to <jmcquah\@disroot.org>. Report bugs on libera.chat #crux-devel
EOT EOT
exit(0); exit(0);
} }
@ -68,7 +69,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 ( $entry eq "." or $entry 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);
@ -92,17 +93,8 @@ sub parse_signature {
sub rm_emptydir { sub rm_emptydir {
my $portpath = shift; my $portpath = shift;
my $postmsg = ($options{rmdir}==1) ? "\n" : "use '-d' to remove empty directories.\n"; my $okremove = shift;
print "$portpath: no Pkgfile found.$postmsg"; rmdir $portpath if ($okremove == 1);
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; }
} }
sub sweep { sub sweep {
@ -120,8 +112,10 @@ sub sweep {
$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;
@ -131,6 +125,7 @@ sub sweep {
} }
} }
closedir (DIR); closedir (DIR);
return (1-$options{dryrun})*($options{rmdir}-$nf);
} }
sub remove_notify { sub remove_notify {
@ -159,11 +154,15 @@ sub do_sweep {
print "Press Ctrl+C within 3 seconds to abort.\n"; print "Press Ctrl+C within 3 seconds to abort.\n";
sleep 3; sleep 3;
} }
if (-f "$port/Pkgfile") { my $premsg = ($options{rmdir}==1) ? " directory will be removed if empty.":
sweep($port); " use -d to remove empty directories.";
} else { $premsg = (-f "$port/Pkgfile") ? "": "$port: no Pkgfile found, $premsg";
rm_emptydir($port); 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 {