2014-10-21 22:02:20 +00:00
|
|
|
package storagedriver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-12-11 22:11:47 +00:00
|
|
|
"regexp"
|
2014-11-06 20:16:14 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-10-21 22:02:20 +00:00
|
|
|
)
|
|
|
|
|
2014-11-17 23:44:07 +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.
|
2014-11-06 20:16:14 +00:00
|
|
|
type Version string
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Major returns the major (primary) component of a version.
|
2014-11-06 20:16:14 +00:00
|
|
|
func (version Version) Major() uint {
|
|
|
|
majorPart := strings.Split(string(version), ".")[0]
|
|
|
|
major, _ := strconv.ParseUint(majorPart, 10, 0)
|
|
|
|
return uint(major)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Minor returns the minor (secondary) component of a version.
|
2014-11-06 20:16:14 +00:00
|
|
|
func (version Version) Minor() uint {
|
|
|
|
minorPart := strings.Split(string(version), ".")[1]
|
|
|
|
minor, _ := strconv.ParseUint(minorPart, 10, 0)
|
|
|
|
return uint(minor)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// CurrentVersion is the current storage driver Version.
|
2014-11-06 20:16:14 +00:00
|
|
|
const CurrentVersion Version = "0.1"
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// StorageDriver defines methods that a Storage Driver must implement for a
|
|
|
|
// filesystem-like key/value object storage.
|
2014-10-21 22:02:20 +00:00
|
|
|
type StorageDriver interface {
|
2014-11-17 23:44:07 +00:00
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
// This should primarily be used for small objects.
|
2014-10-21 22:02:20 +00:00
|
|
|
GetContent(path string) ([]byte, error)
|
2014-10-29 01:15:40 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
// This should primarily be used for small objects.
|
2014-10-21 22:02:20 +00:00
|
|
|
PutContent(path string, content []byte) error
|
2014-10-29 01:15:40 +00:00
|
|
|
|
2014-11-17 23:44:07 +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.
|
2014-12-03 03:01:00 +00:00
|
|
|
ReadStream(path string, offset int64) (io.ReadCloser, error)
|
2014-10-29 01:15:40 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// WriteStream stores the contents of the provided io.ReadCloser at a
|
|
|
|
// location designated by the given path.
|
|
|
|
// The driver will know it has received the full contents when it has read
|
|
|
|
// "size" bytes.
|
|
|
|
// 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.
|
2014-12-03 05:47:28 +00:00
|
|
|
WriteStream(path string, offset int64, reader io.Reader) (nn int64, err error)
|
2014-10-29 01:15:40 +00:00
|
|
|
|
2014-12-03 05:00:42 +00:00
|
|
|
// Stat retrieves the FileInfo for the given path, including the current
|
|
|
|
// size in bytes and the creation time.
|
|
|
|
Stat(path string) (FileInfo, error)
|
2014-10-29 01:15:40 +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.
|
2014-11-04 17:52:24 +00:00
|
|
|
List(path string) ([]string, error)
|
2014-10-29 01:15:40 +00:00
|
|
|
|
2014-11-17 23:44:07 +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.
|
2014-10-21 22:02:20 +00:00
|
|
|
Move(sourcePath string, destPath string) error
|
2014-10-29 01:15:40 +00:00
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
2014-10-21 22:02:20 +00:00
|
|
|
Delete(path string) error
|
|
|
|
}
|
|
|
|
|
2014-12-11 22:11:47 +00:00
|
|
|
// PathComponentRegexp is the regular expression which each repository path
|
|
|
|
// component must match.
|
|
|
|
// A component of a repository path must be at least two characters, optionally
|
|
|
|
// separated by periods, dashes or underscores.
|
|
|
|
var PathComponentRegexp = regexp.MustCompile(`[a-z0-9]+([._-]?[a-z0-9])+`)
|
|
|
|
|
|
|
|
// PathRegexp is the regular expression which each repository path must match.
|
|
|
|
// A repository path is absolute, beginning with a slash and containing a
|
|
|
|
// positive number of path components separated by slashes.
|
|
|
|
var PathRegexp = regexp.MustCompile(`^(/[a-z0-9]+([._-]?[a-z0-9])+)+$`)
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// PathNotFoundError is returned when operating on a nonexistent path.
|
2014-10-21 22:02:20 +00:00
|
|
|
type PathNotFoundError struct {
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err PathNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("Path not found: %s", err.Path)
|
|
|
|
}
|
|
|
|
|
2014-12-11 22:11:47 +00:00
|
|
|
// InvalidPathError is returned when the provided path is malformed.
|
|
|
|
type InvalidPathError struct {
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err InvalidPathError) Error() string {
|
|
|
|
return fmt.Sprintf("Invalid path: %s", err.Path)
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// InvalidOffsetError is returned when attempting to read or write from an
|
|
|
|
// invalid offset.
|
2014-10-21 22:02:20 +00:00
|
|
|
type InvalidOffsetError struct {
|
|
|
|
Path string
|
2014-12-03 03:01:00 +00:00
|
|
|
Offset int64
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err InvalidOffsetError) Error() string {
|
|
|
|
return fmt.Sprintf("Invalid offset: %d for path: %s", err.Offset, err.Path)
|
|
|
|
}
|