archive.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package archive
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "compress/bzip2"
  6. "compress/gzip"
  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 int
  18. type TarOptions struct {
  19. Includes []string
  20. Excludes []string
  21. Recursive bool
  22. Compression Compression
  23. CreateFiles []string
  24. }
  25. const (
  26. Uncompressed Compression = iota
  27. Bzip2
  28. Gzip
  29. Xz
  30. )
  31. func DetectCompression(source []byte) Compression {
  32. sourceLen := len(source)
  33. for compression, m := range map[Compression][]byte{
  34. Bzip2: {0x42, 0x5A, 0x68},
  35. Gzip: {0x1F, 0x8B, 0x08},
  36. Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
  37. } {
  38. fail := false
  39. if len(m) > sourceLen {
  40. utils.Debugf("Len too short")
  41. continue
  42. }
  43. i := 0
  44. for _, b := range m {
  45. if b != source[i] {
  46. fail = true
  47. break
  48. }
  49. i++
  50. }
  51. if !fail {
  52. return compression
  53. }
  54. }
  55. return Uncompressed
  56. }
  57. func xzDecompress(archive io.Reader) (io.Reader, error) {
  58. args := []string{"xz", "-d", "-c", "-q"}
  59. return CmdStream(exec.Command(args[0], args[1:]...), archive, nil)
  60. }
  61. func DecompressStream(archive io.Reader) (io.Reader, error) {
  62. buf := make([]byte, 10)
  63. totalN := 0
  64. for totalN < 10 {
  65. n, err := archive.Read(buf[totalN:])
  66. if err != nil {
  67. if err == io.EOF {
  68. return nil, fmt.Errorf("Tarball too short")
  69. }
  70. return nil, err
  71. }
  72. totalN += n
  73. utils.Debugf("[tar autodetect] n: %d", n)
  74. }
  75. compression := DetectCompression(buf)
  76. wrap := io.MultiReader(bytes.NewReader(buf), archive)
  77. switch compression {
  78. case Uncompressed:
  79. return wrap, nil
  80. case Gzip:
  81. return gzip.NewReader(wrap)
  82. case Bzip2:
  83. return bzip2.NewReader(wrap), nil
  84. case Xz:
  85. return xzDecompress(wrap)
  86. default:
  87. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  88. }
  89. }
  90. func (compression *Compression) Flag() string {
  91. switch *compression {
  92. case Bzip2:
  93. return "j"
  94. case Gzip:
  95. return "z"
  96. case Xz:
  97. return "J"
  98. }
  99. return ""
  100. }
  101. func (compression *Compression) Extension() string {
  102. switch *compression {
  103. case Uncompressed:
  104. return "tar"
  105. case Bzip2:
  106. return "tar.bz2"
  107. case Gzip:
  108. return "tar.gz"
  109. case Xz:
  110. return "tar.xz"
  111. }
  112. return ""
  113. }
  114. // Tar creates an archive from the directory at `path`, and returns it as a
  115. // stream of bytes.
  116. func Tar(path string, compression Compression) (io.Reader, error) {
  117. return TarFilter(path, &TarOptions{Recursive: true, Compression: compression})
  118. }
  119. func escapeName(name string) string {
  120. escaped := make([]byte, 0)
  121. for i, c := range []byte(name) {
  122. if i == 0 && c == '/' {
  123. continue
  124. }
  125. // all printable chars except "-" which is 0x2d
  126. if (0x20 <= c && c <= 0x7E) && c != 0x2d {
  127. escaped = append(escaped, c)
  128. } else {
  129. escaped = append(escaped, fmt.Sprintf("\\%03o", c)...)
  130. }
  131. }
  132. return string(escaped)
  133. }
  134. // Tar creates an archive from the directory at `path`, only including files whose relative
  135. // paths are included in `filter`. If `filter` is nil, then all files are included.
  136. func TarFilter(path string, options *TarOptions) (io.Reader, error) {
  137. args := []string{"tar", "--numeric-owner", "-f", "-", "-C", path, "-T", "-"}
  138. if options.Includes == nil {
  139. options.Includes = []string{"."}
  140. }
  141. args = append(args, "-c"+options.Compression.Flag())
  142. for _, exclude := range options.Excludes {
  143. args = append(args, fmt.Sprintf("--exclude=%s", exclude))
  144. }
  145. if !options.Recursive {
  146. args = append(args, "--no-recursion")
  147. }
  148. files := ""
  149. for _, f := range options.Includes {
  150. files = files + escapeName(f) + "\n"
  151. }
  152. tmpDir := ""
  153. if options.CreateFiles != nil {
  154. var err error // Can't use := here or we override the outer tmpDir
  155. tmpDir, err = ioutil.TempDir("", "docker-tar")
  156. if err != nil {
  157. return nil, err
  158. }
  159. files = files + "-C" + tmpDir + "\n"
  160. for _, f := range options.CreateFiles {
  161. path := filepath.Join(tmpDir, f)
  162. err := os.MkdirAll(filepath.Dir(path), 0600)
  163. if err != nil {
  164. return nil, err
  165. }
  166. if file, err := os.OpenFile(path, os.O_CREATE, 0600); err != nil {
  167. return nil, err
  168. } else {
  169. file.Close()
  170. }
  171. files = files + escapeName(f) + "\n"
  172. }
  173. }
  174. return CmdStream(exec.Command(args[0], args[1:]...), bytes.NewBufferString(files), func() {
  175. if tmpDir != "" {
  176. _ = os.RemoveAll(tmpDir)
  177. }
  178. })
  179. }
  180. // Untar reads a stream of bytes from `archive`, parses it as a tar archive,
  181. // and unpacks it into the directory at `path`.
  182. // The archive may be compressed with one of the following algorithms:
  183. // identity (uncompressed), gzip, bzip2, xz.
  184. // FIXME: specify behavior when target path exists vs. doesn't exist.
  185. func Untar(archive io.Reader, path string, options *TarOptions) error {
  186. if archive == nil {
  187. return fmt.Errorf("Empty archive")
  188. }
  189. buf := make([]byte, 10)
  190. totalN := 0
  191. for totalN < 10 {
  192. n, err := archive.Read(buf[totalN:])
  193. if err != nil {
  194. if err == io.EOF {
  195. return fmt.Errorf("Tarball too short")
  196. }
  197. return err
  198. }
  199. totalN += n
  200. utils.Debugf("[tar autodetect] n: %d", n)
  201. }
  202. compression := DetectCompression(buf)
  203. utils.Debugf("Archive compression detected: %s", compression.Extension())
  204. args := []string{"--numeric-owner", "-f", "-", "-C", path, "-x" + compression.Flag()}
  205. if options != nil {
  206. for _, exclude := range options.Excludes {
  207. args = append(args, fmt.Sprintf("--exclude=%s", exclude))
  208. }
  209. }
  210. cmd := exec.Command("tar", args...)
  211. cmd.Stdin = io.MultiReader(bytes.NewReader(buf), archive)
  212. // Hardcode locale environment for predictable outcome regardless of host configuration.
  213. // (see https://github.com/dotcloud/docker/issues/355)
  214. cmd.Env = []string{"LANG=en_US.utf-8", "LC_ALL=en_US.utf-8"}
  215. output, err := cmd.CombinedOutput()
  216. if err != nil {
  217. return fmt.Errorf("%s: %s", err, output)
  218. }
  219. return nil
  220. }
  221. // TarUntar is a convenience function which calls Tar and Untar, with
  222. // the output of one piped into the other. If either Tar or Untar fails,
  223. // TarUntar aborts and returns the error.
  224. func TarUntar(src string, filter []string, dst string) error {
  225. utils.Debugf("TarUntar(%s %s %s)", src, filter, dst)
  226. archive, err := TarFilter(src, &TarOptions{Compression: Uncompressed, Includes: filter, Recursive: true})
  227. if err != nil {
  228. return err
  229. }
  230. return Untar(archive, dst, nil)
  231. }
  232. // UntarPath is a convenience function which looks for an archive
  233. // at filesystem path `src`, and unpacks it at `dst`.
  234. func UntarPath(src, dst string) error {
  235. if archive, err := os.Open(src); err != nil {
  236. return err
  237. } else if err := Untar(archive, dst, nil); err != nil {
  238. return err
  239. }
  240. return nil
  241. }
  242. // CopyWithTar creates a tar archive of filesystem path `src`, and
  243. // unpacks it at filesystem path `dst`.
  244. // The archive is streamed directly with fixed buffering and no
  245. // intermediary disk IO.
  246. //
  247. func CopyWithTar(src, dst string) error {
  248. srcSt, err := os.Stat(src)
  249. if err != nil {
  250. return err
  251. }
  252. if !srcSt.IsDir() {
  253. return CopyFileWithTar(src, dst)
  254. }
  255. // Create dst, copy src's content into it
  256. utils.Debugf("Creating dest directory: %s", dst)
  257. if err := os.MkdirAll(dst, 0755); err != nil && !os.IsExist(err) {
  258. return err
  259. }
  260. utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
  261. return TarUntar(src, nil, dst)
  262. }
  263. // CopyFileWithTar emulates the behavior of the 'cp' command-line
  264. // for a single file. It copies a regular file from path `src` to
  265. // path `dst`, and preserves all its metadata.
  266. //
  267. // If `dst` ends with a trailing slash '/', the final destination path
  268. // will be `dst/base(src)`.
  269. func CopyFileWithTar(src, dst string) (err error) {
  270. utils.Debugf("CopyFileWithTar(%s, %s)", src, dst)
  271. srcSt, err := os.Stat(src)
  272. if err != nil {
  273. return err
  274. }
  275. if srcSt.IsDir() {
  276. return fmt.Errorf("Can't copy a directory")
  277. }
  278. // Clean up the trailing /
  279. if dst[len(dst)-1] == '/' {
  280. dst = path.Join(dst, filepath.Base(src))
  281. }
  282. // Create the holding directory if necessary
  283. if err := os.MkdirAll(filepath.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  284. return err
  285. }
  286. r, w := io.Pipe()
  287. errC := utils.Go(func() error {
  288. defer w.Close()
  289. srcF, err := os.Open(src)
  290. if err != nil {
  291. return err
  292. }
  293. defer srcF.Close()
  294. tw := tar.NewWriter(w)
  295. hdr, err := tar.FileInfoHeader(srcSt, "")
  296. if err != nil {
  297. return err
  298. }
  299. hdr.Name = filepath.Base(dst)
  300. if err := tw.WriteHeader(hdr); err != nil {
  301. return err
  302. }
  303. if _, err := io.Copy(tw, srcF); err != nil {
  304. return err
  305. }
  306. tw.Close()
  307. return nil
  308. })
  309. defer func() {
  310. if er := <-errC; err != nil {
  311. err = er
  312. }
  313. }()
  314. return Untar(r, filepath.Dir(dst), nil)
  315. }
  316. // CmdStream executes a command, and returns its stdout as a stream.
  317. // If the command fails to run or doesn't complete successfully, an error
  318. // will be returned, including anything written on stderr.
  319. func CmdStream(cmd *exec.Cmd, input io.Reader, atEnd func()) (io.Reader, error) {
  320. if input != nil {
  321. stdin, err := cmd.StdinPipe()
  322. if err != nil {
  323. if atEnd != nil {
  324. atEnd()
  325. }
  326. return nil, err
  327. }
  328. // Write stdin if any
  329. go func() {
  330. io.Copy(stdin, input)
  331. stdin.Close()
  332. }()
  333. }
  334. stdout, err := cmd.StdoutPipe()
  335. if err != nil {
  336. if atEnd != nil {
  337. atEnd()
  338. }
  339. return nil, err
  340. }
  341. stderr, err := cmd.StderrPipe()
  342. if err != nil {
  343. if atEnd != nil {
  344. atEnd()
  345. }
  346. return nil, err
  347. }
  348. pipeR, pipeW := io.Pipe()
  349. errChan := make(chan []byte)
  350. // Collect stderr, we will use it in case of an error
  351. go func() {
  352. errText, e := ioutil.ReadAll(stderr)
  353. if e != nil {
  354. errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
  355. }
  356. errChan <- errText
  357. }()
  358. // Copy stdout to the returned pipe
  359. go func() {
  360. _, err := io.Copy(pipeW, stdout)
  361. if err != nil {
  362. pipeW.CloseWithError(err)
  363. }
  364. errText := <-errChan
  365. if err := cmd.Wait(); err != nil {
  366. pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errText))
  367. } else {
  368. pipeW.Close()
  369. }
  370. if atEnd != nil {
  371. atEnd()
  372. }
  373. }()
  374. // Run the command and return the pipe
  375. if err := cmd.Start(); err != nil {
  376. return nil, err
  377. }
  378. return pipeR, nil
  379. }
  380. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  381. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  382. // the file will be deleted.
  383. func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
  384. f, err := ioutil.TempFile(dir, "")
  385. if err != nil {
  386. return nil, err
  387. }
  388. if _, err := io.Copy(f, src); err != nil {
  389. return nil, err
  390. }
  391. if _, err := f.Seek(0, 0); err != nil {
  392. return nil, err
  393. }
  394. st, err := f.Stat()
  395. if err != nil {
  396. return nil, err
  397. }
  398. size := st.Size()
  399. return &TempArchive{f, size}, nil
  400. }
  401. type TempArchive struct {
  402. *os.File
  403. Size int64 // Pre-computed from Stat().Size() as a convenience
  404. }
  405. func (archive *TempArchive) Read(data []byte) (int, error) {
  406. n, err := archive.File.Read(data)
  407. if err != nil {
  408. os.Remove(archive.File.Name())
  409. }
  410. return n, err
  411. }