| 1 | package POE::Component::DirWatch::Object::NewUnmodifiedFile; | ||
| 2 | # ABSTRACT: extends DirWatch::Object in order to exclude files that have already been processed | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Moose; | ||
| 7 | use POE; | ||
| 8 | |||
| 9 | our $VERSION = "0.002"; | ||
| 10 | |||
| 11 | extends 'POE::Component::DirWatch::Object'; | ||
| 12 | |||
| 13 | has 'seen_files' => (is => 'rw', isa => 'HashRef', default => sub{{}}); | ||
| 14 | |||
| 15 | #--------#---------#---------#---------#---------#---------#---------#---------# | ||
| 16 | |||
| 17 | override '_dispatch' => sub{ | ||
| 18 | my ($self, $kernel, $fname, $fpath) = @_[OBJECT, KERNEL, ARG0, ARG1]; | ||
| 19 | |||
| 20 | return if( $self->seen_files->{ $fpath } ); | ||
| 21 | |||
| 22 | # Get last modify time of file | ||
| 23 | my $mtime = get_mtime($fpath); | ||
| 24 | |||
| 25 | $self->seen_files->{ $fpath } = $mtime; | ||
| 26 | super; | ||
| 27 | }; | ||
| 28 | |||
| 29 | before '_poll' => sub{ | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | # Eliminate from seen_files any files that no longer exist or have a different mtime | ||
| 33 | %{ $self->seen_files } = map {$_ => $self->seen_files->{$_} } | ||
| 34 | grep { $self->seen_files->{$_} == get_mtime($_) } # Check that file has same mtime | ||
| 35 | grep {-e $_ } # Check that file exists | ||
| 36 | keys %{ $self->seen_files }; | ||
| 37 | }; | ||
| 38 | |||
| 39 | sub get_mtime { | ||
| 40 | my $file = shift; | ||
| 41 | |||
| 42 | my $mtime; | ||
| 43 | my @file_stats = stat($file); | ||
| 44 | # In case of file being unavailable for some reason | ||
| 45 | if (@file_stats) { | ||
| 46 | $mtime = $file_stats[9] || 1; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | $mtime = 1; | ||
| 50 | } | ||
| 51 | return $mtime; | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | __END__; |