2015-02-11 01:41:09 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
2015-02-12 00:49:49 +00:00
|
|
|
"github.com/docker/distribution"
|
2015-04-27 22:58:58 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-02-11 01:41:09 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest"
|
2015-02-11 02:14:23 +00:00
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
2015-02-11 01:41:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type layerStore struct {
|
|
|
|
repository *repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *layerStore) Exists(digest digest.Digest) (bool, error) {
|
2015-04-27 22:58:58 +00:00
|
|
|
context.GetLogger(ls.repository.ctx).Debug("(*layerStore).Exists")
|
2015-02-11 01:41:09 +00:00
|
|
|
|
|
|
|
// Because this implementation just follows blob links, an existence check
|
|
|
|
// is pretty cheap by starting and closing a fetch.
|
|
|
|
_, err := ls.Fetch(digest)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
2015-02-12 00:49:49 +00:00
|
|
|
case distribution.ErrUnknownLayer:
|
2015-02-11 01:41:09 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:49:49 +00:00
|
|
|
func (ls *layerStore) Fetch(dgst digest.Digest) (distribution.Layer, error) {
|
2015-04-27 22:58:58 +00:00
|
|
|
ctx := ls.repository.ctx
|
|
|
|
context.GetLogger(ctx).Debug("(*layerStore).Fetch")
|
2015-02-11 01:41:09 +00:00
|
|
|
bp, err := ls.path(dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
fr, err := newFileReader(ctx, ls.repository.driver, bp)
|
2015-02-11 01:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &layerReader{
|
|
|
|
fileReader: *fr,
|
|
|
|
digest: dgst,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-02-12 00:49:49 +00:00
|
|
|
func (ls *layerStore) Upload() (distribution.LayerUpload, error) {
|
2015-04-27 22:58:58 +00:00
|
|
|
ctx := ls.repository.ctx
|
|
|
|
context.GetLogger(ctx).Debug("(*layerStore).Upload")
|
2015-02-11 01:41:09 +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.
|
|
|
|
|
|
|
|
uuid := uuid.New()
|
|
|
|
startedAt := time.Now().UTC()
|
|
|
|
|
2015-04-27 07:18:55 +00:00
|
|
|
path, err := ls.repository.pm.path(uploadDataPathSpec{
|
2015-02-11 01:41:09 +00:00
|
|
|
name: ls.repository.Name(),
|
|
|
|
uuid: uuid,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-27 07:18:55 +00:00
|
|
|
startedAtPath, err := ls.repository.pm.path(uploadStartedAtPathSpec{
|
2015-02-11 01:41:09 +00:00
|
|
|
name: ls.repository.Name(),
|
|
|
|
uuid: uuid,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a startedat file for this upload
|
2015-04-27 22:58:58 +00:00
|
|
|
if err := ls.repository.driver.PutContent(ctx, startedAtPath, []byte(startedAt.Format(time.RFC3339))); err != nil {
|
2015-02-11 01:41:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ls.newLayerUpload(uuid, path, startedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume continues an in progress layer upload, returning the current
|
|
|
|
// state of the upload.
|
2015-02-12 00:49:49 +00:00
|
|
|
func (ls *layerStore) Resume(uuid string) (distribution.LayerUpload, error) {
|
2015-04-27 22:58:58 +00:00
|
|
|
ctx := ls.repository.ctx
|
|
|
|
context.GetLogger(ctx).Debug("(*layerStore).Resume")
|
|
|
|
|
2015-04-27 07:18:55 +00:00
|
|
|
startedAtPath, err := ls.repository.pm.path(uploadStartedAtPathSpec{
|
2015-02-11 01:41:09 +00:00
|
|
|
name: ls.repository.Name(),
|
|
|
|
uuid: uuid,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
startedAtBytes, err := ls.repository.driver.GetContent(ctx, startedAtPath)
|
2015-02-11 01:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
2015-02-12 00:49:49 +00:00
|
|
|
return nil, distribution.ErrLayerUploadUnknown
|
2015-02-11 01:41:09 +00:00
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startedAt, err := time.Parse(time.RFC3339, string(startedAtBytes))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := ls.repository.pm.path(uploadDataPathSpec{
|
|
|
|
name: ls.repository.Name(),
|
|
|
|
uuid: uuid,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ls.newLayerUpload(uuid, path, startedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLayerUpload allocates a new upload controller with the given state.
|
2015-02-12 00:49:49 +00:00
|
|
|
func (ls *layerStore) newLayerUpload(uuid, path string, startedAt time.Time) (distribution.LayerUpload, error) {
|
2015-04-27 22:58:58 +00:00
|
|
|
fw, err := newFileWriter(ls.repository.ctx, ls.repository.driver, path)
|
2015-02-11 01:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-16 01:12:45 +00:00
|
|
|
lw := &layerWriter{
|
2015-03-03 22:47:07 +00:00
|
|
|
layerStore: ls,
|
|
|
|
uuid: uuid,
|
|
|
|
startedAt: startedAt,
|
|
|
|
bufferedFileWriter: *fw,
|
2015-04-16 01:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lw.setupResumableDigester()
|
|
|
|
|
|
|
|
return lw, nil
|
2015-02-11 01:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *layerStore) path(dgst digest.Digest) (string, error) {
|
|
|
|
// We must traverse this path through the link to enforce ownership.
|
2015-04-27 07:18:55 +00:00
|
|
|
layerLinkPath, err := ls.repository.pm.path(layerLinkPathSpec{name: ls.repository.Name(), digest: dgst})
|
2015-02-11 01:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobPath, err := ls.repository.blobStore.resolve(layerLinkPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case storagedriver.PathNotFoundError:
|
2015-02-12 00:49:49 +00:00
|
|
|
return "", distribution.ErrUnknownLayer{
|
|
|
|
FSLayer: manifest.FSLayer{BlobSum: dgst},
|
|
|
|
}
|
2015-02-11 01:41:09 +00:00
|
|
|
default:
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobPath, nil
|
|
|
|
}
|