2014-10-21 22:02:20 +00:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2014-10-21 22:02:20 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
2014-12-05 04:14:41 +00:00
|
|
|
"time"
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/base"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/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{}
|
|
|
|
|
2014-12-18 03:06:55 +00:00
|
|
|
func (factory *inMemoryDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2014-10-29 01:15:40 +00:00
|
|
|
return New(), nil
|
|
|
|
}
|
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
type driver struct {
|
|
|
|
root *dir
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// baseEmbed allows us to hide the Base embed.
|
|
|
|
type baseEmbed struct {
|
|
|
|
base.Base
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-02-04 00:54:52 +00:00
|
|
|
baseEmbed // embedded, hidden base driver.
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
var _ storagedriver.StorageDriver = &Driver{}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// New constructs a new Driver.
|
|
|
|
func New() *Driver {
|
2015-02-04 00:54:52 +00:00
|
|
|
return &Driver{
|
|
|
|
baseEmbed: baseEmbed{
|
|
|
|
Base: base.Base{
|
|
|
|
StorageDriver: &driver{
|
|
|
|
root: &dir{
|
|
|
|
common: common{
|
|
|
|
p: "/",
|
|
|
|
mod: time.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-12-05 04:14:41 +00:00
|
|
|
},
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
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
|
|
|
|
2015-04-23 00:30:01 +00:00
|
|
|
func (d *driver) Name() string {
|
|
|
|
return driverName
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (d *driver) GetContent(ctx context.Context, 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
|
|
|
|
2018-05-30 16:00:57 +00:00
|
|
|
rc, err := d.reader(ctx, path, 0)
|
2014-12-05 04:14:41 +00:00
|
|
|
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()
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
return io.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".
|
2015-04-27 22:58:58 +00:00
|
|
|
func (d *driver) PutContent(ctx context.Context, 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
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
normalized := normalize(p)
|
|
|
|
|
|
|
|
f, err := d.root.mkfile(normalized)
|
2014-12-05 04:14:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
// Reader retrieves an io.ReadCloser for the content stored at "path" with a
|
2014-11-17 23:44:07 +00:00
|
|
|
// given byte offset.
|
2016-02-08 22:29:21 +00:00
|
|
|
func (d *driver) Reader(ctx context.Context, 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
|
|
|
|
2018-05-30 16:00:57 +00:00
|
|
|
return d.reader(ctx, path, offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) reader(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
2014-12-05 19:46:41 +00:00
|
|
|
if offset < 0 {
|
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
normalized := normalize(path)
|
|
|
|
found := d.root.find(normalized)
|
2014-12-05 04:14:41 +00:00
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
if found.path() != normalized {
|
2014-12-05 04:14:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:55:22 +00:00
|
|
|
return io.NopCloser(found.(*file).sectionReader(offset)), nil
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
// Writer returns a FileWriter which will store the content written to it
|
|
|
|
// at the location designated by "path" after the call to Commit.
|
|
|
|
func (d *driver) Writer(ctx context.Context, path string, append bool) (storagedriver.FileWriter, error) {
|
2014-12-05 04:14:41 +00:00
|
|
|
d.mutex.Lock()
|
|
|
|
defer d.mutex.Unlock()
|
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 {
|
2016-02-08 22:29:21 +00:00
|
|
|
return nil, fmt.Errorf("not a file")
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
if !append {
|
|
|
|
f.truncate()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
return d.newWriter(f), nil
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 04:14:41 +00:00
|
|
|
// Stat returns info about the provided path.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (d *driver) Stat(ctx context.Context, 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)
|
2016-02-08 22:29:21 +00:00
|
|
|
found := d.root.find(normalized)
|
2014-12-05 04:14:41 +00:00
|
|
|
|
|
|
|
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.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (d *driver) List(ctx context.Context, path string) ([]string, error) {
|
2015-02-02 19:56:09 +00:00
|
|
|
d.mutex.RLock()
|
|
|
|
defer d.mutex.RUnlock()
|
|
|
|
|
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.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (d *driver) Move(ctx context.Context, 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.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (d *driver) Delete(ctx context.Context, 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
|
|
|
}
|
2015-01-07 16:31:38 +00:00
|
|
|
|
|
|
|
// URLFor returns a URL which may be used to retrieve the content stored at the given path.
|
|
|
|
// May return an UnsupportedMethodErr in certain StorageDriver implementations.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (d *driver) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
|
2015-11-02 21:23:53 +00:00
|
|
|
return "", storagedriver.ErrUnsupportedMethod{}
|
2015-01-07 16:31:38 +00:00
|
|
|
}
|
2016-02-08 22:29:21 +00:00
|
|
|
|
2017-11-29 19:17:39 +00:00
|
|
|
// Walk traverses a filesystem defined within driver, starting
|
2021-06-24 18:42:02 +00:00
|
|
|
// from the given path, calling f on each file and directory
|
2022-07-10 02:04:50 +00:00
|
|
|
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
|
|
|
|
return storagedriver.WalkFallback(ctx, d, path, f, options...)
|
2017-11-29 19:17:39 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
type writer struct {
|
|
|
|
d *driver
|
|
|
|
f *file
|
2018-11-15 10:36:19 +00:00
|
|
|
buffer []byte
|
|
|
|
buffSize int
|
2016-02-08 22:29:21 +00:00
|
|
|
closed bool
|
|
|
|
committed bool
|
|
|
|
cancelled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) newWriter(f *file) storagedriver.FileWriter {
|
|
|
|
return &writer{
|
|
|
|
d: d,
|
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Write(p []byte) (int, error) {
|
|
|
|
if w.closed {
|
|
|
|
return 0, fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return 0, fmt.Errorf("already committed")
|
|
|
|
} else if w.cancelled {
|
|
|
|
return 0, fmt.Errorf("already cancelled")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.d.mutex.Lock()
|
|
|
|
defer w.d.mutex.Unlock()
|
2018-11-15 10:36:19 +00:00
|
|
|
if cap(w.buffer) < len(p)+w.buffSize {
|
|
|
|
data := make([]byte, len(w.buffer), len(p)+w.buffSize)
|
|
|
|
copy(data, w.buffer)
|
|
|
|
w.buffer = data
|
|
|
|
}
|
|
|
|
|
|
|
|
w.buffer = w.buffer[:w.buffSize+len(p)]
|
|
|
|
n := copy(w.buffer[w.buffSize:w.buffSize+len(p)], p)
|
|
|
|
w.buffSize += n
|
2016-02-08 22:29:21 +00:00
|
|
|
|
2018-11-15 10:36:19 +00:00
|
|
|
return n, nil
|
2016-02-08 22:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Size() int64 {
|
|
|
|
w.d.mutex.RLock()
|
|
|
|
defer w.d.mutex.RUnlock()
|
|
|
|
|
|
|
|
return int64(len(w.f.data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Close() error {
|
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
}
|
|
|
|
w.closed = true
|
2018-11-15 10:36:19 +00:00
|
|
|
w.flush()
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 03:58:17 +00:00
|
|
|
func (w *writer) Cancel(ctx context.Context) error {
|
2016-02-08 22:29:21 +00:00
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return fmt.Errorf("already committed")
|
|
|
|
}
|
|
|
|
w.cancelled = true
|
|
|
|
|
|
|
|
w.d.mutex.Lock()
|
|
|
|
defer w.d.mutex.Unlock()
|
|
|
|
|
|
|
|
return w.d.root.delete(w.f.path())
|
|
|
|
}
|
|
|
|
|
2023-10-18 09:34:10 +00:00
|
|
|
func (w *writer) Commit(ctx context.Context) error {
|
2016-02-08 22:29:21 +00:00
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return fmt.Errorf("already committed")
|
|
|
|
} else if w.cancelled {
|
|
|
|
return fmt.Errorf("already cancelled")
|
|
|
|
}
|
|
|
|
w.committed = true
|
2018-11-15 10:36:19 +00:00
|
|
|
w.flush()
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-15 10:36:19 +00:00
|
|
|
|
|
|
|
func (w *writer) flush() {
|
|
|
|
w.d.mutex.Lock()
|
|
|
|
defer w.d.mutex.Unlock()
|
|
|
|
|
|
|
|
w.f.WriteAt(w.buffer, int64(len(w.f.data)))
|
|
|
|
w.buffer = []byte{}
|
|
|
|
w.buffSize = 0
|
|
|
|
}
|