-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
content.go
153 lines (123 loc) · 4.23 KB
/
content.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
package containerd
import (
"context"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/nydus-snapshotter/pkg/errdefs"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
func NewContentStore(store content.Store, ns string) *Store {
return &Store{ns, store}
}
type Store struct {
ns string
content.Store
}
func (c *Store) Namespace() string {
return c.ns
}
func (c *Store) WithNamespace(ns string) *Store {
return NewContentStore(c.Store, ns)
}
func (c *Store) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
ctx = namespaces.WithNamespace(ctx, c.ns)
return c.Store.Info(ctx, dgst)
}
func (c *Store) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
ctx = namespaces.WithNamespace(ctx, c.ns)
return c.Store.Update(ctx, info, fieldpaths...)
}
func (c *Store) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
ctx = namespaces.WithNamespace(ctx, c.ns)
return c.Store.Walk(ctx, fn, filters...)
}
func (c *Store) Delete(ctx context.Context, dgst digest.Digest) error {
return errors.Errorf("contentstore.Delete usage is forbidden")
}
func (c *Store) Status(ctx context.Context, ref string) (content.Status, error) {
ctx = namespaces.WithNamespace(ctx, c.ns)
return c.Store.Status(ctx, ref)
}
func (c *Store) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
ctx = namespaces.WithNamespace(ctx, c.ns)
return c.Store.ListStatuses(ctx, filters...)
}
func (c *Store) Abort(ctx context.Context, ref string) error {
ctx = namespaces.WithNamespace(ctx, c.ns)
return c.Store.Abort(ctx, ref)
}
func (c *Store) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
ctx = namespaces.WithNamespace(ctx, c.ns)
return c.Store.ReaderAt(ctx, desc)
}
func (c *Store) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
return c.writer(ctx, 3, opts...)
}
func (c *Store) WithFallbackNS(ns string) content.Store {
return &nsFallbackStore{
main: c,
fb: c.WithNamespace(ns),
}
}
func (c *Store) writer(ctx context.Context, retries int, opts ...content.WriterOpt) (content.Writer, error) {
ctx = namespaces.WithNamespace(ctx, c.ns)
w, err := c.Store.Writer(ctx, opts...)
if err != nil {
return nil, err
}
return &nsWriter{Writer: w, ns: c.ns}, nil
}
type nsWriter struct {
content.Writer
ns string
}
func (w *nsWriter) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error {
ctx = namespaces.WithNamespace(ctx, w.ns)
return w.Writer.Commit(ctx, size, expected, opts...)
}
type nsFallbackStore struct {
main *Store
fb *Store
}
var _ content.Store = &nsFallbackStore{}
func (c *nsFallbackStore) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
info, err := c.main.Info(ctx, dgst)
if err != nil {
if errdefs.IsNotFound(err) {
return c.fb.Info(ctx, dgst)
}
}
return info, err
}
func (c *nsFallbackStore) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
return c.main.Update(ctx, info, fieldpaths...)
}
func (c *nsFallbackStore) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
return c.main.Walk(ctx, fn, filters...)
}
func (c *nsFallbackStore) Delete(ctx context.Context, dgst digest.Digest) error {
return c.main.Delete(ctx, dgst)
}
func (c *nsFallbackStore) Status(ctx context.Context, ref string) (content.Status, error) {
return c.main.Status(ctx, ref)
}
func (c *nsFallbackStore) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
return c.main.ListStatuses(ctx, filters...)
}
func (c *nsFallbackStore) Abort(ctx context.Context, ref string) error {
return c.main.Abort(ctx, ref)
}
func (c *nsFallbackStore) ReaderAt(ctx context.Context, desc ocispecs.Descriptor) (content.ReaderAt, error) {
ra, err := c.main.ReaderAt(ctx, desc)
if err != nil {
if errdefs.IsNotFound(err) {
return c.fb.ReaderAt(ctx, desc)
}
}
return ra, err
}
func (c *nsFallbackStore) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
return c.main.Writer(ctx, opts...)
}