github.com/ojongerius/docker@v1.11.2/daemon/graphdriver/proxy.go (about)

     1  // +build experimental
     2  
     3  package graphdriver
     4  
     5  import (
     6  	"errors"
     7  	"fmt"
     8  
     9  	"github.com/docker/docker/pkg/archive"
    10  )
    11  
    12  type graphDriverProxy struct {
    13  	name   string
    14  	client pluginClient
    15  }
    16  
    17  type graphDriverRequest struct {
    18  	ID         string `json:",omitempty"`
    19  	Parent     string `json:",omitempty"`
    20  	MountLabel string `json:",omitempty"`
    21  }
    22  
    23  type graphDriverResponse struct {
    24  	Err      string            `json:",omitempty"`
    25  	Dir      string            `json:",omitempty"`
    26  	Exists   bool              `json:",omitempty"`
    27  	Status   [][2]string       `json:",omitempty"`
    28  	Changes  []archive.Change  `json:",omitempty"`
    29  	Size     int64             `json:",omitempty"`
    30  	Metadata map[string]string `json:",omitempty"`
    31  }
    32  
    33  type graphDriverInitRequest struct {
    34  	Home string
    35  	Opts []string
    36  }
    37  
    38  func (d *graphDriverProxy) Init(home string, opts []string) error {
    39  	args := &graphDriverInitRequest{
    40  		Home: home,
    41  		Opts: opts,
    42  	}
    43  	var ret graphDriverResponse
    44  	if err := d.client.Call("GraphDriver.Init", args, &ret); err != nil {
    45  		return err
    46  	}
    47  	if ret.Err != "" {
    48  		return errors.New(ret.Err)
    49  	}
    50  	return nil
    51  }
    52  
    53  func (d *graphDriverProxy) String() string {
    54  	return d.name
    55  }
    56  
    57  func (d *graphDriverProxy) Create(id, parent, mountLabel string) error {
    58  	args := &graphDriverRequest{
    59  		ID:         id,
    60  		Parent:     parent,
    61  		MountLabel: mountLabel,
    62  	}
    63  	var ret graphDriverResponse
    64  	if err := d.client.Call("GraphDriver.Create", args, &ret); err != nil {
    65  		return err
    66  	}
    67  	if ret.Err != "" {
    68  		return errors.New(ret.Err)
    69  	}
    70  	return nil
    71  }
    72  
    73  func (d *graphDriverProxy) Remove(id string) error {
    74  	args := &graphDriverRequest{ID: id}
    75  	var ret graphDriverResponse
    76  	if err := d.client.Call("GraphDriver.Remove", args, &ret); err != nil {
    77  		return err
    78  	}
    79  	if ret.Err != "" {
    80  		return errors.New(ret.Err)
    81  	}
    82  	return nil
    83  }
    84  
    85  func (d *graphDriverProxy) Get(id, mountLabel string) (string, error) {
    86  	args := &graphDriverRequest{
    87  		ID:         id,
    88  		MountLabel: mountLabel,
    89  	}
    90  	var ret graphDriverResponse
    91  	if err := d.client.Call("GraphDriver.Get", args, &ret); err != nil {
    92  		return "", err
    93  	}
    94  	var err error
    95  	if ret.Err != "" {
    96  		err = errors.New(ret.Err)
    97  	}
    98  	return ret.Dir, err
    99  }
   100  
   101  func (d *graphDriverProxy) Put(id string) error {
   102  	args := &graphDriverRequest{ID: id}
   103  	var ret graphDriverResponse
   104  	if err := d.client.Call("GraphDriver.Put", args, &ret); err != nil {
   105  		return err
   106  	}
   107  	if ret.Err != "" {
   108  		return errors.New(ret.Err)
   109  	}
   110  	return nil
   111  }
   112  
   113  func (d *graphDriverProxy) Exists(id string) bool {
   114  	args := &graphDriverRequest{ID: id}
   115  	var ret graphDriverResponse
   116  	if err := d.client.Call("GraphDriver.Exists", args, &ret); err != nil {
   117  		return false
   118  	}
   119  	return ret.Exists
   120  }
   121  
   122  func (d *graphDriverProxy) Status() [][2]string {
   123  	args := &graphDriverRequest{}
   124  	var ret graphDriverResponse
   125  	if err := d.client.Call("GraphDriver.Status", args, &ret); err != nil {
   126  		return nil
   127  	}
   128  	return ret.Status
   129  }
   130  
   131  func (d *graphDriverProxy) GetMetadata(id string) (map[string]string, error) {
   132  	args := &graphDriverRequest{
   133  		ID: id,
   134  	}
   135  	var ret graphDriverResponse
   136  	if err := d.client.Call("GraphDriver.GetMetadata", args, &ret); err != nil {
   137  		return nil, err
   138  	}
   139  	if ret.Err != "" {
   140  		return nil, errors.New(ret.Err)
   141  	}
   142  	return ret.Metadata, nil
   143  }
   144  
   145  func (d *graphDriverProxy) Cleanup() error {
   146  	args := &graphDriverRequest{}
   147  	var ret graphDriverResponse
   148  	if err := d.client.Call("GraphDriver.Cleanup", args, &ret); err != nil {
   149  		return nil
   150  	}
   151  	if ret.Err != "" {
   152  		return errors.New(ret.Err)
   153  	}
   154  	return nil
   155  }
   156  
   157  func (d *graphDriverProxy) Diff(id, parent string) (archive.Archive, error) {
   158  	args := &graphDriverRequest{
   159  		ID:     id,
   160  		Parent: parent,
   161  	}
   162  	body, err := d.client.Stream("GraphDriver.Diff", args)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  	return archive.Archive(body), nil
   167  }
   168  
   169  func (d *graphDriverProxy) Changes(id, parent string) ([]archive.Change, error) {
   170  	args := &graphDriverRequest{
   171  		ID:     id,
   172  		Parent: parent,
   173  	}
   174  	var ret graphDriverResponse
   175  	if err := d.client.Call("GraphDriver.Changes", args, &ret); err != nil {
   176  		return nil, err
   177  	}
   178  	if ret.Err != "" {
   179  		return nil, errors.New(ret.Err)
   180  	}
   181  
   182  	return ret.Changes, nil
   183  }
   184  
   185  func (d *graphDriverProxy) ApplyDiff(id, parent string, diff archive.Reader) (int64, error) {
   186  	var ret graphDriverResponse
   187  	if err := d.client.SendFile(fmt.Sprintf("GraphDriver.ApplyDiff?id=%s&parent=%s", id, parent), diff, &ret); err != nil {
   188  		return -1, err
   189  	}
   190  	if ret.Err != "" {
   191  		return -1, errors.New(ret.Err)
   192  	}
   193  	return ret.Size, nil
   194  }
   195  
   196  func (d *graphDriverProxy) DiffSize(id, parent string) (int64, error) {
   197  	args := &graphDriverRequest{
   198  		ID:     id,
   199  		Parent: parent,
   200  	}
   201  	var ret graphDriverResponse
   202  	if err := d.client.Call("GraphDriver.DiffSize", args, &ret); err != nil {
   203  		return -1, err
   204  	}
   205  	if ret.Err != "" {
   206  		return -1, errors.New(ret.Err)
   207  	}
   208  	return ret.Size, nil
   209  }