-
Notifications
You must be signed in to change notification settings - Fork 23
/
access.go
783 lines (643 loc) · 16 KB
/
access.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package accessio
import (
"bytes"
"fmt"
"io"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/opencontainers/go-digest"
"github.com/open-component-model/ocm/pkg/errors"
)
var (
ErrClosed = errors.ErrClosed()
ErrReadOnly = errors.ErrReadOnly()
)
const (
KIND_BLOB = "blob"
KIND_MEDIATYPE = "media type"
)
func ErrBlobNotFound(digest digest.Digest) error {
return errors.ErrNotFound(KIND_BLOB, digest.String())
}
func IsErrBlobNotFound(err error) bool {
return errors.IsErrNotFoundKind(err, KIND_BLOB)
}
type DataGetter interface {
// Get returns the content as byte array
Get() ([]byte, error)
}
type DataReader interface {
// Reader returns a reader to incrementally access byte stream content
Reader() (io.ReadCloser, error)
}
////////////////////////////////////////////////////////////////////////////////
// DataSource describes some data plus its origin.
type DataSource interface {
DataAccess
Origin() string
}
////////////////////////////////////////////////////////////////////////////////
// DataAccess describes the access to sequence of bytes.
type DataAccess interface {
DataGetter
DataReader
io.Closer
}
type MimeType interface {
// MimeType returns the mime type of the blob
MimeType() string
}
// BlobAccess describes the access to a blob.
type BlobAccess interface {
DataAccess
DigestSource
MimeType
// DigestKnown reports whether digest is already known
DigestKnown() bool
// Size returns the blob size
Size() int64
}
// _blobAccess is to be used for private embedded fields.
type _blobAccess = BlobAccess
// TemporaryBlobAccess describes a blob with temporary allocated external resources.
// They will be releases, when the close method is called.
type TemporaryBlobAccess interface {
_blobAccess
IsValid() bool
}
// _temporaryBlobAccess is to be used for private embedded fields.
type _temporaryBlobAccess = TemporaryBlobAccess
type temporaryBlob struct {
_blobAccess
}
// TemporaryBlobAccessForBlob returns a temporary blob for any blob, which gets
// invalidated whenever closed.
func TemporaryBlobAccessForBlob(blob BlobAccess) TemporaryBlobAccess {
return &temporaryBlob{blob}
}
func (b *temporaryBlob) IsValid() bool {
return b._blobAccess != nil
}
func (b *temporaryBlob) Close() error {
if b._blobAccess == nil {
return errors.ErrInvalid("blob access")
}
err := b._blobAccess.Close()
b._blobAccess = nil
return err
}
type DigestSource interface {
// Digest returns the blob digest
Digest() digest.Digest
}
////////////////////////////////////////////////////////////////////////////////
type NopCloser struct{}
type _nopCloser = NopCloser
func (NopCloser) Close() error {
return nil
}
////////////////////////////////////////////////////////////////////////////////
type readerAccess struct {
_nopCloser
reader func() (io.ReadCloser, error)
origin string
}
var _ DataSource = (*readerAccess)(nil)
func DataAccessForReaderFunction(reader func() (io.ReadCloser, error), origin string) DataAccess {
return &readerAccess{reader: reader, origin: origin}
}
func (a *readerAccess) Get() (data []byte, err error) {
r, err := a.Reader()
if err != nil {
return nil, err
}
defer errors.PropagateError(&err, r.Close)
buf := bytes.Buffer{}
_, err = io.Copy(&buf, r)
if err != nil {
return nil, errors.Wrapf(err, "cannot read %s", a.origin)
}
return buf.Bytes(), nil
}
func (a *readerAccess) Reader() (io.ReadCloser, error) {
r, err := a.reader()
if err != nil {
return nil, errors.Wrapf(err, "errors getting reader for %s", a.origin)
}
return r, nil
}
func (a *readerAccess) Origin() string {
return a.origin
}
////////////////////////////////////////////////////////////////////////////////
type dataAccess struct {
_nopCloser
fs vfs.FileSystem
path string
}
var _ DataSource = (*dataAccess)(nil)
func DataAccessForFile(fs vfs.FileSystem, path string) DataAccess {
return &dataAccess{fs: fs, path: path}
}
func (a *dataAccess) Get() ([]byte, error) {
data, err := vfs.ReadFile(a.fs, a.path)
if err != nil {
return nil, errors.Wrapf(err, "file %q", a.path)
}
return data, nil
}
func (a *dataAccess) Reader() (io.ReadCloser, error) {
file, err := a.fs.Open(a.path)
if err != nil {
return nil, errors.Wrapf(err, "file %q", a.path)
}
return file, nil
}
func (a *dataAccess) Origin() string {
return a.path
}
////////////////////////////////////////////////////////////////////////////////
type bytesAccess struct {
_nopCloser
data []byte
origin string
}
func DataAccessForBytes(data []byte, origin ...string) DataSource {
path := ""
if len(origin) > 0 {
path = filepath.Join(origin...)
}
return &bytesAccess{data: data, origin: path}
}
func DataAccessForString(data string, origin ...string) DataSource {
return DataAccessForBytes([]byte(data), origin...)
}
func (a *bytesAccess) Get() ([]byte, error) {
return a.data, nil
}
func (a *bytesAccess) Reader() (io.ReadCloser, error) {
return ReadCloser(bytes.NewReader(a.data)), nil
}
func (a *bytesAccess) Origin() string {
return a.origin
}
////////////////////////////////////////////////////////////////////////////////
// AnnotatedBlobAccess provides access to the original underlying data source.
type AnnotatedBlobAccess[T DataAccess] interface {
_blobAccess
Source() T
}
type blobAccess[T DataAccess] struct {
lock sync.RWMutex
digest digest.Digest
size int64
mimeType string
closed atomic.Bool
access T
}
const (
BLOB_UNKNOWN_SIZE = int64(-1)
BLOB_UNKNOWN_DIGEST = digest.Digest("")
)
// BlobAccessForDataAccess wraps the general access object into a blob access.
// It closes the wrapped access, if closed.
func BlobAccessForDataAccess[T DataAccess](digest digest.Digest, size int64, mimeType string, access T) AnnotatedBlobAccess[T] {
return &blobAccess[T]{
digest: digest,
size: size,
mimeType: mimeType,
access: access,
}
}
func BlobAccessForString(mimeType string, data string) BlobAccess {
return BlobAccessForData(mimeType, []byte(data))
}
func BlobAccessForData(mimeType string, data []byte) BlobAccess {
return &blobAccess[DataAccess]{
digest: digest.FromBytes(data),
size: int64(len(data)),
mimeType: mimeType,
access: DataAccessForBytes(data),
}
}
func (b *blobAccess[T]) Close() error {
b.lock.Lock()
defer b.lock.Unlock()
if !b.closed.Load() {
tmp := b.access
b.closed.Store(true)
return tmp.Close()
}
return ErrClosed
}
func (b *blobAccess[T]) Get() ([]byte, error) {
if b.closed.Load() {
return nil, ErrClosed
}
return b.access.Get()
}
func (b *blobAccess[T]) Reader() (io.ReadCloser, error) {
if b.closed.Load() {
return nil, ErrClosed
}
return b.access.Reader()
}
func (b *blobAccess[T]) Source() T {
return b.access
}
func (b *blobAccess[T]) MimeType() string {
return b.mimeType
}
func (b *blobAccess[T]) DigestKnown() bool {
b.lock.RLock()
defer b.lock.RUnlock()
return b.digest != ""
}
func (b *blobAccess[T]) Digest() digest.Digest {
b.lock.Lock()
defer b.lock.Unlock()
if b.digest == "" {
b.update()
}
return b.digest
}
func (b *blobAccess[T]) Size() int64 {
b.lock.Lock()
defer b.lock.Unlock()
if b.size < 0 {
b.update()
}
return b.size
}
func (b *blobAccess[T]) update() error {
reader, err := b.Reader()
if err != nil {
return err
}
defer reader.Close()
count := NewCountingReader(reader)
digest, err := digest.Canonical.FromReader(count)
if err != nil {
return err
}
b.size = count.Size()
b.digest = digest
return nil
}
////////////////////////////////////////////////////////////////////////////////
type mimeBlob struct {
_blobAccess
mimetype string
}
func BlobWithMimeType(mimeType string, blob BlobAccess) BlobAccess {
return &mimeBlob{blob, mimeType}
}
func (b *mimeBlob) MimeType() string {
return b.mimetype
}
////////////////////////////////////////////////////////////////////////////////
type fileBlobAccess struct {
dataAccess
mimeType string
}
var _ BlobAccess = (*fileBlobAccess)(nil)
func BlobAccessForFile(mimeType string, path string, fss ...vfs.FileSystem) BlobAccess {
return &fileBlobAccess{
mimeType: mimeType,
dataAccess: dataAccess{fs: FileSystem(fss...), path: path},
}
}
func (f *fileBlobAccess) Size() int64 {
size := BLOB_UNKNOWN_SIZE
fi, err := f.fs.Stat(f.path)
if err == nil {
size = fi.Size()
}
return size
}
func (f *fileBlobAccess) MimeType() string {
return f.mimeType
}
func (f *fileBlobAccess) DigestKnown() bool {
return false
}
func (f *fileBlobAccess) Digest() digest.Digest {
r, err := f.Reader()
if err != nil {
return ""
}
defer r.Close()
d, err := digest.FromReader(r)
if err != nil {
return ""
}
return d
}
////////////////////////////////////////////////////////////////////////////////
type blobNopCloser struct {
_blobAccess
}
func BlobNopCloser(blob BlobAccess) BlobAccess {
return &blobNopCloser{blob}
}
func (b *blobNopCloser) Close() error {
return nil
}
////////////////////////////////////////////////////////////////////////////////
type MultiViewBlobAccess struct {
refs ReferencableCloser
access BlobAccess
}
func NewMultiViewBlobAccess(acc BlobAccess) *MultiViewBlobAccess {
return &MultiViewBlobAccess{
refs: NewRefCloser(acc, true),
access: acc,
}
}
func (m *MultiViewBlobAccess) View() (BlobAccess, error) {
v, err := m.refs.View(false)
if err != nil {
return nil, err
}
return &blobAccessView{v, m.access}, nil
}
type blobAccessView struct {
view CloserView
access BlobAccess
}
func (b *blobAccessView) Close() error {
return b.view.Close()
}
func (b *blobAccessView) IsClosed() bool {
return b.view.IsClosed()
}
func (b *blobAccessView) Get() (result []byte, err error) {
return result, b.view.Execute(func() error {
result, err = b.access.Get()
if err != nil {
return fmt.Errorf("unable to get access: %w", err)
}
return nil
})
}
func (b *blobAccessView) Reader() (result io.ReadCloser, err error) {
return result, b.view.Execute(func() error {
result, err = b.access.Reader()
if err != nil {
return fmt.Errorf("unable to read access: %w", err)
}
return nil
})
}
func (b *blobAccessView) Digest() (result digest.Digest) {
err := b.view.Execute(func() error {
result = b.access.Digest()
return nil
})
if err != nil {
return BLOB_UNKNOWN_DIGEST
}
return
}
func (b *blobAccessView) MimeType() string {
return b.access.MimeType()
}
func (b *blobAccessView) DigestKnown() bool {
return b.access.DigestKnown()
}
func (b *blobAccessView) Size() (result int64) {
err := b.view.Execute(func() error {
result = b.access.Size()
return nil
})
if err != nil {
return BLOB_UNKNOWN_SIZE
}
return
}
////////////////////////////////////////////////////////////////////////////////
type temporaryBlobAccess struct {
_blobAccess
}
func TemporaryBlobAccessFor(blob BlobAccess) TemporaryBlobAccess {
if t, ok := blob.(TemporaryBlobAccess); ok {
return t
}
return &temporaryBlobAccess{blob}
}
func (b *temporaryBlobAccess) IsValid() bool {
return true
}
////////////////////////////////////////////////////////////////////////////////
type TemporaryFileSystemBlobAccess interface {
_temporaryBlobAccess
FileSystem() vfs.FileSystem
Path() string
}
type temporaryFileBlob struct {
_blobAccess
lock sync.Mutex
temp vfs.File
filesystem vfs.FileSystem
}
func TempFileBlobAccess(mime string, fs vfs.FileSystem, temp vfs.File) TemporaryFileSystemBlobAccess {
return &temporaryFileBlob{
_blobAccess: BlobAccessForFile(mime, temp.Name(), fs),
filesystem: fs,
temp: temp,
}
}
func (b *temporaryFileBlob) IsValid() bool {
b.lock.Lock()
defer b.lock.Unlock()
return b.temp != nil
}
func (b *temporaryFileBlob) Close() error {
b.lock.Lock()
defer b.lock.Unlock()
if b.temp != nil {
list := errors.ErrListf("temporary blob")
list.Add(b.temp.Close())
list.Add(b.filesystem.Remove(b.temp.Name()))
b.temp = nil
b._blobAccess = nil
return list.Result()
}
return nil
}
func (b *temporaryFileBlob) FileSystem() vfs.FileSystem {
return b.filesystem
}
func (b *temporaryFileBlob) Path() string {
return b.temp.Name()
}
// TempFile holds a temporary file that should be kept open.
// Close should never be called directly.
// It can be passed to another responsibility realm by calling Release
// For example to be transformed into a TemporaryBlobAccess.
// Close will close and remove an unreleased file and does
// nothing if it has been released.
// If it has been releases the new realm is responsible.
// to close and remove it.
type TempFile struct {
lock sync.Mutex
temp vfs.File
filesystem vfs.FileSystem
}
func NewTempFile(fs vfs.FileSystem, dir string, pattern string) (*TempFile, error) {
temp, err := vfs.TempFile(fs, dir, pattern)
if err != nil {
return nil, err
}
return &TempFile{
temp: temp,
filesystem: fs,
}, nil
}
func (t *TempFile) Name() string {
t.lock.Lock()
defer t.lock.Unlock()
return t.temp.Name()
}
func (t *TempFile) FileSystem() vfs.FileSystem {
t.lock.Lock()
defer t.lock.Unlock()
return t.filesystem
}
func (t *TempFile) Release() vfs.File {
t.lock.Lock()
defer t.lock.Unlock()
if t.temp != nil {
t.temp.Sync()
}
tmp := t.temp
t.temp = nil
return tmp
}
func (t *TempFile) Writer() io.Writer {
t.lock.Lock()
defer t.lock.Unlock()
return t.temp
}
func (t *TempFile) Sync() error {
t.lock.Lock()
defer t.lock.Unlock()
return t.temp.Sync()
}
func (t *TempFile) AsBlob(mime string) TemporaryFileSystemBlobAccess {
return TempFileBlobAccess(mime, t.filesystem, t.Release())
}
func (t *TempFile) Close() error {
t.lock.Lock()
defer t.lock.Unlock()
if t.temp != nil {
name := t.temp.Name()
t.temp.Close()
t.temp = nil
return t.filesystem.Remove(name)
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
type referencingBlobAccess struct {
lock sync.Mutex
closed bool
closer func() error
_blobAccess
}
func ReferencingBlobAccess(b BlobAccess, closer func() error) TemporaryBlobAccess {
return &referencingBlobAccess{closer: closer, _blobAccess: b}
}
func (r *referencingBlobAccess) IsValid() bool {
r.lock.Lock()
defer r.lock.Unlock()
return !r.closed
}
func (r *referencingBlobAccess) Close() error {
r.lock.Lock()
defer r.lock.Unlock()
if r.closed {
return ErrClosed
}
if r.closer != nil {
if err := r.closer(); err != nil {
return err
}
}
r.closed = true
return r._blobAccess.Close()
}
////////////////////////////////////////////////////////////////////////////////
type retriableError struct {
wrapped error
}
func IsRetriableError(err error) bool {
if err == nil {
return false
}
return errors.IsA(err, &retriableError{})
}
func RetriableError(err error) error {
if err == nil {
return nil
}
return &retriableError{err}
}
func RetriableError1[T any](r T, err error) (T, error) {
if err == nil {
return r, nil
}
return r, &retriableError{err}
}
func RetriableError2[S, T any](s S, r T, err error) (S, T, error) {
if err == nil {
return s, r, nil
}
return s, r, &retriableError{err}
}
func (e *retriableError) Error() string {
return e.wrapped.Error()
}
func (e *retriableError) Unwrap() error {
return e.wrapped
}
func Retry(cnt int, d time.Duration, f func() error) error {
for {
err := f()
if err == nil || cnt <= 0 || !IsRetriableError(err) {
return err
}
jitter := time.Duration(rand.Int63n(int64(d))) //nolint: gosec // just an random number
d = 2*d + (d/2-jitter)/10
cnt--
}
}
func Retry1[T any](cnt int, d time.Duration, f func() (T, error)) (T, error) {
for {
r, err := f()
if err == nil || cnt <= 0 || !IsRetriableError(err) {
return r, err
}
jitter := time.Duration(rand.Int63n(int64(d))) //nolint: gosec // just an random number
d = 2*d + (d/2-jitter)/10
cnt--
}
}
func Retry2[S, T any](cnt int, d time.Duration, f func() (S, T, error)) (S, T, error) {
for {
s, t, err := f()
if err == nil || cnt <= 0 || !IsRetriableError(err) {
return s, t, err
}
jitter := time.Duration(rand.Int63n(int64(d))) //nolint: gosec // just an random number
d = 2*d + (d/2-jitter)/10
cnt--
}
}