File: Inotify.pm

package info (click to toggle)
libfile-changenotify-perl 0.31-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 356 kB
  • sloc: perl: 1,626; makefile: 8
file content (315 lines) | stat: -rw-r--r-- 7,519 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package File::ChangeNotify::Watcher::Inotify;

use strict;
use warnings;
use namespace::autoclean;

our $VERSION = '0.31';

use File::Find qw( find );
use Linux::Inotify2 1.2;
use Types::Standard qw( Bool Int );
use Type::Utils qw( class_type );

use Moo;

has is_blocking => (
    is      => 'ro',
    isa     => Bool,
    default => 1,
);

has _inotify => (
    is      => 'ro',
    isa     => class_type('Linux::Inotify2'),
    default => sub {
        Linux::Inotify2->new
            or die "Cannot construct a Linux::Inotify2 object: $!";
    },
    init_arg => undef,
);

has _mask => (
    is      => 'ro',
    isa     => Int,
    lazy    => 1,
    builder => '_build_mask',
);

with 'File::ChangeNotify::Watcher';

sub sees_all_events {1}

sub BUILD {
    my $self = shift;

    $self->_inotify->blocking( $self->is_blocking );

    # If this is done via a lazy_build then the call to
    # ->_watch_directory ends up causing endless recursion when it
    # calls ->_inotify itself.
    $self->_watch_directory($_) for @{ $self->directories };

    $self->_set_map( $self->_current_map )
        if $self->modify_includes_file_attributes
        || $self->modify_includes_content;

    return;
}

sub wait_for_events {
    my $self = shift;

    $self->_inotify->blocking(1);

    while (1) {
        my @events = $self->_interesting_events;
        return @events if @events;
    }
}

around new_events => sub {
    my $orig = shift;
    my $self = shift;

    $self->_inotify->blocking(0);

    return $self->$orig(@_);
};

sub _interesting_events {
    my $self = shift;

    # This may be a blocking read, in which case it will not return until
    # something happens. For Catalyst, the restarter will end up calling
    # ->wait_for_events again after handling the changes.
    my @events = $self->_inotify->read;

    my ( $old_map, $new_map );
    if (   $self->modify_includes_file_attributes
        || $self->modify_includes_content ) {
        $old_map = $self->_map;
        $new_map = $self->_current_map;
    }

    my $filter = $self->filter;

    my @interesting;
    for my $event (@events) {

        # An excluded path will show up here if ...
        #
        # Something created a new directory and that directory needs to be
        # excluded or when the exclusion excludes a file, not a dir.
        next if $self->_path_is_excluded( $event->fullname );

        ## no critic (ControlStructures::ProhibitCascadingIfElse)
        if ( $event->IN_CREATE && $event->IN_ISDIR ) {
            $self->_watch_directory( $event->fullname );
            push @interesting, $event;
            push @interesting,
                $self->_fake_events_for_new_dir( $event->fullname );
        }
        elsif ( $event->IN_DELETE_SELF ) {
            $self->_remove_directory( $event->fullname );
        }
        elsif ( $event->IN_ATTRIB ) {
            next
                unless $self->_path_matches(
                $self->modify_includes_file_attributes,
                $event->fullname
                );
            push @interesting, $event;
        }

        # We just want to check the _file_ name
        elsif ( $event->name =~ /$filter/ ) {
            push @interesting, $event;
        }
    }

    $self->_set_map($new_map)
        if $self->_has_map;

    return map {
              $_->can('path')
            ? $_
            : $self->_convert_event( $_, $old_map, $new_map )
    } @interesting;
}

sub _build_mask {
    my $self = shift;

    my $mask
        = IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF
        | IN_MOVED_TO;
    $mask |= IN_DONT_FOLLOW unless $self->follow_symlinks;
    $mask |= IN_ATTRIB if $self->modify_includes_file_attributes;

    return $mask;
}

sub _watch_directory {
    my $self = shift;
    my $dir  = shift;

    # A directory could be created & then deleted before we get a
    # chance to act on it.
    return unless -d $dir;

    find(
        {
            wanted => sub {
                my $path = $File::Find::name;

                if ( $self->_path_is_excluded($path) ) {
                    $File::Find::prune = 1;
                    return;
                }
                $self->_add_watch_if_dir($path);
            },
            follow_fast => ( $self->follow_symlinks ? 1 : 0 ),
            no_chdir    => 1,
            follow_skip => 2,
        },
        $dir
    );
}

sub _add_watch_if_dir {
    my $self = shift;
    my $path = shift;

    return if -l $path && !$self->follow_symlinks;

    return unless -d $path;
    return if $self->_path_is_excluded($path);

    $self->_inotify->watch( $path, $self->_mask );
}

sub _fake_events_for_new_dir {
    my $self = shift;
    my $dir  = shift;

    return unless -d $dir;

    my @events;
    File::Find::find(
        {
            wanted => sub {
                my $path = $File::Find::name;

                return if $path eq $dir;
                if ( $self->_path_is_excluded($path) ) {
                    $File::Find::prune = 1;
                    return;
                }

                push @events,
                    $self->event_class->new(
                    path => $path,
                    type => 'create',
                    );
            },
            follow_fast => ( $self->follow_symlinks ? 1 : 0 ),
            no_chdir    => 1
        },
        $dir
    );

    return @events;
}

sub _convert_event {
    my $self    = shift;
    my $event   = shift;
    my $old_map = shift;
    my $new_map = shift;

    my $path = $event->fullname;
    my $type
        = $event->IN_CREATE || $event->IN_MOVED_TO ? 'create'
        : $event->IN_MODIFY || $event->IN_ATTRIB   ? 'modify'
        : $event->IN_DELETE ? 'delete'
        :                     'unknown';

    my @extra;
    if (
        $type eq 'modify'
        && (   $self->modify_includes_file_attributes
            || $self->modify_includes_content )
    ) {

        @extra = (
            $self->_modify_event_maybe_file_attribute_changes(
                $path, $old_map, $new_map
            ),
            $self->_modify_event_maybe_content_changes(
                $path, $old_map, $new_map
            ),
        );
    }

    return $self->event_class->new(
        path => $path,
        type => $type,
        @extra,
    );
}

__PACKAGE__->meta->make_immutable;

1;

# ABSTRACT: Inotify-based watcher subclass

__END__

=pod

=encoding UTF-8

=head1 NAME

File::ChangeNotify::Watcher::Inotify - Inotify-based watcher subclass

=head1 VERSION

version 0.31

=head1 DESCRIPTION

This class implements watching by using the L<Linux::Inotify2>
module. This only works on Linux 2.6.13 or newer.

This watcher is much more efficient and accurate than the
C<File::ChangeNotify::Watcher::Default> class.

=head1 SUPPORT

Bugs may be submitted at L<http://rt.cpan.org/Public/Dist/Display.html?Name=File-ChangeNotify> or via email to L<bug-file-changenotify@rt.cpan.org|mailto:bug-file-changenotify@rt.cpan.org>.

I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.

=head1 SOURCE

The source code repository for File-ChangeNotify can be found at L<https://github.com/houseabsolute/File-ChangeNotify>.

=head1 AUTHOR

Dave Rolsky <autarch@urth.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2009 - 2019 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

The full text of the license can be found in the
F<LICENSE> file included with this distribution.

=cut