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