podman
104 строки · 3.2 Кб
1package util
2
3import (
4"fmt"
5"io"
6"os"
7"path/filepath"
8
9"github.com/containers/buildah/define"
10"github.com/containers/common/libimage"
11lplatform "github.com/containers/common/libimage/platform"
12"github.com/containers/image/v5/types"
13"github.com/containers/storage"
14"github.com/containers/storage/pkg/archive"
15"github.com/containers/storage/pkg/chrootarchive"
16"github.com/containers/storage/pkg/unshare"
17v1 "github.com/opencontainers/image-spec/specs-go/v1"
18)
19
20// LookupImage returns *Image to corresponding imagename or id
21func LookupImage(ctx *types.SystemContext, store storage.Store, image string) (*libimage.Image, error) {
22systemContext := ctx
23if systemContext == nil {
24systemContext = &types.SystemContext{}
25}
26runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext})
27if err != nil {
28return nil, err
29}
30localImage, _, err := runtime.LookupImage(image, nil)
31if err != nil {
32return nil, err
33}
34return localImage, nil
35}
36
37// NormalizePlatform validates and translate the platform to the canonical value.
38//
39// For example, if "Aarch64" is encountered, we change it to "arm64" or if
40// "x86_64" is encountered, it becomes "amd64".
41//
42// Wrapper around libimage.NormalizePlatform to return and consume
43// v1.Platform instead of independent os, arch and variant.
44func NormalizePlatform(platform v1.Platform) v1.Platform {
45os, arch, variant := lplatform.Normalize(platform.OS, platform.Architecture, platform.Variant)
46return v1.Platform{
47OS: os,
48Architecture: arch,
49Variant: variant,
50}
51}
52
53// ExportFromReader reads bytes from given reader and exports to external tar, directory or stdout.
54func ExportFromReader(input io.Reader, opts define.BuildOutputOption) error {
55var err error
56if !filepath.IsAbs(opts.Path) {
57opts.Path, err = filepath.Abs(opts.Path)
58if err != nil {
59return err
60}
61}
62if opts.IsDir {
63// In order to keep this feature as close as possible to
64// buildkit it was decided to preserve ownership when
65// invoked as root since caller already has access to artifacts
66// therefore we can preserve ownership as is, however for rootless users
67// ownership has to be changed so exported artifacts can still
68// be accessible by unprivileged users.
69// See: https://github.com/containers/buildah/pull/3823#discussion_r829376633
70noLChown := false
71if unshare.IsRootless() {
72noLChown = true
73}
74
75err = os.MkdirAll(opts.Path, 0700)
76if err != nil {
77return fmt.Errorf("failed while creating the destination path %q: %w", opts.Path, err)
78}
79
80err = chrootarchive.Untar(input, opts.Path, &archive.TarOptions{NoLchown: noLChown})
81if err != nil {
82return fmt.Errorf("failed while performing untar at %q: %w", opts.Path, err)
83}
84} else {
85outFile := os.Stdout
86if !opts.IsStdout {
87outFile, err = os.Create(opts.Path)
88if err != nil {
89return fmt.Errorf("failed while creating destination tar at %q: %w", opts.Path, err)
90}
91defer outFile.Close()
92}
93_, err = io.Copy(outFile, input)
94if err != nil {
95return fmt.Errorf("failed while performing copy to %q: %w", opts.Path, err)
96}
97}
98return nil
99}
100
101func SetHas(m map[string]struct{}, k string) bool {
102_, ok := m[k]
103return ok
104}
105