@@ -5,12 +5,15 @@ 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+ type Store = nsContent
15+
16+ func NewContentStore (store content.Store , ns string ) * Store {
1417 return & nsContent {ns , store }
1518}
1619
@@ -19,6 +22,14 @@ type nsContent struct {
1922 content.Store
2023}
2124
25+ func (c * nsContent ) Namespace () string {
26+ return c .ns
27+ }
28+
29+ func (c * nsContent ) WithNamespace (ns string ) * Store {
30+ return NewContentStore (c .Store , ns )
31+ }
32+
2233func (c * nsContent ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
2334 ctx = namespaces .WithNamespace (ctx , c .ns )
2435 return c .Store .Info (ctx , dgst )
@@ -62,6 +73,13 @@ func (c *nsContent) Writer(ctx context.Context, opts ...content.WriterOpt) (cont
6273 return c .writer (ctx , 3 , opts ... )
6374}
6475
76+ func (c * nsContent ) WithFallbackNS (ns string ) content.Store {
77+ return & nsFallbackStore {
78+ main : c ,
79+ fb : c .WithNamespace (ns ),
80+ }
81+ }
82+
6583func (c * nsContent ) writer (ctx context.Context , retries int , opts ... content.WriterOpt ) (content.Writer , error ) {
6684 ctx = namespaces .WithNamespace (ctx , c .ns )
6785 w , err := c .Store .Writer (ctx , opts ... )
@@ -80,3 +98,58 @@ func (w *nsWriter) Commit(ctx context.Context, size int64, expected digest.Diges
8098 ctx = namespaces .WithNamespace (ctx , w .ns )
8199 return w .Writer .Commit (ctx , size , expected , opts ... )
82100}
101+
102+ type nsFallbackStore struct {
103+ main * nsContent
104+ fb * nsContent
105+ }
106+
107+ var _ content.Store = & nsFallbackStore {}
108+
109+ func (c * nsFallbackStore ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
110+ info , err := c .main .Info (ctx , dgst )
111+ if err != nil {
112+ if errdefs .IsNotFound (err ) {
113+ return c .fb .Info (ctx , dgst )
114+ }
115+ }
116+ return info , err
117+ }
118+
119+ func (c * nsFallbackStore ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
120+ return c .main .Update (ctx , info , fieldpaths ... )
121+ }
122+
123+ func (c * nsFallbackStore ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
124+ return c .main .Walk (ctx , fn , filters ... )
125+ }
126+
127+ func (c * nsFallbackStore ) Delete (ctx context.Context , dgst digest.Digest ) error {
128+ return c .main .Delete (ctx , dgst )
129+ }
130+
131+ func (c * nsFallbackStore ) Status (ctx context.Context , ref string ) (content.Status , error ) {
132+ return c .main .Status (ctx , ref )
133+ }
134+
135+ func (c * nsFallbackStore ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
136+ return c .main .ListStatuses (ctx , filters ... )
137+ }
138+
139+ func (c * nsFallbackStore ) Abort (ctx context.Context , ref string ) error {
140+ return c .main .Abort (ctx , ref )
141+ }
142+
143+ func (c * nsFallbackStore ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
144+ ra , err := c .main .ReaderAt (ctx , desc )
145+ if err != nil {
146+ if errdefs .IsNotFound (err ) {
147+ return c .fb .ReaderAt (ctx , desc )
148+ }
149+ }
150+ return ra , err
151+ }
152+
153+ func (c * nsFallbackStore ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
154+ return c .main .Writer (ctx , opts ... )
155+ }
0 commit comments