1 package File::ChangeNotify::Watcher::Default;
8 use File::Find qw( finddepth );
10 use Time::HiRes qw( sleep );
12 # Trying to import this just blows up on Win32, and checking
13 # Time::HiRes::d_hires_stat() _also_ blows up on Win32.
15 eval { Time::HiRes->import('stat') };
19 use MooseX::SemiAffordanceAccessor;
21 extends 'File::ChangeNotify::Watcher';
26 default => sub { {} },
29 sub sees_all_events {0}
34 $self->_set_map( $self->_build_map() );
45 my $path = $File::Find::name;
47 if ( $self->_path_is_excluded($path) ) {
48 $File::Find::prune = 1;
52 my $entry = $self->_entry_for_map($path) or return;
55 follow_fast => ( $self->follow_symlinks() ? 1 : 0 ),
58 @{ $self->directories() },
68 my $is_dir = -d $path ? 1 : 0;
70 return if -l $path && !$is_dir;
73 my $filter = $self->filter();
74 return unless ( File::Spec->splitpath($path) )[2] =~ /$filter/;
80 size => ( $is_dir ? 0 : -s _ ),
84 # It seems that Time::HiRes's stat does not act exactly like the
85 # built-in, so if I do ( stat _ )[9] it will not work (grr).
96 my @events = $self->_interesting_events();
97 return @events if @events;
99 sleep $self->sleep_interval();
103 sub _interesting_events {
108 my $old_map = $self->_map();
109 my $new_map = $self->_build_map();
111 for my $path ( sort keys %{$old_map} ) {
112 if ( !exists $new_map->{$path} ) {
113 if ( $old_map->{$path}{is_dir} ) {
114 $self->_remove_directory($path);
117 push @interesting, $self->event_class()->new(
123 !$old_map->{$path}{is_dir}
124 && ( $old_map->{$path}{mtime} != $new_map->{$path}{mtime}
125 || $old_map->{$path}{size} != $new_map->{$path}{size} )
127 push @interesting, $self->event_class()->new(
134 for my $path ( sort grep { !exists $old_map->{$_} } keys %{$new_map} ) {
136 push @interesting, $self->event_class()->new(
143 push @interesting, $self->event_class()->new(
150 $self->_set_map($new_map);
157 __PACKAGE__->meta()->make_immutable();
165 File::ChangeNotify::Watcher::Default - Fallback default watcher subclass
169 This class implements watching by comparing two snapshots of the filesystem
170 tree. It if inefficient and dumb, and so it is the subclass of last resort.
172 Its C<< $watcher->wait_for_events() >> method sleeps between
173 comparisons of the filesystem snapshot it takes.
177 Dave Rolsky, E<lt>autarch@urth.orgE<gt>
179 =head1 COPYRIGHT & LICENSE
181 Copyright 2009 Dave Rolsky, All Rights Reserved.
183 This program is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.