github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/docker/internal/volumes/utils.go (about)

     1  package volumes
     2  
     3  import (
     4  	"crypto/md5"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"gitlab.com/gitlab-org/gitlab-runner/executors/docker/internal/volumes/parser"
     9  )
    10  
    11  var (
    12  	errDirectoryNotAbsolute = errors.New("build directory needs to be an absolute path")
    13  	errDirectoryIsRootPath  = errors.New("build directory needs to be a non-root path")
    14  )
    15  
    16  type debugLogger interface {
    17  	Debugln(args ...interface{})
    18  }
    19  
    20  func IsHostMountedVolume(volumeParser parser.Parser, dir string, volumes ...string) (bool, error) {
    21  	if !volumeParser.Path().IsAbs(dir) {
    22  		return false, errDirectoryNotAbsolute
    23  	}
    24  
    25  	if volumeParser.Path().IsRoot(dir) {
    26  		return false, errDirectoryIsRootPath
    27  	}
    28  
    29  	for _, volume := range volumes {
    30  		parsedVolume, err := volumeParser.ParseVolume(volume)
    31  		if err != nil {
    32  			return false, err
    33  		}
    34  
    35  		if parsedVolume.Len() < 2 {
    36  			continue
    37  		}
    38  
    39  		if volumeParser.Path().Contains(parsedVolume.Destination, dir) {
    40  			return true, nil
    41  		}
    42  	}
    43  	return false, nil
    44  }
    45  
    46  func hashContainerPath(containerPath string) string {
    47  	return fmt.Sprintf("%x", md5.Sum([]byte(containerPath)))
    48  }
    49  
    50  type ErrVolumeAlreadyDefined struct {
    51  	containerPath string
    52  }
    53  
    54  func (e *ErrVolumeAlreadyDefined) Error() string {
    55  	return fmt.Sprintf("volume for container path %q is already defined", e.containerPath)
    56  }
    57  
    58  func NewErrVolumeAlreadyDefined(containerPath string) *ErrVolumeAlreadyDefined {
    59  	return &ErrVolumeAlreadyDefined{
    60  		containerPath: containerPath,
    61  	}
    62  }
    63  
    64  type pathList map[string]bool
    65  
    66  func (m pathList) Add(containerPath string) error {
    67  	if m[containerPath] {
    68  		return NewErrVolumeAlreadyDefined(containerPath)
    69  	}
    70  
    71  	m[containerPath] = true
    72  
    73  	return nil
    74  }