File: Default.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 (170 lines) | stat: -rw-r--r-- 3,946 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
package File::ChangeNotify::Watcher::Default;

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

our $VERSION = '0.31';

use Time::HiRes qw( sleep );

use Moo;

with 'File::ChangeNotify::Watcher';

sub sees_all_events {0}

## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
sub _always_requires_mtime {1}
## use critic

sub BUILD {
    my $self = shift;

    $self->_set_map( $self->_current_map );

    return;
}

sub wait_for_events {
    my $self = shift;

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

        sleep $self->sleep_interval;
    }
}

sub _interesting_events {
    my $self = shift;

    my @interesting;

    my $old_map = $self->_map;
    my $new_map = $self->_current_map;

    for my $path ( sort keys %{$old_map} ) {
        if ( !exists $new_map->{$path} ) {
            if ( $old_map->{$path}{is_dir} ) {
                $self->_remove_directory($path);
            }

            push @interesting, $self->event_class->new(
                path => $path,
                type => 'delete',
            );
        }
        else {
            # If we're tracking stat info changes then we get the old & new
            # stat info back in @extra. No need to stat the path _again_.
            my ( $modified, @extra )
                = $self->_path_was_modified( $path, $old_map, $new_map );
            if ($modified) {
                push @interesting, $self->event_class->new(
                    path => $path,
                    type => 'modify',
                    @extra,
                    $self->_modify_event_maybe_content_changes(
                        $path, $old_map, $new_map
                    ),
                );
            }
        }
    }

    for my $path ( sort grep { !exists $old_map->{$_} } keys %{$new_map} ) {
        push @interesting, $self->event_class->new(
            path => $path,
            type => 'create',
        );
    }

    $self->_set_map($new_map);

    return @interesting;
}

sub _path_was_modified {
    my $self    = shift;
    my $path    = shift;
    my $old_map = shift;
    my $new_map = shift;

    my $old_entry = $old_map->{$path};
    my $new_entry = $new_map->{$path};

    # If it's a file and the mtime or size changed we know it's been modified
    # in some way.
    return 1
        if !$old_entry->{is_dir}
        && ( $old_entry->{stat}{mtime} != $new_entry->{stat}{mtime}
        || $old_entry->{size} != $new_entry->{size} );

    if (
        my @attrs = $self->_modify_event_maybe_file_attribute_changes(
            $path, $old_map, $new_map
        )
    ) {

        return ( 1, @attrs );
    }

    return 0;
}

__PACKAGE__->meta->make_immutable;

1;

# ABSTRACT: Fallback default watcher subclass

__END__

=pod

=encoding UTF-8

=head1 NAME

File::ChangeNotify::Watcher::Default - Fallback default watcher subclass

=head1 VERSION

version 0.31

=head1 DESCRIPTION

This class implements watching by comparing two snapshots of the filesystem
tree. It if inefficient and dumb, and so it is the subclass of last resort.

Its C<< $watcher->wait_for_events >> method sleeps between
comparisons of the filesystem snapshot it takes.

=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