2018-03-22 01:10:08 +00:00
|
|
|
package contentutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/remotes"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-07-03 09:59:33 +00:00
|
|
|
func FromFetcher(f remotes.Fetcher) content.Provider {
|
2018-03-22 01:10:08 +00:00
|
|
|
return &fetchedProvider{
|
2018-07-03 09:59:33 +00:00
|
|
|
f: f,
|
2018-03-22 01:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fetchedProvider struct {
|
2018-07-03 09:59:33 +00:00
|
|
|
f remotes.Fetcher
|
2018-03-22 01:10:08 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
func (p *fetchedProvider) ReaderAt(ctx context.Context, desc ocispec.Descriptor) (content.ReaderAt, error) {
|
2018-07-03 09:59:33 +00:00
|
|
|
rc, err := p.f.Fetch(ctx, desc)
|
2018-03-22 01:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-03 09:59:33 +00:00
|
|
|
return &readerAt{Reader: rc, Closer: rc, size: desc.Size}, nil
|
2018-03-22 01:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type readerAt struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
size int64
|
|
|
|
offset int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readerAt) ReadAt(b []byte, off int64) (int, error) {
|
|
|
|
if ra, ok := r.Reader.(io.ReaderAt); ok {
|
|
|
|
return ra.ReadAt(b, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.offset != off {
|
|
|
|
if seeker, ok := r.Reader.(io.Seeker); ok {
|
|
|
|
if _, err := seeker.Seek(off, io.SeekStart); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
r.offset = off
|
|
|
|
} else {
|
|
|
|
return 0, errors.Errorf("unsupported offset")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalN int
|
|
|
|
for len(b) > 0 {
|
|
|
|
n, err := r.Reader.Read(b)
|
2018-09-11 00:08:07 +00:00
|
|
|
if err == io.EOF && n == len(b) {
|
|
|
|
err = nil
|
|
|
|
}
|
2018-03-22 01:10:08 +00:00
|
|
|
r.offset += int64(n)
|
|
|
|
totalN += n
|
|
|
|
b = b[n:]
|
|
|
|
if err != nil {
|
|
|
|
return totalN, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalN, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readerAt) Size() int64 {
|
|
|
|
return r.size
|
|
|
|
}
|