File: ProtoConn.pm

package info (click to toggle)
dgit 14.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,396 kB
  • sloc: perl: 14,097; sh: 7,449; makefile: 346; python: 334; tcl: 69
file content (248 lines) | stat: -rw-r--r-- 6,520 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# -*- perl -*-
# dgit
# Debian::Dgit::Proto: protocol helper utilities
#
# Copyright (C)2015-2020,2022-2024 Ian Jackson
# Copyright (C)2020-2024           Sean Whitton
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.

package Debian::Dgit::ProtoConn;

use strict;
use warnings;

use Debian::Dgit;
use Debian::Dgit::I18n;

use Carp;
use IPC::Open2 ();

sub new ($$) {
    # Arguments to new:
    #   $fh_r      filehandle to read from
    #   $fh_w      filehandle to write to
    my ($class, $fh_r, $fh_w) = @_;
    bless {
        R => $fh_r,
        W => $fh_w,
	Cmd => undef,
	Pid => undef,
	Desc => undef,
        EofHook => sub { undef; },
        FailHook => sub { undef; },
    } => $class;
}

sub open2 {
    my $class = shift;
    my $pid = IPC::Open2::open2(my $fh_r, my $fh_w, @_);
    my $r = Debian::Dgit::ProtoConn->new($fh_r, $fh_w);
    $r->{Pid} = $pid;
    $r->{Cmd} = [ @_ ];
    $r
}

# $hook is called when we get unexpected eof and
# should return a (translated) message,
# or undef if a generic message is fine.
#
# The hook is called by the following functions:
#   expect
#   read_bytes
#   readfail
# in each case, if the stream got EOF, but not if our read got an error.
sub set_eof_hook ($$) {
    my ($self, $hook) = @_;

    $self->{EofHook} = $hook;
}

# $hook is called just before fail, and it is passed the error message as sole argument
sub set_fail_hook ($$) {
    my ($self, $hook) = @_;

    $self->{FailHook} = $hook;
}

sub set_description ($$) {
    my ($self, $desc) = @_;

    $self->{Desc} = $desc;
}

sub _fail ($$) {
    my ($self, $m) = @_;
    $self->{FailHook}($m);
    fail +(defined $self->{Desc} ? $self->{Desc}.': ' : '').$m;
}

sub bad ($$) {
    my ($self, $m) = @_;
    $self->_fail(f_ "connection lost: %s", $!) if $self->{R}->error;
    $self->_fail(f_ "protocol violation; %s not expected", $m);
}

sub get_pid ($$) {
    my ($self) = @_;
    $self->{Pid};
}
sub get_command ($$) {
    my ($self) = @_;
    $self->{Cmd};
}
sub get_fh_r ($) {
    my ($self) = @_;
    $self->{R};
}
sub get_fh_w ($) {
    my ($self) = @_;
    $self->{W};
}

# die due to a read error.
#
# `$wh` is a (translated) description of what we were trying to read
# (which is used on EOF, if set_eof_hook was not called.)
sub readfail ($$) {
    my ($self, $wh) = @_;
    $self->_fail(f_ "connection lost: %s", $!) if $!;
    my $report = $self->{EofHook}();
    $self->_fail($report) if defined $report;
    $self->bad(f_ "eof (reading %s)", $wh);
}

# Expects to receive a message in some particular form(s)
#
# $match->() is used to analyse the received message.
# Calls $match->() having set `$_` to the received line (chomped).
#
# In array context, calls $match->() in array context;
# a nonempty array means the value matched,
# and is then returned.
#
# In other contexts, calls $match->() in scalar context;
# a true value means the value matched, and is returned.
#
# If $match returns false, it is bad (expect calls $self->bad()).
sub expect ($&) {
    my ($self, $match) = @_;
    # Bind $_ for the benefit of the user's matcher.
    local $_ = readline $self->{R};
    defined && chomp or $self->readfail(__ "protocol message");
    printdebug +($self->{Desc} // '')."<< $_\n";
    if (wantarray) {
	my @r = &$match;
	return @r if @r;
    } else {
	my $r = &$match;
	return $r if $r;
    }
    $self->bad(f_ "\`%s'", $_);
}

sub read_bytes ($$) {
    my ($self, $nbytes) = @_;
    $nbytes =~ m/^[1-9]\d{0,5}$|^0$/ or $self->bad(__ "bad byte count");
    my $d;
    my $got = read $self->{R}, $d, $nbytes;
    $got==$nbytes or $self->readfail(__ "data block");
    return $d;
}

# Receive data sent via zero or more `data-block` messages.
#
# Successive data blocks are passed as the sole argument to `$take_data`.
#
# The caller should consider doing at least one `printdebug`
# before calling this function, if there isn't one nearby already.
sub receive_data_blocks ($&) {
    my ($self, $take_data) = @_;
    for (;;) {
	my ($more_data, $l) = $self->expect(sub {
	    m/^data-block (.+)$/ ? (1,$1) :
	    m/^data-end$/        ? (0,)   :
	                           ();
	});
	last unless $more_data;
	my $d = $self->read_bytes($l);
	$take_data->($d);
    }
}

# Receive exactly one data block and return its data.
sub receive_data_block ($) {
    my $self = shift;
    my $ret;
    $self->receive_data_blocks(sub {
	if ($ret) {
	    $self->bad("unexpected data-block");
	} else {
	    $ret = shift;
	}
    });
    $ret
}

sub receive_file ($$) {
    my ($self, $ourfn) = @_;
    printdebug +($self->{Desc} // '')."() $ourfn\n";
    my $pf = new IO::File($ourfn, ">") or die "$ourfn: $!";
    $self->receive_data_blocks(sub {
	print $pf $_[0] or confess "$!";
    });
    close $pf or confess "$!";
}

# Like `send` but doesn't add a newline and doesn't call `printdebug`
sub send_raw (&$) {
    my ($self, $msg) = @_;
    my $fh = $self->{W};
    print $fh $msg or confess "$!";
    $fh->flush or confess "$!";
}

sub send (&$) {
    my ($self, $msg) = @_;
    printdebug +($self->{Desc} // '').">> $msg\n";
    $self->send_raw("$msg\n");
}

sub send_counted_message ($$$) {
    my ($self, $command, $message) = @_;
    # length() returns the number of bytes only for non-UTF8.
    # See: <https://salsa.debian.org/dgit-team/dgit/-/merge_requests/363#note_707041>
    confess "ProtoConn cannot handle UTF8 strings"
      if utf8::is_utf8($message);
    $self->send("$command ".length($message));
    $self->send_raw($message);
}

sub send_file ($$) {
    my ($self, $ourfn) = @_;
    my $pf = new IO::File($ourfn, '<') or die "$ourfn: $!";
    my $fh = $self->{W};
    for (;;) {
	my $d;
	my $got = (read $pf, $d, 65536) // die "$ourfn: $!";
	last if !$got;
	print $fh "data-block ".length($d)."\n" or confess "$!";
	print $fh $d or confess "$!";
    }
    $pf->error and die "$ourfn $!";
    print $fh "data-end\n" or confess "$!";
    close $pf;
}

1;