| 1 | package CSS::Watcher::Monitor; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Log::Log4perl qw(:easy); | ||
| 8 | use File::Spec; | ||
| 9 | use Fcntl ':mode'; | ||
| 10 | use List::MoreUtils qw(any); | ||
| 11 | |||
| 12 | our @STAT_FIELDS = qw( | ||
| 13 | dev inode mode num_links uid gid rdev size atime mtime ctime | ||
| 14 | blk_size blocks | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $class= shift; | ||
| 19 | my $options = shift; | ||
| 20 | |||
| 21 | return bless ({ | ||
| 22 | dir => $options->{dir} // undef, | ||
| 23 | oldstats => {}, | ||
| 24 | }, $class); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub dir { | ||
| 28 | my $self = shift; | ||
| 29 | croak "dir attribute is read-only" if @_; | ||
| 30 | return $self->{dir}; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub scan { | ||
| 34 | my ($self, $callback, $skip_dirs) = @_; | ||
| 35 | |||
| 36 | return 0 unless (defined $callback && defined $self->dir && -d $self->dir); | ||
| 37 | |||
| 38 | my $newstat = $self->_get_files_info( $self->dir, $skip_dirs ); | ||
| 39 | |||
| 40 | my $changes = 0; | ||
| 41 | while ( my( $fname, $stat ) = each %{$newstat->{files}} ) { | ||
| 42 | unless ($self->_deep_compare ($self->_get_stat ($fname), $stat )) { | ||
| 43 | $self->_set_stat ($fname, $stat); | ||
| 44 | $callback->($fname); | ||
| 45 | $changes++; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | return $changes; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub is_changed { | ||
| 52 | my ( $self, $filename ) = @_; | ||
| 53 | my %objstat; | ||
| 54 | @objstat{@STAT_FIELDS} = stat ( $filename ); | ||
| 55 | |||
| 56 | # this file may never present before and not exist, return false | ||
| 57 | return 0 unless (defined ($objstat{atime}) && -f $filename); | ||
| 58 | |||
| 59 | not $self->_deep_compare ( | ||
| 60 | $self->_get_stat ($filename), | ||
| 61 | \%objstat); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub make_dirty { | ||
| 65 | my $self = shift; | ||
| 66 | $self->{oldstats} = {}; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _get_stat { | ||
| 70 | my ( $self, $filename ) = @_; | ||
| 71 | return $self->{oldstats}{$filename} // {}; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _set_stat { | ||
| 75 | my ( $self, $filename, $stat ) = @_; | ||
| 76 | $self->{oldstats}{$filename} = $stat; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _deep_compare { | ||
| 80 | my ( $self, $this, $that ) = @_; | ||
| 81 | use Storable qw/freeze/; | ||
| 82 | local $Storable::canonical = 1; | ||
| 83 | return freeze( $this ) eq freeze( $that ); | ||
| 84 | } | ||
| 85 | |||
| 86 | # Scan our target object | ||
| 87 | sub _get_files_info { | ||
| 88 | my ( $self, $dir, $skip_dirs ) = @_; | ||
| 89 | my %info; | ||
| 90 | |||
| 91 | $skip_dirs ||= []; | ||
| 92 | |||
| 93 | eval { | ||
| 94 | if ( -d $dir ) { | ||
| 95 | |||
| 96 | # Expand whole directory tree | ||
| 97 | my @work = $self->_read_dir( $dir ); | ||
| 98 | while ( my $obj = shift @work ) { | ||
| 99 | next # // skip symlinks that have "../" (circular symlink) | ||
| 100 | if ( -d $obj | ||
| 101 | && -l $obj | ||
| 102 | && readlink($obj) =~ m|\.\./| ); | ||
| 103 | if (-f $obj) { | ||
| 104 | my %objstat; | ||
| 105 | @objstat{@STAT_FIELDS} = stat ( $obj ); | ||
| 106 | $info{ files }{ $obj } = \%objstat; | ||
| 107 | } | ||
| 108 | elsif ( -d $obj && ( !any { $obj =~ m/$_/; } @{$skip_dirs} ) ) { | ||
| 109 | # Depth first to simulate recursion | ||
| 110 | unshift @work, $self->_read_dir( $obj ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | }; | ||
| 115 | |||
| 116 | $info{error} = $@; | ||
| 117 | |||
| 118 | return \%info; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _read_dir { | ||
| 122 | my $self = shift; | ||
| 123 | my $dir = shift; | ||
| 124 | |||
| 125 | opendir( my $dh, $dir ) or LOGDIE "Can't read $dir ($!)"; | ||
| 126 | my @files = map { File::Spec->catfile( $dir, $_ ) } | ||
| 127 | sort | ||
| 128 | grep { $_ !~ /^[.]{1,2}$/ } readdir( $dh ); | ||
| 129 | closedir( $dh ); | ||
| 130 | |||
| 131 | return @files; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | =head1 NAME | ||
| 138 | |||
| 139 | CSS::Watcher::Monitor - Monitor files for changes. | ||
| 140 | |||
| 141 | =head1 SYNOPSIS | ||
| 142 | |||
| 143 | use CSS::Watcher::Monitor; | ||
| 144 | my $cm = CSS::Watcher::Monitor->new (dir => '/foo/bar'); | ||
| 145 | |||
| 146 | # return num of files modified | ||
| 147 | $cm->scan( | ||
| 148 | sub { | ||
| 149 | my $file = shift; | ||
| 150 | # process changed file or first scan new file | ||
| 151 | } ); | ||
| 152 | |||
| 153 | # Check does file changed since last $cm->scan | ||
| 154 | say $cm->is_changed('/foo/bar/baz.txt'); | ||
| 155 | |||
| 156 | # clean old file stat cache | ||
| 157 | $cm->make_dirty(); | ||
| 158 | |||
| 159 | =head1 DESCRIPTION | ||
| 160 | |||
| 161 | Watch for changes, call callback sub. Call callback on first scan too. | ||
| 162 | |||
| 163 | =head1 SEE ALSO | ||
| 164 | |||
| 165 | File::Monitor - I get some patterns from there | ||
| 166 | |||
| 167 | =head1 AUTHOR | ||
| 168 | |||
| 169 | Olexandr Sydorchuk (olexandr.syd@gmail.com) | ||
| 170 | |||
| 171 | =head1 COPYRIGHT AND LICENSE | ||
| 172 | |||
| 173 | Copyright (C) 2014 by Olexandr Sydorchuk | ||
| 174 | |||
| 175 | This program is free software; you can redistribute it and/or modify | ||
| 176 | it under the same terms as Perl itself, either Perl version 5.8.2 or, | ||
| 177 | at your option, any later version of Perl 5 you may have available. | ||
| 178 | |||
| 179 | =cut |