+ my $r = sysread($fh, $$rbuf, $PIPE_BUFSIZ, length($$rbuf))
+ and next;
+
+ # return whatever's left on EOF
+ return substr($$rbuf, 0, length($$rbuf)+1, '') if defined($r);
+
+ next if ($! == EAGAIN and poll_in($fh));
+ next if $! == EINTR; # may be set by sysread or poll_in
+ return; # unrecoverable error
+ }
+}
+
+sub cat_async_retry ($$$$$) {
+ my ($self, $inflight, $req, $cb, $arg) = @_;
+
+ # {inflight} may be non-existent, but if it isn't we delete it
+ # here to prevent cleanup() from waiting:
+ delete $self->{inflight};
+ cleanup($self);
+
+ $self->{inflight} = $inflight;
+ batch_prepare($self);
+ my $buf = "$req\n";
+ for (my $i = 0; $i < @$inflight; $i += 3) {
+ $buf .= "$inflight->[$i]\n";
+ }
+ print { $self->{out} } $buf or $self->fail("write error: $!");
+ unshift(@$inflight, \$req, $cb, $arg); # \$ref to indicate retried
+
+ cat_async_step($self, $inflight); # take one step
+}
+
+sub cat_async_step ($$) {
+ my ($self, $inflight) = @_;
+ die 'BUG: inflight empty or odd' if scalar(@$inflight) < 3;
+ my ($req, $cb, $arg) = splice(@$inflight, 0, 3);
+ my $rbuf = delete($self->{cat_rbuf}) // \(my $new = '');
+ my ($bref, $oid, $type, $size);
+ my $head = my_readline($self->{in}, $rbuf);
+ # ->fail may be called via Gcf2Client.pm
+ if ($head =~ /^([0-9a-f]{40,}) (\S+) ([0-9]+)$/) {
+ ($oid, $type, $size) = ($1, $2, $3 + 0);
+ $bref = my_read($self->{in}, $rbuf, $size + 1) or
+ $self->fail(defined($bref) ? 'read EOF' : "read: $!");
+ chop($$bref) eq "\n" or $self->fail('LF missing after blob');
+ } elsif ($head =~ s/ missing\n//s) {
+ $oid = $head;
+ # ref($req) indicates it's already been retried
+ # -gcf2 retries internally, so it never hits this path:
+ if (!ref($req) && !$in_cleanup && $self->alternates_changed) {
+ return cat_async_retry($self, $inflight,
+ $req, $cb, $arg);
+ }
+ $type = 'missing';
+ $oid = ref($req) ? $$req : $req if $oid eq '';
+ } else {
+ my $err = $! ? " ($!)" : '';
+ $self->fail("bad result from async cat-file: $head$err");
+ }
+ $self->{cat_rbuf} = $rbuf if $$rbuf ne '';
+ eval { $cb->($bref, $oid, $type, $size, $arg) };
+ warn "E: $oid: $@\n" if $@;
+}
+
+sub cat_async_wait ($) {
+ my ($self) = @_;
+ my $inflight = $self->{inflight} or return;
+ while (scalar(@$inflight)) {
+ cat_async_step($self, $inflight);
+ }
+}
+
+sub batch_prepare ($) {
+ _bidi_pipe($_[0], qw(--batch in out pid));
+}
+
+sub _cat_file_cb {
+ my ($bref, undef, undef, $size, $result) = @_;
+ @$result = ($bref, $size);
+}
+
+sub cat_file {
+ my ($self, $oid, $sizeref) = @_;
+ my $result = [];
+ cat_async($self, $oid, \&_cat_file_cb, $result);
+ cat_async_wait($self);
+ $$sizeref = $result->[1] if $sizeref;
+ $result->[0];
+}
+
+sub check_async_step ($$) {
+ my ($self, $inflight_c) = @_;
+ die 'BUG: inflight empty or odd' if scalar(@$inflight_c) < 3;
+ my ($req, $cb, $arg) = splice(@$inflight_c, 0, 3);
+ my $rbuf = delete($self->{chk_rbuf}) // \(my $new = '');
+ chomp(my $line = my_readline($self->{in_c}, $rbuf));
+ my ($hex, $type, $size) = split(/ /, $line);
+
+ # Future versions of git.git may have type=ambiguous, but for now,
+ # we must handle 'dangling' below (and maybe some other oddball
+ # stuff):
+ # https://public-inbox.org/git/20190118033845.s2vlrb3wd3m2jfzu@dcvr/T/
+ if ($hex eq 'dangling' || $hex eq 'notdir' || $hex eq 'loop') {
+ my $ret = my_read($self->{in_c}, $rbuf, $type + 1);
+ $self->fail(defined($ret) ? 'read EOF' : "read: $!") if !$ret;
+ }
+ $self->{chk_rbuf} = $rbuf if $$rbuf ne '';
+ eval { $cb->($hex, $type, $size, $arg, $self) };
+ warn "E: check($req) $@\n" if $@;
+}
+
+sub check_async_wait ($) {
+ my ($self) = @_;
+ my $inflight_c = $self->{inflight_c} or return;
+ while (scalar(@$inflight_c)) {
+ check_async_step($self, $inflight_c);