From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] spamcheck/spamc: pass GLOB handles instead of FD numbers
Date: Tue, 31 Dec 2019 22:34:16 +0000 [thread overview]
Message-ID: <20191231223416.32025-1-e@80x24.org> (raw)
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;
}
reply other threads:[~2019-12-31 22:34 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191231223416.32025-1-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).