* [PATCH] spamcheck/spamc: pass GLOB handles instead of FD numbers
@ 2019-12-31 22:34 Eric Wong
0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2019-12-31 22:34 UTC (permalink / raw)
To: meta
The spawn() interface improvements[1] propagate to popen_rd,
too, so we can avoid weird dances to keep the GLOB handle
references live and just pass the handle around.
[1] commit 267371b1273b518215939e817e53733584b68af7
("spawn: allow passing GLOB handles for redirects")
---
lib/PublicInbox/Spamcheck/Spamc.pm | 30 ++++++++++++------------------
1 file changed, 12 insertions(+), 18 deletions(-)
diff --git a/lib/PublicInbox/Spamcheck/Spamc.pm b/lib/PublicInbox/Spamcheck/Spamc.pm
index e34748c6..bb288b16 100644
--- a/lib/PublicInbox/Spamcheck/Spamc.pm
+++ b/lib/PublicInbox/Spamcheck/Spamc.pm
@@ -7,7 +7,7 @@ use strict;
use warnings;
use PublicInbox::Spawn qw(popen_rd spawn);
use IO::Handle;
-use Fcntl qw(:DEFAULT SEEK_SET);
+use Fcntl qw(SEEK_SET);
sub new {
my ($class) = @_;
@@ -21,9 +21,7 @@ sub new {
sub spamcheck {
my ($self, $msg, $out) = @_;
- my $tmp;
- my $fd = _msg_to_fd($self, $msg, \$tmp);
- my $rdr = { 0 => $fd };
+ my $rdr = { 0 => _msg_to_fh($self, $msg) };
my ($fh, $pid) = popen_rd($self->{checkcmd}, undef, $rdr);
defined $pid or die "failed to popen_rd spamc: $!\n";
my $r;
@@ -57,10 +55,9 @@ sub spamlearn {
sub _learn {
my ($self, $msg, $rdr, $field) = @_;
$rdr ||= {};
+ $rdr->{0} = _msg_to_fh($self, $msg);
$rdr->{1} ||= $self->_devnull;
$rdr->{2} ||= $self->_devnull;
- my $tmp;
- $rdr->{0} = _msg_to_fd($self, $msg, \$tmp);
my $pid = spawn($self->{$field}, undef, $rdr);
waitpid($pid, 0);
!$?;
@@ -68,20 +65,18 @@ sub _learn {
sub _devnull {
my ($self) = @_;
- my $fd = $self->{-devnullfd};
- return $fd if defined $fd;
- open my $fh, '+>', '/dev/null' or
+ $self->{-devnull} //= do {
+ open my $fh, '+>', '/dev/null' or
die "failed to open /dev/null: $!";
- $self->{-devnull} = $fh;
- $self->{-devnullfd} = fileno($fh);
+ $fh
+ }
}
-sub _msg_to_fd {
- my ($self, $msg, $tmpref) = @_;
- my $fd;
+sub _msg_to_fh {
+ my ($self, $msg) = @_;
if (my $ref = ref($msg)) {
- my $fileno = eval { fileno($msg) };
- return $fileno if defined $fileno;
+ my $fd = eval { fileno($msg) };
+ return $msg if defined($fd) && $fd >= 0;
open(my $tmpfh, '+>', undef) or die "failed to open: $!";
$tmpfh->autoflush(1);
@@ -89,9 +84,8 @@ sub _msg_to_fd {
print $tmpfh $$msg or die "failed to print: $!";
sysseek($tmpfh, 0, SEEK_SET) or
die "sysseek(fh) failed: $!";
- $$tmpref = $tmpfh;
- return fileno($tmpfh);
+ return $tmpfh;
}
$msg;
}
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2019-12-31 22:34 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-31 22:34 [PATCH] spamcheck/spamc: pass GLOB handles instead of FD numbers Eric Wong
Code repositories for project(s) associated with this public 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).