github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/driver/storagedriver.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"regexp"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/docker/distribution/context"
    11  )
    12  
    13  // Version is a string representing the storage driver version, of the form
    14  // Major.Minor.
    15  // The registry must accept storage drivers with equal major version and greater
    16  // minor version, but may not be compatible with older storage driver versions.
    17  type Version string
    18  
    19  // Major returns the major (primary) component of a version.
    20  func (version Version) Major() uint {
    21  	majorPart := strings.Split(string(version), ".")[0]
    22  	major, _ := strconv.ParseUint(majorPart, 10, 0)
    23  	return uint(major)
    24  }
    25  
    26  // Minor returns the minor (secondary) component of a version.
    27  func (version Version) Minor() uint {
    28  	minorPart := strings.Split(string(version), ".")[1]
    29  	minor, _ := strconv.ParseUint(minorPart, 10, 0)
    30  	return uint(minor)
    31  }
    32  
    33  // CurrentVersion is the current storage driver Version.
    34  const CurrentVersion Version = "0.1"
    35  
    36  // StorageDriver defines methods that a Storage Driver must implement for a
    37  // filesystem-like key/value object storage.
    38  type StorageDriver interface {
    39  	// Name returns the human-readable "name" of the driver, useful in error
    40  	// messages and logging. By convention, this will just be the registration
    41  	// name, but drivers may provide other information here.
    42  	Name() string
    43  
    44  	// GetContent retrieves the content stored at "path" as a []byte.
    45  	// This should primarily be used for small objects.
    46  	GetContent(ctx context.Context, path string) ([]byte, error)
    47  
    48  	// PutContent stores the []byte content at a location designated by "path".
    49  	// This should primarily be used for small objects.
    50  	PutContent(ctx context.Context, path string, content []byte) error
    51  
    52  	// ReadStream retrieves an io.ReadCloser for the content stored at "path"
    53  	// with a given byte offset.
    54  	// May be used to resume reading a stream by providing a nonzero offset.
    55  	ReadStream(ctx context.Context, path string, offset int64) (io.ReadCloser, error)
    56  
    57  	// WriteStream stores the contents of the provided io.ReadCloser at a
    58  	// location designated by the given path.
    59  	// May be used to resume writing a stream by providing a nonzero offset.
    60  	// The offset must be no larger than the CurrentSize for this path.
    61  	WriteStream(ctx context.Context, path string, offset int64, reader io.Reader) (nn int64, err error)
    62  
    63  	// Stat retrieves the FileInfo for the given path, including the current
    64  	// size in bytes and the creation time.
    65  	Stat(ctx context.Context, path string) (FileInfo, error)
    66  
    67  	// List returns a list of the objects that are direct descendants of the
    68  	//given path.
    69  	List(ctx context.Context, path string) ([]string, error)
    70  
    71  	// Move moves an object stored at sourcePath to destPath, removing the
    72  	// original object.
    73  	// Note: This may be no more efficient than a copy followed by a delete for
    74  	// many implementations.
    75  	Move(ctx context.Context, sourcePath string, destPath string) error
    76  
    77  	// Delete recursively deletes all objects stored at "path" and its subpaths.
    78  	Delete(ctx context.Context, path string) error
    79  
    80  	// URLFor returns a URL which may be used to retrieve the content stored at
    81  	// the given path, possibly using the given options.
    82  	// May return an ErrUnsupportedMethod in certain StorageDriver
    83  	// implementations.
    84  	URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error)
    85  }
    86  
    87  // PathRegexp is the regular expression which each file path must match. A
    88  // file path is absolute, beginning with a slash and containing a positive
    89  // number of path components separated by slashes, where each component is
    90  // restricted to alphanumeric characters or a period, underscore, or
    91  // hyphen.
    92  var PathRegexp = regexp.MustCompile(`^(/[A-Za-z0-9._-]+)+$`)
    93  
    94  // ErrUnsupportedMethod may be returned in the case where a StorageDriver implementation does not support an optional method.
    95  type ErrUnsupportedMethod struct {
    96  	DriverName string
    97  }
    98  
    99  func (err ErrUnsupportedMethod) Error() string {
   100  	return fmt.Sprintf("%s: unsupported method", err.DriverName)
   101  }
   102  
   103  // PathNotFoundError is returned when operating on a nonexistent path.
   104  type PathNotFoundError struct {
   105  	Path       string
   106  	DriverName string
   107  }
   108  
   109  func (err PathNotFoundError) Error() string {
   110  	return fmt.Sprintf("%s: Path not found: %s", err.DriverName, err.Path)
   111  }
   112  
   113  // InvalidPathError is returned when the provided path is malformed.
   114  type InvalidPathError struct {
   115  	Path       string
   116  	DriverName string
   117  }
   118  
   119  func (err InvalidPathError) Error() string {
   120  	return fmt.Sprintf("%s: invalid path: %s", err.DriverName, err.Path)
   121  }
   122  
   123  // InvalidOffsetError is returned when attempting to read or write from an
   124  // invalid offset.
   125  type InvalidOffsetError struct {
   126  	Path       string
   127  	Offset     int64
   128  	DriverName string
   129  }
   130  
   131  func (err InvalidOffsetError) Error() string {
   132  	return fmt.Sprintf("%s: invalid offset: %d for path: %s", err.DriverName, err.Offset, err.Path)
   133  }
   134  
   135  // Error is a catch-all error type which captures an error string and
   136  // the driver type on which it occured.
   137  type Error struct {
   138  	DriverName string
   139  	Enclosed   error
   140  }
   141  
   142  func (err Error) Error() string {
   143  	return fmt.Sprintf("%s: %s", err.DriverName, err.Enclosed)
   144  }