-
Notifications
You must be signed in to change notification settings - Fork 23
/
format.go
168 lines (134 loc) · 4.24 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 accessobj
import (
"fmt"
"io"
"sync"
"time"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/v2/pkg/common/accessio"
"github.com/open-component-model/ocm/v2/pkg/errors"
)
const KIND_FILEFORMAT = accessio.KIND_FILEFORMAT
const (
DirMode = 0o755
FileMode = 0o644
)
var ModTime = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
type FileFormat = accessio.FileFormat
type FormatHandler interface {
accessio.Option
Format() accessio.FileFormat
Open(info AccessObjectInfo, acc AccessMode, path string, opts accessio.Options) (*AccessObject, error)
Create(info AccessObjectInfo, path string, opts accessio.Options, mode vfs.FileMode) (*AccessObject, error)
Write(obj *AccessObject, path string, opts accessio.Options, mode vfs.FileMode) error
}
////////////////////////////////////////////////////////////////////////////////
var (
fileFormats = map[FileFormat]FormatHandler{}
lock sync.RWMutex
)
func RegisterFormat(f FormatHandler) {
lock.Lock()
defer lock.Unlock()
fileFormats[f.Format()] = f
}
func GetFormat(name FileFormat) FormatHandler {
lock.RLock()
defer lock.RUnlock()
return fileFormats[name]
}
func GetFormats() map[accessio.FileFormat]FormatHandler {
lock.RLock()
defer lock.RUnlock()
m := map[accessio.FileFormat]FormatHandler{}
for k, v := range fileFormats {
m[k] = v
}
return m
}
////////////////////////////////////////////////////////////////////////////////
type Closer interface {
Close(*AccessObject) error
}
type CloserFunction func(*AccessObject) error
func (f CloserFunction) Close(obj *AccessObject) error {
return f(obj)
}
////////////////////////////////////////////////////////////////////////////////
type Setup interface {
Setup(vfs.FileSystem) error
}
type SetupFunction func(vfs.FileSystem) error
func (f SetupFunction) Setup(fs vfs.FileSystem) error {
return f(fs)
}
////////////////////////////////////////////////////////////////////////////////
type fsCloser struct {
closer Closer
}
func FSCloser(closer Closer) Closer {
return &fsCloser{closer}
}
func (f fsCloser) Close(obj *AccessObject) error {
err := errors.ErrListf("cannot close %s", obj.info.GetObjectTypeName())
if f.closer != nil {
err.Add(f.closer.Close(obj))
}
err.Add(vfs.Cleanup(obj.fs))
return err.Result()
}
type StandardReaderHandler interface {
Write(obj *AccessObject, path string, opts accessio.Options, mode vfs.FileMode) error
NewFromReader(info AccessObjectInfo, acc AccessMode, in io.Reader, opts accessio.Options, closer Closer) (*AccessObject, error)
}
func DefaultOpenOptsFileHandling(kind string, info AccessObjectInfo, acc AccessMode, path string, opts accessio.Options, handler StandardReaderHandler) (*AccessObject, error) {
if err := opts.ValidForPath(path); err != nil {
return nil, err
}
var file vfs.File
var err error
var closer Closer
reader := opts.GetReader()
switch {
case reader != nil:
case opts.GetFile() == nil:
// we expect that the path point to a tar
file, err = opts.GetPathFileSystem().Open(path)
if err != nil {
return nil, fmt.Errorf("unable to open %s from %s: %w", kind, path, err)
}
defer file.Close()
default:
file = opts.GetFile()
}
if file != nil {
reader = file
fi, err := file.Stat()
if err != nil {
return nil, err
}
closer = CloserFunction(func(obj *AccessObject) error { return handler.Write(obj, path, opts, fi.Mode()) })
}
return handler.NewFromReader(info, acc, reader, opts, closer)
}
func DefaultCreateOptsFileHandling(kind string, info AccessObjectInfo, path string, opts accessio.Options, mode vfs.FileMode, handler StandardReaderHandler) (*AccessObject, error) {
if err := opts.ValidForPath(path); err != nil {
return nil, err
}
if opts.GetReader() != nil {
return nil, errors.ErrNotSupported("reader option not supported")
}
if opts.GetFile() == nil {
ok, err := vfs.Exists(opts.GetPathFileSystem(), path)
if err != nil {
return nil, err
}
if ok {
return nil, vfs.ErrExist
}
}
return NewAccessObject(info, ACC_CREATE, opts.GetRepresentation(), nil, CloserFunction(func(obj *AccessObject) error { return handler.Write(obj, path, opts, mode) }), DirMode)
}