]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/FakeInotify.pm
9275861368a0a00df5b95ff8acb4776413568d34
[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 sub IN_MODIFY () { 0x02 } # 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 new { bless { watch => {} }, __PACKAGE__ }
18
19 # behaves like Linux::Inotify2->watch
20 sub watch {
21         my ($self, $path, $mask) = @_;
22         my @st = stat($path) or return;
23         my $k = "$path\0$mask";
24         $self->{watch}->{$k} = $st[10]; # 10 - ctime
25         bless [ $self->{watch}, $k ], 'PublicInbox::FakeInotify::Watch';
26 }
27
28 sub on_new_files ($$$$) {
29         my ($events, $dh, $path, $old_ctime) = @_;
30         while (defined(my $base = readdir($dh))) {
31                 next if $base =~ /\A\.\.?\z/;
32                 my $full = "$path/$base";
33                 my @st = stat($full);
34                 if (@st && $st[10] > $old_ctime) {
35                         push @$events,
36                                 bless(\$full, 'PublicInbox::FakeInotify::Event')
37                 }
38         }
39 }
40
41 # behaves like non-blocking Linux::Inotify2->read
42 sub read {
43         my ($self) = @_;
44         my $watch = $self->{watch} or return ();
45         my $events = [];
46         for my $x (keys %$watch) {
47                 my ($path, $mask) = split(/\0/, $x, 2);
48                 my @now = stat($path) or next;
49                 my $old_ctime = $watch->{$x};
50                 $watch->{$x} = $now[10];
51                 next if $old_ctime == $now[10];
52                 if ($mask & IN_MODIFY) {
53                         push @$events,
54                                 bless(\$path, 'PublicInbox::FakeInotify::Event')
55                 } elsif ($mask & MOVED_TO_OR_CREATE) {
56                         opendir(my $dh, $path) or do {
57                                 warn "W: opendir $path: $!\n";
58                                 next;
59                         };
60                         on_new_files($events, $dh, $path, $old_ctime);
61                 }
62         }
63         @$events;
64 }
65
66 sub poll_once {
67         my ($obj) = @_;
68         $obj->event_step; # PublicInbox::InboxIdle::event_step
69         PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $obj);
70 }
71
72 package PublicInbox::FakeInotify::Watch;
73 use strict;
74
75 sub cancel {
76         my ($self) = @_;
77         delete $self->[0]->{$self->[1]};
78 }
79
80 sub name {
81         my ($self) = @_;
82         (split(/\0/, $self->[1], 2))[0];
83 }
84
85 package PublicInbox::FakeInotify::Event;
86 use strict;
87
88 sub fullname { ${$_[0]} }
89 1;