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>
4 # for systems lacking Linux::Inotify2 or IO::KQueue, just emulates
5 # enough of Linux::Inotify2
6 package PublicInbox::FakeInotify;
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 () { 0x200 }
17 sub IN_DELETE_SELF () { 0x400 }
18 sub IN_MOVE_SELF () { 0x800 }
20 our @EXPORT_OK = qw(fill_dirlist on_dir_change);
22 my $poll_intvl = 2; # same as Filesys::Notify::Simple
24 sub new { bless { watch => {}, dirlist => {} }, __PACKAGE__ }
26 sub fill_dirlist ($$$) {
27 my ($self, $path, $dh) = @_;
28 my $dirlist = $self->{dirlist}->{$path} = {};
29 while (defined(my $n = readdir($dh))) {
30 $dirlist->{$n} = undef if $n !~ /\A\.\.?\z/;
34 # behaves like Linux::Inotify2->watch
36 my ($self, $path, $mask) = @_;
37 my @st = stat($path) or return;
38 my $k = "$path\0$mask";
39 $self->{watch}->{$k} = $st[10]; # 10 - ctime
40 if ($mask & IN_DELETE) {
41 opendir(my $dh, $path) or return;
42 fill_dirlist($self, $path, $dh);
44 bless [ $self->{watch}, $k ], 'PublicInbox::FakeInotify::Watch';
47 # also used by KQNotify since it kevent requires readdir on st_nlink
49 sub on_dir_change ($$$$$) {
50 my ($events, $dh, $path, $old_ctime, $dirlist) = @_;
51 my $oldlist = $dirlist->{$path};
52 my $newlist = $oldlist ? {} : undef;
53 while (defined(my $base = readdir($dh))) {
54 next if $base =~ /\A\.\.?\z/;
55 my $full = "$path/$base";
57 if (@st && $st[10] > $old_ctime) {
59 bless(\$full, 'PublicInbox::FakeInotify::Event')
62 # ignore ENOENT due to race
63 warn "unhandled stat($full) error: $!\n" if !$!{ENOENT};
65 $newlist->{$base} = undef;
69 delete @$oldlist{keys %$newlist};
70 $dirlist->{$path} = $newlist;
72 bless \"$path/$_", 'PublicInbox::FakeInotify::GoneEvent'
76 # behaves like non-blocking Linux::Inotify2->read
79 my $watch = $self->{watch} or return ();
82 for my $x (keys %$watch) {
83 my ($path, $mask) = split(/\0/, $x, 2);
84 my @now = stat($path);
85 if (!@now && $!{ENOENT} && ($mask & IN_DELETE_SELF)) {
86 push @$events, bless(\$path,
87 'PublicInbox::FakeInotify::SelfGoneEvent');
89 delete $self->{dirlist}->{$path};
92 my $old_ctime = $watch->{$x};
93 $watch->{$x} = $now[10];
94 next if $old_ctime == $now[10];
95 if ($mask & IN_MODIFY) {
97 bless(\$path, 'PublicInbox::FakeInotify::Event')
98 } elsif ($mask & (MOVED_TO_OR_CREATE | IN_DELETE)) {
99 if (opendir(my $dh, $path)) {
100 on_dir_change($events, $dh, $path, $old_ctime,
102 } elsif ($!{ENOENT}) {
103 push @watch_gone, $x;
104 delete $self->{dirlist}->{$path};
106 warn "W: opendir $path: $!\n";
110 delete @$watch{@watch_gone};
116 $obj->event_step; # PublicInbox::InboxIdle::event_step
117 add_timer($poll_intvl, \&poll_once, $obj);
120 package PublicInbox::FakeInotify::Watch;
125 delete $self->[0]->{$self->[1]};
130 (split(/\0/, $self->[1], 2))[0];
133 package PublicInbox::FakeInotify::Event;
136 sub fullname { ${$_[0]} }
139 sub IN_DELETE_SELF { 0 }
141 package PublicInbox::FakeInotify::GoneEvent;
143 our @ISA = qw(PublicInbox::FakeInotify::Event);
147 package PublicInbox::FakeInotify::SelfGoneEvent;
149 our @ISA = qw(PublicInbox::FakeInotify::GoneEvent);
151 sub IN_DELETE_SELF { 1 }