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 (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-02-04 00:54:52 +00:00
|
|
|
"io"
|
2018-08-06 19:12:07 +00:00
|
|
|
"time"
|
2015-02-04 00:54:52 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
dcontext "github.com/distribution/distribution/v3/context"
|
|
|
|
prometheus "github.com/distribution/distribution/v3/metrics"
|
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
2017-11-17 00:43:38 +00:00
|
|
|
"github.com/docker/go-metrics"
|
2015-02-04 00:54:52 +00:00
|
|
|
)
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
var (
|
|
|
|
// storageAction is the metrics of blob related operations
|
|
|
|
storageAction = prometheus.StorageNamespace.NewLabeledTimer("action", "The number of seconds that the storage action takes", "driver", "action")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
metrics.Register(prometheus.StorageNamespace)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-02 23:19:06 +00:00
|
|
|
// Format errors received from the storage driver
|
|
|
|
func (base *Base) setDriverName(e error) error {
|
2015-11-02 21:23:53 +00:00
|
|
|
switch actual := e.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case storagedriver.ErrUnsupportedMethod:
|
|
|
|
actual.DriverName = base.StorageDriver.Name()
|
|
|
|
return actual
|
|
|
|
case storagedriver.PathNotFoundError:
|
|
|
|
actual.DriverName = base.StorageDriver.Name()
|
|
|
|
return actual
|
|
|
|
case storagedriver.InvalidPathError:
|
|
|
|
actual.DriverName = base.StorageDriver.Name()
|
|
|
|
return actual
|
|
|
|
case storagedriver.InvalidOffsetError:
|
|
|
|
actual.DriverName = base.StorageDriver.Name()
|
|
|
|
return actual
|
|
|
|
default:
|
|
|
|
storageError := storagedriver.Error{
|
|
|
|
DriverName: base.StorageDriver.Name(),
|
|
|
|
Enclosed: e,
|
2015-10-02 23:19:06 +00:00
|
|
|
}
|
2015-11-02 21:23:53 +00:00
|
|
|
|
|
|
|
return storageError
|
2015-10-02 23:19:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 00:54:52 +00:00
|
|
|
// 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) {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.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) {
|
2015-10-02 23:19:06 +00:00
|
|
|
return nil, storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
start := time.Now()
|
2015-10-02 23:19:06 +00:00
|
|
|
b, e := base.StorageDriver.GetContent(ctx, path)
|
2017-11-17 00:43:38 +00:00
|
|
|
storageAction.WithValues(base.Name(), "GetContent").UpdateSince(start)
|
2015-10-02 23:19:06 +00:00
|
|
|
return b, base.setDriverName(e)
|
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 {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.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) {
|
2015-10-02 23:19:06 +00:00
|
|
|
return storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
start := time.Now()
|
|
|
|
err := base.setDriverName(base.StorageDriver.PutContent(ctx, path, content))
|
|
|
|
storageAction.WithValues(base.Name(), "PutContent").UpdateSince(start)
|
|
|
|
return err
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
// Reader wraps Reader of underlying storage driver.
|
|
|
|
func (base *Base) Reader(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.WithTrace(ctx)
|
2016-02-08 22:29:21 +00:00
|
|
|
defer done("%s.Reader(%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 {
|
2015-10-02 23:19:06 +00:00
|
|
|
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
2015-10-02 23:19:06 +00:00
|
|
|
return nil, storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
rc, e := base.StorageDriver.Reader(ctx, path, offset)
|
2015-10-02 23:19:06 +00:00
|
|
|
return rc, base.setDriverName(e)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
// Writer wraps Writer of underlying storage driver.
|
|
|
|
func (base *Base) Writer(ctx context.Context, path string, append bool) (storagedriver.FileWriter, error) {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.WithTrace(ctx)
|
2016-02-08 22:29:21 +00:00
|
|
|
defer done("%s.Writer(%q, %v)", base.Name(), path, append)
|
2015-02-04 00:54:52 +00:00
|
|
|
|
|
|
|
if !storagedriver.PathRegexp.MatchString(path) {
|
2016-02-08 22:29:21 +00:00
|
|
|
return nil, storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 22:29:21 +00:00
|
|
|
writer, e := base.StorageDriver.Writer(ctx, path, append)
|
|
|
|
return writer, base.setDriverName(e)
|
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) {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.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
|
|
|
|
2016-12-21 06:21:29 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
2015-10-02 23:19:06 +00:00
|
|
|
return nil, storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
start := time.Now()
|
2015-10-02 23:19:06 +00:00
|
|
|
fi, e := base.StorageDriver.Stat(ctx, path)
|
2017-11-17 00:43:38 +00:00
|
|
|
storageAction.WithValues(base.Name(), "Stat").UpdateSince(start)
|
2015-10-02 23:19:06 +00:00
|
|
|
return fi, base.setDriverName(e)
|
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) {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.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 != "/" {
|
2015-10-02 23:19:06 +00:00
|
|
|
return nil, storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
start := time.Now()
|
2015-10-02 23:19:06 +00:00
|
|
|
str, e := base.StorageDriver.List(ctx, path)
|
2017-11-17 00:43:38 +00:00
|
|
|
storageAction.WithValues(base.Name(), "List").UpdateSince(start)
|
2015-10-02 23:19:06 +00:00
|
|
|
return str, base.setDriverName(e)
|
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 {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.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) {
|
2015-10-02 23:19:06 +00:00
|
|
|
return storagedriver.InvalidPathError{Path: sourcePath, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
} else if !storagedriver.PathRegexp.MatchString(destPath) {
|
2015-10-02 23:19:06 +00:00
|
|
|
return storagedriver.InvalidPathError{Path: destPath, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
start := time.Now()
|
|
|
|
err := base.setDriverName(base.StorageDriver.Move(ctx, sourcePath, destPath))
|
|
|
|
storageAction.WithValues(base.Name(), "Move").UpdateSince(start)
|
|
|
|
return err
|
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 {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.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) {
|
2015-10-02 23:19:06 +00:00
|
|
|
return storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
start := time.Now()
|
|
|
|
err := base.setDriverName(base.StorageDriver.Delete(ctx, path))
|
|
|
|
storageAction.WithValues(base.Name(), "Delete").UpdateSince(start)
|
|
|
|
return err
|
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) {
|
2017-08-11 22:31:16 +00:00
|
|
|
ctx, done := dcontext.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) {
|
2015-10-02 23:19:06 +00:00
|
|
|
return "", storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:43:38 +00:00
|
|
|
start := time.Now()
|
2015-10-02 23:19:06 +00:00
|
|
|
str, e := base.StorageDriver.URLFor(ctx, path, options)
|
2017-11-17 00:43:38 +00:00
|
|
|
storageAction.WithValues(base.Name(), "URLFor").UpdateSince(start)
|
2015-10-02 23:19:06 +00:00
|
|
|
return str, base.setDriverName(e)
|
2015-02-04 00:54:52 +00:00
|
|
|
}
|
2017-11-29 19:17:39 +00:00
|
|
|
|
|
|
|
// Walk wraps Walk of underlying storage driver.
|
|
|
|
func (base *Base) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
|
|
|
|
ctx, done := dcontext.WithTrace(ctx)
|
|
|
|
defer done("%s.Walk(%q)", base.Name(), path)
|
|
|
|
|
2018-01-18 20:10:20 +00:00
|
|
|
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
|
2017-11-29 19:17:39 +00:00
|
|
|
return storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
|
|
|
|
}
|
|
|
|
|
|
|
|
return base.setDriverName(base.StorageDriver.Walk(ctx, path, f))
|
|
|
|
}
|