2014-11-14 02:42:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2014-11-20 02:52:09 +00:00
|
|
|
"github.com/docker/docker-registry/digest"
|
2014-11-22 03:29:08 +00:00
|
|
|
"github.com/docker/docker-registry/storage"
|
2014-11-14 02:42:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrLayerAlreadyExists is returned when attempting to create a layer with
|
|
|
|
// a tarsum that is already in use.
|
2014-12-12 06:24:25 +00:00
|
|
|
ErrLayerAlreadyExists = fmt.Errorf("Layer already exists")
|
2014-11-14 02:42:49 +00:00
|
|
|
|
|
|
|
// ErrLayerLocked is returned when attempting to write to a layer which is
|
|
|
|
// currently being written to.
|
2014-12-12 06:24:25 +00:00
|
|
|
ErrLayerLocked = fmt.Errorf("Layer locked")
|
2014-11-14 02:42:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ObjectStore is an interface which is designed to approximate the docker
|
|
|
|
// engine storage. This interface is subject to change to conform to the
|
|
|
|
// future requirements of the engine.
|
|
|
|
type ObjectStore interface {
|
|
|
|
// Manifest retrieves the image manifest stored at the given repository name
|
|
|
|
// and tag
|
2014-11-22 03:29:08 +00:00
|
|
|
Manifest(name, tag string) (*storage.SignedManifest, error)
|
2014-11-14 02:42:49 +00:00
|
|
|
|
|
|
|
// WriteManifest stores an image manifest at the given repository name and
|
|
|
|
// tag
|
2014-11-22 03:29:08 +00:00
|
|
|
WriteManifest(name, tag string, manifest *storage.SignedManifest) error
|
2014-11-14 02:42:49 +00:00
|
|
|
|
|
|
|
// Layer returns a handle to a layer for reading and writing
|
2014-11-20 02:52:09 +00:00
|
|
|
Layer(dgst digest.Digest) (Layer, error)
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layer is a generic image layer interface.
|
2014-11-18 23:44:39 +00:00
|
|
|
// A Layer may not be written to if it is already complete.
|
2014-11-14 02:42:49 +00:00
|
|
|
type Layer interface {
|
2014-11-18 23:44:39 +00:00
|
|
|
// Reader returns a LayerReader or an error if the layer has not been
|
|
|
|
// written to or is currently being written to.
|
|
|
|
Reader() (LayerReader, error)
|
2014-11-14 02:42:49 +00:00
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
// Writer returns a LayerWriter or an error if the layer has been fully
|
|
|
|
// written to or is currently being written to.
|
|
|
|
Writer() (LayerWriter, error)
|
2014-11-14 02:42:49 +00:00
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
// Wait blocks until the Layer can be read from.
|
2014-11-14 02:42:49 +00:00
|
|
|
Wait() error
|
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
// LayerReader is a read-only handle to a Layer, which exposes the CurrentSize
|
|
|
|
// and full Size in addition to implementing the io.ReadCloser interface.
|
|
|
|
type LayerReader interface {
|
|
|
|
io.ReadCloser
|
|
|
|
|
|
|
|
// CurrentSize returns the number of bytes written to the underlying Layer
|
|
|
|
CurrentSize() int
|
|
|
|
|
|
|
|
// Size returns the full size of the underlying Layer
|
|
|
|
Size() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// LayerWriter is a write-only handle to a Layer, which exposes the CurrentSize
|
|
|
|
// and full Size in addition to implementing the io.WriteCloser interface.
|
|
|
|
// SetSize must be called on this LayerWriter before it can be written to.
|
|
|
|
type LayerWriter interface {
|
|
|
|
io.WriteCloser
|
|
|
|
|
|
|
|
// CurrentSize returns the number of bytes written to the underlying Layer
|
|
|
|
CurrentSize() int
|
|
|
|
|
|
|
|
// Size returns the full size of the underlying Layer
|
|
|
|
Size() int
|
|
|
|
|
|
|
|
// SetSize sets the full size of the underlying Layer.
|
|
|
|
// This must be called before any calls to Write
|
|
|
|
SetSize(int) error
|
|
|
|
}
|
|
|
|
|
2014-11-14 02:42:49 +00:00
|
|
|
// memoryObjectStore is an in-memory implementation of the ObjectStore interface
|
|
|
|
type memoryObjectStore struct {
|
|
|
|
mutex *sync.Mutex
|
2014-11-22 03:29:08 +00:00
|
|
|
manifestStorage map[string]*storage.SignedManifest
|
2014-11-20 02:52:09 +00:00
|
|
|
layerStorage map[digest.Digest]Layer
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 03:29:08 +00:00
|
|
|
func (objStore *memoryObjectStore) Manifest(name, tag string) (*storage.SignedManifest, error) {
|
2014-11-14 02:42:49 +00:00
|
|
|
objStore.mutex.Lock()
|
|
|
|
defer objStore.mutex.Unlock()
|
|
|
|
|
|
|
|
manifest, ok := objStore.manifestStorage[name+":"+tag]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("No manifest found with Name: %q, Tag: %q", name, tag)
|
|
|
|
}
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
2014-11-22 03:29:08 +00:00
|
|
|
func (objStore *memoryObjectStore) WriteManifest(name, tag string, manifest *storage.SignedManifest) error {
|
2014-11-14 02:42:49 +00:00
|
|
|
objStore.mutex.Lock()
|
|
|
|
defer objStore.mutex.Unlock()
|
|
|
|
|
|
|
|
objStore.manifestStorage[name+":"+tag] = manifest
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:52:09 +00:00
|
|
|
func (objStore *memoryObjectStore) Layer(dgst digest.Digest) (Layer, error) {
|
2014-11-14 02:42:49 +00:00
|
|
|
objStore.mutex.Lock()
|
|
|
|
defer objStore.mutex.Unlock()
|
|
|
|
|
2014-11-20 02:52:09 +00:00
|
|
|
layer, ok := objStore.layerStorage[dgst]
|
2014-11-14 02:42:49 +00:00
|
|
|
if !ok {
|
|
|
|
layer = &memoryLayer{cond: sync.NewCond(new(sync.Mutex))}
|
2014-11-20 02:52:09 +00:00
|
|
|
objStore.layerStorage[dgst] = layer
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return layer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type memoryLayer struct {
|
2014-11-18 23:44:39 +00:00
|
|
|
cond *sync.Cond
|
|
|
|
contents []byte
|
|
|
|
expectedSize int
|
|
|
|
writing bool
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
func (ml *memoryLayer) Reader() (LayerReader, error) {
|
2014-11-14 02:42:49 +00:00
|
|
|
ml.cond.L.Lock()
|
|
|
|
defer ml.cond.L.Unlock()
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
if ml.contents == nil {
|
|
|
|
return nil, fmt.Errorf("Layer has not been written to yet")
|
|
|
|
}
|
|
|
|
if ml.writing {
|
|
|
|
return nil, ErrLayerLocked
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
return &memoryLayerReader{ml: ml, reader: bytes.NewReader(ml.contents)}, nil
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
func (ml *memoryLayer) Writer() (LayerWriter, error) {
|
2014-11-14 02:42:49 +00:00
|
|
|
ml.cond.L.Lock()
|
|
|
|
defer ml.cond.L.Unlock()
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
if ml.contents != nil {
|
|
|
|
if ml.writing {
|
|
|
|
return nil, ErrLayerLocked
|
|
|
|
}
|
|
|
|
if ml.expectedSize == len(ml.contents) {
|
|
|
|
return nil, ErrLayerAlreadyExists
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ml.contents = make([]byte, 0)
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
ml.writing = true
|
|
|
|
return &memoryLayerWriter{ml: ml, buffer: bytes.NewBuffer(ml.contents)}, nil
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *memoryLayer) Wait() error {
|
|
|
|
ml.cond.L.Lock()
|
|
|
|
defer ml.cond.L.Unlock()
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
if ml.contents == nil {
|
2014-11-14 02:42:49 +00:00
|
|
|
return fmt.Errorf("No writer to wait on")
|
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
for ml.writing {
|
2014-11-14 02:42:49 +00:00
|
|
|
ml.cond.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-18 23:44:39 +00:00
|
|
|
type memoryLayerReader struct {
|
|
|
|
ml *memoryLayer
|
|
|
|
reader *bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlr *memoryLayerReader) Read(p []byte) (int, error) {
|
|
|
|
return mlr.reader.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlr *memoryLayerReader) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlr *memoryLayerReader) CurrentSize() int {
|
|
|
|
return len(mlr.ml.contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlr *memoryLayerReader) Size() int {
|
|
|
|
return mlr.ml.expectedSize
|
|
|
|
}
|
|
|
|
|
2014-11-14 02:42:49 +00:00
|
|
|
type memoryLayerWriter struct {
|
2014-11-18 23:44:39 +00:00
|
|
|
ml *memoryLayer
|
2014-11-14 02:42:49 +00:00
|
|
|
buffer *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlw *memoryLayerWriter) Write(p []byte) (int, error) {
|
2014-11-18 23:44:39 +00:00
|
|
|
if mlw.ml.expectedSize == 0 {
|
|
|
|
return 0, fmt.Errorf("Must set size before writing to layer")
|
|
|
|
}
|
|
|
|
wrote, err := mlw.buffer.Write(p)
|
|
|
|
mlw.ml.contents = mlw.buffer.Bytes()
|
|
|
|
return wrote, err
|
2014-11-14 02:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mlw *memoryLayerWriter) Close() error {
|
2014-11-26 23:33:33 +00:00
|
|
|
mlw.ml.cond.L.Lock()
|
|
|
|
defer mlw.ml.cond.L.Unlock()
|
|
|
|
|
|
|
|
return mlw.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlw *memoryLayerWriter) close() error {
|
2014-11-18 23:44:39 +00:00
|
|
|
mlw.ml.writing = false
|
|
|
|
mlw.ml.cond.Broadcast()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlw *memoryLayerWriter) CurrentSize() int {
|
|
|
|
return len(mlw.ml.contents)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlw *memoryLayerWriter) Size() int {
|
|
|
|
return mlw.ml.expectedSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mlw *memoryLayerWriter) SetSize(size int) error {
|
|
|
|
if !mlw.ml.writing {
|
|
|
|
return fmt.Errorf("Layer is closed for writing")
|
|
|
|
}
|
|
|
|
mlw.ml.expectedSize = size
|
2014-11-14 02:42:49 +00:00
|
|
|
return nil
|
|
|
|
}
|