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
|
package repository
import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strconv"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"gitlab.com/gitlab-org/gitaly/v14/internal/command"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
)
func (s *server) RepositorySize(ctx context.Context, in *gitalypb.RepositorySizeRequest) (*gitalypb.RepositorySizeResponse, error) {
path, err := s.locator.GetPath(in.Repository)
if err != nil {
return nil, err
}
return &gitalypb.RepositorySizeResponse{Size: getPathSize(ctx, path)}, nil
}
func (s *server) GetObjectDirectorySize(ctx context.Context, in *gitalypb.GetObjectDirectorySizeRequest) (*gitalypb.GetObjectDirectorySizeResponse, error) {
path, err := s.locator.GetObjectDirectoryPath(in.Repository)
if err != nil {
return nil, err
}
return &gitalypb.GetObjectDirectorySizeResponse{Size: getPathSize(ctx, path)}, nil
}
func getPathSize(ctx context.Context, path string) int64 {
cmd, err := command.New(ctx, exec.Command("du", "-sk", path), nil, nil, nil)
if err != nil {
ctxlogrus.Extract(ctx).WithError(err).Warn("ignoring du command error")
return 0
}
sizeLine, err := io.ReadAll(cmd)
if err != nil {
ctxlogrus.Extract(ctx).WithError(err).Warn("ignoring command read error")
return 0
}
if err := cmd.Wait(); err != nil {
ctxlogrus.Extract(ctx).WithError(err).Warn("ignoring du wait error")
return 0
}
sizeParts := bytes.Split(sizeLine, []byte("\t"))
if len(sizeParts) != 2 {
ctxlogrus.Extract(ctx).Warn(fmt.Sprintf("ignoring du malformed output: %q", sizeLine))
return 0
}
size, err := strconv.ParseInt(string(sizeParts[0]), 10, 0)
if err != nil {
ctxlogrus.Extract(ctx).WithError(err).Warn("ignoring parsing size error")
return 0
}
return size
}
|