2014-11-18 00:29:42 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/storagedriver"
|
2014-11-18 00:29:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type layerStore struct {
|
|
|
|
driver storagedriver.StorageDriver
|
|
|
|
pathMapper *pathMapper
|
|
|
|
uploadStore layerUploadStore
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:39:32 +00:00
|
|
|
func (ls *layerStore) Exists(name string, digest digest.Digest) (bool, error) {
|
2014-11-18 00:29:42 +00:00
|
|
|
// Because this implementation just follows blob links, an existence check
|
|
|
|
// is pretty cheap by starting and closing a fetch.
|
2014-11-19 22:39:32 +00:00
|
|
|
_, err := ls.Fetch(name, digest)
|
2014-11-18 00:29:42 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2014-11-26 20:52:52 +00:00
|
|
|
switch err.(type) {
|
|
|
|
case ErrUnknownLayer:
|
2014-11-18 00:29:42 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:39:32 +00:00
|
|
|
func (ls *layerStore) Fetch(name string, digest digest.Digest) (Layer, error) {
|
2014-11-25 00:21:02 +00:00
|
|
|
blobPath, err := ls.resolveBlobPath(name, digest)
|
2014-11-18 00:29:42 +00:00
|
|
|
if err != nil {
|
2014-11-25 00:21:02 +00:00
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError, *storagedriver.PathNotFoundError:
|
2014-11-26 20:52:52 +00:00
|
|
|
return nil, ErrUnknownLayer{FSLayer{BlobSum: digest}}
|
2014-11-25 00:21:02 +00:00
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
fr, err := newFileReader(ls.driver, blobPath)
|
2014-11-18 00:29:42 +00:00
|
|
|
if err != nil {
|
2014-11-21 01:49:35 +00:00
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError, *storagedriver.PathNotFoundError:
|
2014-11-26 20:52:52 +00:00
|
|
|
return nil, ErrUnknownLayer{FSLayer{BlobSum: digest}}
|
2014-11-21 01:49:35 +00:00
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 01:49:35 +00:00
|
|
|
return &layerReader{
|
|
|
|
fileReader: *fr,
|
2014-11-18 00:29:42 +00:00
|
|
|
name: name,
|
2014-11-19 22:39:32 +00:00
|
|
|
digest: digest,
|
2014-11-21 01:49:35 +00:00
|
|
|
}, nil
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload begins a layer upload, returning a handle. If the layer upload
|
|
|
|
// is already in progress or the layer has already been uploaded, this
|
|
|
|
// will return an error.
|
2014-11-19 22:39:32 +00:00
|
|
|
func (ls *layerStore) Upload(name string) (LayerUpload, error) {
|
2014-11-18 00:29:42 +00:00
|
|
|
|
|
|
|
// NOTE(stevvooe): Consider the issues with allowing concurrent upload of
|
|
|
|
// the same two layers. Should it be disallowed? For now, we allow both
|
|
|
|
// parties to proceed and the the first one uploads the layer.
|
|
|
|
|
2014-11-19 22:39:32 +00:00
|
|
|
lus, err := ls.uploadStore.New(name)
|
2014-11-18 00:29:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ls.newLayerUpload(lus), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume continues an in progress layer upload, returning the current
|
|
|
|
// state of the upload.
|
2014-11-19 22:39:32 +00:00
|
|
|
func (ls *layerStore) Resume(uuid string) (LayerUpload, error) {
|
2014-11-18 00:29:42 +00:00
|
|
|
lus, err := ls.uploadStore.GetState(uuid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ls.newLayerUpload(lus), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLayerUpload allocates a new upload controller with the given state.
|
|
|
|
func (ls *layerStore) newLayerUpload(lus LayerUploadState) LayerUpload {
|
|
|
|
return &layerUploadController{
|
|
|
|
LayerUploadState: lus,
|
|
|
|
layerStore: ls,
|
|
|
|
uploadStore: ls.uploadStore,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
// resolveBlobId looks up the blob location in the repositories from a
|
|
|
|
// layer/blob link file, returning blob path or an error on failure.
|
|
|
|
func (ls *layerStore) resolveBlobPath(name string, dgst digest.Digest) (string, error) {
|
|
|
|
pathSpec := layerLinkPathSpec{name: name, digest: dgst}
|
|
|
|
layerLinkPath, err := ls.pathMapper.path(pathSpec)
|
|
|
|
|
2014-11-18 00:29:42 +00:00
|
|
|
if err != nil {
|
2014-11-25 00:21:02 +00:00
|
|
|
return "", err
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
layerLinkContent, err := ls.driver.GetContent(layerLinkPath)
|
2014-11-18 00:29:42 +00:00
|
|
|
if err != nil {
|
2014-11-25 00:21:02 +00:00
|
|
|
return "", err
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
// NOTE(stevvooe): The content of the layer link should match the digest.
|
|
|
|
// This layer of indirection is for name-based content protection.
|
2014-11-18 00:29:42 +00:00
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
linked, err := digest.ParseDigest(string(layerLinkContent))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
bp := blobPathSpec{digest: linked}
|
2014-11-18 00:29:42 +00:00
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
return ls.pathMapper.path(bp)
|
2014-11-18 00:29:42 +00:00
|
|
|
}
|