github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/daemon/graphdriver/proxy.go (about)

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