archive.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package docker
  2. import (
  3. "archive/tar"
  4. "bufio"
  5. "bytes"
  6. "errors"
  7. "fmt"
  8. "github.com/dotcloud/docker/utils"
  9. "io"
  10. "io/ioutil"
  11. "os"
  12. "os/exec"
  13. "path"
  14. "path/filepath"
  15. )
  16. type Archive io.Reader
  17. type Compression uint32
  18. const (
  19. Uncompressed Compression = iota
  20. Bzip2
  21. Gzip
  22. Xz
  23. )
  24. func DetectCompression(source []byte) Compression {
  25. for _, c := range source[:10] {
  26. utils.Debugf("%x", c)
  27. }
  28. sourceLen := len(source)
  29. for compression, m := range map[Compression][]byte{
  30. Bzip2: {0x42, 0x5A, 0x68},
  31. Gzip: {0x1F, 0x8B, 0x08},
  32. Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
  33. } {
  34. fail := false
  35. if len(m) > sourceLen {
  36. utils.Debugf("Len too short")
  37. continue
  38. }
  39. i := 0
  40. for _, b := range m {
  41. if b != source[i] {
  42. fail = true
  43. break
  44. }
  45. i++
  46. }
  47. if !fail {
  48. return compression
  49. }
  50. }
  51. return Uncompressed
  52. }
  53. func (compression *Compression) Flag() string {
  54. switch *compression {
  55. case Bzip2:
  56. return "j"
  57. case Gzip:
  58. return "z"
  59. case Xz:
  60. return "J"
  61. }
  62. return ""
  63. }
  64. func (compression *Compression) Extension() string {
  65. switch *compression {
  66. case Uncompressed:
  67. return "tar"
  68. case Bzip2:
  69. return "tar.bz2"
  70. case Gzip:
  71. return "tar.gz"
  72. case Xz:
  73. return "tar.xz"
  74. }
  75. return ""
  76. }
  77. // Tar creates an archive from the directory at `path`, and returns it as a
  78. // stream of bytes.
  79. func Tar(path string, compression Compression) (io.Reader, error) {
  80. return TarFilter(path, compression, nil)
  81. }
  82. // Tar creates an archive from the directory at `path`, only including files whose relative
  83. // paths are included in `filter`. If `filter` is nil, then all files are included.
  84. func TarFilter(path string, compression Compression, filter []string) (io.Reader, error) {
  85. args := []string{"tar", "--numeric-owner", "-f", "-", "-C", path}
  86. if filter == nil {
  87. filter = []string{"."}
  88. }
  89. for _, f := range filter {
  90. args = append(args, "-c"+compression.Flag(), f)
  91. }
  92. return CmdStream(exec.Command(args[0], args[1:]...))
  93. }
  94. // Untar reads a stream of bytes from `archive`, parses it as a tar archive,
  95. // and unpacks it into the directory at `path`.
  96. // The archive may be compressed with one of the following algorithgms:
  97. // identity (uncompressed), gzip, bzip2, xz.
  98. // FIXME: specify behavior when target path exists vs. doesn't exist.
  99. func Untar(archive io.Reader, path string) error {
  100. if archive == nil {
  101. return fmt.Errorf("Empty archive")
  102. }
  103. bufferedArchive := bufio.NewReaderSize(archive, 10)
  104. buf, err := bufferedArchive.Peek(10)
  105. if err != nil {
  106. return err
  107. }
  108. compression := DetectCompression(buf)
  109. utils.Debugf("Archive compression detected: %s", compression.Extension())
  110. cmd := exec.Command("tar", "--numeric-owner", "-f", "-", "-C", path, "-x"+compression.Flag())
  111. cmd.Stdin = bufferedArchive
  112. // Hardcode locale environment for predictable outcome regardless of host configuration.
  113. // (see https://github.com/dotcloud/docker/issues/355)
  114. cmd.Env = []string{"LANG=en_US.utf-8", "LC_ALL=en_US.utf-8"}
  115. output, err := cmd.CombinedOutput()
  116. if err != nil {
  117. return fmt.Errorf("%s: %s", err, output)
  118. }
  119. return nil
  120. }
  121. // TarUntar is a convenience function which calls Tar and Untar, with
  122. // the output of one piped into the other. If either Tar or Untar fails,
  123. // TarUntar aborts and returns the error.
  124. func TarUntar(src string, filter []string, dst string) error {
  125. utils.Debugf("TarUntar(%s %s %s)", src, filter, dst)
  126. archive, err := TarFilter(src, Uncompressed, filter)
  127. if err != nil {
  128. return err
  129. }
  130. return Untar(archive, dst)
  131. }
  132. // UntarPath is a convenience function which looks for an archive
  133. // at filesystem path `src`, and unpacks it at `dst`.
  134. func UntarPath(src, dst string) error {
  135. if archive, err := os.Open(src); err != nil {
  136. return err
  137. } else if err := Untar(archive, dst); err != nil {
  138. return err
  139. }
  140. return nil
  141. }
  142. // CopyWithTar creates a tar archive of filesystem path `src`, and
  143. // unpacks it at filesystem path `dst`.
  144. // The archive is streamed directly with fixed buffering and no
  145. // intermediary disk IO.
  146. //
  147. func CopyWithTar(src, dst string) error {
  148. srcSt, err := os.Stat(src)
  149. if err != nil {
  150. return err
  151. }
  152. if !srcSt.IsDir() {
  153. return CopyFileWithTar(src, dst)
  154. }
  155. // Create dst, copy src's content into it
  156. utils.Debugf("Creating dest directory: %s", dst)
  157. if err := os.MkdirAll(dst, 0700); err != nil && !os.IsExist(err) {
  158. return err
  159. }
  160. utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
  161. return TarUntar(src, nil, dst)
  162. }
  163. // CopyFileWithTar emulates the behavior of the 'cp' command-line
  164. // for a single file. It copies a regular file from path `src` to
  165. // path `dst`, and preserves all its metadata.
  166. //
  167. // If `dst` ends with a trailing slash '/', the final destination path
  168. // will be `dst/base(src)`.
  169. func CopyFileWithTar(src, dst string) error {
  170. utils.Debugf("CopyFileWithTar(%s, %s)", src, dst)
  171. srcSt, err := os.Stat(src)
  172. if err != nil {
  173. return err
  174. }
  175. if srcSt.IsDir() {
  176. return fmt.Errorf("Can't copy a directory")
  177. }
  178. // Clean up the trailing /
  179. if dst[len(dst)-1] == '/' {
  180. dst = path.Join(dst, filepath.Base(src))
  181. }
  182. // Create the holding directory if necessary
  183. if err := os.MkdirAll(filepath.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  184. return err
  185. }
  186. buf := new(bytes.Buffer)
  187. tw := tar.NewWriter(buf)
  188. hdr, err := tar.FileInfoHeader(srcSt, "")
  189. if err != nil {
  190. return err
  191. }
  192. hdr.Name = filepath.Base(dst)
  193. if err := tw.WriteHeader(hdr); err != nil {
  194. return err
  195. }
  196. srcF, err := os.Open(src)
  197. if err != nil {
  198. return err
  199. }
  200. if _, err := io.Copy(tw, srcF); err != nil {
  201. return err
  202. }
  203. tw.Close()
  204. return Untar(buf, filepath.Dir(dst))
  205. }
  206. // CmdStream executes a command, and returns its stdout as a stream.
  207. // If the command fails to run or doesn't complete successfully, an error
  208. // will be returned, including anything written on stderr.
  209. func CmdStream(cmd *exec.Cmd) (io.Reader, error) {
  210. stdout, err := cmd.StdoutPipe()
  211. if err != nil {
  212. return nil, err
  213. }
  214. stderr, err := cmd.StderrPipe()
  215. if err != nil {
  216. return nil, err
  217. }
  218. pipeR, pipeW := io.Pipe()
  219. errChan := make(chan []byte)
  220. // Collect stderr, we will use it in case of an error
  221. go func() {
  222. errText, e := ioutil.ReadAll(stderr)
  223. if e != nil {
  224. errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
  225. }
  226. errChan <- errText
  227. }()
  228. // Copy stdout to the returned pipe
  229. go func() {
  230. _, err := io.Copy(pipeW, stdout)
  231. if err != nil {
  232. pipeW.CloseWithError(err)
  233. }
  234. errText := <-errChan
  235. if err := cmd.Wait(); err != nil {
  236. pipeW.CloseWithError(errors.New(err.Error() + ": " + string(errText)))
  237. } else {
  238. pipeW.Close()
  239. }
  240. }()
  241. // Run the command and return the pipe
  242. if err := cmd.Start(); err != nil {
  243. return nil, err
  244. }
  245. return pipeR, nil
  246. }
  247. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  248. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  249. // the file will be deleted.
  250. func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
  251. f, err := ioutil.TempFile(dir, "")
  252. if err != nil {
  253. return nil, err
  254. }
  255. if _, err := io.Copy(f, src); err != nil {
  256. return nil, err
  257. }
  258. if _, err := f.Seek(0, 0); err != nil {
  259. return nil, err
  260. }
  261. st, err := f.Stat()
  262. if err != nil {
  263. return nil, err
  264. }
  265. size := st.Size()
  266. return &TempArchive{f, size}, nil
  267. }
  268. type TempArchive struct {
  269. *os.File
  270. Size int64 // Pre-computed from Stat().Size() as a convenience
  271. }
  272. func (archive *TempArchive) Read(data []byte) (int, error) {
  273. n, err := archive.File.Read(data)
  274. if err != nil {
  275. os.Remove(archive.File.Name())
  276. }
  277. return n, err
  278. }