]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/KQNotify.pm
fc321a1674992558904e85c33251e6c3e15721ae
[public-inbox.git] / lib / PublicInbox / KQNotify.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 # implements the small subset of Linux::Inotify2 functionality we use
5 # using IO::KQueue on *BSD systems.
6 package PublicInbox::KQNotify;
7 use strict;
8 use v5.10.1;
9 use IO::KQueue;
10 use PublicInbox::DSKQXS; # wraps IO::KQueue for fork-safe DESTROY
11 use PublicInbox::FakeInotify qw(fill_dirlist on_dir_change);
12 use Time::HiRes qw(stat);
13
14 # NOTE_EXTEND detects rename(2), NOTE_WRITE detects link(2)
15 sub MOVED_TO_OR_CREATE () { NOTE_EXTEND|NOTE_WRITE }
16
17 sub new {
18         my ($class) = @_;
19         bless { dskq => PublicInbox::DSKQXS->new, watch => {} }, $class;
20 }
21
22 sub watch {
23         my ($self, $path, $mask) = @_;
24         my ($fh, $watch);
25         if (-d $path) {
26                 opendir($fh, $path) or return;
27                 my @st = stat($fh);
28                 $watch = bless [ $fh, $path, $st[10] ],
29                         'PublicInbox::KQNotify::Watchdir';
30         } else {
31                 open($fh, '<', $path) or return;
32                 $watch = bless [ $fh, $path ],
33                         'PublicInbox::KQNotify::Watch';
34         }
35         my $ident = fileno($fh);
36         $self->{dskq}->{kq}->EV_SET($ident, # ident
37                 EVFILT_VNODE, # filter
38                 EV_ADD | EV_CLEAR, # flags
39                 $mask, # fflags
40                 0, 0); # data, udata
41         if ($mask & (MOVED_TO_OR_CREATE | NOTE_DELETE)) {
42                 $self->{watch}->{$ident} = $watch;
43                 fill_dirlist($self, $path, $fh) if $mask & NOTE_DELETE;
44         } else {
45                 die "TODO Not implemented: $mask";
46         }
47         $watch;
48 }
49
50 # emulate Linux::Inotify::fileno
51 sub fileno { ${$_[0]->{dskq}->{kq}} }
52
53 # noop for Linux::Inotify2 compatibility.  Unlike inotify,
54 # kqueue doesn't seem to overflow since it's limited by the number of
55 # open FDs the process has
56 sub on_overflow {}
57
58 # noop for Linux::Inotify2 compatibility, we use `0' timeout for ->kevent
59 sub blocking {}
60
61 # behave like Linux::Inotify2->read
62 sub read {
63         my ($self) = @_;
64         my @kevents = $self->{dskq}->{kq}->kevent(0);
65         my $events = [];
66         for my $kev (@kevents) {
67                 my $ident = $kev->[KQ_IDENT];
68                 my $mask = $kev->[KQ_FFLAGS];
69                 my ($dh, $path, $old_ctime) = @{$self->{watch}->{$ident}};
70                 if (!defined($old_ctime)) {
71                         push @$events,
72                                 bless(\$path, 'PublicInbox::FakeInotify::Event')
73                 } elsif ($mask & (MOVED_TO_OR_CREATE | NOTE_DELETE)) {
74                         my @new_st = stat($path) or next;
75                         $self->{watch}->{$ident}->[3] = $new_st[10]; # ctime
76                         rewinddir($dh);
77                         on_dir_change($events, $dh, $path, $old_ctime,
78                                         $self->{dirlist});
79                 }
80         }
81         @$events;
82 }
83
84 package PublicInbox::KQNotify::Watch;
85 use strict;
86
87 sub name { $_[0]->[1] }
88
89 sub cancel { close $_[0]->[0] or die "close: $!" }
90
91 package PublicInbox::KQNotify::Watchdir;
92 use strict;
93
94 sub name { $_[0]->[1] }
95
96 sub cancel { closedir $_[0]->[0] or die "closedir: $!" }
97
98 1;