-
Notifications
You must be signed in to change notification settings - Fork 18
/
format.go
168 lines (143 loc) · 4.91 KB
/
format.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package ctf
import (
"sort"
"strings"
"sync"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/accessobj"
"github.com/open-component-model/ocm/pkg/contexts/oci/cpi"
"github.com/open-component-model/ocm/pkg/contexts/oci/repositories/ctf/format"
"github.com/open-component-model/ocm/pkg/errors"
)
const (
ArtifactIndexFileName = format.ArtifactIndexFileName
BlobsDirectoryName = format.BlobsDirectoryName
)
var accessObjectInfo = &accessobj.DefaultAccessObjectInfo{
DescriptorFileName: ArtifactIndexFileName,
ObjectTypeName: "repository",
ElementDirectoryName: BlobsDirectoryName,
ElementTypeName: "blob",
DescriptorHandlerFactory: NewStateHandler,
}
type Object = Repository
type FormatHandler interface {
accessio.Option
Format() accessio.FileFormat
Open(ctx cpi.Context, acc accessobj.AccessMode, path string, opts accessio.Options) (*Object, error)
Create(ctx cpi.Context, path string, opts accessio.Options, mode vfs.FileMode) (*Object, error)
Write(obj *Object, path string, opts accessio.Options, mode vfs.FileMode) error
}
type formatHandler struct {
accessobj.FormatHandler
}
var (
FormatDirectory = RegisterFormat(accessobj.FormatDirectory)
FormatTAR = RegisterFormat(accessobj.FormatTAR)
FormatTGZ = RegisterFormat(accessobj.FormatTGZ)
)
////////////////////////////////////////////////////////////////////////////////
var (
fileFormats = map[accessio.FileFormat]FormatHandler{}
lock sync.RWMutex
)
func RegisterFormat(f accessobj.FormatHandler) FormatHandler {
lock.Lock()
defer lock.Unlock()
h := &formatHandler{f}
fileFormats[f.Format()] = h
return h
}
func GetFormats() []string {
lock.RLock()
defer lock.RUnlock()
return accessio.GetFormatsFor(fileFormats)
}
func GetFormat(name accessio.FileFormat) FormatHandler {
lock.RLock()
defer lock.RUnlock()
return fileFormats[name]
}
func SupportedFormats() []accessio.FileFormat {
lock.RLock()
defer lock.RUnlock()
result := make([]accessio.FileFormat, 0, len(fileFormats))
for f := range fileFormats {
result = append(result, f)
}
sort.Slice(result, func(i, j int) bool { return strings.Compare(string(result[i]), string(result[j])) < 0 })
return result
}
////////////////////////////////////////////////////////////////////////////////
func OpenFromBlob(ctx cpi.Context, acc accessobj.AccessMode, blob accessio.BlobAccess, opts ...accessio.Option) (*Object, error) {
o, err := accessio.AccessOptions(nil, opts...)
if err != nil {
return nil, err
}
if o.GetFile() != nil || o.GetReader() != nil {
return nil, errors.ErrInvalid("file or reader option nor possible for blob access")
}
reader, err := blob.Reader()
if err != nil {
return nil, err
}
defer reader.Close()
o.SetReader(reader)
fmt := accessio.FormatTar
mime := blob.MimeType()
if strings.HasSuffix(mime, "+gzip") {
fmt = accessio.FormatTGZ
}
o.SetFileFormat(fmt)
return Open(ctx, acc&accessobj.ACC_READONLY, "", 0, o)
}
func Open(ctx cpi.Context, acc accessobj.AccessMode, path string, mode vfs.FileMode, opts ...accessio.Option) (*Object, error) {
o, create, err := accessobj.HandleAccessMode(acc, path, nil, opts...)
if err != nil {
return nil, err
}
h, ok := fileFormats[*o.GetFileFormat()]
if !ok {
return nil, errors.ErrUnknown(accessobj.KIND_FILEFORMAT, o.GetFileFormat().String())
}
if create {
return h.Create(ctx, path, o, mode)
}
return h.Open(ctx, acc, path, o)
}
func Create(ctx cpi.Context, acc accessobj.AccessMode, path string, mode vfs.FileMode, opts ...accessio.Option) (*Object, error) {
o, err := accessio.AccessOptions(nil, opts...)
if err != nil {
return nil, err
}
o.DefaultFormat(accessio.FormatDirectory)
h, ok := fileFormats[*o.GetFileFormat()]
if !ok {
return nil, errors.ErrUnknown(accessobj.KIND_FILEFORMAT, o.GetFileFormat().String())
}
return h.Create(ctx, path, o, mode)
}
func (h *formatHandler) Open(ctx cpi.Context, acc accessobj.AccessMode, path string, opts accessio.Options) (*Object, error) {
obj, err := h.FormatHandler.Open(accessObjectInfo, acc, path, opts)
if err != nil {
return nil, err
}
spec, err := NewRepositorySpec(acc, path, opts)
return _Wrap(ctx, spec, obj, err)
}
func (h *formatHandler) Create(ctx cpi.Context, path string, opts accessio.Options, mode vfs.FileMode) (*Object, error) {
obj, err := h.FormatHandler.Create(accessObjectInfo, path, opts, mode)
if err != nil {
return nil, err
}
spec, err := NewRepositorySpec(accessobj.ACC_CREATE, path, opts)
return _Wrap(ctx, spec, obj, err)
}
// WriteToFilesystem writes the current object to a filesystem.
func (h *formatHandler) Write(obj *Object, path string, opts accessio.Options, mode vfs.FileMode) error {
return h.FormatHandler.Write(obj.base.Access(), path, opts, mode)
}