layers.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package fs
  2. import (
  3. "errors"
  4. "path"
  5. "path/filepath"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "fmt"
  11. "../future"
  12. )
  13. type LayerStore struct {
  14. Root string
  15. }
  16. type Compression uint32
  17. const (
  18. Uncompressed Compression = iota
  19. Bzip2
  20. Gzip
  21. )
  22. func NewLayerStore(root string) (*LayerStore, error) {
  23. abspath, err := filepath.Abs(root)
  24. if err != nil {
  25. return nil, err
  26. }
  27. // Create the root directory if it doesn't exists
  28. if err := os.Mkdir(root, 0700); err != nil && !os.IsExist(err) {
  29. return nil, err
  30. }
  31. return &LayerStore{
  32. Root: abspath,
  33. }, nil
  34. }
  35. func (store *LayerStore) List() []string {
  36. files, err := ioutil.ReadDir(store.Root)
  37. if err != nil {
  38. return []string{}
  39. }
  40. var layers []string
  41. for _, st := range files {
  42. if st.IsDir() {
  43. layers = append(layers, path.Join(store.Root, st.Name()))
  44. }
  45. }
  46. return layers
  47. }
  48. func (store *LayerStore) Get(id string) string {
  49. if !store.Exists(id) {
  50. return ""
  51. }
  52. return store.layerPath(id)
  53. }
  54. func (store *LayerStore) rootExists() (bool, error) {
  55. if stat, err := os.Stat(store.Root); err != nil {
  56. if os.IsNotExist(err) {
  57. return false, nil
  58. }
  59. return false, err
  60. } else if !stat.IsDir() {
  61. return false, errors.New("Not a directory: " + store.Root)
  62. }
  63. return true, nil
  64. }
  65. func (store *LayerStore) Init() error {
  66. if exists, err := store.rootExists(); err != nil {
  67. return err
  68. } else if exists {
  69. return nil
  70. }
  71. return os.Mkdir(store.Root, 0700)
  72. }
  73. func (store *LayerStore) Mktemp() (string, error) {
  74. tmpName := future.RandomId()
  75. tmpPath := path.Join(store.Root, "tmp-" + tmpName)
  76. if err := os.Mkdir(tmpPath, 0700); err != nil {
  77. return "", err
  78. }
  79. return tmpPath, nil
  80. }
  81. func (store *LayerStore) layerPath(id string) string {
  82. return path.Join(store.Root, id)
  83. }
  84. func (store *LayerStore) AddLayer(id string, archive Archive, stderr io.Writer, compression Compression) (string, error) {
  85. if _, err := os.Stat(store.layerPath(id)); err == nil {
  86. return "", errors.New("Layer already exists: " + id)
  87. }
  88. tmp, err := store.Mktemp()
  89. defer os.RemoveAll(tmp)
  90. if err != nil {
  91. return "", errors.New(fmt.Sprintf("Mktemp failed: %s", err))
  92. }
  93. extractFlags := "-x"
  94. if compression == Bzip2 {
  95. extractFlags += "j"
  96. } else if compression == Gzip {
  97. extractFlags += "z"
  98. }
  99. untarCmd := exec.Command("tar", "-C", tmp, extractFlags)
  100. untarW, err := untarCmd.StdinPipe()
  101. if err != nil {
  102. return "", errors.New(fmt.Sprintf("Could not obtain stdin pipe: %s", err))
  103. }
  104. untarStderr, err := untarCmd.StderrPipe()
  105. if err != nil {
  106. return "", errors.New(fmt.Sprintf("Could not obtain stderr pipe: %s", err))
  107. }
  108. go io.Copy(stderr, untarStderr)
  109. untarStdout, err := untarCmd.StdoutPipe()
  110. if err != nil {
  111. return "", errors.New(fmt.Sprintf("Could not obtain stdout pipe: %s", err))
  112. }
  113. go io.Copy(stderr, untarStdout)
  114. untarCmd.Start()
  115. job_copy := future.Go(func() error {
  116. _, err := io.Copy(untarW, archive)
  117. untarW.Close()
  118. return err
  119. })
  120. if err := untarCmd.Wait(); err != nil {
  121. return "", errors.New(fmt.Sprintf("Error while waiting for untar command to complete: %s", err))
  122. }
  123. if err := <-job_copy; err != nil {
  124. return "", errors.New(fmt.Sprintf("Error while copying: %s", err))
  125. }
  126. layer := store.layerPath(id)
  127. if !store.Exists(id) {
  128. if err := os.Rename(tmp, layer); err != nil {
  129. return "", errors.New(fmt.Sprintf("Could not rename temp dir to layer %s: %s", layer, err))
  130. }
  131. }
  132. return layer, nil
  133. }
  134. func (store *LayerStore) Exists(id string) bool {
  135. st, err := os.Stat(store.layerPath(id))
  136. if err != nil {
  137. return false
  138. }
  139. return st.IsDir()
  140. }