]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/FakeInotify.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / FakeInotify.pm
1 # Copyright (C) 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 v5.12;
8 use parent qw(Exporter);
9 use Time::HiRes qw(stat);
10 use PublicInbox::DS qw(add_timer);
11 sub IN_MODIFY () { 0x02 } # match Linux inotify
12 # my $IN_MOVED_FROM      0x00000040     /* File was moved from X.  */
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 }
19
20 our @EXPORT_OK = qw(fill_dirlist on_dir_change);
21
22 my $poll_intvl = 2; # same as Filesys::Notify::Simple
23
24 sub new { bless { watch => {}, dirlist => {} }, __PACKAGE__ }
25
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/;
31         }
32 }
33
34 # behaves like Linux::Inotify2->watch
35 sub 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);
43         }
44         bless [ $self->{watch}, $k ], 'PublicInbox::FakeInotify::Watch';
45 }
46
47 # also used by KQNotify since it kevent requires readdir on st_nlink
48 # count changes.
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";
56                 my @st = stat($full);
57                 if (@st && $st[10] > $old_ctime) {
58                         push @$events,
59                                 bless(\$full, 'PublicInbox::FakeInotify::Event')
60                 }
61                 if (!@st) {
62                         # ignore ENOENT due to race
63                         warn "unhandled stat($full) error: $!\n" if !$!{ENOENT};
64                 } elsif ($newlist) {
65                         $newlist->{$base} = undef;
66                 }
67         }
68         return if !$newlist;
69         delete @$oldlist{keys %$newlist};
70         $dirlist->{$path} = $newlist;
71         push(@$events, map {
72                 bless \"$path/$_", 'PublicInbox::FakeInotify::GoneEvent'
73         } keys %$oldlist);
74 }
75
76 # behaves like non-blocking Linux::Inotify2->read
77 sub read {
78         my ($self) = @_;
79         my $watch = $self->{watch} or return ();
80         my $events = [];
81         my @watch_gone;
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');
88                         push @watch_gone, $x;
89                         delete $self->{dirlist}->{$path};
90                 }
91                 next if !@now;
92                 my $old_ctime = $watch->{$x};
93                 $watch->{$x} = $now[10];
94                 next if $old_ctime == $now[10];
95                 if ($mask & IN_MODIFY) {
96                         push @$events,
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,
101                                                 $self->{dirlist});
102                         } elsif ($!{ENOENT}) {
103                                 push @watch_gone, $x;
104                                 delete $self->{dirlist}->{$path};
105                         } else {
106                                 warn "W: opendir $path: $!\n";
107                         }
108                 }
109         }
110         delete @$watch{@watch_gone};
111         @$events;
112 }
113
114 sub poll_once {
115         my ($obj) = @_;
116         $obj->event_step; # PublicInbox::InboxIdle::event_step
117         add_timer($poll_intvl, \&poll_once, $obj);
118 }
119
120 package PublicInbox::FakeInotify::Watch;
121 use v5.12;
122
123 sub cancel {
124         my ($self) = @_;
125         delete $self->[0]->{$self->[1]};
126 }
127
128 sub name {
129         my ($self) = @_;
130         (split(/\0/, $self->[1], 2))[0];
131 }
132
133 package PublicInbox::FakeInotify::Event;
134 use v5.12;
135
136 sub fullname { ${$_[0]} }
137
138 sub IN_DELETE { 0 }
139 sub IN_MOVED_FROM { 0 }
140 sub IN_DELETE_SELF { 0 }
141
142 package PublicInbox::FakeInotify::GoneEvent;
143 use v5.12;
144 our @ISA = qw(PublicInbox::FakeInotify::Event);
145
146 sub IN_DELETE { 1 }
147 sub IN_MOVED_FROM { 0 }
148
149 package PublicInbox::FakeInotify::SelfGoneEvent;
150 use v5.12;
151 our @ISA = qw(PublicInbox::FakeInotify::GoneEvent);
152
153 sub IN_DELETE_SELF { 1 }
154
155 1;