2018-03-22 01:10:08 +00:00
|
|
|
package contentutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2018-09-11 00:08:07 +00:00
|
|
|
"sync"
|
2018-03-22 01:10:08 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
2018-09-11 00:08:07 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
2021-07-15 02:21:08 +00:00
|
|
|
"github.com/moby/buildkit/util/resolver/limited"
|
2020-11-12 03:31:50 +00:00
|
|
|
"github.com/moby/buildkit/util/resolver/retryhandler"
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-09-11 00:08:07 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-03-22 01:10:08 +00:00
|
|
|
)
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func Copy(ctx context.Context, ingester content.Ingester, provider content.Provider, desc ocispecs.Descriptor, ref string, logger func([]byte)) error {
|
2021-07-15 02:21:08 +00:00
|
|
|
if _, err := retryhandler.New(limited.FetchHandler(ingester, &localFetcher{provider}, ref), logger)(ctx, desc); err != nil {
|
2018-03-22 01:10:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type localFetcher struct {
|
|
|
|
content.Provider
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func (f *localFetcher) Fetch(ctx context.Context, desc ocispecs.Descriptor) (io.ReadCloser, error) {
|
2018-06-06 08:02:01 +00:00
|
|
|
r, err := f.Provider.ReaderAt(ctx, desc)
|
2018-03-22 01:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rc{ReaderAt: r}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type rc struct {
|
|
|
|
content.ReaderAt
|
2021-07-15 07:01:30 +00:00
|
|
|
offset int64
|
2018-03-22 01:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rc) Read(b []byte) (int, error) {
|
2021-07-15 07:01:30 +00:00
|
|
|
n, err := r.ReadAt(b, r.offset)
|
|
|
|
r.offset += int64(n)
|
2018-03-22 01:10:08 +00:00
|
|
|
if n > 0 && err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2018-09-11 00:08:07 +00:00
|
|
|
|
2021-07-15 03:18:45 +00:00
|
|
|
func (r *rc) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
2021-07-15 07:01:30 +00:00
|
|
|
r.offset = offset
|
2021-07-15 03:18:45 +00:00
|
|
|
case io.SeekCurrent:
|
2021-07-15 07:01:30 +00:00
|
|
|
r.offset += offset
|
2021-07-15 03:18:45 +00:00
|
|
|
case io.SeekEnd:
|
2021-07-15 07:01:30 +00:00
|
|
|
r.offset = r.Size() - offset
|
2021-07-15 03:18:45 +00:00
|
|
|
}
|
2021-07-15 07:01:30 +00:00
|
|
|
return r.offset, nil
|
2021-07-15 03:18:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
func CopyChain(ctx context.Context, ingester content.Ingester, provider content.Provider, desc ocispecs.Descriptor) error {
|
2018-09-11 00:08:07 +00:00
|
|
|
var m sync.Mutex
|
2021-07-26 08:53:30 +00:00
|
|
|
manifestStack := []ocispecs.Descriptor{}
|
2018-09-11 00:08:07 +00:00
|
|
|
|
2021-07-26 08:53:30 +00:00
|
|
|
filterHandler := images.HandlerFunc(func(ctx context.Context, desc ocispecs.Descriptor) ([]ocispecs.Descriptor, error) {
|
2018-09-11 00:08:07 +00:00
|
|
|
switch desc.MediaType {
|
2021-07-26 08:53:30 +00:00
|
|
|
case images.MediaTypeDockerSchema2Manifest, ocispecs.MediaTypeImageManifest,
|
|
|
|
images.MediaTypeDockerSchema2ManifestList, ocispecs.MediaTypeImageIndex:
|
2018-09-11 00:08:07 +00:00
|
|
|
m.Lock()
|
|
|
|
manifestStack = append(manifestStack, desc)
|
|
|
|
m.Unlock()
|
|
|
|
return nil, images.ErrStopHandler
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handlers := []images.Handler{
|
|
|
|
images.ChildrenHandler(provider),
|
|
|
|
filterHandler,
|
2021-07-15 02:21:08 +00:00
|
|
|
retryhandler.New(limited.FetchHandler(ingester, &localFetcher{provider}, ""), func(_ []byte) {}),
|
2018-09-11 00:08:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 01:57:35 +00:00
|
|
|
if err := images.Dispatch(ctx, images.Handlers(handlers...), nil, desc); err != nil {
|
2018-09-11 00:08:07 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(manifestStack) - 1; i >= 0; i-- {
|
2021-07-13 05:28:35 +00:00
|
|
|
if err := Copy(ctx, ingester, provider, manifestStack[i], "", nil); err != nil {
|
2018-09-11 00:08:07 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|