-
Notifications
You must be signed in to change notification settings - Fork 23
/
chartaccess.go
135 lines (109 loc) · 2.71 KB
/
chartaccess.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package helm
import (
"fmt"
"io"
"os"
"sync"
"github.com/mandelsoft/vfs/pkg/vfs"
"helm.sh/helm/v3/pkg/registry"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/contexts/oci/artdesc"
"github.com/open-component-model/ocm/pkg/errors"
)
const (
ChartMediaType = registry.ChartLayerMediaType
ProvenanceMediaType = registry.ProvLayerMediaType
)
type ChartAccess interface {
io.Closer
Chart() (accessio.TemporaryBlobAccess, error)
Prov() (accessio.TemporaryBlobAccess, error)
ArtefactSet() (accessio.TemporaryBlobAccess, error)
}
func newFileAccess(c *chartAccess, path string, mime string) accessio.TemporaryBlobAccess {
c.refcnt++
return accessio.ReferencingBlobAccess(accessio.BlobAccessForFile(mime, path, c.fs), c.unref)
}
type chartAccess struct {
lock sync.Mutex
closed bool
refcnt int
fs vfs.FileSystem
root string
chart string
prov string
aset string
}
var _ ChartAccess = (*chartAccess)(nil)
func newTempChartAccess(fss ...vfs.FileSystem) (*chartAccess, error) {
fs := accessio.FileSystem(fss...)
temp, err := vfs.TempDir(fs, "", "helmchart")
if err != nil {
return nil, err
}
return &chartAccess{
fs: fs,
root: temp,
}, nil
}
func NewChartAccessByFiles(chart, prov string, fss ...vfs.FileSystem) ChartAccess {
return &chartAccess{
fs: accessio.FileSystem(fss...),
chart: chart,
prov: prov,
}
}
func (c *chartAccess) unref() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.refcnt == 0 {
return fmt.Errorf("oops: refcount is already zero")
}
c.refcnt--
return nil
}
func (c *chartAccess) Close() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.refcnt > 0 {
return errors.ErrStillInUse("chart access")
}
defer func() { c.closed = true }()
if c.root != "" && !c.closed {
return os.RemoveAll(c.root)
}
return nil
}
func (c *chartAccess) Chart() (accessio.TemporaryBlobAccess, error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return nil, accessio.ErrClosed
}
return newFileAccess(c, c.chart, ChartMediaType), nil
}
func (c *chartAccess) Prov() (accessio.TemporaryBlobAccess, error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return nil, accessio.ErrClosed
}
if c.prov == "" {
return nil, nil
}
return newFileAccess(c, c.prov, ProvenanceMediaType), nil
}
func (c *chartAccess) ArtefactSet() (accessio.TemporaryBlobAccess, error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return nil, accessio.ErrClosed
}
if c.aset == "" {
return nil, nil
}
return newFileAccess(c, c.aset, artdesc.MediaTypeImageManifest), nil
}