2016-11-22 19:32:10 +00:00
|
|
|
package layout
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/containers/image/types"
|
2016-10-17 13:53:40 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-11-22 19:32:10 +00:00
|
|
|
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ociImageSource struct {
|
2017-05-17 17:18:35 +00:00
|
|
|
ref ociReference
|
|
|
|
descriptor imgspecv1.ManifestDescriptor
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newImageSource returns an ImageSource for reading from an existing directory.
|
2017-05-17 17:18:35 +00:00
|
|
|
func newImageSource(ref ociReference) (types.ImageSource, error) {
|
|
|
|
descriptor, err := ref.getManifestDescriptor()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ociImageSource{ref: ref, descriptor: descriptor}, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reference returns the reference used to set up this source.
|
|
|
|
func (s *ociImageSource) Reference() types.ImageReference {
|
|
|
|
return s.ref
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close removes resources associated with an initialized ImageSource, if any.
|
2017-03-13 16:33:17 +00:00
|
|
|
func (s *ociImageSource) Close() error {
|
|
|
|
return nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetManifest returns the image's manifest along with its MIME type (which may be empty when it can't be determined but the manifest is available).
|
|
|
|
// It may use a remote (= slow) service.
|
|
|
|
func (s *ociImageSource) GetManifest() ([]byte, string, error) {
|
2017-05-17 17:18:35 +00:00
|
|
|
manifestPath, err := s.ref.blobPath(digest.Digest(s.descriptor.Digest))
|
2016-11-22 19:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
m, err := ioutil.ReadFile(manifestPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
return m, s.descriptor.MediaType, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ociImageSource) GetTargetManifest(digest digest.Digest) ([]byte, string, error) {
|
|
|
|
manifestPath, err := s.ref.blobPath(digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := ioutil.ReadFile(manifestPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2017-03-13 16:33:17 +00:00
|
|
|
// XXX: GetTargetManifest means that we don't have the context of what
|
|
|
|
// mediaType the manifest has. In OCI this means that we don't know
|
|
|
|
// what reference it came from, so we just *assume* that its
|
|
|
|
// MediaTypeImageManifest.
|
|
|
|
return m, imgspecv1.MediaTypeImageManifest, nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlob returns a stream for the specified blob, and the blob's size.
|
|
|
|
func (s *ociImageSource) GetBlob(info types.BlobInfo) (io.ReadCloser, int64, error) {
|
|
|
|
path, err := s.ref.blobPath(info.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
fi, err := r.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return r, fi.Size(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ociImageSource) GetSignatures() ([][]byte, error) {
|
|
|
|
return [][]byte{}, nil
|
|
|
|
}
|