prtsweep.pl: improved handling of @ARGV when using shell globs

This commit is contained in:
John McQuah 2022-05-28 12:51:14 -04:00
parent 9ccc1ebf59
commit 954afb830f
1 changed files with 22 additions and 30 deletions

View File

@ -7,10 +7,7 @@
# This is a script for removing rubbish from a CRUX port tree.
# Distributed under the terms of the GPL license.
#
# 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
# ChangeLog and author information available in the prt-utils tarball.
#
########################################################################
@ -39,6 +36,7 @@ EOT
sub parse_args {
foreach my $arg (@ARGV) {
next if (-f $arg); # no filenames, only options or directories
if ($arg =~ /^-a$/) {
$options{auto} = 1;
} elsif ($arg =~ /^-d$/) {
@ -69,7 +67,7 @@ sub list_subdirs {
opendir(DIR, $path) or return;
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";
}
closedir(DIR);
@ -91,17 +89,6 @@ sub parse_signature {
return @signed ;
}
sub rm_emptydir {
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 {
my $port = shift;
while ($port =~ s/\/\//\//g) {}
@ -116,32 +103,26 @@ sub sweep {
opendir (DIR, $port) or return;
foreach my $f (sort(readdir(DIR))) {
next if ( $f eq "." or $f eq ".." );
next if ( $f eq '.' or $f eq '..' );
$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;
} else {
my $ft=remove_notify("$port/$f");
remove_forreal("$port/$f", $ft) if $options{dryrun} == 0;
remove ("$port/$f");
}
}
closedir (DIR);
}
sub remove_notify {
sub remove {
my $path=shift;
my $type = (-d $path) ? "directory" : "file";
my $append = ($options{dryrun}==1) ? "(dry run)\n" : "\n";
print "+ removing $type $path $append";
return $type;
}
sub remove_forreal {
my $path = shift; my $type = shift;
if ($type eq "file") {
unlink "$path" or return;
if (-d $path) {
print "+ removing directory $path $append";
rmtree ($path,0,1) if ($options{dryrun}==0);
} else {
rmtree ($path,0,1);
print "+ removing file $path $append";
if ($options{dryrun}==0) { unlink "$path" or return; }
}
}
@ -161,6 +142,17 @@ sub do_sweep {
}
}
sub rm_emptydir {
my $port = shift; my $nf = shift;
my $msg = ($options{rmdir}==1) ? "\n":
" use -d to remove empty directories.\n";
my $modal = ($options{dryrun}==0) ? "" : "would be";
$msg = ($nf == 0) ? "$msg empty directory $port $modal deleted.\n" :
"$msg cannot remove $port: directory not empty\n";
print $msg;
rmdir ($port) if (($nf == 0) and ($options{dryrun} == 0));
}
sub getportdirs {
my $collection;
my @basedirs;