]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/FakeInotify.pm
dir_idle: detect files which are gone
[public-inbox.git] / lib / PublicInbox / FakeInotify.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 # for systems lacking Linux::Inotify2 or IO::KQueue, just emulates
5 # enough of Linux::Inotify2
6 package PublicInbox::FakeInotify;
7 use strict;
8 use v5.10.1;
9 use parent qw(Exporter);
10 use Time::HiRes qw(stat);
11 use PublicInbox::DS qw(add_timer);
12 sub IN_MODIFY () { 0x02 } # match Linux inotify
13 # my $IN_MOVED_TO = 0x80;
14 # my $IN_CREATE = 0x100;
15 sub MOVED_TO_OR_CREATE () { 0x80 | 0x100 }
16 sub IN_DELETE () { 0x00000200 }
17
18 our @EXPORT_OK = qw(fill_dirlist on_dir_change);
19
20 my $poll_intvl = 2; # same as Filesys::Notify::Simple
21
22 sub new { bless { watch => {}, dirlist => {} }, __PACKAGE__ }
23
24 sub fill_dirlist ($$$) {
25         my ($self, $path, $dh) = @_;
26         my $dirlist = $self->{dirlist}->{$path} = {};
27         while (defined(my $n = readdir($dh))) {
28                 $dirlist->{$n} = undef if $n !~ /\A\.\.?\z/;
29         }
30 }
31
32 # behaves like Linux::Inotify2->watch
33 sub watch {
34         my ($self, $path, $mask) = @_;
35         my @st = stat($path) or return;
36         my $k = "$path\0$mask";
37         $self->{watch}->{$k} = $st[10]; # 10 - ctime
38         if ($mask & IN_DELETE) {
39                 opendir(my $dh, $path) or return;
40                 fill_dirlist($self, $path, $dh);
41         }
42         bless [ $self->{watch}, $k ], 'PublicInbox::FakeInotify::Watch';
43 }
44
45 # also used by KQNotify since it kevent requires readdir on st_nlink
46 # count changes.
47 sub on_dir_change ($$$$;$) {
48         my ($events, $dh, $path, $old_ctime, $dirlist) = @_;
49         my $oldlist = $dirlist->{$path};
50         my $newlist = $oldlist ? {} : undef;
51         while (defined(my $base = readdir($dh))) {
52                 next if $base =~ /\A\.\.?\z/;
53                 my $full = "$path/$base";
54                 my @st = stat($full);
55                 if (@st && $st[10] > $old_ctime) {
56                         push @$events,
57                                 bless(\$full, 'PublicInbox::FakeInotify::Event')
58                 }
59                 if (!@st) {
60                         # ignore ENOENT due to race
61                         warn "unhandled stat($full) error: $!\n" if !$!{ENOENT};
62                 } elsif ($newlist) {
63                         $newlist->{$base} = undef;
64                 }
65         }
66         return if !$newlist;
67         delete @$oldlist{keys %$newlist};
68         $dirlist->{$path} = $newlist;
69         push(@$events, map {
70                 bless \"$path/$_", 'PublicInbox::FakeInotify::GoneEvent'
71         } keys %$oldlist);
72 }
73
74 # behaves like non-blocking Linux::Inotify2->read
75 sub read {
76         my ($self) = @_;
77         my $watch = $self->{watch} or return ();
78         my $events = [];
79         my @watch_gone;
80         for my $x (keys %$watch) {
81                 my ($path, $mask) = split(/\0/, $x, 2);
82                 my @now = stat($path) or next;
83                 my $old_ctime = $watch->{$x};
84                 $watch->{$x} = $now[10];
85                 next if $old_ctime == $now[10];
86                 if ($mask & IN_MODIFY) {
87                         push @$events,
88                                 bless(\$path, 'PublicInbox::FakeInotify::Event')
89                 } elsif ($mask & (MOVED_TO_OR_CREATE | IN_DELETE)) {
90                         if (opendir(my $dh, $path)) {
91                                 on_dir_change($events, $dh, $path, $old_ctime,
92                                                 $self->{dirlist});
93                         } elsif ($!{ENOENT}) {
94                                 push @watch_gone, $x;
95                         } else {
96                                 warn "W: opendir $path: $!\n";
97                         }
98                 }
99         }
100         delete @$watch{@watch_gone};
101         @$events;
102 }
103
104 sub poll_once {
105         my ($obj) = @_;
106         $obj->event_step; # PublicInbox::InboxIdle::event_step
107         add_timer($poll_intvl, \&poll_once, $obj);
108 }
109
110 package PublicInbox::FakeInotify::Watch;
111 use strict;
112
113 sub cancel {
114         my ($self) = @_;
115         delete $self->[0]->{$self->[1]};
116 }
117
118 sub name {
119         my ($self) = @_;
120         (split(/\0/, $self->[1], 2))[0];
121 }
122
123 package PublicInbox::FakeInotify::Event;
124 use strict;
125
126 sub fullname { ${$_[0]} }
127
128 sub IN_DELETE { 0 }
129
130 package PublicInbox::FakeInotify::GoneEvent;
131 use strict;
132 our @ISA = qw(PublicInbox::FakeInotify::Event);
133
134 sub IN_DELETE { 1 }
135
136 1;