proxy.go 5.1 KB

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