graph.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package graph
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "time"
  9. )
  10. type Graph struct {
  11. Root string
  12. }
  13. func New(root string) (*Graph, error) {
  14. abspath, err := filepath.Abs(root)
  15. if err != nil {
  16. return nil, err
  17. }
  18. // Create the root directory if it doesn't exists
  19. if err := os.Mkdir(root, 0700); err != nil && !os.IsExist(err) {
  20. return nil, err
  21. }
  22. return &Graph{
  23. Root: abspath,
  24. }, nil
  25. }
  26. func (graph *Graph) Exists(id string) bool {
  27. if _, err := graph.Get(id); err != nil {
  28. return false
  29. }
  30. return true
  31. }
  32. func (graph *Graph) Get(id string) (*Image, error) {
  33. img, err := LoadImage(graph.imageRoot(id))
  34. if err != nil {
  35. return nil, err
  36. }
  37. if img.Id != id {
  38. return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.Id)
  39. }
  40. img.graph = graph
  41. return img, nil
  42. }
  43. func (graph *Graph) Create(layerData Archive, parent, comment string) (*Image, error) {
  44. img := &Image{
  45. Id: GenerateId(),
  46. Parent: parent,
  47. Comment: comment,
  48. Created: time.Now(),
  49. }
  50. if err := graph.Register(layerData, img); err != nil {
  51. return nil, err
  52. }
  53. return img, nil
  54. }
  55. func (graph *Graph) Register(layerData Archive, img *Image) error {
  56. if err := ValidateId(img.Id); err != nil {
  57. return err
  58. }
  59. // (This is a convenience to save time. Race conditions are taken care of by os.Rename)
  60. if graph.Exists(img.Id) {
  61. return fmt.Errorf("Image %s already exists", img.Id)
  62. }
  63. tmp, err := graph.Mktemp(img.Id)
  64. defer os.RemoveAll(tmp)
  65. if err != nil {
  66. return fmt.Errorf("Mktemp failed: %s", err)
  67. }
  68. if err := StoreImage(img, layerData, tmp); err != nil {
  69. return err
  70. }
  71. // Commit
  72. if err := os.Rename(tmp, graph.imageRoot(img.Id)); err != nil {
  73. return err
  74. }
  75. img.graph = graph
  76. return nil
  77. }
  78. func (graph *Graph) Mktemp(id string) (string, error) {
  79. tmp, err := New(path.Join(graph.Root, ":tmp:"))
  80. if err != nil {
  81. return "", fmt.Errorf("Couldn't create temp: %s", err)
  82. }
  83. if tmp.Exists(id) {
  84. return "", fmt.Errorf("Image %d already exists", id)
  85. }
  86. return tmp.imageRoot(id), nil
  87. }
  88. func (graph *Graph) Garbage() (*Graph, error) {
  89. return New(path.Join(graph.Root, ":garbage:"))
  90. }
  91. func (graph *Graph) Delete(id string) error {
  92. garbage, err := graph.Garbage()
  93. if err != nil {
  94. return err
  95. }
  96. return os.Rename(graph.imageRoot(id), garbage.imageRoot(id))
  97. }
  98. func (graph *Graph) Undelete(id string) error {
  99. garbage, err := graph.Garbage()
  100. if err != nil {
  101. return err
  102. }
  103. return os.Rename(garbage.imageRoot(id), graph.imageRoot(id))
  104. }
  105. func (graph *Graph) GarbageCollect() error {
  106. garbage, err := graph.Garbage()
  107. if err != nil {
  108. return err
  109. }
  110. return os.RemoveAll(garbage.Root)
  111. }
  112. func (graph *Graph) Map() (map[string]*Image, error) {
  113. // FIXME: this should replace All()
  114. all, err := graph.All()
  115. if err != nil {
  116. return nil, err
  117. }
  118. images := make(map[string]*Image, len(all))
  119. for _, image := range all {
  120. images[image.Id] = image
  121. }
  122. return images, nil
  123. }
  124. func (graph *Graph) All() ([]*Image, error) {
  125. files, err := ioutil.ReadDir(graph.Root)
  126. if err != nil {
  127. return nil, err
  128. }
  129. var images []*Image
  130. for _, st := range files {
  131. if img, err := graph.Get(st.Name()); err != nil {
  132. // Skip image
  133. continue
  134. } else {
  135. images = append(images, img)
  136. }
  137. }
  138. return images, nil
  139. }
  140. func (graph *Graph) imageRoot(id string) string {
  141. return path.Join(graph.Root, id)
  142. }