blob: aea65d2c7f9a20ca58e9b2ca3c7df4bf7c939236 (
plain)
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
|
package storage
import (
"os"
"path/filepath"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/internal/helper"
)
func (s *server) ListDirectories(req *gitalypb.ListDirectoriesRequest, stream gitalypb.StorageService_ListDirectoriesServer) error {
storageDir, err := helper.GetStorageByName(req.StorageName)
if err != nil {
return status.Errorf(codes.InvalidArgument, "storage lookup failed: %v", err)
}
storageDir = storageDir + "/"
maxDepth := dirDepth(storageDir) + req.GetDepth()
var dirs []string
err = filepath.Walk(storageDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
relPath := strings.TrimPrefix(path, storageDir)
if relPath == "" {
return nil
}
dirs = append(dirs, relPath)
if len(dirs) > 100 {
stream.Send(&gitalypb.ListDirectoriesResponse{Paths: dirs})
dirs = dirs[:]
}
if dirDepth(path)+1 > maxDepth {
return filepath.SkipDir
}
return nil
}
return nil
})
if len(dirs) > 0 {
stream.Send(&gitalypb.ListDirectoriesResponse{Paths: dirs})
}
return err
}
func dirDepth(dir string) uint32 {
return uint32(len(strings.Split(dir, string(os.PathSeparator)))) + 1
}
|