about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-12-31 22:34:16 +0000
committerEric Wong <e@80x24.org>2020-01-01 07:50:13 +0000
commit9004fa901b09d7c50be31e0f800474503ece3d5a (patch)
treefa66bbf5d4cf50b78f4b019b52812b868a2b2e53 /lib
parent57c2bb80e2f94dfcbf08532f72c7ed568ceef7be (diff)
downloadpublic-inbox-9004fa901b09d7c50be31e0f800474503ece3d5a.tar.gz
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")
Diffstat (limited to 'lib')
-rw-r--r--lib/PublicInbox/Spamcheck/Spamc.pm30
1 files 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;
 }