archive.go 7.4 KB

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