File: tarball_transport.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (75 lines) | stat: -rw-r--r-- 1,892 bytes parent folder | download | duplicates (3)
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
package tarball

import (
	"errors"
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/containers/image/v5/transports"
	"github.com/containers/image/v5/types"
)

const (
	transportName = "tarball"
	separator     = ":"
)

var (
	// Transport implements the types.ImageTransport interface for "tarball:" images,
	// which are makeshift images constructed using one or more possibly-compressed tar
	// archives.
	Transport = &tarballTransport{}
)

type tarballTransport struct {
}

func (t *tarballTransport) Name() string {
	return transportName
}

func (t *tarballTransport) ParseReference(reference string) (types.ImageReference, error) {
	var stdin []byte
	var err error
	filenames := strings.Split(reference, separator)
	for _, filename := range filenames {
		if filename == "-" {
			stdin, err = io.ReadAll(os.Stdin)
			if err != nil {
				return nil, fmt.Errorf("error buffering stdin: %v", err)
			}
			continue
		}
		f, err := os.Open(filename)
		if err != nil {
			return nil, fmt.Errorf("error opening %q: %v", filename, err)
		}
		f.Close()
	}
	return NewReference(filenames, stdin)
}

// NewReference creates a new "tarball:" reference for the listed fileNames.
// If any of the fileNames is "-", the contents of stdin are used instead.
func NewReference(fileNames []string, stdin []byte) (types.ImageReference, error) {
	for _, path := range fileNames {
		if strings.Contains(path, separator) {
			return nil, fmt.Errorf("Invalid path %q: paths including the separator %q are not supported", path, separator)
		}
	}
	return &tarballReference{
		filenames: fileNames,
		stdin:     stdin,
	}, nil
}

func (t *tarballTransport) ValidatePolicyConfigurationScope(scope string) error {
	// See the explanation in daemonReference.PolicyConfigurationIdentity.
	return errors.New(`tarball: does not support any scopes except the default "" one`)
}

func init() {
	transports.Register(Transport)
}