Skip to content

Commit

Permalink
Add a /tmp cleaner program
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmv committed May 27, 2010
1 parent 25b91a8 commit 63f5acb
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/tmp_cleaner.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env perl

use warnings;
use strict;
use File::Find;
use constant TMPDIR => "/tmp";

my @args = splice @ARGV;
unless ( grep $_ eq '--clean', @args ) {
print "$_\n" for file_list();
} else {
my %skip;
$skip{$_}++ for split /\n/, do {local $/; scalar <>};
my @destroy = grep {!$skip{$_}} file_list();
for (@destroy) {
if (-d $_) {
rmdir($_) or die "Can't rmdir $_: $!";
} else {
unlink($_) or die "Can't unlink $_: $!";
}
}
}

sub file_list {
my %open;
# Find all the open files under /tmp
$open{$_}++ for map {s/^n//;$_} grep {/^n(.*)/}
split /\n/, `lsof +D @{[TMPDIR]} -F 'n'`;

for my $file (keys %open) {
# Add the parent dirs, as well
$open{$file}++ while $file ne "/" and $file =~ s{/[^/]+$}{};
}

my @found;
finddepth(
{
preprocess => sub {
# Skip directories which had open files in them
return grep {not $open{$File::Find::dir."/".$_}} @_;
},
wanted => sub {
# Everything else gets listed
push @found, $File::Find::name;
}
},
TMPDIR
);
return @found;
}

0 comments on commit 63f5acb

Please sign in to comment.