/
utils.go
74 lines (57 loc) · 1.58 KB
/
utils.go
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
package volumes
import (
"crypto/md5"
"errors"
"fmt"
"gitlab.com/gitlab-org/gitlab-runner/executors/docker/internal/volumes/parser"
)
var (
errDirectoryNotAbsolute = errors.New("build directory needs to be an absolute path")
errDirectoryIsRootPath = errors.New("build directory needs to be a non-root path")
)
type debugLogger interface {
Debugln(args ...interface{})
}
func IsHostMountedVolume(volumeParser parser.Parser, dir string, volumes ...string) (bool, error) {
if !volumeParser.Path().IsAbs(dir) {
return false, errDirectoryNotAbsolute
}
if volumeParser.Path().IsRoot(dir) {
return false, errDirectoryIsRootPath
}
for _, volume := range volumes {
parsedVolume, err := volumeParser.ParseVolume(volume)
if err != nil {
return false, err
}
if parsedVolume.Len() < 2 {
continue
}
if volumeParser.Path().Contains(parsedVolume.Destination, dir) {
return true, nil
}
}
return false, nil
}
func hashContainerPath(containerPath string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(containerPath)))
}
type ErrVolumeAlreadyDefined struct {
containerPath string
}
func (e *ErrVolumeAlreadyDefined) Error() string {
return fmt.Sprintf("volume for container path %q is already defined", e.containerPath)
}
func NewErrVolumeAlreadyDefined(containerPath string) *ErrVolumeAlreadyDefined {
return &ErrVolumeAlreadyDefined{
containerPath: containerPath,
}
}
type pathList map[string]bool
func (m pathList) Add(containerPath string) error {
if m[containerPath] {
return NewErrVolumeAlreadyDefined(containerPath)
}
m[containerPath] = true
return nil
}