2015-01-14 21:02:43 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
"github.com/docker/distribution/context"
|
2015-01-14 21:02:43 +01:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-02-11 03:14:23 +01:00
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
2015-01-14 21:02:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(stevvooe): Currently, the blobStore implementation used by the
|
|
|
|
// manifest store. The layer store should be refactored to better leverage the
|
|
|
|
// blobStore, reducing duplicated code.
|
|
|
|
|
|
|
|
// blobStore implements a generalized blob store over a driver, supporting the
|
|
|
|
// read side and link management. This object is intentionally a leaky
|
|
|
|
// abstraction, providing utility methods that support creating and traversing
|
|
|
|
// backend links.
|
|
|
|
type blobStore struct {
|
2015-04-02 01:41:33 +02:00
|
|
|
driver storagedriver.StorageDriver
|
|
|
|
pm *pathMapper
|
|
|
|
ctx context.Context
|
2015-01-14 21:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// exists reports whether or not the path exists. If the driver returns error
|
|
|
|
// other than storagedriver.PathNotFound, an error may be returned.
|
|
|
|
func (bs *blobStore) exists(dgst digest.Digest) (bool, error) {
|
|
|
|
path, err := bs.path(dgst)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
ok, err := exists(bs.ctx, bs.driver, path)
|
2015-01-14 21:02:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// get retrieves the blob by digest, returning it a byte slice. This should
|
|
|
|
// only be used for small objects.
|
|
|
|
func (bs *blobStore) get(dgst digest.Digest) ([]byte, error) {
|
|
|
|
bp, err := bs.path(dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
return bs.driver.GetContent(bs.ctx, bp)
|
2015-01-14 21:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// link links the path to the provided digest by writing the digest into the
|
|
|
|
// target file.
|
|
|
|
func (bs *blobStore) link(path string, dgst digest.Digest) error {
|
|
|
|
if exists, err := bs.exists(dgst); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !exists {
|
|
|
|
return fmt.Errorf("cannot link non-existent blob")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The contents of the "link" file are the exact string contents of the
|
|
|
|
// digest, which is specified in that package.
|
2015-04-28 00:58:58 +02:00
|
|
|
return bs.driver.PutContent(bs.ctx, path, []byte(dgst))
|
2015-01-14 21:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// linked reads the link at path and returns the content.
|
|
|
|
func (bs *blobStore) linked(path string) ([]byte, error) {
|
|
|
|
linked, err := bs.readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs.get(linked)
|
|
|
|
}
|
|
|
|
|
|
|
|
// readlink returns the linked digest at path.
|
|
|
|
func (bs *blobStore) readlink(path string) (digest.Digest, error) {
|
2015-04-28 00:58:58 +02:00
|
|
|
content, err := bs.driver.GetContent(bs.ctx, path)
|
2015-01-14 21:02:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
linked, err := digest.ParseDigest(string(content))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists, err := bs.exists(linked); err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if !exists {
|
|
|
|
return "", fmt.Errorf("link %q invalid: blob %s does not exist", path, linked)
|
|
|
|
}
|
|
|
|
|
|
|
|
return linked, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve reads the digest link at path and returns the blob store link.
|
|
|
|
func (bs *blobStore) resolve(path string) (string, error) {
|
|
|
|
dgst, err := bs.readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs.path(dgst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// put stores the content p in the blob store, calculating the digest. If the
|
|
|
|
// content is already present, only the digest will be returned. This should
|
|
|
|
// only be used for small objects, such as manifests.
|
|
|
|
func (bs *blobStore) put(p []byte) (digest.Digest, error) {
|
|
|
|
dgst, err := digest.FromBytes(p)
|
|
|
|
if err != nil {
|
2015-04-28 00:58:58 +02:00
|
|
|
context.GetLogger(bs.ctx).Errorf("error digesting content: %v, %s", err, string(p))
|
2015-01-14 21:02:43 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
bp, err := bs.path(dgst)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the content already exists, just return the digest.
|
|
|
|
if exists, err := bs.exists(dgst); err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if exists {
|
|
|
|
return dgst, nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
return dgst, bs.driver.PutContent(bs.ctx, bp, p)
|
2015-01-14 21:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// path returns the canonical path for the blob identified by digest. The blob
|
|
|
|
// may or may not exist.
|
|
|
|
func (bs *blobStore) path(dgst digest.Digest) (string, error) {
|
|
|
|
bp, err := bs.pm.path(blobDataPathSpec{
|
|
|
|
digest: dgst,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bp, nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
// exists provides a utility method to test whether or not a path exists
|
|
|
|
func exists(ctx context.Context, driver storagedriver.StorageDriver, path string) (bool, error) {
|
|
|
|
if _, err := driver.Stat(ctx, path); err != nil {
|
2015-01-14 21:02:43 +01:00
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|