github.com/openshift/moby-moby@v1.13.2-0.20170601211448-f5ec1e2936dc/daemon/graphdriver/proxy.go (about)

     1  package graphdriver
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"path/filepath"
     8  
     9  	"github.com/docker/docker/pkg/archive"
    10  	"github.com/docker/docker/pkg/idtools"
    11  	"github.com/docker/docker/pkg/plugingetter"
    12  )
    13  
    14  type graphDriverProxy struct {
    15  	name string
    16  	p    plugingetter.CompatPlugin
    17  }
    18  
    19  type graphDriverRequest struct {
    20  	ID         string            `json:",omitempty"`
    21  	Parent     string            `json:",omitempty"`
    22  	MountLabel string            `json:",omitempty"`
    23  	StorageOpt map[string]string `json:",omitempty"`
    24  }
    25  
    26  type graphDriverResponse struct {
    27  	Err      string            `json:",omitempty"`
    28  	Dir      string            `json:",omitempty"`
    29  	Exists   bool              `json:",omitempty"`
    30  	Status   [][2]string       `json:",omitempty"`
    31  	Changes  []archive.Change  `json:",omitempty"`
    32  	Size     int64             `json:",omitempty"`
    33  	Metadata map[string]string `json:",omitempty"`
    34  }
    35  
    36  type graphDriverInitRequest struct {
    37  	Home    string
    38  	Opts    []string        `json:"Opts"`
    39  	UIDMaps []idtools.IDMap `json:"UIDMaps"`
    40  	GIDMaps []idtools.IDMap `json:"GIDMaps"`
    41  }
    42  
    43  func (d *graphDriverProxy) Init(home string, opts []string, uidMaps, gidMaps []idtools.IDMap) error {
    44  	if !d.p.IsV1() {
    45  		if cp, ok := d.p.(plugingetter.CountedPlugin); ok {
    46  			// always acquire here, it will be cleaned up on daemon shutdown
    47  			cp.Acquire()
    48  		}
    49  	}
    50  	args := &graphDriverInitRequest{
    51  		Home:    home,
    52  		Opts:    opts,
    53  		UIDMaps: uidMaps,
    54  		GIDMaps: gidMaps,
    55  	}
    56  	var ret graphDriverResponse
    57  	if err := d.p.Client().Call("GraphDriver.Init", args, &ret); err != nil {
    58  		return err
    59  	}
    60  	if ret.Err != "" {
    61  		return errors.New(ret.Err)
    62  	}
    63  	return nil
    64  }
    65  
    66  func (d *graphDriverProxy) String() string {
    67  	return d.name
    68  }
    69  
    70  func (d *graphDriverProxy) CreateReadWrite(id, parent string, opts *CreateOpts) error {
    71  	args := &graphDriverRequest{
    72  		ID:     id,
    73  		Parent: parent,
    74  	}
    75  	if opts != nil {
    76  		args.MountLabel = opts.MountLabel
    77  		args.StorageOpt = opts.StorageOpt
    78  	}
    79  
    80  	var ret graphDriverResponse
    81  	if err := d.p.Client().Call("GraphDriver.CreateReadWrite", args, &ret); err != nil {
    82  		return err
    83  	}
    84  	if ret.Err != "" {
    85  		return errors.New(ret.Err)
    86  	}
    87  	return nil
    88  }
    89  
    90  func (d *graphDriverProxy) Create(id, parent string, opts *CreateOpts) error {
    91  	args := &graphDriverRequest{
    92  		ID:     id,
    93  		Parent: parent,
    94  	}
    95  	if opts != nil {
    96  		args.MountLabel = opts.MountLabel
    97  		args.StorageOpt = opts.StorageOpt
    98  	}
    99  	var ret graphDriverResponse
   100  	if err := d.p.Client().Call("GraphDriver.Create", args, &ret); err != nil {
   101  		return err
   102  	}
   103  	if ret.Err != "" {
   104  		return errors.New(ret.Err)
   105  	}
   106  	return nil
   107  }
   108  
   109  func (d *graphDriverProxy) Remove(id string) error {
   110  	args := &graphDriverRequest{ID: id}
   111  	var ret graphDriverResponse
   112  	if err := d.p.Client().Call("GraphDriver.Remove", args, &ret); err != nil {
   113  		return err
   114  	}
   115  	if ret.Err != "" {
   116  		return errors.New(ret.Err)
   117  	}
   118  	return nil
   119  }
   120  
   121  func (d *graphDriverProxy) Get(id, mountLabel string) (string, error) {
   122  	args := &graphDriverRequest{
   123  		ID:         id,
   124  		MountLabel: mountLabel,
   125  	}
   126  	var ret graphDriverResponse
   127  	if err := d.p.Client().Call("GraphDriver.Get", args, &ret); err != nil {
   128  		return "", err
   129  	}
   130  	var err error
   131  	if ret.Err != "" {
   132  		err = errors.New(ret.Err)
   133  	}
   134  	return filepath.Join(d.p.BasePath(), ret.Dir), err
   135  }
   136  
   137  func (d *graphDriverProxy) Put(id string) error {
   138  	args := &graphDriverRequest{ID: id}
   139  	var ret graphDriverResponse
   140  	if err := d.p.Client().Call("GraphDriver.Put", args, &ret); err != nil {
   141  		return err
   142  	}
   143  	if ret.Err != "" {
   144  		return errors.New(ret.Err)
   145  	}
   146  	return nil
   147  }
   148  
   149  func (d *graphDriverProxy) Exists(id string) bool {
   150  	args := &graphDriverRequest{ID: id}
   151  	var ret graphDriverResponse
   152  	if err := d.p.Client().Call("GraphDriver.Exists", args, &ret); err != nil {
   153  		return false
   154  	}
   155  	return ret.Exists
   156  }
   157  
   158  func (d *graphDriverProxy) Status() [][2]string {
   159  	args := &graphDriverRequest{}
   160  	var ret graphDriverResponse
   161  	if err := d.p.Client().Call("GraphDriver.Status", args, &ret); err != nil {
   162  		return nil
   163  	}
   164  	return ret.Status
   165  }
   166  
   167  func (d *graphDriverProxy) GetMetadata(id string) (map[string]string, error) {
   168  	args := &graphDriverRequest{
   169  		ID: id,
   170  	}
   171  	var ret graphDriverResponse
   172  	if err := d.p.Client().Call("GraphDriver.GetMetadata", args, &ret); err != nil {
   173  		return nil, err
   174  	}
   175  	if ret.Err != "" {
   176  		return nil, errors.New(ret.Err)
   177  	}
   178  	return ret.Metadata, nil
   179  }
   180  
   181  func (d *graphDriverProxy) Cleanup() error {
   182  	if !d.p.IsV1() {
   183  		if cp, ok := d.p.(plugingetter.CountedPlugin); ok {
   184  			// always release
   185  			defer cp.Release()
   186  		}
   187  	}
   188  
   189  	args := &graphDriverRequest{}
   190  	var ret graphDriverResponse
   191  	if err := d.p.Client().Call("GraphDriver.Cleanup", args, &ret); err != nil {
   192  		return nil
   193  	}
   194  	if ret.Err != "" {
   195  		return errors.New(ret.Err)
   196  	}
   197  	return nil
   198  }
   199  
   200  func (d *graphDriverProxy) Diff(id, parent string) (io.ReadCloser, error) {
   201  	args := &graphDriverRequest{
   202  		ID:     id,
   203  		Parent: parent,
   204  	}
   205  	body, err := d.p.Client().Stream("GraphDriver.Diff", args)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  	return body, nil
   210  }
   211  
   212  func (d *graphDriverProxy) Changes(id, parent string) ([]archive.Change, error) {
   213  	args := &graphDriverRequest{
   214  		ID:     id,
   215  		Parent: parent,
   216  	}
   217  	var ret graphDriverResponse
   218  	if err := d.p.Client().Call("GraphDriver.Changes", args, &ret); err != nil {
   219  		return nil, err
   220  	}
   221  	if ret.Err != "" {
   222  		return nil, errors.New(ret.Err)
   223  	}
   224  
   225  	return ret.Changes, nil
   226  }
   227  
   228  func (d *graphDriverProxy) ApplyDiff(id, parent string, diff io.Reader) (int64, error) {
   229  	var ret graphDriverResponse
   230  	if err := d.p.Client().SendFile(fmt.Sprintf("GraphDriver.ApplyDiff?id=%s&parent=%s", id, parent), diff, &ret); err != nil {
   231  		return -1, err
   232  	}
   233  	if ret.Err != "" {
   234  		return -1, errors.New(ret.Err)
   235  	}
   236  	return ret.Size, nil
   237  }
   238  
   239  func (d *graphDriverProxy) DiffSize(id, parent string) (int64, error) {
   240  	args := &graphDriverRequest{
   241  		ID:     id,
   242  		Parent: parent,
   243  	}
   244  	var ret graphDriverResponse
   245  	if err := d.p.Client().Call("GraphDriver.DiffSize", args, &ret); err != nil {
   246  		return -1, err
   247  	}
   248  	if ret.Err != "" {
   249  		return -1, errors.New(ret.Err)
   250  	}
   251  	return ret.Size, nil
   252  }