archive.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package docker
  2. import (
  3. "errors"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. )
  9. type Archive io.Reader
  10. type Compression uint32
  11. const (
  12. Uncompressed Compression = iota
  13. Bzip2
  14. Gzip
  15. Xz
  16. )
  17. func (compression *Compression) Flag() string {
  18. switch *compression {
  19. case Bzip2:
  20. return "j"
  21. case Gzip:
  22. return "z"
  23. case Xz:
  24. return "J"
  25. }
  26. return ""
  27. }
  28. func Tar(path string, compression Compression) (io.Reader, error) {
  29. cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-c"+compression.Flag(), ".")
  30. return CmdStream(cmd)
  31. }
  32. func Untar(archive io.Reader, path string) error {
  33. cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-x")
  34. cmd.Stdin = archive
  35. output, err := cmd.CombinedOutput()
  36. if err != nil {
  37. return errors.New(err.Error() + ": " + string(output))
  38. }
  39. return nil
  40. }
  41. // CmdStream executes a command, and returns its stdout as a stream.
  42. // If the command fails to run or doesn't complete successfully, an error
  43. // will be returned, including anything written on stderr.
  44. func CmdStream(cmd *exec.Cmd) (io.Reader, error) {
  45. stdout, err := cmd.StdoutPipe()
  46. if err != nil {
  47. return nil, err
  48. }
  49. stderr, err := cmd.StderrPipe()
  50. if err != nil {
  51. return nil, err
  52. }
  53. pipeR, pipeW := io.Pipe()
  54. errChan := make(chan []byte)
  55. // Collect stderr, we will use it in case of an error
  56. go func() {
  57. errText, e := ioutil.ReadAll(stderr)
  58. if e != nil {
  59. errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
  60. }
  61. errChan <- errText
  62. }()
  63. // Copy stdout to the returned pipe
  64. go func() {
  65. _, err := io.Copy(pipeW, stdout)
  66. if err != nil {
  67. pipeW.CloseWithError(err)
  68. }
  69. errText := <-errChan
  70. if err := cmd.Wait(); err != nil {
  71. pipeW.CloseWithError(errors.New(err.Error() + ": " + string(errText)))
  72. } else {
  73. pipeW.Close()
  74. }
  75. }()
  76. // Run the command and return the pipe
  77. if err := cmd.Start(); err != nil {
  78. return nil, err
  79. }
  80. return pipeR, nil
  81. }
  82. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  83. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  84. // the file will be deleted.
  85. func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
  86. f, err := ioutil.TempFile(dir, "")
  87. if err != nil {
  88. return nil, err
  89. }
  90. if _, err := io.Copy(f, src); err != nil {
  91. return nil, err
  92. }
  93. if _, err := f.Seek(0, 0); err != nil {
  94. return nil, err
  95. }
  96. st, err := f.Stat()
  97. if err != nil {
  98. return nil, err
  99. }
  100. size := st.Size()
  101. return &TempArchive{f, size}, nil
  102. }
  103. type TempArchive struct {
  104. *os.File
  105. Size int64 // Pre-computed from Stat().Size() as a convenience
  106. }
  107. func (archive *TempArchive) Read(data []byte) (int, error) {
  108. n, err := archive.File.Read(data)
  109. if err != nil {
  110. os.Remove(archive.File.Name())
  111. }
  112. return n, err
  113. }