t0021/rot13-filter: refactor packet reading functions
To make it possible in a following commit to move packet reading and writing functions into a Packet.pm module, let's refactor these functions, so they don't handle printing debug output and exiting. While at it let's create packet_required_key_val_read() to still handle erroring out in a common case. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
0a26882621
commit
2c9ea595a7
@ -74,8 +74,7 @@ sub packet_bin_read {
|
|||||||
my $bytes_read = read STDIN, $buffer, 4;
|
my $bytes_read = read STDIN, $buffer, 4;
|
||||||
if ( $bytes_read == 0 ) {
|
if ( $bytes_read == 0 ) {
|
||||||
# EOF - Git stopped talking to us!
|
# EOF - Git stopped talking to us!
|
||||||
print $debug "STOP\n";
|
return ( -1, "" );
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
elsif ( $bytes_read != 4 ) {
|
elsif ( $bytes_read != 4 ) {
|
||||||
die "invalid packet: '$buffer'";
|
die "invalid packet: '$buffer'";
|
||||||
@ -99,12 +98,21 @@ sub packet_bin_read {
|
|||||||
|
|
||||||
sub packet_txt_read {
|
sub packet_txt_read {
|
||||||
my ( $res, $buf ) = packet_bin_read();
|
my ( $res, $buf ) = packet_bin_read();
|
||||||
unless ( $buf eq '' or $buf =~ s/\n$// ) {
|
unless ( $res == -1 or $buf eq '' or $buf =~ s/\n$// ) {
|
||||||
die "A non-binary line MUST be terminated by an LF.";
|
die "A non-binary line MUST be terminated by an LF.";
|
||||||
}
|
}
|
||||||
return ( $res, $buf );
|
return ( $res, $buf );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub packet_required_key_val_read {
|
||||||
|
my ( $key ) = @_;
|
||||||
|
my ( $res, $buf ) = packet_txt_read();
|
||||||
|
unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
|
||||||
|
die "bad $key: '$buf'";
|
||||||
|
}
|
||||||
|
return ( $res, $buf );
|
||||||
|
}
|
||||||
|
|
||||||
sub packet_bin_write {
|
sub packet_bin_write {
|
||||||
my $buf = shift;
|
my $buf = shift;
|
||||||
print STDOUT sprintf( "%04x", length($buf) + 4 );
|
print STDOUT sprintf( "%04x", length($buf) + 4 );
|
||||||
@ -152,13 +160,18 @@ print $debug "init handshake complete\n";
|
|||||||
$debug->flush();
|
$debug->flush();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
my ( $command ) = packet_txt_read() =~ /^command=(.+)$/;
|
my ( $res, $command ) = packet_required_key_val_read("command");
|
||||||
|
if ( $res == -1 ) {
|
||||||
|
print $debug "STOP\n";
|
||||||
|
exit();
|
||||||
|
}
|
||||||
print $debug "IN: $command";
|
print $debug "IN: $command";
|
||||||
$debug->flush();
|
$debug->flush();
|
||||||
|
|
||||||
if ( $command eq "list_available_blobs" ) {
|
if ( $command eq "list_available_blobs" ) {
|
||||||
# Flush
|
# Flush
|
||||||
packet_bin_read();
|
packet_compare_lists([1, ""], packet_bin_read()) ||
|
||||||
|
die "bad list_available_blobs end";
|
||||||
|
|
||||||
foreach my $pathname ( sort keys %DELAY ) {
|
foreach my $pathname ( sort keys %DELAY ) {
|
||||||
if ( $DELAY{$pathname}{"requested"} >= 1 ) {
|
if ( $DELAY{$pathname}{"requested"} >= 1 ) {
|
||||||
@ -184,14 +197,13 @@ while (1) {
|
|||||||
packet_flush();
|
packet_flush();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
my ( $pathname ) = packet_txt_read() =~ /^pathname=(.+)$/;
|
my ( $res, $pathname ) = packet_required_key_val_read("pathname");
|
||||||
|
if ( $res == -1 ) {
|
||||||
|
die "unexpected EOF while expecting pathname";
|
||||||
|
}
|
||||||
print $debug " $pathname";
|
print $debug " $pathname";
|
||||||
$debug->flush();
|
$debug->flush();
|
||||||
|
|
||||||
if ( $pathname eq "" ) {
|
|
||||||
die "bad pathname '$pathname'";
|
|
||||||
}
|
|
||||||
|
|
||||||
# Read until flush
|
# Read until flush
|
||||||
my ( $done, $buffer ) = packet_txt_read();
|
my ( $done, $buffer ) = packet_txt_read();
|
||||||
while ( $buffer ne '' ) {
|
while ( $buffer ne '' ) {
|
||||||
@ -205,6 +217,9 @@ while (1) {
|
|||||||
|
|
||||||
( $done, $buffer ) = packet_txt_read();
|
( $done, $buffer ) = packet_txt_read();
|
||||||
}
|
}
|
||||||
|
if ( $done == -1 ) {
|
||||||
|
die "unexpected EOF after pathname '$pathname'";
|
||||||
|
}
|
||||||
|
|
||||||
my $input = "";
|
my $input = "";
|
||||||
{
|
{
|
||||||
@ -215,6 +230,9 @@ while (1) {
|
|||||||
( $done, $buffer ) = packet_bin_read();
|
( $done, $buffer ) = packet_bin_read();
|
||||||
$input .= $buffer;
|
$input .= $buffer;
|
||||||
}
|
}
|
||||||
|
if ( $done == -1 ) {
|
||||||
|
die "unexpected EOF while reading input for '$pathname'";
|
||||||
|
}
|
||||||
print $debug " " . length($input) . " [OK] -- ";
|
print $debug " " . length($input) . " [OK] -- ";
|
||||||
$debug->flush();
|
$debug->flush();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user