2022-05-26 17:11:09 -04:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
#
|
|
|
|
# prtsweep
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# This is a script for removing rubbish from a CRUX port tree.
|
|
|
|
# Distributed under the terms of the GPL license.
|
|
|
|
#
|
2022-05-28 12:51:14 -04:00
|
|
|
# ChangeLog and author information available in the prt-utils tarball.
|
2022-05-26 17:11:09 -04:00
|
|
|
#
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Cwd qw(cwd getcwd);
|
|
|
|
use File::Path;
|
|
|
|
our $version = "1.2.1";
|
|
|
|
our %options = ( auto => 0, dryrun => 0, rmdir => 0, pkgtoo => 0, quiet => 0 );
|
|
|
|
our @portdirs;
|
|
|
|
our $argports;
|
|
|
|
|
2022-05-29 12:35:18 -04:00
|
|
|
######################### main routine ################################
|
|
|
|
parse_args();
|
|
|
|
print_usage() if ((2*$argports-1)*(1-2*$options{auto}) < 0);
|
|
|
|
|
|
|
|
if ($options{auto} == 1) {
|
|
|
|
my @basedirs = getportdirs();
|
|
|
|
foreach my $collection (@basedirs) {
|
|
|
|
print "====> Sweeping port collection $collection\n";
|
|
|
|
foreach my $port (list_subdirs($collection)) {
|
|
|
|
do_sweep($port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach my $port (@portdirs) {
|
|
|
|
do_sweep($port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
######################### subroutines #################################
|
2022-05-26 17:11:09 -04:00
|
|
|
sub print_usage {
|
|
|
|
print <<EOT;
|
|
|
|
Usage: prtsweep [OPTION]... [PORTDIRS]...
|
|
|
|
|
|
|
|
-a automatic mode, only valid when [PORTDIRS] are omitted
|
2022-05-29 12:35:18 -04:00
|
|
|
-d remove directories of dropped ports (signature file not found)
|
2022-05-26 17:11:09 -04:00
|
|
|
-n dry-run, don't actually delete anything
|
|
|
|
-p delete any built packages too
|
2022-05-29 12:35:18 -04:00
|
|
|
-q quiet mode, only print messages when files are removed
|
2022-05-26 17:11:09 -04:00
|
|
|
|
2022-05-27 09:31:43 -04:00
|
|
|
Report bugs on libera.chat #crux-devel
|
2022-05-26 17:11:09 -04:00
|
|
|
EOT
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub parse_args {
|
|
|
|
foreach my $arg (@ARGV) {
|
2022-05-29 12:35:18 -04:00
|
|
|
if ($arg eq "-a") {
|
2022-05-26 17:11:09 -04:00
|
|
|
$options{auto} = 1;
|
2022-05-29 12:35:18 -04:00
|
|
|
} elsif ($arg eq "-d") {
|
2022-05-26 17:11:09 -04:00
|
|
|
$options{rmdir} = 1;
|
2022-05-29 12:35:18 -04:00
|
|
|
} elsif ($arg eq "-n") {
|
2022-05-26 17:11:09 -04:00
|
|
|
$options{dryrun} = 1;
|
2022-05-29 12:35:18 -04:00
|
|
|
} elsif ($arg eq "-p") {
|
2022-05-26 17:11:09 -04:00
|
|
|
$options{pkgtoo} = 1;
|
2022-05-29 12:35:18 -04:00
|
|
|
} elsif ($arg eq "-q") {
|
2022-05-26 17:11:09 -04:00
|
|
|
$options{quiet} = 1;
|
2022-05-29 12:35:18 -04:00
|
|
|
} elsif ($arg eq "--version") {
|
2022-05-26 17:11:09 -04:00
|
|
|
print $version."\n";
|
|
|
|
exit 0;
|
2022-05-29 12:35:18 -04:00
|
|
|
} elsif ((-d "$arg") || # false for symlink to a portdir,
|
|
|
|
(-f "$arg/.signature")) { # so a second test is needed
|
2022-05-26 17:11:09 -04:00
|
|
|
push (@portdirs, $arg);
|
2022-05-29 12:35:18 -04:00
|
|
|
} elsif (-f "$arg") {
|
|
|
|
print "WARN: $arg is not a port directory or recognized option, ignoring.\n";
|
2022-05-26 17:11:09 -04:00
|
|
|
} else {
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$argports = @portdirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub list_subdirs {
|
|
|
|
my $path = shift;
|
|
|
|
my @list;
|
|
|
|
while ($path =~ s/\/\//\//g) {}
|
|
|
|
$path =~ s/\/$//;
|
|
|
|
|
|
|
|
opendir(DIR, $path) or return;
|
|
|
|
foreach my $entry(sort(readdir(DIR))) {
|
2022-05-28 12:51:14 -04:00
|
|
|
next if ( substr($entry,0,1) eq '.' );
|
2022-05-29 12:35:18 -04:00
|
|
|
push (@list, "$path/$entry") if ((-d "$path/$entry")
|
|
|
|
or (-f "$path/$entry/.signature"));
|
2022-05-26 17:11:09 -04:00
|
|
|
}
|
|
|
|
closedir(DIR);
|
|
|
|
return @list;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub parse_signature {
|
|
|
|
my @signed = ("Pkgfile",".footprint",".signature","README","README.md",
|
|
|
|
"pre-install","post-install",".32bit",".nostrip");
|
|
|
|
my $sigfile = shift;
|
2022-05-27 14:08:07 -04:00
|
|
|
open (FILE, $sigfile) or return @signed;
|
2022-05-26 17:11:09 -04:00
|
|
|
while (<FILE>) {
|
|
|
|
if (/^SHA256 \(.+\) =.*$/) {
|
|
|
|
$_ =~ s/^SHA256 \((.+)\) =.*$/$1/;
|
|
|
|
push (@signed, $_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close (FILE);
|
|
|
|
return @signed ;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sweep {
|
|
|
|
my $port = shift;
|
|
|
|
while ($port =~ s/\/\//\//g) {}
|
|
|
|
$port =~ s/\/$//;
|
|
|
|
my @path = split /\//, $port;
|
|
|
|
|
|
|
|
print "=======> $port\n" unless $options{quiet}==1;
|
|
|
|
my @wanted = parse_signature ("$port/.signature");
|
2022-05-29 12:35:18 -04:00
|
|
|
my $builtpkg=$path[-1].'#.*pkg\.tar\.(bz2|gz|lz|xz)$';
|
|
|
|
$builtpkg =~ s/\+/\\\+/; # plus sign in filenames interferes with regex search
|
2022-05-26 17:11:09 -04:00
|
|
|
|
|
|
|
opendir (DIR, $port) or return;
|
|
|
|
foreach my $f (sort(readdir(DIR))) {
|
2022-05-28 12:51:14 -04:00
|
|
|
next if ( $f eq '.' or $f eq '..' );
|
2022-05-29 12:35:18 -04:00
|
|
|
$f =~ s/\+/\\\+/;
|
|
|
|
if ((grep /$f/, @wanted) >= 1 or
|
|
|
|
($f =~ /$builtpkg/)*($options{pkgtoo}==0)) {
|
2022-05-26 17:11:09 -04:00
|
|
|
print "... keeping file $port/$f.\n" unless $options{quiet} == 1;
|
|
|
|
} else {
|
2022-05-28 12:51:14 -04:00
|
|
|
remove ("$port/$f");
|
2022-05-26 17:11:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir (DIR);
|
|
|
|
}
|
|
|
|
|
2022-05-28 12:51:14 -04:00
|
|
|
sub remove {
|
2022-05-26 17:11:09 -04:00
|
|
|
my $path=shift;
|
|
|
|
my $append = ($options{dryrun}==1) ? "(dry run)\n" : "\n";
|
2022-05-28 12:51:14 -04:00
|
|
|
if (-d $path) {
|
|
|
|
print "+ removing directory $path $append";
|
|
|
|
rmtree ($path,0,1) if ($options{dryrun}==0);
|
2022-05-26 17:11:09 -04:00
|
|
|
} else {
|
2022-05-28 12:51:14 -04:00
|
|
|
print "+ removing file $path $append";
|
|
|
|
if ($options{dryrun}==0) { unlink "$path" or return; }
|
2022-05-26 17:11:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub do_sweep {
|
2022-05-29 12:35:18 -04:00
|
|
|
# argument either a real directory (not symlink) or has a signature;
|
|
|
|
# this subroutine determines which condition was satisfied.
|
2022-05-27 14:08:07 -04:00
|
|
|
my $port = shift; my $nf = 0;
|
2022-05-26 17:11:09 -04:00
|
|
|
if (! -f "$port/.signature") {
|
2022-05-29 12:35:18 -04:00
|
|
|
opendir (PORTDIR,$port) or return;
|
2022-05-27 14:08:07 -04:00
|
|
|
foreach my $f (readdir PORTDIR) {
|
|
|
|
next if ($f eq '.' or $f eq '..');
|
|
|
|
$nf += 1;
|
|
|
|
}
|
|
|
|
closedir (PORTDIR);
|
2022-05-29 12:35:18 -04:00
|
|
|
print "WARN: $port/.signature not found, invalid port directory.";
|
2022-05-27 14:08:07 -04:00
|
|
|
rm_emptydir($port,$nf);
|
|
|
|
} else {
|
|
|
|
sweep($port);
|
2022-05-26 17:11:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 12:51:14 -04:00
|
|
|
sub rm_emptydir {
|
|
|
|
my $port = shift; my $nf = shift;
|
|
|
|
my $msg = ($options{rmdir}==1) ? "\n":
|
2022-05-29 12:35:18 -04:00
|
|
|
"\n Use -d to remove empty directories.\n";
|
2022-05-28 12:51:14 -04:00
|
|
|
my $modal = ($options{dryrun}==0) ? "" : "would be";
|
2022-05-29 12:35:18 -04:00
|
|
|
my $post = ($nf == 0) ? " Empty directory $port $modal deleted.\n" :
|
|
|
|
" Cannot remove $port: directory not empty\n";
|
|
|
|
$msg = ($options{rmdir}==1) ? "$msg $post" : $msg ;
|
2022-05-28 12:51:14 -04:00
|
|
|
print $msg;
|
|
|
|
rmdir ($port) if (($nf == 0) and ($options{dryrun} == 0));
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:11:09 -04:00
|
|
|
sub getportdirs {
|
|
|
|
my $collection;
|
|
|
|
my @basedirs;
|
|
|
|
my $portetc = "/etc/ports/";
|
|
|
|
opendir (PORTS_DEFS,$portetc) or die "cannot open $portetc for reading";
|
|
|
|
foreach (readdir PORTS_DEFS) {
|
|
|
|
next if ($_ eq '.' or $_ eq '..');
|
|
|
|
if (/.*(rsync|httpup)$/) {
|
|
|
|
open SYNC, $portetc.$_ or die "cannot open $portetc.$_";
|
|
|
|
while (<SYNC>) {
|
|
|
|
$collection=$2 if /^(destination|ROOT_DIR)=(.+)$/;
|
|
|
|
}
|
|
|
|
close SYNC;
|
|
|
|
push (@basedirs , $collection);
|
|
|
|
} elsif (/.*git$/) {
|
|
|
|
open SYNC, $portetc.$_ or die "cannot open $portetc.$_";
|
|
|
|
while (<SYNC>) {
|
|
|
|
$collection="/usr/ports/$1" if /^NAME=(.+)$/;
|
|
|
|
}
|
|
|
|
close SYNC;
|
|
|
|
push (@basedirs , $collection);
|
|
|
|
} else {}
|
|
|
|
}
|
|
|
|
closedir PORTS_DEFS;
|
|
|
|
return @basedirs ;
|
|
|
|
}
|