From 544856338d731e08a8ae00e694aa8cc2a45d141d Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sat, 24 Apr 2004 14:55:49 +0000 Subject: Switch to a common readwrite_gpg() --- Echolot/Conf.pm | 8 +------- Echolot/Pinger/CPunk.pm | 15 ++------------ Echolot/Stats.pm | 15 ++------------ Echolot/Tools.pm | 52 ++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/Echolot/Conf.pm b/Echolot/Conf.pm index 19ea43f..abe69c8 100644 --- a/Echolot/Conf.pm +++ b/Echolot/Conf.pm @@ -365,13 +365,7 @@ sub parse_cpunk_key($$$) { commands => [qw{--with-colons}], command_args => [qw{--no-options --no-secmem-warning --no-default-keyring --fast-list-mode}], handles => $handles ); - print $stdin_fh $key; - 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($key, $stdin_fh, $stdout_fh, $stderr_fh, $status_fh); waitpid $pid, 0; ($stderr eq '') or diff --git a/Echolot/Pinger/CPunk.pm b/Echolot/Pinger/CPunk.pm index 1aa8f07..09145d2 100644 --- a/Echolot/Pinger/CPunk.pm +++ b/Echolot/Pinger/CPunk.pm @@ -44,13 +44,7 @@ sub encrypt_to($$$$) { commands => [ '--import' ], command_args => [qw{--no-options --no-secmem-warning --no-default-keyring --fast-list-mode --keyring}, $keyring, '--', '-' ], handles => $handles ); - print $stdin_fh $keys->{$recipient}->{'key'}; - 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($keys->{$recipient}->{'key'}, $stdin_fh, $stdout_fh, $stderr_fh, $status_fh); waitpid $pid, 0; ($stdout eq '') or @@ -102,12 +96,7 @@ sub encrypt_to($$$$) { $pid = $GnuPG->encrypt( command_args => $command_args, handles => $handles ); - close($stdin_fh); - - $stdout = join '', <$stdout_fh>; close($stdout_fh); - $stderr = join '', <$stderr_fh>; close($stderr_fh); - $status = join '', <$status_fh>; close($status_fh); - + my ($stdout, $stderr, $status) = readwrite_gpg('', $stdin_fh, $stdout_fh, $stderr_fh, $status_fh); waitpid $pid, 0; #($stderr eq '') or diff --git a/Echolot/Stats.pm b/Echolot/Stats.pm index 31eb2b6..0d97783 100644 --- a/Echolot/Stats.pm +++ b/Echolot/Stats.pm @@ -861,13 +861,7 @@ sub build_pgpring_type($$$$) { commands => [ '--import' ], command_args => [qw{--no-options --no-secmem-warning --no-default-keyring --fast-list-mode --keyring}, $keyring, '--', '-' ], handles => $handles ); - print $stdin_fh $key{'key'}; - 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($key{'key'}, $stdin_fh, $stdout_fh, $stderr_fh, $status_fh); waitpid $pid, 0; ($stdout eq '') or @@ -895,12 +889,7 @@ sub build_pgpring_export($$$$) { commands => [ '--export' ], command_args => [qw{--no-options --no-secmem-warning --no-default-keyring --keyring}, $keyring, @$keyids ], handles => $handles ); - 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('', $stdin_fh, $stdout_fh, $stderr_fh, $status_fh); waitpid $pid, 0; open (F, ">$file") or 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') { -- cgit v1.2.3