96a90ffab3
- Ensure that $to_delete is always defined - Re-indent to 4 spaces for readability - Don't only delete files -- it's ok to delete directories, too - Print the directory from which we are deleting Signed-off-by: Jeff Squyres <jsquyres@cisco.com>
60 строки
1.4 KiB
Perl
Исполняемый файл
60 строки
1.4 KiB
Perl
Исполняемый файл
#!/usr/bin/env perl
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use POSIX qw(strftime);
|
|
|
|
my $happy = 1;
|
|
|
|
my $savedays = $ARGV[0];
|
|
my $dir = $ARGV[1];
|
|
|
|
$happy = 0
|
|
if ($savedays <= 0 || ! -d $dir);
|
|
die "Must specify number of days and a directory"
|
|
if (!$happy);
|
|
|
|
#------------------------------------------------------------------
|
|
|
|
# Read in all the dir entries
|
|
opendir(DIR, $dir) || die "Cannot open $dir";
|
|
my @files = readdir(DIR);
|
|
closedir(DIR);
|
|
|
|
# How many days to keep?
|
|
my $t = time() - ($savedays * 60 * 60 * 24);
|
|
print "Deleting anything in $dir before: " . strftime("%D", localtime($t)) . "\n";
|
|
my $to_delete = "";
|
|
|
|
# Check everything in the dir; if is a dir, is not . or .., and is
|
|
# older than the save date, keep it for deleting later.
|
|
foreach my $file (sort(@files)) {
|
|
if ($file ne "index.php" &&
|
|
$file ne "md5sums.txt" &&
|
|
$file ne "sha1sums.txt" &&
|
|
$file ne "latest_snapshot.txt" &&
|
|
$file ne "." &&
|
|
$file ne "..") {
|
|
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
|
|
$atime,$mtime,$ctime,$blksize,$blocks) = stat("$dir/$file");
|
|
my $str = "SAVE";
|
|
if ($mtime < $t) {
|
|
$to_delete = "$to_delete $dir/$file";
|
|
$str = "DELETE";
|
|
}
|
|
print "Found $file: $str (mtime: " . strftime("%D", localtime($mtime)) . ")\n";
|
|
}
|
|
}
|
|
|
|
# If we found anything to delete, do so.
|
|
if ($to_delete ne "") {
|
|
print "Deleting: $to_delete\n";
|
|
system("chmod -R u=rwx $to_delete");
|
|
system("rm -rf $to_delete");
|
|
} else {
|
|
print "Nothing to delete!\n";
|
|
}
|
|
|
|
exit(0);
|