Debian Codegraph @

content/storage.go

raw
1 /*
2 Copyright The ORAS Authors.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 package content
17
18 import (
19 "context"
20 "io"
21
22 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
23 )
24
25 // Fetcher fetches content.
26 type Fetcher interface {
27 // Fetch fetches the content identified by the descriptor.
28 Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error)
29 }
30
31 // Pusher pushes content.
32 type Pusher interface {
33 // Push pushes the content, matching the expected descriptor.
34 // Reader is preferred to Writer so that the suitable buffer size can be
35 // chosen by the underlying implementation. Furthermore, the implementation
36 // can also do reflection on the Reader for more advanced I/O optimization.
37 Push(ctx context.Context, expected ocispec.Descriptor, content io.Reader) error
38 }
39
40 // Storage represents a content-addressable storage (CAS) where contents are
41 // accessed via Descriptors.
42 // The storage is designed to handle blobs of large sizes.
43 type Storage interface {
44 ReadOnlyStorage
45 Pusher
46 }
47
48 // ReadOnlyStorage represents a read-only Storage.
49 type ReadOnlyStorage interface {
50 Fetcher
51
52 // Exists returns true if the described content exists.
53 Exists(ctx context.Context, target ocispec.Descriptor) (bool, error)
54 }
55
56 // Deleter removes content.
57 // Deleter is an extension of Storage.
58 type Deleter interface {
59 // Delete removes the content identified by the descriptor.
60 Delete(ctx context.Context, target ocispec.Descriptor) error
61 }
62
63 // FetchAll safely fetches the content described by the descriptor.
64 // The fetched content is verified against the size and the digest.
65 func FetchAll(ctx context.Context, fetcher Fetcher, desc ocispec.Descriptor) ([]byte, error) {
66 rc, err := fetcher.Fetch(ctx, desc)
67 if err != nil {
68 return nil, err
69 }
70 defer rc.Close()
71 return ReadAll(rc, desc)
72 }
73
74 // FetcherFunc is the basic Fetch method defined in Fetcher.
75 type FetcherFunc func(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error)
76
77 // Fetch performs Fetch operation by the FetcherFunc.
78 func (fn FetcherFunc) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) {
79 return fn(ctx, target)
80 }