]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/FakeInotify.pm
kqnotify|fake_inotify: detect Maildir write ops
[public-inbox.git] / lib / PublicInbox / FakeInotify.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 # for systems lacking Linux::Inotify2 or IO::KQueue, just emulates
5 # enough of Linux::Inotify2
6 package PublicInbox::FakeInotify;
7 use strict;
8 use Time::HiRes qw(stat);
9 use PublicInbox::DS;
10 my $IN_CLOSE = 0x08 | 0x10; # match Linux inotify
11 # my $IN_MOVED_TO = 0x80;
12 # my $IN_CREATE = 0x100;
13 sub MOVED_TO_OR_CREATE () { 0x80 | 0x100 }
14
15 my $poll_intvl = 2; # same as Filesys::Notify::Simple
16
17 sub poll_once {
18         my ($self) = @_;
19         sub {
20                 eval { $self->poll };
21                 warn "E: FakeInotify->poll: $@\n" if $@;
22                 PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
23         };
24 }
25
26 sub new {
27         my $self = bless { watch => {} }, __PACKAGE__;
28         PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
29         $self;
30 }
31
32 # behaves like Linux::Inotify2->watch
33 sub watch {
34         my ($self, $path, $mask, $cb) = @_;
35         my @st = stat($path) or return;
36         my $k = "$path\0$mask";
37         $self->{watch}->{$k} = [ $st[10], $cb ]; # 10 - ctime
38         bless [ $self->{watch}, $k ], 'PublicInbox::FakeInotify::Watch';
39 }
40
41 sub on_new_files ($$$$) {
42         my ($dh, $cb, $path, $old_ctime) = @_;
43         while (defined(my $base = readdir($dh))) {
44                 next if $base =~ /\A\.\.?\z/;
45                 my $full = "$path/$base";
46                 my @st = stat($full);
47                 if (@st && $st[10] > $old_ctime) {
48                         bless \$full, 'PublicInbox::FakeInotify::Event';
49                         eval { $cb->(\$full) };
50                 }
51         }
52 }
53
54 # behaves like non-blocking Linux::Inotify2->poll
55 sub poll {
56         my ($self) = @_;
57         my $watch = $self->{watch} or return;
58         for my $x (keys %$watch) {
59                 my ($path, $mask) = split(/\0/, $x, 2);
60                 my @now = stat($path) or next;
61                 my $prv = $watch->{$x};
62                 my $cb = $prv->[-1];
63                 my $old_ctime = $prv->[0];
64                 if ($old_ctime != $now[10]) {
65                         if (($mask & $IN_CLOSE) == $IN_CLOSE) {
66                                 eval { $cb->() };
67                         } elsif ($mask & MOVED_TO_OR_CREATE) {
68                                 opendir(my $dh, $path) or do {
69                                         warn "W: opendir $path: $!\n";
70                                         next;
71                                 };
72                                 on_new_files($dh, $cb, $path, $old_ctime);
73                         }
74                 }
75                 @$prv = ($now[10], $cb);
76         }
77 }
78
79 package PublicInbox::FakeInotify::Watch;
80 use strict;
81
82 sub cancel {
83         my ($self) = @_;
84         delete $self->[0]->{$self->[1]};
85 }
86
87 package PublicInbox::FakeInotify::Event;
88 use strict;
89
90 sub fullname { ${$_[0]} }
91 1;