2015-02-11 02:14:23 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-04-27 22:58:58 +00:00
|
|
|
|
|
|
|
"github.com/docker/distribution/context"
|
2015-02-11 02:14:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Version is a string representing the storage driver version, of the form
|
|
|
|
// Major.Minor.
|
|
|
|
// The registry must accept storage drivers with equal major version and greater
|
|
|
|
// minor version, but may not be compatible with older storage driver versions.
|
|
|
|
type Version string
|
|
|
|
|
|
|
|
// Major returns the major (primary) component of a version.
|
|
|
|
func (version Version) Major() uint {
|
|
|
|
majorPart := strings.Split(string(version), ".")[0]
|
|
|
|
major, _ := strconv.ParseUint(majorPart, 10, 0)
|
|
|
|
return uint(major)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minor returns the minor (secondary) component of a version.
|
|
|
|
func (version Version) Minor() uint {
|
|
|
|
minorPart := strings.Split(string(version), ".")[1]
|
|
|
|
minor, _ := strconv.ParseUint(minorPart, 10, 0)
|
|
|
|
return uint(minor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentVersion is the current storage driver Version.
|
|
|
|
const CurrentVersion Version = "0.1"
|
|
|
|
|
|
|
|
// StorageDriver defines methods that a Storage Driver must implement for a
|
|
|
|
// filesystem-like key/value object storage.
|
|
|
|
type StorageDriver interface {
|
2015-04-23 00:30:01 +00:00
|
|
|
// Name returns the human-readable "name" of the driver, useful in error
|
|
|
|
// messages and logging. By convention, this will just be the registration
|
|
|
|
// name, but drivers may provide other information here.
|
|
|
|
Name() string
|
|
|
|
|
2015-02-11 02:14:23 +00:00
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
// This should primarily be used for small objects.
|
2015-04-27 22:58:58 +00:00
|
|
|
GetContent(ctx context.Context, path string) ([]byte, error)
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
// This should primarily be used for small objects.
|
2015-04-27 22:58:58 +00:00
|
|
|
PutContent(ctx context.Context, path string, content []byte) error
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// ReadStream retrieves an io.ReadCloser for the content stored at "path"
|
|
|
|
// with a given byte offset.
|
|
|
|
// May be used to resume reading a stream by providing a nonzero offset.
|
2015-04-27 22:58:58 +00:00
|
|
|
ReadStream(ctx context.Context, path string, offset int64) (io.ReadCloser, error)
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// WriteStream stores the contents of the provided io.ReadCloser at a
|
|
|
|
// location designated by the given path.
|
|
|
|
// May be used to resume writing a stream by providing a nonzero offset.
|
|
|
|
// The offset must be no larger than the CurrentSize for this path.
|
2015-04-27 22:58:58 +00:00
|
|
|
WriteStream(ctx context.Context, path string, offset int64, reader io.Reader) (nn int64, err error)
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// Stat retrieves the FileInfo for the given path, including the current
|
|
|
|
// size in bytes and the creation time.
|
2015-04-27 22:58:58 +00:00
|
|
|
Stat(ctx context.Context, path string) (FileInfo, error)
|
2015-02-11 02:14:23 +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
|
|
|
List(ctx context.Context, path string) ([]string, error)
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the
|
|
|
|
// original object.
|
|
|
|
// Note: This may be no more efficient than a copy followed by a delete for
|
|
|
|
// many implementations.
|
2015-04-27 22:58:58 +00:00
|
|
|
Move(ctx context.Context, sourcePath string, destPath string) error
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
2015-04-27 22:58:58 +00:00
|
|
|
Delete(ctx context.Context, path string) error
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// URLFor returns a URL which may be used to retrieve the content stored at
|
|
|
|
// the given path, possibly using the given options.
|
2015-03-13 02:31:41 +00:00
|
|
|
// May return an ErrUnsupportedMethod in certain StorageDriver
|
2015-02-11 02:14:23 +00:00
|
|
|
// implementations.
|
2015-04-27 22:58:58 +00:00
|
|
|
URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error)
|
2015-02-11 02:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PathRegexp is the regular expression which each file path must match. A
|
|
|
|
// file path is absolute, beginning with a slash and containing a positive
|
|
|
|
// number of path components separated by slashes, where each component is
|
|
|
|
// restricted to lowercase alphanumeric characters or a period, underscore, or
|
|
|
|
// hyphen.
|
2015-04-07 21:14:45 +00:00
|
|
|
var PathRegexp = regexp.MustCompile(`^(/[A-Za-z0-9._-]+)+$`)
|
2015-02-11 02:14:23 +00:00
|
|
|
|
2015-03-13 02:31:41 +00:00
|
|
|
// ErrUnsupportedMethod may be returned in the case where a StorageDriver implementation does not support an optional method.
|
2015-10-02 23:19:06 +00:00
|
|
|
type ErrUnsupportedMethod struct {
|
|
|
|
DriverName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrUnsupportedMethod) Error() string {
|
|
|
|
return fmt.Sprintf("[%s] unsupported method", err.DriverName)
|
|
|
|
}
|
2015-02-11 02:14:23 +00:00
|
|
|
|
|
|
|
// PathNotFoundError is returned when operating on a nonexistent path.
|
|
|
|
type PathNotFoundError struct {
|
2015-10-02 23:19:06 +00:00
|
|
|
Path string
|
|
|
|
DriverName string
|
2015-02-11 02:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err PathNotFoundError) Error() string {
|
2015-10-02 23:19:06 +00:00
|
|
|
return fmt.Sprintf("[%s] Path not found: %s", err.DriverName, err.Path)
|
2015-02-11 02:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidPathError is returned when the provided path is malformed.
|
|
|
|
type InvalidPathError struct {
|
2015-10-02 23:19:06 +00:00
|
|
|
Path string
|
|
|
|
DriverName string
|
2015-02-11 02:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err InvalidPathError) Error() string {
|
2015-10-02 23:19:06 +00:00
|
|
|
return fmt.Sprintf("[%s] Invalid path: %s", err.DriverName, err.Path)
|
2015-02-11 02:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidOffsetError is returned when attempting to read or write from an
|
|
|
|
// invalid offset.
|
|
|
|
type InvalidOffsetError struct {
|
2015-10-02 23:19:06 +00:00
|
|
|
Path string
|
|
|
|
Offset int64
|
|
|
|
DriverName string
|
2015-02-11 02:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err InvalidOffsetError) Error() string {
|
2015-10-02 23:19:06 +00:00
|
|
|
return fmt.Sprintf("[%s] Invalid offset: %d for path: %s", err.DriverName, err.Offset, err.Path)
|
2015-02-11 02:14:23 +00:00
|
|
|
}
|
2015-11-02 21:23:53 +00:00
|
|
|
|
|
|
|
// Error is a catch-all error type which captures an error string and
|
|
|
|
// the driver type on which it occured.
|
|
|
|
type Error struct {
|
|
|
|
DriverName string
|
|
|
|
Enclosed error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err Error) Error() string {
|
|
|
|
return fmt.Sprintf("[%s] %s", err.DriverName, err.Enclosed)
|
|
|
|
}
|