1 # Copyright (C) 2020 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;
8 use Time::HiRes qw(stat);
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 }
15 my $poll_intvl = 2; # same as Filesys::Notify::Simple
17 sub new { bless { watch => {} }, __PACKAGE__ }
19 # behaves like Linux::Inotify2->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';
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";
34 if (@st && $st[10] > $old_ctime) {
36 bless(\$full, 'PublicInbox::FakeInotify::Event')
41 # behaves like non-blocking Linux::Inotify2->read
44 my $watch = $self->{watch} or return ();
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) {
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";
60 on_new_files($events, $dh, $path, $old_ctime);
68 $obj->event_step; # PublicInbox::InboxIdle::event_step
69 PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $obj);
72 package PublicInbox::FakeInotify::Watch;
77 delete $self->[0]->{$self->[1]};
82 (split(/\0/, $self->[1], 2))[0];
85 package PublicInbox::FakeInotify::Event;
88 sub fullname { ${$_[0]} }