X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FProcessPipe.pm;h=336d5ac4fa97d18a1b34304486138fe6a5ecea3a;hb=af0b0fb7a454470a32c452119d0392e0dedb3fe1;hp=30509d022842fdb75d5404a9afbc2beca24022b2;hpb=9bd675d33ad1e49bd2ebe12a1d216216e61380de;p=public-inbox.git diff --git a/lib/PublicInbox/ProcessPipe.pm b/lib/PublicInbox/ProcessPipe.pm index 30509d02..336d5ac4 100644 --- a/lib/PublicInbox/ProcessPipe.pm +++ b/lib/PublicInbox/ProcessPipe.pm @@ -1,32 +1,46 @@ -# Copyright (C) 2016-2019 all contributors +# Copyright (C) 2016-2021 all contributors # License: AGPL-3.0+ # a tied handle for auto reaping of children tied to a pipe, see perltie(1) package PublicInbox::ProcessPipe; use strict; -use warnings; +use v5.10.1; +use PublicInbox::DS qw(dwaitpid); sub TIEHANDLE { - my ($class, $pid, $fh) = @_; - bless { pid => $pid, fh => $fh }, $class; + my ($class, $pid, $fh, $cb, $arg) = @_; + bless { pid => $pid, fh => $fh, cb => $cb, arg => $arg }, $class; } sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) } sub READLINE { readline($_[0]->{fh}) } +sub WRITE { + use bytes qw(length); + syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0); +} + +sub PRINT { + my $self = shift; + print { $self->{fh} } @_; +} + +sub adjust_ret { # dwaitpid callback + my ($retref, $pid) = @_; + $$retref = '' if $? +} + sub CLOSE { my $fh = delete($_[0]->{fh}); my $ret = defined $fh ? close($fh) : ''; - my $pid = delete $_[0]->{pid}; + my ($pid, $cb, $arg) = delete @{$_[0]}{qw(pid cb arg)}; if (defined $pid) { - # PublicInbox::DS may not be loaded - eval { PublicInbox::DS::dwaitpid($pid, undef, undef) }; - - if ($@) { # ok, not in the event loop, work synchronously - waitpid($pid, 0); - $ret = '' if $?; + unless ($cb) { + $cb = \&adjust_ret; + $arg = \$ret; } + dwaitpid $pid, $cb, $arg; } $ret; }