user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
1b5c578ef3df8aef15a9adefab55c00c11ded432 blob 1782 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
# Copyright (C) 2020 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# implements the small subset of Linux::Inotify2 functionality we use
# using IO::KQueue on *BSD systems.
package PublicInbox::KQNotify;
use strict;
use IO::KQueue;
use PublicInbox::DSKQXS; # wraps IO::KQueue for fork-safe DESTROY

# only true as far as public-inbox is concerned with .lock files:
sub IN_CLOSE () { NOTE_WRITE }
#sub IN_CLOSE () { 0x200 } # NOTE_CLOSE_WRITE (FreeBSD 11+ only)

sub new {
	my ($class) = @_;
	bless { dskq => PublicInbox::DSKQXS->new, watch => {} }, $class;
}

sub watch {
	my ($self, $path, $mask, $cb) = @_;
	open(my $fh, '<', $path) or return;
	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 == IN_CLOSE) {
		$self->{watch}->{$ident} = [ $fh, $cb ];
	} else {
		die "TODO Not implemented: $mask";
	}
	bless \$fh, 'PublicInbox::KQNotify::Watch';
}

# emulate Linux::Inotify::fileno
sub fileno { ${$_[0]->{dskq}->{kq}} }

# noop for Linux::Inotify2 compatibility.  Unlike inotify,
# kqueue doesn't seem to overflow since it's limited by the number of
# open FDs the process has
sub on_overflow {}

# noop for Linux::Inotify2 compatibility, we use `0' timeout for ->kevent
sub blocking {}

# behave like Linux::Inotify2::poll
sub poll {
	my ($self) = @_;
	my @kevents = $self->{dskq}->{kq}->kevent(0);
	for my $kev (@kevents) {
		my $ident = $kev->[KQ_IDENT];
		my $mask = $kev->[KQ_FFLAGS];
		if (($mask & IN_CLOSE) == IN_CLOSE) {
			eval { $self->{watch}->{$ident}->[1]->() };
		}
	}
}

package PublicInbox::KQNotify::Watch;
use strict;

sub cancel { close ${$_[0]} or die "close: $!" }

1;
debug log:

solving 1b5c578ef3d ...
found 1b5c578ef3d in public-inbox.git

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).