-
Notifications
You must be signed in to change notification settings - Fork 23
/
repository.go
190 lines (161 loc) · 4.44 KB
/
repository.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package comparch
import (
"strings"
"sync"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/attrs/vfsattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/errors"
)
type Repository struct {
lock sync.RWMutex
ctx cpi.Context
spec *RepositorySpec
arch *ComponentArchive
}
var _ cpi.Repository = (*Repository)(nil)
func NewRepository(ctx cpi.Context, s *RepositorySpec) (*Repository, error) {
if s.GetPathFileSystem() == nil {
s.SetPathFileSystem(vfsattr.Get(ctx))
}
a, err := Open(ctx, s.AccessMode, s.FilePath, 0o700, s)
if err != nil {
return nil, err
}
return a.comp.repo, nil
}
func (r *Repository) ComponentLister() cpi.ComponentLister {
return r
}
func (r *Repository) matchPrefix(prefix string, closure bool) bool {
if r.arch.GetName() != prefix {
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
if !closure || !strings.HasPrefix(r.arch.GetName(), prefix) {
return false
}
}
return true
}
func (r *Repository) NumComponents(prefix string) (int, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if r.arch == nil {
return -1, accessio.ErrClosed
}
if !r.matchPrefix(prefix, true) {
return 0, nil
}
return 1, nil
}
func (r *Repository) GetComponents(prefix string, closure bool) ([]string, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if r.arch == nil {
return nil, accessio.ErrClosed
}
if !r.matchPrefix(prefix, closure) {
return []string{}, nil
}
return []string{r.arch.GetName()}, nil
}
func (r *Repository) Get() *ComponentArchive {
r.lock.RLock()
defer r.lock.RUnlock()
if r.arch != nil {
return r.arch
}
return nil
}
func (r *Repository) Open() (*ComponentArchive, error) {
r.lock.Lock()
defer r.lock.Unlock()
if r.arch != nil {
return r.arch, nil
}
a, err := Open(r.ctx, r.spec.AccessMode, r.spec.FilePath, 0o700, r.spec)
if err != nil {
return nil, err
}
r.arch = a
return a, nil
}
func (r *Repository) GetContext() cpi.Context {
return r.ctx
}
func (r *Repository) GetSpecification() cpi.RepositorySpec {
return r.spec
}
func (r *Repository) ExistsComponentVersion(name string, ref string) (bool, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if r.arch == nil {
return false, accessio.ErrClosed
}
return r.arch.GetName() == name && r.arch.GetVersion() == ref, nil
}
func (r *Repository) LookupComponentVersion(name string, version string) (cpi.ComponentVersionAccess, error) {
r.lock.RLock()
defer r.lock.RUnlock()
ok, err := r.ExistsComponentVersion(name, version)
if ok {
return r.arch, nil
}
if err == nil {
err = errors.ErrNotFound(cpi.KIND_COMPONENTVERSION, common.NewNameVersion(name, version).String(), Type)
}
return nil, err
}
func (r *Repository) LookupComponent(name string) (cpi.ComponentAccess, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if r.arch == nil {
return nil, accessio.ErrClosed
}
if r.arch.GetName() != name {
return nil, errors.ErrNotFound(errors.KIND_COMPONENT, name, Type)
}
return r.arch.comp, nil
}
func (r *Repository) Close() error {
r.lock.Lock()
defer r.lock.Unlock()
if r.arch != nil {
r.arch.Close()
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
type ComponentAccess struct {
repo *Repository
}
var _ cpi.ComponentAccess = (*ComponentAccess)(nil)
func (c *ComponentAccess) GetContext() cpi.Context {
return c.repo.GetContext()
}
func (c *ComponentAccess) Close() error {
return nil
}
func (c *ComponentAccess) Dup() (cpi.ComponentAccess, error) {
return c, nil
}
func (c *ComponentAccess) GetName() string {
return c.repo.arch.GetName()
}
func (c *ComponentAccess) ListVersions() ([]string, error) {
return []string{c.repo.arch.GetVersion()}, nil
}
func (c *ComponentAccess) LookupVersion(ref string) (cpi.ComponentVersionAccess, error) {
return c.repo.LookupComponentVersion(c.repo.arch.GetName(), ref)
}
func (c *ComponentAccess) AddVersion(access cpi.ComponentVersionAccess) error {
return errors.ErrNotSupported(errors.KIND_FUNCTION, "add version", Type)
}
func (c *ComponentAccess) NewVersion(version string, overrides ...bool) (cpi.ComponentVersionAccess, error) {
return nil, errors.ErrNotSupported(errors.KIND_FUNCTION, "new version", Type)
}