diff options
author | Peter Palfrader <peter@palfrader.org> | 2004-04-24 14:55:49 +0000 |
---|---|---|
committer | Peter Palfrader <peter@palfrader.org> | 2004-04-24 14:55:49 +0000 |
commit | 544856338d731e08a8ae00e694aa8cc2a45d141d (patch) | |
tree | aebceeec67e955bc4fca215b5f2b3152e032d1b8 /Echolot/Tools.pm | |
parent | 75da8751b1e5a5a39f8743b44971224b0ab34c31 (diff) |
Switch to a common readwrite_gpg()
Diffstat (limited to 'Echolot/Tools.pm')
-rw-r--r-- | Echolot/Tools.pm | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/Echolot/Tools.pm b/Echolot/Tools.pm index 9dea6a3..332f2e8 100644 --- a/Echolot/Tools.pm +++ b/Echolot/Tools.pm @@ -274,6 +274,50 @@ sub make_gpg_fds() { return ($fds{'stdin'}, $fds{'stdout'}, $fds{'stderr'}, $fds{'status'}, $handles); }; +sub readwrite_gpg($$$$$) { + my ($in, $inputfd, $stdoutfd, $stderrfd, $statusfd) = @_; + local $INPUT_RECORD_SEPARATOR = undef; + my $s = IO::Select->new(); + + $stdoutfd->blocking(0); + $statusfd->blocking(0); + $stderrfd->blocking(0); + $s->add($stdoutfd); + $s->add($stderrfd); + $s->add($statusfd); + + my ($stdout, $stderr, $status) = ("", "", ""); + + print $inputfd $in; close $inputfd; + + my @ready; + while ($s->count() > 0) { + @ready = $s->can_read(42); + next unless (scalar(@ready)); # Wait some more. + + for my $rfd (@ready) { + if ($rfd->eof) { + $s->remove($rfd); + close($rfd); + next; + } + if ($rfd == $stdoutfd) { + $stdout .= <$rfd>; + next; + } + if ($rfd == $statusfd) { + $status .= <$rfd>; + next; + } + if ($rfd == $stderrfd) { + $stderr .= <$rfd>; + next; + } + } + } + return ($stdout, $stderr, $status); +} + sub crypt_symmetrically($$) { my ($msg, $direction) = @_; @@ -294,13 +338,7 @@ sub crypt_symmetrically($$) { $direction eq 'encrypt' ? $GnuPG->encrypt_symmetrically( handles => $handles ) : $GnuPG->decrypt( handles => $handles ); - print $stdin_fh $msg; - close($stdin_fh); - - my $stdout = join '', <$stdout_fh>; close($stdout_fh); - my $stderr = join '', <$stderr_fh>; close($stderr_fh); - my $status = join '', <$status_fh>; close($status_fh); - + my ($stdout, $stderr, $status) = readwrite_gpg($msg, $stdin_fh, $stdout_fh, $stderr_fh, $status_fh); waitpid $pid, 0; if ($direction eq 'encrypt') { |