2014-10-21 22:02:20 +00:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"sync"
|
2014-12-05 04:14:41 +00:00
|
|
|
"time"
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
2014-10-29 01:15:40 +00:00
|
|
|
"github.com/docker/docker-registry/storagedriver/factory"
|
2014-10-21 22:02:20 +00:00
|
|
|
)
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
const driverName = "inmemory"
|
2014-10-29 01:15:40 +00:00
|
|
|
|
|
|
|
func init() {
|
2014-11-17 23:44:07 +00:00
|
|
|
factory.Register(driverName, &inMemoryDriverFactory{})
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// inMemoryDriverFacotry implements the factory.StorageDriverFactory interface.
|
2014-10-29 01:15:40 +00:00
|
|
|
type inMemoryDriverFactory struct{}
|
|
|
|
|
|
|
|
func (factory *inMemoryDriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) {
|
|
|
|
return New(), nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Driver is a storagedriver.StorageDriver implementation backed by a local map.
|
|
|
|
// Intended solely for example and testing purposes.
|
|
|
|
type Driver struct {
|
2014-12-05 04:14:41 +00:00
|
|
|
root *dir
|
|
|
|
mutex sync.RWMutex
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// New constructs a new Driver.
|
|
|
|
func New() *Driver {
|
2014-12-05 04:14:41 +00:00
|
|
|
return &Driver{root: &dir{
|
|
|
|
common: common{
|
|
|
|
p: "/",
|
|
|
|
mod: time.Now(),
|
|
|
|
},
|
|
|
|
}}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Implement the storagedriver.StorageDriver interface.
|
2014-10-29 19:14:19 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
func (d *Driver) GetContent(path string) ([]byte, error) {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
2014-12-05 04:14:41 +00:00
|
|
|
|
|
|
|
rc, err := d.ReadStream(path, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
2014-12-05 04:14:41 +00:00
|
|
|
defer rc.Close()
|
|
|
|
|
|
|
|
return ioutil.ReadAll(rc)
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
2014-12-05 04:14:41 +00:00
|
|
|
func (d *Driver) PutContent(p string, contents []byte) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2014-12-05 04:14:41 +00:00
|
|
|
|
|
|
|
f, err := d.root.mkfile(p)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(stevvooe): Again, we need to clarify when this is not a
|
|
|
|
// directory in StorageDriver API.
|
|
|
|
return fmt.Errorf("not a file")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.truncate()
|
|
|
|
f.WriteAt(contents, 0)
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// ReadStream retrieves an io.ReadCloser for the content stored at "path" with a
|
|
|
|
// given byte offset.
|
2014-12-03 03:01:00 +00:00
|
|
|
func (d *Driver) ReadStream(path string, offset int64) (io.ReadCloser, error) {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
2014-12-05 04:14:41 +00:00
|
|
|
|
2014-12-05 19:46:41 +00:00
|
|
|
if offset < 0 {
|
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
|
2014-12-10 01:20:10 +00:00
|
|
|
path = normalize(path)
|
2014-12-05 04:14:41 +00:00
|
|
|
found := d.root.find(path)
|
|
|
|
|
|
|
|
if found.path() != path {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
if found.isdir() {
|
|
|
|
return nil, fmt.Errorf("%q is a directory", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.NopCloser(found.(*file).sectionReader(offset)), nil
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// WriteStream stores the contents of the provided io.ReadCloser at a location
|
|
|
|
// designated by the given path.
|
2014-12-05 04:14:41 +00:00
|
|
|
func (d *Driver) WriteStream(path string, offset int64, reader io.Reader) (nn int64, err error) {
|
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
if offset < 0 {
|
|
|
|
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 01:20:10 +00:00
|
|
|
normalized := normalize(path)
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
f, err := d.root.mkfile(normalized)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
2014-12-05 04:14:41 +00:00
|
|
|
return 0, fmt.Errorf("not a file")
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
nn, err = buf.ReadFrom(reader)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(stevvooe): This condition is odd and we may need to clarify:
|
|
|
|
// we've read nn bytes from reader but have written nothing to the
|
|
|
|
// backend. What is the correct return value? Really, the caller needs
|
|
|
|
// to know that the reader has been advanced and reattempting the
|
|
|
|
// operation is incorrect.
|
|
|
|
return nn, err
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
f.WriteAt(buf.Bytes(), offset)
|
|
|
|
return nn, err
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
// Stat returns info about the provided path.
|
|
|
|
func (d *Driver) Stat(path string) (storagedriver.FileInfo, error) {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
2014-12-05 04:14:41 +00:00
|
|
|
|
2014-12-10 01:20:10 +00:00
|
|
|
normalized := normalize(path)
|
2014-12-05 04:14:41 +00:00
|
|
|
found := d.root.find(path)
|
|
|
|
|
|
|
|
if found.path() != normalized {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
fi := storagedriver.FileInfoFields{
|
|
|
|
Path: path,
|
|
|
|
IsDir: found.isdir(),
|
|
|
|
ModTime: found.modtime(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir {
|
|
|
|
fi.Size = int64(len(found.(*file).data))
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
2014-12-05 04:14:41 +00:00
|
|
|
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// List returns a list of the objects that are direct descendants of the given
|
|
|
|
// path.
|
|
|
|
func (d *Driver) List(path string) ([]string, error) {
|
2014-12-10 01:20:10 +00:00
|
|
|
normalized := normalize(path)
|
2014-12-05 04:14:41 +00:00
|
|
|
|
|
|
|
found := d.root.find(normalized)
|
|
|
|
|
|
|
|
if !found.isdir() {
|
|
|
|
return nil, fmt.Errorf("not a directory") // TODO(stevvooe): Need error type for this...
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
entries, err := found.(*dir).list(normalized)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case errNotExists:
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
case errIsNotDir:
|
|
|
|
return nil, fmt.Errorf("not a directory")
|
|
|
|
default:
|
|
|
|
return nil, err
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
return entries, nil
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
|
|
|
// object.
|
|
|
|
func (d *Driver) Move(sourcePath string, destPath string) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
2014-12-05 04:14:41 +00:00
|
|
|
|
2014-12-10 01:20:10 +00:00
|
|
|
normalizedSrc, normalizedDst := normalize(sourcePath), normalize(destPath)
|
2014-12-05 04:14:41 +00:00
|
|
|
|
|
|
|
err := d.root.move(normalizedSrc, normalizedDst)
|
|
|
|
switch err {
|
|
|
|
case errNotExists:
|
|
|
|
return storagedriver.PathNotFoundError{Path: destPath}
|
|
|
|
default:
|
|
|
|
return err
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
|
|
|
func (d *Driver) Delete(path string) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
|
|
|
|
2014-12-10 01:20:10 +00:00
|
|
|
normalized := normalize(path)
|
2014-12-05 04:14:41 +00:00
|
|
|
|
|
|
|
err := d.root.delete(normalized)
|
|
|
|
switch err {
|
|
|
|
case errNotExists:
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
2014-12-05 04:14:41 +00:00
|
|
|
default:
|
|
|
|
return err
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
2014-12-05 04:14:41 +00:00
|
|
|
}
|