File: util.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (111 lines) | stat: -rw-r--r-- 2,626 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package imagebuildah

import (
	"archive/tar"
	"io"
	"os"
	"path/filepath"
	"strings"

	"github.com/containers/buildah"
	digest "github.com/opencontainers/go-digest"
)

type mountInfo struct {
	Type   string
	Source string
	From   string
}

// Consumes mount flag in format of `--mount=type=bind,src=/path,from=image` and
// return mountInfo with values, otherwise values are empty if keys are not present in the option.
func getFromAndSourceKeysFromMountFlag(mount string) mountInfo {
	tokens := strings.Split(strings.TrimPrefix(mount, "--mount="), ",")
	source := ""
	from := ""
	mountType := ""
	for _, option := range tokens {
		if optionSplit := strings.Split(option, "="); len(optionSplit) == 2 {
			if optionSplit[0] == "src" || optionSplit[0] == "source" {
				source = optionSplit[1]
			}
			if optionSplit[0] == "from" {
				from = optionSplit[1]
			}
			if optionSplit[0] == "type" {
				mountType = optionSplit[1]
			}
		}
	}
	return mountInfo{Source: source, From: from, Type: mountType}
}

// generatePathChecksum generates the SHA-256 checksum for a file or a directory.
func generatePathChecksum(sourcePath string) (string, error) {
	digester := digest.SHA256.Digester()
	tarWriter := tar.NewWriter(digester.Hash())

	err := filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		var linkTarget string
		if info.Mode()&os.ModeSymlink != 0 {
			// If the file is a symlink, get the target
			linkTarget, err = os.Readlink(path)
			if err != nil {
				return err
			}
		}

		header, err := tar.FileInfoHeader(info, linkTarget)
		if err != nil {
			return err
		}

		relPath, err := filepath.Rel(sourcePath, path)
		if err != nil {
			return err
		}
		header.Name = filepath.ToSlash(relPath)

		if err := tarWriter.WriteHeader(header); err != nil {
			return err
		}

		if !info.Mode().IsRegular() {
			return nil
		}

		file, err := os.Open(path)
		if err != nil {
			return err
		}
		defer file.Close()

		_, err = io.Copy(tarWriter, file)
		return err
	})
	tarWriter.Close()
	if err != nil {
		return "", err
	}
	return digester.Digest().String(), nil
}

// InitReexec is a wrapper for buildah.InitReexec().  It should be called at
// the start of main(), and if it returns true, main() should return
// successfully immediately.
func InitReexec() bool {
	return buildah.InitReexec()
}

// argsMapToSlice returns the contents of a map[string]string as a slice of keys
// and values joined with "=".
func argsMapToSlice(m map[string]string) []string {
	s := make([]string, 0, len(m))
	for k, v := range m {
		s = append(s, k+"="+v)
	}
	return s
}