2016-06-28 15:24:25 +08:00
|
|
|
|
package directory
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2017-07-29 04:47:24 +08:00
|
|
|
|
"context"
|
2016-06-28 15:24:25 +08:00
|
|
|
|
"io"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
2022-07-02 08:46:57 +08:00
|
|
|
|
"github.com/containers/image/v5/internal/imagesource/impl"
|
2022-07-02 08:06:00 +08:00
|
|
|
|
"github.com/containers/image/v5/internal/imagesource/stubs"
|
|
|
|
|
|
"github.com/containers/image/v5/internal/private"
|
2019-10-26 04:27:45 +08:00
|
|
|
|
"github.com/containers/image/v5/manifest"
|
|
|
|
|
|
"github.com/containers/image/v5/types"
|
2017-01-09 18:10:30 +08:00
|
|
|
|
"github.com/opencontainers/go-digest"
|
2016-06-28 15:24:25 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type dirImageSource struct {
|
2022-07-02 08:46:57 +08:00
|
|
|
|
impl.PropertyMethodsInitialize
|
2022-07-02 08:06:00 +08:00
|
|
|
|
stubs.NoGetBlobAtInitialize
|
|
|
|
|
|
|
2016-07-14 09:37:11 +08:00
|
|
|
|
ref dirReference
|
2016-06-28 15:24:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-14 09:37:11 +08:00
|
|
|
|
// newImageSource returns an ImageSource reading from an existing directory.
|
2016-08-27 05:24:39 +08:00
|
|
|
|
// The caller must call .Close() on the returned ImageSource.
|
2022-07-02 08:06:00 +08:00
|
|
|
|
func newImageSource(ref dirReference) private.ImageSource {
|
|
|
|
|
|
return &dirImageSource{
|
2022-07-02 08:46:57 +08:00
|
|
|
|
PropertyMethodsInitialize: impl.PropertyMethods(impl.Properties{
|
|
|
|
|
|
HasThreadSafeGetBlob: false,
|
|
|
|
|
|
}),
|
2022-07-02 08:06:00 +08:00
|
|
|
|
NoGetBlobAtInitialize: stubs.NoGetBlobAt(ref),
|
|
|
|
|
|
|
|
|
|
|
|
ref: ref,
|
|
|
|
|
|
}
|
2016-06-28 15:24:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-14 03:50:04 +08:00
|
|
|
|
// Reference returns the reference used to set up this source, _as specified by the user_
|
|
|
|
|
|
// (not as the image itself, or its underlying storage, claims). This can be used e.g. to determine which public keys are trusted for this image.
|
|
|
|
|
|
func (s *dirImageSource) Reference() types.ImageReference {
|
|
|
|
|
|
return s.ref
|
2016-06-28 15:24:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-27 05:24:39 +08:00
|
|
|
|
// Close removes resources associated with an initialized ImageSource, if any.
|
2017-02-06 15:13:00 +08:00
|
|
|
|
func (s *dirImageSource) Close() error {
|
|
|
|
|
|
return nil
|
2016-08-27 05:24:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-02 01:20:36 +08: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.
|
2017-09-16 05:42:07 +08:00
|
|
|
|
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve (when the primary manifest is a manifest list);
|
|
|
|
|
|
// this never happens if the primary manifest is not a manifest list (e.g. if the source never returns manifest lists).
|
2018-02-23 17:45:56 +08:00
|
|
|
|
func (s *dirImageSource) GetManifest(ctx context.Context, instanceDigest *digest.Digest) ([]byte, string, error) {
|
2022-04-14 01:33:42 +08:00
|
|
|
|
m, err := os.ReadFile(s.ref.manifestPath(instanceDigest))
|
2016-06-28 15:24:25 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, "", err
|
|
|
|
|
|
}
|
2016-10-29 00:07:29 +08:00
|
|
|
|
return m, manifest.GuessMIMEType(m), err
|
2016-06-28 15:24:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-27 09:13:00 +08:00
|
|
|
|
// GetBlob returns a stream for the specified blob, and the blob’s size (or -1 if unknown).
|
2018-08-25 11:48:44 +08:00
|
|
|
|
// The Digest field in BlobInfo is guaranteed to be provided, Size may be -1 and MediaType may be optionally provided.
|
|
|
|
|
|
// May update BlobInfoCache, preferably after it knows for certain that a blob truly exists at a specific location.
|
|
|
|
|
|
func (s *dirImageSource) GetBlob(ctx context.Context, info types.BlobInfo, cache types.BlobInfoCache) (io.ReadCloser, int64, error) {
|
2016-11-02 17:20:10 +08:00
|
|
|
|
r, err := os.Open(s.ref.layerPath(info.Digest))
|
2016-06-28 15:24:25 +08:00
|
|
|
|
if err != nil {
|
2018-02-20 01:06:10 +08:00
|
|
|
|
return nil, -1, err
|
2016-06-28 15:24:25 +08:00
|
|
|
|
}
|
2016-07-14 00:59:27 +08:00
|
|
|
|
fi, err := r.Stat()
|
2016-06-28 15:24:25 +08:00
|
|
|
|
if err != nil {
|
2018-02-20 01:06:10 +08:00
|
|
|
|
return nil, -1, err
|
2016-06-28 15:24:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
return r, fi.Size(), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-16 06:43:30 +08:00
|
|
|
|
// GetSignatures returns the image's signatures. It may use a remote (= slow) service.
|
|
|
|
|
|
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve signatures for
|
|
|
|
|
|
// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list
|
|
|
|
|
|
// (e.g. if the source never returns manifest lists).
|
|
|
|
|
|
func (s *dirImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
|
2016-06-28 15:24:25 +08:00
|
|
|
|
signatures := [][]byte{}
|
|
|
|
|
|
for i := 0; ; i++ {
|
2022-04-14 01:33:42 +08:00
|
|
|
|
signature, err := os.ReadFile(s.ref.signaturePath(i, instanceDigest))
|
2016-06-28 15:24:25 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
signatures = append(signatures, signature)
|
|
|
|
|
|
}
|
|
|
|
|
|
return signatures, nil
|
|
|
|
|
|
}
|
2017-10-24 03:10:23 +08:00
|
|
|
|
|
Add manifest list support
Add the manifest.List interface, and implementations for OCIv1 Index and
Docker Schema2List documents.
Add an instanceDigest parameter to PutManifest(), PutSignatures(), and
LayerInfosForCopy, for symmetry with GetManifest() and GetSignatures().
Return an error if the instanceDigest is supplied to destinations which
don't support them, and add stubs that do so even to the transports
which would support it, so that we don't break compilation here.
Add a MultipleImages flag to copy.Options, and if the source for a copy
operation contains multiple images, copy all of the images if we can.
If we can't copy them all, but we were told to, return an error.
Use the generic manifest list API to select a single image to copy from
a list, so that we aren't just limited to the Docker manifest list
format for those cases.
When guessing at the type of a manifest, if the manifest contains a list
of manifests, use its declared MIME type if it included one, else assume
it's an OCI index, because an OCI index doesn't include its MIME type.
When copying, switch from using an encode-then-compare of the original
and updated versions of the list to checking if the instance list was
changed (one of the things we might have changed) or if its type has
changed due to conversion (the other change we might have made). If
neither has changed, then we don't need to change the encoded value of
the manifest.
When copying, when checking for a digest mismatch in a target image
reference, ignore a mismatch between the digest in the reference and the
digest of the main manifest if we're copying one element from a list,
and the digest in the reference matches the digest of the manifest list.
When copying, if conversion of manifests for single images is being
forced, convert manifest lists to the corresponding list types.
When copying, supply the unparsed top level to Commit() by attaching the
value to the context.Context.
Support manifest lists in the directory transport by using the instance
digest as a prefix of the filename used to store a manifest or a piece
of signature data.
Support manifest lists in the oci-layout transport by accepting indexes
as we do images, and stop guessing about Platform values to add to the
top-level index.
Support storing manifest lists to registries in the docker: transport by
using the manifest digest when we're writing one image as part of
pushing a list of them, and by using the instance digest when reading or
writing signature data, when one is specified, or the cached digest of
the non-instanced digest when one is not specified.
Add partial support for manifest lists to the storage transport: when
committing one image from a list into storage, also add a copy of the
manifest list by extracting it from the context.Context. The logic is
already in place to enable locating an image using any of multiple
manifest digests.
When writing an image that has an instanceDigest value (meaning it's a
secondary image), don't try to generate a canonical reference to add to
the image's list of names if the reference for the primary image doesn't
contain a name. That should only happen if we're writing using just an
image ID, which is unlikely, but we still need to handle it.
Avoid computing the digest of the manifest, or retrieving the
either-a-tag-or-a-digest value from the target reference, if we're given
an instanceDigest, which would override them anyway.
Move the check for non-nil instanceDigest values up into the main
PutSignatures() method instead of duplicating it in the per-strategy
helpers.
Add mention of the instanceDigest parameter and its use to various
PutManifest, PutSignatures, and LayerInfosForCopy implementations and
their declarations in interfaces.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2018-01-09 14:03:18 +08:00
|
|
|
|
// LayerInfosForCopy returns either nil (meaning the values in the manifest are fine), or updated values for the layer
|
|
|
|
|
|
// blobsums that are listed in the image's manifest. If values are returned, they should be used when using GetBlob()
|
|
|
|
|
|
// to read the image's layers.
|
|
|
|
|
|
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve BlobInfos for
|
|
|
|
|
|
// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list
|
|
|
|
|
|
// (e.g. if the source never returns manifest lists).
|
|
|
|
|
|
// The Digest field is guaranteed to be provided; Size may be -1.
|
|
|
|
|
|
// WARNING: The list may contain duplicates, and they are semantically relevant.
|
|
|
|
|
|
func (s *dirImageSource) LayerInfosForCopy(context.Context, *digest.Digest) ([]types.BlobInfo, error) {
|
2018-01-22 23:56:54 +08:00
|
|
|
|
return nil, nil
|
2017-10-24 03:10:23 +08:00
|
|
|
|
}
|