2014-12-05 22:05:37 +00:00
|
|
|
// +build ignore
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
|
2014-11-06 20:16:14 +00:00
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
2014-10-21 22:02:20 +00:00
|
|
|
"github.com/docker/libchan"
|
|
|
|
)
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// StorageDriver is the interface which IPC storage drivers must implement. As external storage
|
2014-11-06 20:16:14 +00:00
|
|
|
// drivers may be defined to use a different version of the storagedriver.StorageDriver interface,
|
|
|
|
// we use an additional version check to determine compatiblity.
|
2014-11-17 23:44:07 +00:00
|
|
|
type StorageDriver interface {
|
2014-11-06 20:16:14 +00:00
|
|
|
// Version returns the storagedriver.StorageDriver interface version which this storage driver
|
|
|
|
// implements, which is used to determine driver compatibility
|
|
|
|
Version() (storagedriver.Version, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncompatibleVersionError is returned when a storage driver is using an incompatible version of
|
|
|
|
// the storagedriver.StorageDriver api
|
|
|
|
type IncompatibleVersionError struct {
|
|
|
|
version storagedriver.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e IncompatibleVersionError) Error() string {
|
|
|
|
return fmt.Sprintf("Incompatible storage driver version: %s", e.version)
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// Request defines a remote method call request
|
2014-10-29 01:15:40 +00:00
|
|
|
// A return value struct is to be sent over the ResponseChannel
|
2014-10-21 22:02:20 +00:00
|
|
|
type Request struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Type string `codec:",omitempty"`
|
|
|
|
Parameters map[string]interface{} `codec:",omitempty"`
|
|
|
|
ResponseChannel libchan.Sender `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// ResponseError is a serializable error type.
|
2014-11-19 01:41:48 +00:00
|
|
|
// The Type and Parameters may be used to reconstruct the same error on the
|
|
|
|
// client side, falling back to using the Type and Message if this cannot be
|
|
|
|
// done.
|
2014-11-17 23:44:07 +00:00
|
|
|
type ResponseError struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Type string `codec:",omitempty"`
|
|
|
|
Message string `codec:",omitempty"`
|
|
|
|
Parameters map[string]interface{} `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// WrapError wraps an error in a serializable struct containing the error's type
|
|
|
|
// and message.
|
|
|
|
func WrapError(err error) *ResponseError {
|
2014-10-21 22:02:20 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-19 01:41:48 +00:00
|
|
|
v := reflect.ValueOf(err)
|
|
|
|
re := ResponseError{
|
|
|
|
Type: v.Type().String(),
|
2014-10-21 22:02:20 +00:00
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-11-19 01:41:48 +00:00
|
|
|
|
|
|
|
if v.Kind() == reflect.Struct {
|
|
|
|
re.Parameters = make(map[string]interface{})
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
field := v.Type().Field(i)
|
|
|
|
re.Parameters[field.Name] = v.Field(i).Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &re
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap returns the underlying error if it can be reconstructed, or the
|
|
|
|
// original ResponseError otherwise.
|
|
|
|
func (err *ResponseError) Unwrap() error {
|
|
|
|
var errVal reflect.Value
|
|
|
|
var zeroVal reflect.Value
|
|
|
|
|
|
|
|
switch err.Type {
|
|
|
|
case "storagedriver.PathNotFoundError":
|
|
|
|
errVal = reflect.ValueOf(&storagedriver.PathNotFoundError{})
|
|
|
|
case "storagedriver.InvalidOffsetError":
|
|
|
|
errVal = reflect.ValueOf(&storagedriver.InvalidOffsetError{})
|
|
|
|
}
|
|
|
|
if errVal == zeroVal {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range err.Parameters {
|
|
|
|
fieldVal := errVal.Elem().FieldByName(k)
|
|
|
|
if fieldVal == zeroVal {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fieldVal.Set(reflect.ValueOf(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
if unwrapped, ok := errVal.Elem().Interface().(error); ok {
|
|
|
|
return unwrapped
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
func (err *ResponseError) Error() string {
|
2014-10-21 22:02:20 +00:00
|
|
|
return fmt.Sprintf("%s: %s", err.Type, err.Message)
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// IPC method call response object definitions
|
|
|
|
|
2014-11-06 20:16:14 +00:00
|
|
|
// VersionResponse is a response for a Version request
|
|
|
|
type VersionResponse struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Version storagedriver.Version `codec:",omitempty"`
|
|
|
|
Error *ResponseError `codec:",omitempty"`
|
2014-11-06 20:16:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// ReadStreamResponse is a response for a ReadStream request
|
2014-10-21 22:02:20 +00:00
|
|
|
type ReadStreamResponse struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Reader io.ReadCloser `codec:",omitempty"`
|
|
|
|
Error *ResponseError `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// WriteStreamResponse is a response for a WriteStream request
|
2014-10-21 22:02:20 +00:00
|
|
|
type WriteStreamResponse struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Error *ResponseError `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 20:58:48 +00:00
|
|
|
// CurrentSizeResponse is a response for a CurrentSize request
|
|
|
|
type CurrentSizeResponse struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Position uint64 `codec:",omitempty"`
|
|
|
|
Error *ResponseError `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// ListResponse is a response for a List request
|
2014-10-21 22:02:20 +00:00
|
|
|
type ListResponse struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Keys []string `codec:",omitempty"`
|
|
|
|
Error *ResponseError `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// MoveResponse is a response for a Move request
|
2014-10-21 22:02:20 +00:00
|
|
|
type MoveResponse struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Error *ResponseError `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// DeleteResponse is a response for a Delete request
|
2014-10-21 22:02:20 +00:00
|
|
|
type DeleteResponse struct {
|
2014-11-20 22:11:49 +00:00
|
|
|
Error *ResponseError `codec:",omitempty"`
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|