archive.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package docker
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "github.com/dotcloud/docker/utils"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. "os/exec"
  11. )
  12. type Archive io.Reader
  13. type Compression uint32
  14. const (
  15. Uncompressed Compression = iota
  16. Bzip2
  17. Gzip
  18. Xz
  19. )
  20. func DetectCompression(source []byte) Compression {
  21. for _, c := range source[:10] {
  22. utils.Debugf("%x", c)
  23. }
  24. sourceLen := len(source)
  25. for compression, m := range map[Compression][]byte{
  26. Bzip2: {0x42, 0x5A, 0x68},
  27. Gzip: {0x1F, 0x8B, 0x08},
  28. Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
  29. } {
  30. fail := false
  31. if len(m) > sourceLen {
  32. utils.Debugf("Len too short")
  33. continue
  34. }
  35. i := 0
  36. for _, b := range m {
  37. if b != source[i] {
  38. fail = true
  39. break
  40. }
  41. i++
  42. }
  43. if !fail {
  44. return compression
  45. }
  46. }
  47. return Uncompressed
  48. }
  49. func (compression *Compression) Flag() string {
  50. switch *compression {
  51. case Bzip2:
  52. return "j"
  53. case Gzip:
  54. return "z"
  55. case Xz:
  56. return "J"
  57. }
  58. return ""
  59. }
  60. func (compression *Compression) Extension() string {
  61. switch *compression {
  62. case Uncompressed:
  63. return "tar"
  64. case Bzip2:
  65. return "tar.bz2"
  66. case Gzip:
  67. return "tar.gz"
  68. case Xz:
  69. return "tar.xz"
  70. }
  71. return ""
  72. }
  73. func Tar(path string, compression Compression) (io.Reader, error) {
  74. return CmdStream(exec.Command("tar", "-f", "-", "-C", path, "-c"+compression.Flag(), "."))
  75. }
  76. func Untar(archive io.Reader, path string) error {
  77. bufferedArchive := bufio.NewReaderSize(archive, 10)
  78. buf, err := bufferedArchive.Peek(10)
  79. if err != nil {
  80. return err
  81. }
  82. compression := DetectCompression(buf)
  83. utils.Debugf("Archive compression detected: %s", compression.Extension())
  84. cmd := exec.Command("tar", "-f", "-", "-C", path, "-x"+compression.Flag())
  85. cmd.Stdin = bufferedArchive
  86. // Hardcode locale environment for predictable outcome regardless of host configuration.
  87. // (see https://github.com/dotcloud/docker/issues/355)
  88. cmd.Env = []string{"LANG=en_US.utf-8", "LC_ALL=en_US.utf-8"}
  89. output, err := cmd.CombinedOutput()
  90. if err != nil {
  91. return fmt.Errorf("%s: %s", err, output)
  92. }
  93. return nil
  94. }
  95. // CmdStream executes a command, and returns its stdout as a stream.
  96. // If the command fails to run or doesn't complete successfully, an error
  97. // will be returned, including anything written on stderr.
  98. func CmdStream(cmd *exec.Cmd) (io.Reader, error) {
  99. stdout, err := cmd.StdoutPipe()
  100. if err != nil {
  101. return nil, err
  102. }
  103. stderr, err := cmd.StderrPipe()
  104. if err != nil {
  105. return nil, err
  106. }
  107. pipeR, pipeW := io.Pipe()
  108. errChan := make(chan []byte)
  109. // Collect stderr, we will use it in case of an error
  110. go func() {
  111. errText, e := ioutil.ReadAll(stderr)
  112. if e != nil {
  113. errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
  114. }
  115. errChan <- errText
  116. }()
  117. // Copy stdout to the returned pipe
  118. go func() {
  119. _, err := io.Copy(pipeW, stdout)
  120. if err != nil {
  121. pipeW.CloseWithError(err)
  122. }
  123. errText := <-errChan
  124. if err := cmd.Wait(); err != nil {
  125. pipeW.CloseWithError(errors.New(err.Error() + ": " + string(errText)))
  126. } else {
  127. pipeW.Close()
  128. }
  129. }()
  130. // Run the command and return the pipe
  131. if err := cmd.Start(); err != nil {
  132. return nil, err
  133. }
  134. return pipeR, nil
  135. }
  136. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  137. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  138. // the file will be deleted.
  139. func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
  140. f, err := ioutil.TempFile(dir, "")
  141. if err != nil {
  142. return nil, err
  143. }
  144. if _, err := io.Copy(f, src); err != nil {
  145. return nil, err
  146. }
  147. if _, err := f.Seek(0, 0); err != nil {
  148. return nil, err
  149. }
  150. st, err := f.Stat()
  151. if err != nil {
  152. return nil, err
  153. }
  154. size := st.Size()
  155. return &TempArchive{f, size}, nil
  156. }
  157. type TempArchive struct {
  158. *os.File
  159. Size int64 // Pre-computed from Stat().Size() as a convenience
  160. }
  161. func (archive *TempArchive) Read(data []byte) (int, error) {
  162. n, err := archive.File.Read(data)
  163. if err != nil {
  164. os.Remove(archive.File.Name())
  165. }
  166. return n, err
  167. }