App-FileCleanerByDiskUage
view release on metacpan or search on metacpan
lib/App/FileCleanerByDiskUage.pm view on Meta::CPAN
$opts{dry_run} = 1,;
}
my $df = df($du_path);
# the results to be returned
my $results = {
unlinked => [],
unlink_failed => [],
unlink_errors => [],
found_files => [],
found_files_count => 0,
unlinked_count => 0,
unlink_failed_count => 0,
du_target => $opts{du},
du_starting => $df->{per},
du_ending => $df->{per},
min_files => 0,
dry_run => $opts{dry_run},
path => \@paths,
missing_paths => \@missing_paths,
};
if ( !defined( $paths[0] ) ) {
if ( $opts{use_pid} ) {
unlink_pid_file($pid_file);
}
return $results;
}
if ( $df->{per} < $opts{du} ) {
if ( $opts{use_pid} ) {
unlink_pid_file($pid_file);
}
return $results;
}
my @files;
if ( defined( $opts{ignore} ) ) {
my $ignore_rule = File::Find::Rule->new;
$ignore_rule->name(qr/$opts{ignore}/);
@files = File::Find::Rule->file()->not($ignore_rule)->in(@paths);
} else {
@files = File::Find::Rule->file()->in(@paths);
}
$results->{found_files_count} = $#files + 1;
# if we have a min number of files specified, make sure have that many defined
if ( $opts{min_files} && !defined( $files[ $opts{min_files} ] ) ) {
$results->{min_files} = $opts{min_files};
if ( $opts{use_pid} ) {
unlink_pid_file($pid_file);
}
return $results;
}
# get the stats for all the files
my @files_info;
foreach my $file (@files) {
my %file_info;
my $not_used;
(
$not_used, $not_used, $not_used, $not_used, $not_used,
$not_used, $not_used, $not_used, $not_used, $file_info{mtime},
$not_used, $not_used, $not_used
) = stat($file);
$file_info{name} = $file;
push( @files_info, \%file_info );
} ## end foreach my $file (@files)
# sort files oldest to newest based on mtime
@files_info = sort { $a->{mtime} cmp $b->{mtime} } @files_info;
# set this here as we are saving it into the hashref as a array ref
my @files_info_copy = @files_info;
$results->{found_files} = \@files_info_copy;
# remove the newest files if mins_files is greater than or equal to 1
if ( defined( $opts{min_files} ) && $opts{min_files} > 0 ) {
$results->{min_files} = $opts{min_files};
my $min_files_int = 1;
while ( $min_files_int <= $opts{min_files} ) {
pop(@files_info);
$min_files_int++;
}
}
# go through files and remove the oldest till we
my $int = 0;
while ( $df->{per} >= $opts{du} && defined( $files_info[$int] ) ) {
eval {
if ( $opts{dry_run} && !-w $files_info[$int]{name} ) {
die('file is not writable');
} else {
unlink( $files_info[$int]{name} ) or die($!);
}
};
my %tmp_hash = %{ $files_info[$int] };
if ($@) {
push( @{ $results->{unlink_errors} }, 'Failed to remove "' . $files_info[$int]{name} . '"... ' . $@ );
push( @{ $results->{unlink_failed} }, \%tmp_hash );
} else {
push( @{ $results->{unlinked} }, \%tmp_hash );
}
$int++;
$df = df($du_path);
} ## end while ( $df->{per} >= $opts{du} && defined( $files_info...))
$results->{du_ending} = $df->{per};
if ( defined( $results->{unlinked}[0] ) ) {
$results->{unlinked_count} = $#{ $results->{unlinked} } + 1;
}
if ( defined( $results->{unlink_failed}[0] ) ) {
$results->{unlink_failed_count} = $#{ $results->{unlink_failed} } + 1;
}
if ( $opts{use_pid} ) {
unlink_pid_file($pid_file);
}
return $results;
} ## end sub clean
=head1 AUTHOR
( run in 2.519 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )