2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # Ensure KQNotify can pick up rename(2) and link(2) operations
6 # used by Maildir writing tools
8 use PublicInbox::TestCommon;
11 require_mods('IO::KQueue');
12 use_ok 'PublicInbox::KQNotify';
13 my ($tmpdir, $for_destroy) = tmpdir();
16 my $kqn = PublicInbox::KQNotify->new;
17 my $mask = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
18 my $w = $kqn->watch("$tmpdir/new", $mask);
20 open my $fh, '>', "$tmpdir/tst";
22 rename("$tmpdir/tst", "$tmpdir/new/tst");
23 my $hit = [ map { $_->fullname } $kqn->read ];
24 is_deeply($hit, ["$tmpdir/new/tst"],
25 'rename(2) detected (via NOTE_EXTEND)')
26 or diag explain($hit);
28 open $fh, '>', "$tmpdir/tst";
30 link("$tmpdir/tst", "$tmpdir/new/link");
31 my @read = map { $_->fullname } $kqn->read;
32 $hit = [ grep(m!/link$!, @read) ];
33 is_deeply($hit, ["$tmpdir/new/link"], 'link(2) detected (via NOTE_WRITE)')
34 or diag explain(\@read);
37 my $d = "$tmpdir/new/ANOTHER";
39 $hit = [ map { $_->fullname } $kqn->read ];
40 is_xdeeply($hit, [ $d ], 'mkdir detected');
42 # TODO: should we always watch for directory removals?
46 link("$tmpdir/new/tst", "$tmpdir/new/link2");
47 $hit = [ map { $_->fullname } $kqn->read ];
48 is_deeply($hit, [], 'link(2) not detected after cancel');
51 my $GONE = PublicInbox::KQNotify::NOTE_DELETE() |
52 PublicInbox::KQNotify::NOTE_REVOKE() |
53 PublicInbox::KQNotify::NOTE_ATTRIB() |
54 PublicInbox::KQNotify::NOTE_WRITE() |
55 PublicInbox::KQNotify::NOTE_RENAME();
56 $w = $kqn->watch("$tmpdir/new", $mask|$GONE);
57 my @unlink = sort glob("$tmpdir/new/*");
59 $hit = [ sort(map { $_->fullname } $kqn->read) ];
60 is_xdeeply($hit, \@unlink, 'unlinked files match');
62 # this is unreliable on Dragonfly tmpfs (fixed post-6.4)
64 $hit = [ sort(map { $_->fullname } $kqn->read) ];
65 is(scalar(@$hit), 1, 'detected self removal') or check_broken_tmpfs;