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
|
package blob
import (
"fmt"
"io"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/streamio"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *server) GetBlob(in *gitalypb.GetBlobRequest, stream gitalypb.BlobService_GetBlobServer) error {
ctx := stream.Context()
repo := s.localrepo(in.GetRepository())
if err := validateRequest(in); err != nil {
return status.Errorf(codes.InvalidArgument, "GetBlob: %v", err)
}
c, err := s.catfileCache.BatchProcess(stream.Context(), repo)
if err != nil {
return status.Errorf(codes.Internal, "GetBlob: %v", err)
}
objectInfo, err := c.Info(ctx, git.Revision(in.Oid))
if err != nil && !catfile.IsNotFound(err) {
return status.Errorf(codes.Internal, "GetBlob: %v", err)
}
if catfile.IsNotFound(err) || objectInfo.Type != "blob" {
return helper.DecorateError(codes.Unavailable, stream.Send(&gitalypb.GetBlobResponse{}))
}
readLimit := objectInfo.Size
if in.Limit >= 0 && in.Limit < readLimit {
readLimit = in.Limit
}
firstMessage := &gitalypb.GetBlobResponse{
Size: objectInfo.Size,
Oid: objectInfo.Oid.String(),
}
if readLimit == 0 {
return helper.DecorateError(codes.Unavailable, stream.Send(firstMessage))
}
blobObj, err := c.Blob(ctx, git.Revision(objectInfo.Oid))
if err != nil {
return status.Errorf(codes.Internal, "GetBlob: %v", err)
}
sw := streamio.NewWriter(func(p []byte) error {
msg := &gitalypb.GetBlobResponse{}
if firstMessage != nil {
msg = firstMessage
firstMessage = nil
}
msg.Data = p
return stream.Send(msg)
})
_, err = io.CopyN(sw, blobObj.Reader, readLimit)
if err != nil {
return status.Errorf(codes.Unavailable, "GetBlob: send: %v", err)
}
return nil
}
func validateRequest(in *gitalypb.GetBlobRequest) error {
if len(in.GetOid()) == 0 {
return fmt.Errorf("empty Oid")
}
return nil
}
|