More levels of verbosity.

This commit is contained in:
hjp 1999-08-21 12:37:53 +00:00
parent 9c27ca365e
commit 715ca2cab9
1 changed files with 25 additions and 13 deletions

View File

@ -3,6 +3,7 @@ use strict;
use File::stat; use File::stat;
my $verbose = 0; my $verbose = 0;
my $nop = 0;
sub usage { sub usage {
print STDERR "Usage: $0 [-d days] dir ...\n"; print STDERR "Usage: $0 [-d days] dir ...\n";
@ -10,37 +11,46 @@ sub usage {
} }
sub cleandir { sub cleandir {
my ($dir, $since) = (@_); my ($dir, $since, $level) = (@_);
my $notremoved = 0; my $notremoved = 0;
if ($verbose) { if ($verbose > 1) {
print STDERR "$0: cleandir $dir $since {\n"; print STDERR "$0:", " " x $level, " cleandir $dir $since {\n";
} }
if (!opendir(DIR, $dir)) { if (!opendir(DIR, $dir)) {
printf STDERR "$0: cannot opendir $dir: $!"; printf STDERR "$0:", " " x $level, " cannot opendir $dir: $!";
return; return;
} }
for my $i (readdir(DIR)) { for my $i (readdir(DIR)) {
if ($i eq "." || $i eq "..") {next} if ($i eq "." || $i eq "..") {next}
if ($verbose) { if ($verbose > 2) {
print STDERR "$0: checking $dir/$i\n"; print STDERR "$0:", " " x $level, " checking $dir/$i\n";
} }
my $st = lstat("$dir/$i"); my $st = lstat("$dir/$i");
if ($verbose) { if ($verbose > 3) {
print STDERR "$0: mtime=", $st->mtime, " atime=", $st->atime, "\n"; print STDERR "$0:", " " x $level, " mtime=", $st->mtime, " atime=", $st->atime, "\n";
} }
if (-d _) { if (-d _) {
if (cleandir("$dir/$i", $since) == 0 && $st->mtime < $since) { if (cleandir("$dir/$i", $since, $level+1) == 0 && $st->mtime < $since) {
if (rmdir("$dir/$i")) {next} if (rmdir("$dir/$i")) {next}
} }
} elsif ($st->mtime < $since && $st->atime < $since) { } elsif ($st->mtime < $since && $st->atime < $since) {
if ($nop) {
print "would remove $dir/$i\n";
} else {
if ($verbose > 0) {
print STDERR "$0:", " " x $level, " removing $dir/$i\n";
}
if (unlink("$dir/$i")) {next} if (unlink("$dir/$i")) {next}
print STDERR "$0:", " " x $level, " removing $dir/$i failed: $!/$i\n";
}
} }
$notremoved++; $notremoved++;
} }
if ($verbose) { if ($verbose > 1) {
print STDERR "$0: cleandir: $notremoved }\n"; print STDERR "$0:", " " x $level, " cleandir: $notremoved }\n";
} }
return $notremoved; return $notremoved;
} }
@ -54,8 +64,10 @@ sub main {
$since = time() - $days * 86400; $since = time() - $days * 86400;
} elsif ($i eq "-v") { } elsif ($i eq "-v") {
$verbose++; $verbose++;
} elsif ($i eq "-n") {
$nop++;
} else { } else {
cleandir($i, $since); cleandir($i, $since, 0);
} }
} }
exit(0); exit(0);