]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/DirIdle.pm
watch: remove Filesys::Notify::Simple dependency
[public-inbox.git] / lib / PublicInbox / DirIdle.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Used by public-inbox-watch for Maildir (and possibly MH in the future)
5 package PublicInbox::DirIdle;
6 use strict;
7 use base 'PublicInbox::DS';
8 use fields qw(inot);
9 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
10 use PublicInbox::In2Tie;
11
12 my ($MAIL_IN, $ino_cls);
13 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
14         $MAIL_IN = Linux::Inotify2::IN_MOVED_TO() |
15                 Linux::Inotify2::IN_CREATE();
16         $ino_cls = 'Linux::Inotify2';
17 } elsif (eval { require PublicInbox::KQNotify }) {
18         $MAIL_IN = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
19         $ino_cls = 'PublicInbox::KQNotify';
20 } else {
21         require PublicInbox::FakeInotify;
22         $MAIL_IN = PublicInbox::FakeInotify::MOVED_TO_OR_CREATE();
23 }
24
25 sub new {
26         my ($class, $dirs, $cb) = @_;
27         my $self = fields::new($class);
28         my $inot;
29         if ($ino_cls) {
30                 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
31                 my $io = PublicInbox::In2Tie::io($inot);
32                 $self->SUPER::new($io, EPOLLIN | EPOLLET);
33         } else {
34                 require PublicInbox::FakeInotify;
35                 $inot = PublicInbox::FakeInotify->new; # starts timer
36         }
37
38         # Linux::Inotify2->watch or similar
39         $inot->watch($_, $MAIL_IN, $cb) for @$dirs;
40         $self->{inot} = $inot;
41         $self;
42 }
43
44 sub event_step {
45         my ($self) = @_;
46         eval { $self->{inot}->poll }; # Linux::Inotify2::poll
47         warn "$self->{inot}->poll err: $@\n" if $@;
48 }
49
50 1;