github.com/dctrud/umoci@v0.4.3-0.20191016193643-05a1d37de015/oci/cas/cas.go (about)

     1  /*
     2   * umoci: Umoci Modifies Open Containers' Images
     3   * Copyright (C) 2016, 2017, 2018 SUSE LLC.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package cas
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  
    24  	// We need to include sha256 in order for go-digest to properly handle such
    25  	// hashes, since Go's crypto library like to lazy-load cryptographic
    26  	// libraries.
    27  	_ "crypto/sha256"
    28  
    29  	"github.com/opencontainers/go-digest"
    30  	ispec "github.com/opencontainers/image-spec/specs-go/v1"
    31  	"golang.org/x/net/context"
    32  )
    33  
    34  const (
    35  	// BlobAlgorithm is the name of the only supported digest algorithm for blobs.
    36  	// FIXME: We can make this a list.
    37  	BlobAlgorithm = digest.SHA256
    38  )
    39  
    40  // Exposed errors.
    41  var (
    42  	// ErrNotExist is effectively an implementation-neutral version of
    43  	// os.ErrNotExist.
    44  	ErrNotExist = fmt.Errorf("no such blob or index")
    45  
    46  	// ErrInvalid is returned when an image was detected as being invalid.
    47  	ErrInvalid = fmt.Errorf("invalid image detected")
    48  
    49  	// ErrUnknownType is returned when an unknown (or otherwise unparseable)
    50  	// mediatype is encountered. Callers should not ignore this error unless it
    51  	// is in a context where ignoring it is more friendly to spec extensions.
    52  	ErrUnknownType = fmt.Errorf("unknown mediatype encountered")
    53  
    54  	// ErrNotImplemented is returned when a requested operation has not been
    55  	// implementing the backing image store.
    56  	ErrNotImplemented = fmt.Errorf("operation not implemented")
    57  
    58  	// ErrClobber is returned when a requested operation would require clobbering a
    59  	// reference or blob which already exists.
    60  	ErrClobber = fmt.Errorf("operation would clobber existing object")
    61  )
    62  
    63  // Engine is an interface that provides methods for accessing and modifying an
    64  // OCI image, namely allowing access to reference descriptors and blobs.
    65  type Engine interface {
    66  	// PutBlob adds a new blob to the image. This is idempotent; a nil error
    67  	// means that "the content is stored at DIGEST" without implying "because
    68  	// of this PutBlob() call".
    69  	PutBlob(ctx context.Context, reader io.Reader) (digest digest.Digest, size int64, err error)
    70  
    71  	// GetBlob returns a reader for retrieving a blob from the image, which the
    72  	// caller must Close(). Returns ErrNotExist if the digest is not found.
    73  	GetBlob(ctx context.Context, digest digest.Digest) (reader io.ReadCloser, err error)
    74  
    75  	// PutIndex sets the index of the OCI image to the given index, replacing
    76  	// the previously existing index. This operation is atomic; any readers
    77  	// attempting to access the OCI image while it is being modified will only
    78  	// ever see the new or old index.
    79  	PutIndex(ctx context.Context, index ispec.Index) (err error)
    80  
    81  	// GetIndex returns the index of the OCI image. Return ErrNotExist if the
    82  	// digest is not found. If the image doesn't have an index, ErrInvalid is
    83  	// returned (a valid OCI image MUST have an image index).
    84  	//
    85  	// It is not recommended that users of cas.Engine use this interface
    86  	// directly, due to the complication of properly handling references as
    87  	// well as correctly handling nested indexes. casext.Engine provides a
    88  	// wrapper for cas.Engine that implements various reference resolution
    89  	// functions that should work for most users.
    90  	GetIndex(ctx context.Context) (index ispec.Index, ierr error)
    91  
    92  	// DeleteBlob removes a blob from the image. This is idempotent; a nil
    93  	// error means "the content is not in the store" without implying "because
    94  	// of this DeleteBlob() call".
    95  	DeleteBlob(ctx context.Context, digest digest.Digest) (err error)
    96  
    97  	// ListBlobs returns the set of blob digests stored in the image.
    98  	ListBlobs(ctx context.Context) (digests []digest.Digest, err error)
    99  
   100  	// Clean executes a garbage collection of any non-blob garbage in the store
   101  	// (this includes temporary files and directories not reachable from the
   102  	// CAS interface). This MUST NOT remove any blobs or references in the
   103  	// store.
   104  	Clean(ctx context.Context) (err error)
   105  
   106  	// Close releases all references held by the engine. Subsequent operations
   107  	// may fail.
   108  	Close() (err error)
   109  }