]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/DirIdle.pm
dir_idle: treat IN_MOVED_FROM as a gone event
[public-inbox.git] / lib / PublicInbox / DirIdle.pm
1 # Copyright (C) 2020-2021 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 parent 'PublicInbox::DS';
8 use PublicInbox::Syscall qw(EPOLLIN);
9 use PublicInbox::In2Tie;
10
11 my ($MAIL_IN, $MAIL_GONE, $ino_cls);
12 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
13         $MAIL_IN = Linux::Inotify2::IN_MOVED_TO() |
14                 Linux::Inotify2::IN_CREATE();
15         $MAIL_GONE = Linux::Inotify2::IN_DELETE() |
16                         Linux::Inotify2::IN_DELETE_SELF() |
17                         Linux::Inotify2::IN_MOVE_SELF() |
18                         Linux::Inotify2::IN_MOVED_FROM();
19         $ino_cls = 'Linux::Inotify2';
20 # Perl 5.22+ is needed for fileno(DIRHANDLE) support:
21 } elsif ($^V ge v5.22 && eval { require PublicInbox::KQNotify }) {
22         $MAIL_IN = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
23         $MAIL_GONE = PublicInbox::KQNotify::NOTE_DELETE() |
24                 PublicInbox::KQNotify::NOTE_REVOKE() |
25                 PublicInbox::KQNotify::NOTE_RENAME();
26         $ino_cls = 'PublicInbox::KQNotify';
27 } else {
28         require PublicInbox::FakeInotify;
29         $MAIL_IN = PublicInbox::FakeInotify::MOVED_TO_OR_CREATE();
30         $MAIL_GONE = PublicInbox::FakeInotify::IN_DELETE() |
31                         PublicInbox::FakeInotify::IN_DELETE_SELF() |
32                         PublicInbox::FakeInotify::IN_MOVE_SELF();
33 }
34
35 sub new {
36         my ($class, $cb) = @_;
37         my $self = bless { cb => $cb }, $class;
38         my $inot;
39         if ($ino_cls) {
40                 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
41                 my $io = PublicInbox::In2Tie::io($inot);
42                 $self->SUPER::new($io, EPOLLIN);
43         } else {
44                 require PublicInbox::FakeInotify;
45                 $inot = PublicInbox::FakeInotify->new; # starts timer
46         }
47         $self->{inot} = $inot;
48         $self;
49 }
50
51 sub add_watches {
52         my ($self, $dirs, $gone) = @_;
53         my $fl = $MAIL_IN | ($gone ? $MAIL_GONE : 0);
54         my @ret;
55         for my $d (@$dirs) {
56                 my $w = $self->{inot}->watch($d, $fl) or next;
57                 push @ret, $w;
58         }
59         PublicInbox::FakeInotify::poll_once($self) if !$ino_cls;
60         @ret
61 }
62
63 sub rm_watches {
64         my ($self, $dir) = @_;
65         my $inot = $self->{inot};
66         if (my $cb = $inot->can('rm_watches')) { # TODO for fake watchers
67                 $cb->($inot, $dir);
68         }
69 }
70
71 sub event_step {
72         my ($self) = @_;
73         my $cb = $self->{cb};
74         local $PublicInbox::DS::in_loop = 0; # waitpid() synchronously
75         eval {
76                 my @events = $self->{inot}->read; # Linux::Inotify2->read
77                 $cb->($_) for @events;
78         };
79         warn "$self->{inot}->read err: $@\n" if $@;
80 }
81
82 sub force_close {
83         my ($self) = @_;
84         my $inot = delete $self->{inot} // return;
85         if ($inot->can('fh')) { # Linux::Inotify2 2.3+
86                 close($inot->fh) or warn "CLOSE ERROR: $!";
87         } elsif ($inot->isa('Linux::Inotify2')) {
88                 require PublicInbox::LI2Wrap;
89                 PublicInbox::LI2Wrap::wrapclose($inot);
90         }
91 }
92
93 1;