2014-10-21 22:02:20 +00:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"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-10-21 22:02:20 +00:00
|
|
|
storage map[string][]byte
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// New constructs a new Driver.
|
|
|
|
func New() *Driver {
|
|
|
|
return &Driver{storage: make(map[string][]byte)}
|
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()
|
|
|
|
contents, ok := d.storage[path]
|
|
|
|
if !ok {
|
2014-11-13 01:19:19 +00:00
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
return contents, nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
func (d *Driver) PutContent(path string, contents []byte) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
|
|
|
d.storage[path] = contents
|
|
|
|
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()
|
|
|
|
contents, err := d.GetContent(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-26 05:43:12 +00:00
|
|
|
} else if len(contents) <= int(offset) {
|
2014-11-13 01:19:19 +00:00
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
src := contents[offset:]
|
|
|
|
buf := make([]byte, len(src))
|
|
|
|
copy(buf, src)
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(buf)), nil
|
|
|
|
}
|
|
|
|
|
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-03 03:01:00 +00:00
|
|
|
func (d *Driver) WriteStream(path string, offset, size int64, reader io.ReadCloser) error {
|
2014-10-21 22:02:20 +00:00
|
|
|
defer reader.Close()
|
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
|
2014-11-07 20:58:48 +00:00
|
|
|
resumableOffset, err := d.CurrentSize(path)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-03 03:01:00 +00:00
|
|
|
if offset > int64(resumableOffset) {
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset > 0 {
|
|
|
|
contents = append(d.storage[path][0:offset], contents...)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.storage[path] = contents
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// CurrentSize retrieves the curernt size in bytes of the object at the given
|
|
|
|
// path.
|
|
|
|
func (d *Driver) CurrentSize(path string) (uint64, error) {
|
2014-10-21 22:02:20 +00:00
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
contents, ok := d.storage[path]
|
|
|
|
if !ok {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return uint64(len(contents)), nil
|
|
|
|
}
|
|
|
|
|
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-11-20 22:11:49 +00:00
|
|
|
if path[len(path)-1] != '/' {
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
subPathMatcher, err := regexp.Compile(fmt.Sprintf("^%s[^/]+", path))
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
2014-11-20 22:11:49 +00:00
|
|
|
// we use map to collect unique keys
|
2014-10-21 22:02:20 +00:00
|
|
|
keySet := make(map[string]struct{})
|
|
|
|
for k := range d.storage {
|
|
|
|
if key := subPathMatcher.FindString(k); key != "" {
|
|
|
|
keySet[key] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(keySet))
|
|
|
|
for k := range keySet {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
contents, ok := d.storage[sourcePath]
|
|
|
|
if !ok {
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: sourcePath}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
d.storage[destPath] = contents
|
|
|
|
delete(d.storage, sourcePath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-11-17 23:44:07 +00:00
|
|
|
var subPaths []string
|
2014-10-21 22:02:20 +00:00
|
|
|
for k := range d.storage {
|
|
|
|
if strings.HasPrefix(k, path) {
|
|
|
|
subPaths = append(subPaths, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(subPaths) == 0 {
|
2014-11-13 01:19:19 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, subPath := range subPaths {
|
|
|
|
delete(d.storage, subPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|