podman

Форк
0
79 строк · 2.1 Кб
1
package parse
2

3
import (
4
	"fmt"
5
	"path/filepath"
6
	"strings"
7

8
	"github.com/containers/common/pkg/parse"
9
	"github.com/containers/storage/pkg/fileutils"
10
	specs "github.com/opencontainers/runtime-spec/specs-go"
11
)
12

13
// ValidateVolumeMountHostDir validates the host path of buildah --volume
14
func ValidateVolumeMountHostDir(hostDir string) error {
15
	if !filepath.IsAbs(hostDir) {
16
		return fmt.Errorf("invalid host path, must be an absolute path %q", hostDir)
17
	}
18
	if err := fileutils.Exists(hostDir); err != nil {
19
		return err
20
	}
21
	return nil
22
}
23

24
// RevertEscapedColon converts "\:" to ":"
25
func RevertEscapedColon(source string) string {
26
	return strings.ReplaceAll(source, "\\:", ":")
27
}
28

29
// SplitStringWithColonEscape splits string into slice by colon. Backslash-escaped colon (i.e. "\:") will not be regarded as separator
30
func SplitStringWithColonEscape(str string) []string {
31
	result := make([]string, 0, 3)
32
	sb := &strings.Builder{}
33
	for idx, r := range str {
34
		if r == ':' {
35
			// the colon is backslash-escaped
36
			if idx-1 > 0 && str[idx-1] == '\\' {
37
				sb.WriteRune(r)
38
			} else {
39
				// os.Stat will fail if path contains escaped colon
40
				result = append(result, RevertEscapedColon(sb.String()))
41
				sb.Reset()
42
			}
43
		} else {
44
			sb.WriteRune(r)
45
		}
46
	}
47
	if sb.Len() > 0 {
48
		result = append(result, RevertEscapedColon(sb.String()))
49
	}
50
	return result
51
}
52

53
// Volume parses the input of --volume
54
func Volume(volume string) (specs.Mount, error) {
55
	mount := specs.Mount{}
56
	arr := SplitStringWithColonEscape(volume)
57
	if len(arr) < 2 {
58
		return mount, fmt.Errorf("incorrect volume format %q, should be host-dir:ctr-dir[:option]", volume)
59
	}
60
	if err := ValidateVolumeMountHostDir(arr[0]); err != nil {
61
		return mount, err
62
	}
63
	if err := parse.ValidateVolumeCtrDir(arr[1]); err != nil {
64
		return mount, err
65
	}
66
	mountOptions := ""
67
	if len(arr) > 2 {
68
		mountOptions = arr[2]
69
		if _, err := parse.ValidateVolumeOpts(strings.Split(arr[2], ",")); err != nil {
70
			return mount, err
71
		}
72
	}
73
	mountOpts := strings.Split(mountOptions, ",")
74
	mount.Source = arr[0]
75
	mount.Destination = arr[1]
76
	mount.Type = "rbind"
77
	mount.Options = mountOpts
78
	return mount, nil
79
}
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.