@@ -5,64 +5,80 @@ import (
55
66 "github.com/containerd/containerd/content"
77 "github.com/containerd/containerd/namespaces"
8+ "github.com/containerd/nydus-snapshotter/pkg/errdefs"
89 digest "github.com/opencontainers/go-digest"
910 ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1011 "github.com/pkg/errors"
1112)
1213
13- func NewContentStore (store content.Store , ns string ) content. Store {
14- return & nsContent {ns , store }
14+ func NewContentStore (store content.Store , ns string ) * Store {
15+ return & Store {ns , store }
1516}
1617
17- type nsContent struct {
18+ type Store struct {
1819 ns string
1920 content.Store
2021}
2122
22- func (c * nsContent ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
23+ func (c * Store ) Namespace () string {
24+ return c .ns
25+ }
26+
27+ func (c * Store ) WithNamespace (ns string ) * Store {
28+ return NewContentStore (c .Store , ns )
29+ }
30+
31+ func (c * Store ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
2332 ctx = namespaces .WithNamespace (ctx , c .ns )
2433 return c .Store .Info (ctx , dgst )
2534}
2635
27- func (c * nsContent ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
36+ func (c * Store ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
2837 ctx = namespaces .WithNamespace (ctx , c .ns )
2938 return c .Store .Update (ctx , info , fieldpaths ... )
3039}
3140
32- func (c * nsContent ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
41+ func (c * Store ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
3342 ctx = namespaces .WithNamespace (ctx , c .ns )
3443 return c .Store .Walk (ctx , fn , filters ... )
3544}
3645
37- func (c * nsContent ) Delete (ctx context.Context , dgst digest.Digest ) error {
46+ func (c * Store ) Delete (ctx context.Context , dgst digest.Digest ) error {
3847 return errors .Errorf ("contentstore.Delete usage is forbidden" )
3948}
4049
41- func (c * nsContent ) Status (ctx context.Context , ref string ) (content.Status , error ) {
50+ func (c * Store ) Status (ctx context.Context , ref string ) (content.Status , error ) {
4251 ctx = namespaces .WithNamespace (ctx , c .ns )
4352 return c .Store .Status (ctx , ref )
4453}
4554
46- func (c * nsContent ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
55+ func (c * Store ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
4756 ctx = namespaces .WithNamespace (ctx , c .ns )
4857 return c .Store .ListStatuses (ctx , filters ... )
4958}
5059
51- func (c * nsContent ) Abort (ctx context.Context , ref string ) error {
60+ func (c * Store ) Abort (ctx context.Context , ref string ) error {
5261 ctx = namespaces .WithNamespace (ctx , c .ns )
5362 return c .Store .Abort (ctx , ref )
5463}
5564
56- func (c * nsContent ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
65+ func (c * Store ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
5766 ctx = namespaces .WithNamespace (ctx , c .ns )
5867 return c .Store .ReaderAt (ctx , desc )
5968}
6069
61- func (c * nsContent ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
70+ func (c * Store ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
6271 return c .writer (ctx , 3 , opts ... )
6372}
6473
65- func (c * nsContent ) writer (ctx context.Context , retries int , opts ... content.WriterOpt ) (content.Writer , error ) {
74+ func (c * Store ) WithFallbackNS (ns string ) content.Store {
75+ return & nsFallbackStore {
76+ main : c ,
77+ fb : c .WithNamespace (ns ),
78+ }
79+ }
80+
81+ func (c * Store ) writer (ctx context.Context , retries int , opts ... content.WriterOpt ) (content.Writer , error ) {
6682 ctx = namespaces .WithNamespace (ctx , c .ns )
6783 w , err := c .Store .Writer (ctx , opts ... )
6884 if err != nil {
@@ -80,3 +96,58 @@ func (w *nsWriter) Commit(ctx context.Context, size int64, expected digest.Diges
8096 ctx = namespaces .WithNamespace (ctx , w .ns )
8197 return w .Writer .Commit (ctx , size , expected , opts ... )
8298}
99+
100+ type nsFallbackStore struct {
101+ main * Store
102+ fb * Store
103+ }
104+
105+ var _ content.Store = & nsFallbackStore {}
106+
107+ func (c * nsFallbackStore ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
108+ info , err := c .main .Info (ctx , dgst )
109+ if err != nil {
110+ if errdefs .IsNotFound (err ) {
111+ return c .fb .Info (ctx , dgst )
112+ }
113+ }
114+ return info , err
115+ }
116+
117+ func (c * nsFallbackStore ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
118+ return c .main .Update (ctx , info , fieldpaths ... )
119+ }
120+
121+ func (c * nsFallbackStore ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
122+ return c .main .Walk (ctx , fn , filters ... )
123+ }
124+
125+ func (c * nsFallbackStore ) Delete (ctx context.Context , dgst digest.Digest ) error {
126+ return c .main .Delete (ctx , dgst )
127+ }
128+
129+ func (c * nsFallbackStore ) Status (ctx context.Context , ref string ) (content.Status , error ) {
130+ return c .main .Status (ctx , ref )
131+ }
132+
133+ func (c * nsFallbackStore ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
134+ return c .main .ListStatuses (ctx , filters ... )
135+ }
136+
137+ func (c * nsFallbackStore ) Abort (ctx context.Context , ref string ) error {
138+ return c .main .Abort (ctx , ref )
139+ }
140+
141+ func (c * nsFallbackStore ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
142+ ra , err := c .main .ReaderAt (ctx , desc )
143+ if err != nil {
144+ if errdefs .IsNotFound (err ) {
145+ return c .fb .ReaderAt (ctx , desc )
146+ }
147+ }
148+ return ra , err
149+ }
150+
151+ func (c * nsFallbackStore ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
152+ return c .main .Writer (ctx , opts ... )
153+ }
0 commit comments