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