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_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 }
21 our @EXPORT_OK = qw(fill_dirlist on_dir_change);
23 my $poll_intvl = 2; # same as Filesys::Notify::Simple
25 sub new { bless { watch => {}, dirlist => {} }, __PACKAGE__ }
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/;
35 # behaves like Linux::Inotify2->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);
45 bless [ $self->{watch}, $k ], 'PublicInbox::FakeInotify::Watch';
48 # also used by KQNotify since it kevent requires readdir on st_nlink
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";
58 if (@st && $st[10] > $old_ctime) {
60 bless(\$full, 'PublicInbox::FakeInotify::Event')
63 # ignore ENOENT due to race
64 warn "unhandled stat($full) error: $!\n" if !$!{ENOENT};
66 $newlist->{$base} = undef;
70 delete @$oldlist{keys %$newlist};
71 $dirlist->{$path} = $newlist;
73 bless \"$path/$_", 'PublicInbox::FakeInotify::GoneEvent'
77 # behaves like non-blocking Linux::Inotify2->read
80 my $watch = $self->{watch} or return ();
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');
90 delete $self->{dirlist}->{$path};
93 my $old_ctime = $watch->{$x};
94 $watch->{$x} = $now[10];
95 next if $old_ctime == $now[10];
96 if ($mask & IN_MODIFY) {
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,
103 } elsif ($!{ENOENT}) {
104 push @watch_gone, $x;
105 delete $self->{dirlist}->{$path};
107 warn "W: opendir $path: $!\n";
111 delete @$watch{@watch_gone};
117 $obj->event_step; # PublicInbox::InboxIdle::event_step
118 add_timer($poll_intvl, \&poll_once, $obj);
121 package PublicInbox::FakeInotify::Watch;
126 delete $self->[0]->{$self->[1]};
131 (split(/\0/, $self->[1], 2))[0];
134 package PublicInbox::FakeInotify::Event;
137 sub fullname { ${$_[0]} }
140 sub IN_MOVED_FROM { 0 }
141 sub IN_DELETE_SELF { 0 }
143 package PublicInbox::FakeInotify::GoneEvent;
145 our @ISA = qw(PublicInbox::FakeInotify::Event);
148 sub IN_MOVED_FROM { 0 }
150 package PublicInbox::FakeInotify::SelfGoneEvent;
152 our @ISA = qw(PublicInbox::FakeInotify::GoneEvent);
154 sub IN_DELETE_SELF { 1 }