driver.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package graphdriver
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/archive"
  5. )
  6. type InitFunc func(root string) (Driver, error)
  7. type Driver interface {
  8. Create(id, parent string) error
  9. Remove(id string) error
  10. Get(id string) (dir string, err error)
  11. Diff(id string) (archive.Archive, error)
  12. DiffSize(id string) (bytes int64, err error)
  13. Changes(id string) ([]archive.Change, error)
  14. Cleanup() error
  15. }
  16. var (
  17. // All registred drivers
  18. drivers map[string]InitFunc
  19. // Slice of drivers that should be used in an order
  20. priority = []string{
  21. "aufs",
  22. "devicemapper",
  23. }
  24. )
  25. func init() {
  26. drivers = make(map[string]InitFunc)
  27. }
  28. func Register(name string, initFunc InitFunc) error {
  29. if _, exists := drivers[name]; exists {
  30. return fmt.Errorf("Name already registered %s", name)
  31. }
  32. drivers[name] = initFunc
  33. return nil
  34. }
  35. func New(root string) (Driver, error) {
  36. var driver Driver
  37. var lastError error
  38. // Check for priority drivers first
  39. for _, name := range priority {
  40. if initFunc, exists := drivers[name]; exists {
  41. driver, lastError = initFunc(root)
  42. if lastError != nil {
  43. continue
  44. }
  45. return driver, nil
  46. }
  47. }
  48. // Check all registered drivers if no priority driver is found
  49. for _, initFunc := range drivers {
  50. driver, lastError = initFunc(root)
  51. if lastError != nil {
  52. continue
  53. }
  54. return driver, nil
  55. }
  56. return nil, lastError
  57. }