Skip to content

Commit 2c9ea59

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a26882 commit 2c9ea59

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

t/t0021/rot13-filter.pl

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ sub packet_bin_read {
7474
my $bytes_read = read STDIN, $buffer, 4;
7575
if ( $bytes_read == 0 ) {
7676
# EOF - Git stopped talking to us!
77-
print $debug "STOP\n";
78-
exit();
77+
return ( -1, "" );
7978
}
8079
elsif ( $bytes_read != 4 ) {
8180
die "invalid packet: '$buffer'";
@@ -99,12 +98,21 @@ sub packet_bin_read {
9998

10099
sub packet_txt_read {
101100
my ( $res, $buf ) = packet_bin_read();
102-
unless ( $buf eq '' or $buf =~ s/\n$// ) {
101+
unless ( $res == -1 or $buf eq '' or $buf =~ s/\n$// ) {
103102
die "A non-binary line MUST be terminated by an LF.";
104103
}
105104
return ( $res, $buf );
106105
}
107106

107+
sub packet_required_key_val_read {
108+
my ( $key ) = @_;
109+
my ( $res, $buf ) = packet_txt_read();
110+
unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
111+
die "bad $key: '$buf'";
112+
}
113+
return ( $res, $buf );
114+
}
115+
108116
sub packet_bin_write {
109117
my $buf = shift;
110118
print STDOUT sprintf( "%04x", length($buf) + 4 );
@@ -152,13 +160,18 @@ sub packet_flush {
152160
$debug->flush();
153161

154162
while (1) {
155-
my ( $command ) = packet_txt_read() =~ /^command=(.+)$/;
163+
my ( $res, $command ) = packet_required_key_val_read("command");
164+
if ( $res == -1 ) {
165+
print $debug "STOP\n";
166+
exit();
167+
}
156168
print $debug "IN: $command";
157169
$debug->flush();
158170

159171
if ( $command eq "list_available_blobs" ) {
160172
# Flush
161-
packet_bin_read();
173+
packet_compare_lists([1, ""], packet_bin_read()) ||
174+
die "bad list_available_blobs end";
162175

163176
foreach my $pathname ( sort keys %DELAY ) {
164177
if ( $DELAY{$pathname}{"requested"} >= 1 ) {
@@ -184,14 +197,13 @@ sub packet_flush {
184197
packet_flush();
185198
}
186199
else {
187-
my ( $pathname ) = packet_txt_read() =~ /^pathname=(.+)$/;
200+
my ( $res, $pathname ) = packet_required_key_val_read("pathname");
201+
if ( $res == -1 ) {
202+
die "unexpected EOF while expecting pathname";
203+
}
188204
print $debug " $pathname";
189205
$debug->flush();
190206

191-
if ( $pathname eq "" ) {
192-
die "bad pathname '$pathname'";
193-
}
194-
195207
# Read until flush
196208
my ( $done, $buffer ) = packet_txt_read();
197209
while ( $buffer ne '' ) {
@@ -205,6 +217,9 @@ sub packet_flush {
205217

206218
( $done, $buffer ) = packet_txt_read();
207219
}
220+
if ( $done == -1 ) {
221+
die "unexpected EOF after pathname '$pathname'";
222+
}
208223

209224
my $input = "";
210225
{
@@ -215,6 +230,9 @@ sub packet_flush {
215230
( $done, $buffer ) = packet_bin_read();
216231
$input .= $buffer;
217232
}
233+
if ( $done == -1 ) {
234+
die "unexpected EOF while reading input for '$pathname'";
235+
}
218236
print $debug " " . length($input) . " [OK] -- ";
219237
$debug->flush();
220238
}

0 commit comments

Comments
 (0)