2015-02-04 00:54:52 +00:00
|
|
|
// Package base provides a base implementation of the storage driver that can
|
|
|
|
// be used to implement common checks. The goal is to increase the amount of
|
|
|
|
// code sharing.
|
|
|
|
//
|
|
|
|
// The canonical approach to use this class is to embed in the exported driver
|
|
|
|
// struct such that calls are proxied through this implementation. First,
|
|
|
|
// declare the internal driver, as follows:
|
|
|
|
//
|
|
|
|
// type driver struct { ... internal ...}
|
|
|
|
//
|
|
|
|
// The resulting type should implement StorageDriver such that it can be the
|
|
|
|
// target of a Base struct. The exported type can then be declared as follows:
|
|
|
|
//
|
|
|
|
// type Driver struct {
|
|
|
|
// Base
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Because Driver embeds Base, it effectively implements Base. If the driver
|
|
|
|
// needs to intercept a call, before going to base, Driver should implement
|
|
|
|
// that method. Effectively, Driver can intercept calls before coming in and
|
|
|
|
// driver implements the actual logic.
|
|
|
|
//
|
|
|
|
// To further shield the embed from other packages, it is recommended to
|
|
|
|
// employ a private embed struct:
|
|
|
|
//
|
|
|
|
// type baseEmbed struct {
|
|
|
|
// base.Base
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Then, declare driver to embed baseEmbed, rather than Base directly:
|
|
|
|
//
|
|
|
|
// type Driver struct {
|
|
|
|
// baseEmbed
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The type now implements StorageDriver, proxying through Base, without
|
2015-04-16 18:37:31 +00:00
|
|
|
// exporting an unnecessary field.
|
2015-02-04 00:54:52 +00:00
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2015-04-10 01:50:57 +00:00
|
|
|
"github.com/docker/distribution/context"
|
2015-02-11 02:14:23 +00:00
|
|
|
storagedriver "github.com/docker/distribution/registry/storage/driver"
|
2015-02-04 00:54:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Base provides a wrapper around a storagedriver implementation that provides
|
|
|
|
// common path and bounds checking.
|
|
|
|
type Base struct {
|
|
|
|
storagedriver.StorageDriver
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContent wraps GetContent of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) GetContent(ctx context.Context, path string) ([]byte, error) {
|
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.GetContent(%q)", base.Name(), path)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.GetContent(ctx, path)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutContent wraps PutContent of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) PutContent(ctx context.Context, path string, content []byte) error {
|
2015-06-30 17:28:14 +00:00
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.PutContent(%q)", base.Name(), path)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.PutContent(ctx, path, content)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadStream wraps ReadStream of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) ReadStream(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
2015-06-30 17:28:14 +00:00
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.ReadStream(%q, %d)", base.Name(), path, offset)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if offset < 0 {
|
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.ReadStream(ctx, path, offset)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteStream wraps WriteStream of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) WriteStream(ctx context.Context, path string, offset int64, reader io.Reader) (nn int64, err error) {
|
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.WriteStream(%q, %d)", base.Name(), path, offset)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if offset < 0 {
|
|
|
|
return 0, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return 0, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.WriteStream(ctx, path, offset, reader)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat wraps Stat of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {
|
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.Stat(%q)", base.Name(), path)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.Stat(ctx, path)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List wraps List of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) List(ctx context.Context, path string) ([]string, error) {
|
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.List(%q)", base.Name(), path)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
|
|
|
return nil, storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.List(ctx, path)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move wraps Move of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) Move(ctx context.Context, sourcePath string, destPath string) error {
|
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.Move(%q, %q", base.Name(), sourcePath, destPath)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(sourcePath) {
|
|
|
|
return storagedriver.InvalidPathError{Path: sourcePath}
|
|
|
|
} else if !storagedriver.PathRegexp.MatchString(destPath) {
|
|
|
|
return storagedriver.InvalidPathError{Path: destPath}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.Move(ctx, sourcePath, destPath)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete wraps Delete of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) Delete(ctx context.Context, path string) error {
|
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.Delete(%q)", base.Name(), path)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.Delete(ctx, path)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// URLFor wraps URLFor of underlying storage driver.
|
2015-04-27 22:58:58 +00:00
|
|
|
func (base *Base) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
|
|
|
|
ctx, done := context.WithTrace(ctx)
|
2015-04-23 00:30:31 +00:00
|
|
|
defer done("%s.URLFor(%q)", base.Name(), path)
|
2015-04-10 01:50:57 +00:00
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
|
|
|
return "", storagedriver.InvalidPathError{Path: path}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return base.StorageDriver.URLFor(ctx, path, options)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|