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