github.com/psychoss/docker@v1.9.0/daemon/graphdriver/proxy.go (about)

     1  // +build experimental
     2  // +build daemon
     3  
     4  package graphdriver
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/docker/docker/pkg/archive"
    11  )
    12  
    13  type graphDriverProxy struct {
    14  	name   string
    15  	client pluginClient
    16  }
    17  
    18  type graphDriverRequest struct {
    19  	ID         string `json:",omitempty"`
    20  	Parent     string `json:",omitempty"`
    21  	MountLabel string `json:",omitempty"`
    22  }
    23  
    24  type graphDriverResponse struct {
    25  	Err      string            `json:",omitempty"`
    26  	Dir      string            `json:",omitempty"`
    27  	Exists   bool              `json:",omitempty"`
    28  	Status   [][2]string       `json:",omitempty"`
    29  	Changes  []archive.Change  `json:",omitempty"`
    30  	Size     int64             `json:",omitempty"`
    31  	Metadata map[string]string `json:",omitempty"`
    32  }
    33  
    34  type graphDriverInitRequest struct {
    35  	Home string
    36  	Opts []string
    37  }
    38  
    39  func (d *graphDriverProxy) Init(home string, opts []string) error {
    40  	args := &graphDriverInitRequest{
    41  		Home: home,
    42  		Opts: opts,
    43  	}
    44  	var ret graphDriverResponse
    45  	if err := d.client.Call("GraphDriver.Init", args, &ret); err != nil {
    46  		return err
    47  	}
    48  	if ret.Err != "" {
    49  		return errors.New(ret.Err)
    50  	}
    51  	return nil
    52  }
    53  
    54  func (d *graphDriverProxy) String() string {
    55  	return d.name
    56  }
    57  
    58  func (d *graphDriverProxy) Create(id, parent string) error {
    59  	args := &graphDriverRequest{
    60  		ID:     id,
    61  		Parent: parent,
    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  		body.Close()
   165  		return nil, err
   166  	}
   167  	return archive.Archive(body), nil
   168  }
   169  
   170  func (d *graphDriverProxy) Changes(id, parent string) ([]archive.Change, error) {
   171  	args := &graphDriverRequest{
   172  		ID:     id,
   173  		Parent: parent,
   174  	}
   175  	var ret graphDriverResponse
   176  	if err := d.client.Call("GraphDriver.Changes", args, &ret); err != nil {
   177  		return nil, err
   178  	}
   179  	if ret.Err != "" {
   180  		return nil, errors.New(ret.Err)
   181  	}
   182  
   183  	return ret.Changes, nil
   184  }
   185  
   186  func (d *graphDriverProxy) ApplyDiff(id, parent string, diff archive.Reader) (int64, error) {
   187  	var ret graphDriverResponse
   188  	if err := d.client.SendFile(fmt.Sprintf("GraphDriver.ApplyDiff?id=%s&parent=%s", id, parent), diff, &ret); err != nil {
   189  		return -1, err
   190  	}
   191  	if ret.Err != "" {
   192  		return -1, errors.New(ret.Err)
   193  	}
   194  	return ret.Size, nil
   195  }
   196  
   197  func (d *graphDriverProxy) DiffSize(id, parent string) (int64, error) {
   198  	args := &graphDriverRequest{
   199  		ID:     id,
   200  		Parent: parent,
   201  	}
   202  	var ret graphDriverResponse
   203  	if err := d.client.Call("GraphDriver.DiffSize", args, &ret); err != nil {
   204  		return -1, err
   205  	}
   206  	if ret.Err != "" {
   207  		return -1, errors.New(ret.Err)
   208  	}
   209  	return ret.Size, nil
   210  }