t/eml_content_type.t
t/epoll.t
t/fail-bin/spamc
+t/fake_inotify.t
t/feed.t
t/filter_base-junk.eml
t/filter_base-xhtml.eml
t/indexlevels-mirror.t
t/init.t
t/iso-2202-jp.eml
+t/kqnotify.t
t/linkify.t
t/main-bin/spamc
t/mda-mime.eml
package PublicInbox::FakeInotify;
use strict;
use Time::HiRes qw(stat);
+use PublicInbox::DS;
my $IN_CLOSE = 0x08 | 0x10; # match Linux inotify
+# my $IN_MOVED_TO = 0x80;
+# my $IN_CREATE = 0x100;
+sub MOVED_TO_OR_CREATE () { 0x80 | 0x100 }
my $poll_intvl = 2; # same as Filesys::Notify::Simple
-my $for_cancel = bless \(my $x), 'PublicInbox::FakeInotify::Watch';
sub poll_once {
my ($self) = @_;
sub watch {
my ($self, $path, $mask, $cb) = @_;
my @st = stat($path) or return;
- $self->{watch}->{"$path\0$mask"} = [ @st, $cb ];
- $for_cancel;
+ my $k = "$path\0$mask";
+ $self->{watch}->{$k} = [ $st[10], $cb ]; # 10 - ctime
+ bless [ $self->{watch}, $k ], 'PublicInbox::FakeInotify::Watch';
+}
+
+sub on_new_files ($$$$) {
+ my ($dh, $cb, $path, $old_ctime) = @_;
+ while (defined(my $base = readdir($dh))) {
+ next if $base =~ /\A\.\.?\z/;
+ my $full = "$path/$base";
+ my @st = stat($full);
+ if (@st && $st[10] > $old_ctime) {
+ bless \$full, 'PublicInbox::FakeInotify::Event';
+ eval { $cb->(\$full) };
+ }
+ }
}
# behaves like non-blocking Linux::Inotify2->poll
my @now = stat($path) or next;
my $prv = $watch->{$x};
my $cb = $prv->[-1];
- # 10: ctime, 7: size
- if ($prv->[10] != $now[10]) {
+ my $old_ctime = $prv->[0];
+ if ($old_ctime != $now[10]) {
if (($mask & $IN_CLOSE) == $IN_CLOSE) {
eval { $cb->() };
+ } elsif ($mask & MOVED_TO_OR_CREATE) {
+ opendir(my $dh, $path) or do {
+ warn "W: opendir $path: $!\n";
+ next;
+ };
+ on_new_files($dh, $cb, $path, $old_ctime);
}
}
- @$prv = (@now, $cb);
+ @$prv = ($now[10], $cb);
}
}
package PublicInbox::FakeInotify::Watch;
-sub cancel {} # noop
+use strict;
+
+sub cancel {
+ my ($self) = @_;
+ delete $self->[0]->{$self->[1]};
+}
+
+package PublicInbox::FakeInotify::Event;
+use strict;
+sub fullname { ${$_[0]} }
1;
use strict;
use IO::KQueue;
use PublicInbox::DSKQXS; # wraps IO::KQueue for fork-safe DESTROY
+use PublicInbox::FakeInotify;
+use Time::HiRes qw(stat);
+
+# NOTE_EXTEND detects rename(2), NOTE_WRITE detects link(2)
+sub MOVED_TO_OR_CREATE () { NOTE_EXTEND|NOTE_WRITE }
sub new {
my ($class) = @_;
sub watch {
my ($self, $path, $mask, $cb) = @_;
- open(my $fh, '<', $path) or return;
+ my ($fh, $cls, @extra);
+ if (-d $path) {
+ opendir($fh, $path) or return;
+ my @st = stat($fh);
+ @extra = ($path, $st[10]); # 10: ctime
+ $cls = 'PublicInbox::KQNotify::Watchdir';
+ } else {
+ open($fh, '<', $path) or return;
+ $cls = 'PublicInbox::KQNotify::Watch';
+ }
my $ident = fileno($fh);
$self->{dskq}->{kq}->EV_SET($ident, # ident
EVFILT_VNODE, # filter
EV_ADD | EV_CLEAR, # flags
$mask, # fflags
0, 0); # data, udata
- if ($mask == NOTE_WRITE) {
- $self->{watch}->{$ident} = [ $fh, $cb ];
+ if ($mask == NOTE_WRITE || $mask == MOVED_TO_OR_CREATE) {
+ $self->{watch}->{$ident} = [ $fh, $cb, @extra ];
} else {
die "TODO Not implemented: $mask";
}
- bless \$fh, 'PublicInbox::KQNotify::Watch';
+ bless \$fh, $cls;
}
# emulate Linux::Inotify::fileno
for my $kev (@kevents) {
my $ident = $kev->[KQ_IDENT];
my $mask = $kev->[KQ_FFLAGS];
- if (($mask & NOTE_WRITE) == NOTE_WRITE) {
- eval { $self->{watch}->{$ident}->[1]->() };
+ my ($dh, $cb, $path, $old_ctime) = @{$self->{watch}->{$ident}};
+ if (!defined($path) && ($mask & NOTE_WRITE) == NOTE_WRITE) {
+ eval { $cb->() };
+ } elsif ($mask & MOVED_TO_OR_CREATE) {
+ my @new_st = stat($path) or next;
+ $self->{watch}->{$ident}->[3] = $new_st[10]; # ctime
+ rewinddir($dh);
+ PublicInbox::FakeInotify::on_new_files($dh, $cb,
+ $path, $old_ctime);
}
}
}
sub cancel { close ${$_[0]} or die "close: $!" }
+package PublicInbox::KQNotify::Watchdir;
+use strict;
+
+sub cancel { closedir ${$_[0]} or die "closedir: $!" }
+
1;
--- /dev/null
+#!perl -w
+# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# Ensure FakeInotify can pick up rename(2) and link(2) operations
+# used by Maildir writing tools
+use strict;
+use Test::More;
+use PublicInbox::TestCommon;
+use_ok 'PublicInbox::FakeInotify';
+my $MIN_FS_TICK = 0.011; # for low-res CONFIG_HZ=100 systems
+my ($tmpdir, $for_destroy) = tmpdir();
+mkdir "$tmpdir/new" or BAIL_OUT "mkdir: $!";
+open my $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
+close $fh or BAIL_OUT "close: $!";
+
+my $fi = PublicInbox::FakeInotify->new;
+my $mask = PublicInbox::FakeInotify::MOVED_TO_OR_CREATE();
+my $hit = [];
+my $cb = sub { push @$hit, map { $_->fullname } @_ };
+my $w = $fi->watch("$tmpdir/new", $mask, $cb);
+
+select undef, undef, undef, $MIN_FS_TICK;
+rename("$tmpdir/tst", "$tmpdir/new/tst") or BAIL_OUT "rename: $!";
+$fi->poll;
+is_deeply($hit, ["$tmpdir/new/tst"], 'rename(2) detected');
+
+@$hit = ();
+select undef, undef, undef, $MIN_FS_TICK;
+open $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
+close $fh or BAIL_OUT "close: $!";
+link("$tmpdir/tst", "$tmpdir/new/link") or BAIL_OUT "link: $!";
+$fi->poll;
+is_deeply($hit, ["$tmpdir/new/link"], 'link(2) detected');
+
+$w->cancel;
+@$hit = ();
+select undef, undef, undef, $MIN_FS_TICK;
+link("$tmpdir/new/tst", "$tmpdir/new/link2") or BAIL_OUT "link: $!";
+$fi->poll;
+is_deeply($hit, [], 'link(2) not detected after cancel');
+
+PublicInbox::DS->Reset;
+
+done_testing;
--- /dev/null
+#!perl -w
+# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# Ensure KQNotify can pick up rename(2) and link(2) operations
+# used by Maildir writing tools
+use strict;
+use Test::More;
+use PublicInbox::TestCommon;
+plan skip_all => 'KQNotify is only for *BSD systems' if $^O !~ /bsd/;
+require_mods('IO::KQueue');
+use_ok 'PublicInbox::KQNotify';
+my ($tmpdir, $for_destroy) = tmpdir();
+mkdir "$tmpdir/new" or BAIL_OUT "mkdir: $!";
+open my $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
+close $fh or BAIL_OUT "close: $!";
+
+my $kqn = PublicInbox::KQNotify->new;
+my $mask = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
+my $hit = [];
+my $cb = sub { push @$hit, map { $_->fullname } @_ };
+my $w = $kqn->watch("$tmpdir/new", $mask, $cb);
+
+rename("$tmpdir/tst", "$tmpdir/new/tst") or BAIL_OUT "rename: $!";
+$kqn->poll;
+is_deeply($hit, ["$tmpdir/new/tst"], 'rename(2) detected (via NOTE_EXTEND)');
+
+@$hit = ();
+open $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
+close $fh or BAIL_OUT "close: $!";
+link("$tmpdir/tst", "$tmpdir/new/link") or BAIL_OUT "link: $!";
+$kqn->poll;
+is_deeply($hit, ["$tmpdir/new/link"], 'link(2) detected (via NOTE_WRITE)');
+
+$w->cancel;
+@$hit = ();
+link("$tmpdir/new/tst", "$tmpdir/new/link2") or BAIL_OUT "link: $!";
+$kqn->poll;
+is_deeply($hit, [], 'link(2) not detected after cancel');
+
+done_testing;