archive.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. package archive
  2. import (
  3. "bufio"
  4. "compress/bzip2"
  5. "compress/gzip"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "strings"
  15. "syscall"
  16. "github.com/dotcloud/docker/pkg/system"
  17. "github.com/dotcloud/docker/utils"
  18. "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  19. )
  20. type (
  21. Archive io.ReadCloser
  22. ArchiveReader io.Reader
  23. Compression int
  24. TarOptions struct {
  25. Includes []string
  26. Compression Compression
  27. }
  28. )
  29. var (
  30. ErrNotImplemented = errors.New("Function not implemented")
  31. )
  32. const (
  33. Uncompressed Compression = iota
  34. Bzip2
  35. Gzip
  36. Xz
  37. )
  38. func DetectCompression(source []byte) Compression {
  39. sourceLen := len(source)
  40. for compression, m := range map[Compression][]byte{
  41. Bzip2: {0x42, 0x5A, 0x68},
  42. Gzip: {0x1F, 0x8B, 0x08},
  43. Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
  44. } {
  45. fail := false
  46. if len(m) > sourceLen {
  47. utils.Debugf("Len too short")
  48. continue
  49. }
  50. i := 0
  51. for _, b := range m {
  52. if b != source[i] {
  53. fail = true
  54. break
  55. }
  56. i++
  57. }
  58. if !fail {
  59. return compression
  60. }
  61. }
  62. return Uncompressed
  63. }
  64. func xzDecompress(archive io.Reader) (io.ReadCloser, error) {
  65. args := []string{"xz", "-d", "-c", "-q"}
  66. return CmdStream(exec.Command(args[0], args[1:]...), archive)
  67. }
  68. func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
  69. buf := bufio.NewReader(archive)
  70. bs, err := buf.Peek(10)
  71. if err != nil {
  72. return nil, err
  73. }
  74. utils.Debugf("[tar autodetect] n: %v", bs)
  75. compression := DetectCompression(bs)
  76. switch compression {
  77. case Uncompressed:
  78. return ioutil.NopCloser(buf), nil
  79. case Gzip:
  80. return gzip.NewReader(buf)
  81. case Bzip2:
  82. return ioutil.NopCloser(bzip2.NewReader(buf)), nil
  83. case Xz:
  84. return xzDecompress(buf)
  85. default:
  86. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  87. }
  88. }
  89. func CompressStream(dest io.WriteCloser, compression Compression) (io.WriteCloser, error) {
  90. switch compression {
  91. case Uncompressed:
  92. return utils.NopWriteCloser(dest), nil
  93. case Gzip:
  94. return gzip.NewWriter(dest), nil
  95. case Bzip2, Xz:
  96. // archive/bzip2 does not support writing, and there is no xz support at all
  97. // However, this is not a problem as docker only currently generates gzipped tars
  98. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  99. default:
  100. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  101. }
  102. }
  103. func (compression *Compression) Extension() string {
  104. switch *compression {
  105. case Uncompressed:
  106. return "tar"
  107. case Bzip2:
  108. return "tar.bz2"
  109. case Gzip:
  110. return "tar.gz"
  111. case Xz:
  112. return "tar.xz"
  113. }
  114. return ""
  115. }
  116. func addTarFile(path, name string, tw *tar.Writer) error {
  117. fi, err := os.Lstat(path)
  118. if err != nil {
  119. return err
  120. }
  121. link := ""
  122. if fi.Mode()&os.ModeSymlink != 0 {
  123. if link, err = os.Readlink(path); err != nil {
  124. return err
  125. }
  126. }
  127. hdr, err := tar.FileInfoHeader(fi, link)
  128. if err != nil {
  129. return err
  130. }
  131. if fi.IsDir() && !strings.HasSuffix(name, "/") {
  132. name = name + "/"
  133. }
  134. hdr.Name = name
  135. stat, ok := fi.Sys().(*syscall.Stat_t)
  136. if ok {
  137. // Currently go does not fill in the major/minors
  138. if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK ||
  139. stat.Mode&syscall.S_IFCHR == syscall.S_IFCHR {
  140. hdr.Devmajor = int64(major(uint64(stat.Rdev)))
  141. hdr.Devminor = int64(minor(uint64(stat.Rdev)))
  142. }
  143. }
  144. capability, _ := system.Lgetxattr(path, "security.capability")
  145. if capability != nil {
  146. hdr.Xattrs = make(map[string]string)
  147. hdr.Xattrs["security.capability"] = string(capability)
  148. }
  149. if err := tw.WriteHeader(hdr); err != nil {
  150. return err
  151. }
  152. if hdr.Typeflag == tar.TypeReg {
  153. if file, err := os.Open(path); err != nil {
  154. return err
  155. } else {
  156. _, err := io.Copy(tw, file)
  157. if err != nil {
  158. return err
  159. }
  160. file.Close()
  161. }
  162. }
  163. return nil
  164. }
  165. func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader) error {
  166. // hdr.Mode is in linux format, which we can use for sycalls,
  167. // but for os.Foo() calls we need the mode converted to os.FileMode,
  168. // so use hdrInfo.Mode() (they differ for e.g. setuid bits)
  169. hdrInfo := hdr.FileInfo()
  170. switch hdr.Typeflag {
  171. case tar.TypeDir:
  172. // Create directory unless it exists as a directory already.
  173. // In that case we just want to merge the two
  174. if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
  175. if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
  176. return err
  177. }
  178. }
  179. case tar.TypeReg, tar.TypeRegA:
  180. // Source is regular file
  181. file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
  182. if err != nil {
  183. return err
  184. }
  185. if _, err := io.Copy(file, reader); err != nil {
  186. file.Close()
  187. return err
  188. }
  189. file.Close()
  190. case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
  191. mode := uint32(hdr.Mode & 07777)
  192. switch hdr.Typeflag {
  193. case tar.TypeBlock:
  194. mode |= syscall.S_IFBLK
  195. case tar.TypeChar:
  196. mode |= syscall.S_IFCHR
  197. case tar.TypeFifo:
  198. mode |= syscall.S_IFIFO
  199. }
  200. if err := syscall.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
  201. return err
  202. }
  203. case tar.TypeLink:
  204. if err := os.Link(filepath.Join(extractDir, hdr.Linkname), path); err != nil {
  205. return err
  206. }
  207. case tar.TypeSymlink:
  208. if err := os.Symlink(hdr.Linkname, path); err != nil {
  209. return err
  210. }
  211. case tar.TypeXGlobalHeader:
  212. utils.Debugf("PAX Global Extended Headers found and ignored")
  213. return nil
  214. default:
  215. return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag)
  216. }
  217. if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
  218. return err
  219. }
  220. for key, value := range hdr.Xattrs {
  221. if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil {
  222. return err
  223. }
  224. }
  225. // There is no LChmod, so ignore mode for symlink. Also, this
  226. // must happen after chown, as that can modify the file mode
  227. if hdr.Typeflag != tar.TypeSymlink {
  228. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  229. return err
  230. }
  231. }
  232. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  233. // syscall.UtimesNano doesn't support a NOFOLLOW flag atm, and
  234. if hdr.Typeflag != tar.TypeSymlink {
  235. if err := system.UtimesNano(path, ts); err != nil {
  236. return err
  237. }
  238. } else {
  239. if err := system.LUtimesNano(path, ts); err != nil {
  240. return err
  241. }
  242. }
  243. return nil
  244. }
  245. // Tar creates an archive from the directory at `path`, and returns it as a
  246. // stream of bytes.
  247. func Tar(path string, compression Compression) (io.ReadCloser, error) {
  248. return TarFilter(path, &TarOptions{Compression: compression})
  249. }
  250. func escapeName(name string) string {
  251. escaped := make([]byte, 0)
  252. for i, c := range []byte(name) {
  253. if i == 0 && c == '/' {
  254. continue
  255. }
  256. // all printable chars except "-" which is 0x2d
  257. if (0x20 <= c && c <= 0x7E) && c != 0x2d {
  258. escaped = append(escaped, c)
  259. } else {
  260. escaped = append(escaped, fmt.Sprintf("\\%03o", c)...)
  261. }
  262. }
  263. return string(escaped)
  264. }
  265. // Tar creates an archive from the directory at `path`, only including files whose relative
  266. // paths are included in `filter`. If `filter` is nil, then all files are included.
  267. func TarFilter(srcPath string, options *TarOptions) (io.ReadCloser, error) {
  268. pipeReader, pipeWriter := io.Pipe()
  269. compressWriter, err := CompressStream(pipeWriter, options.Compression)
  270. if err != nil {
  271. return nil, err
  272. }
  273. tw := tar.NewWriter(compressWriter)
  274. go func() {
  275. // In general we log errors here but ignore them because
  276. // during e.g. a diff operation the container can continue
  277. // mutating the filesystem and we can see transient errors
  278. // from this
  279. if options.Includes == nil {
  280. options.Includes = []string{"."}
  281. }
  282. for _, include := range options.Includes {
  283. filepath.Walk(filepath.Join(srcPath, include), func(filePath string, f os.FileInfo, err error) error {
  284. if err != nil {
  285. utils.Debugf("Tar: Can't stat file %s to tar: %s\n", srcPath, err)
  286. return nil
  287. }
  288. relFilePath, err := filepath.Rel(srcPath, filePath)
  289. if err != nil {
  290. return nil
  291. }
  292. if err := addTarFile(filePath, relFilePath, tw); err != nil {
  293. utils.Debugf("Can't add file %s to tar: %s\n", srcPath, err)
  294. }
  295. return nil
  296. })
  297. }
  298. // Make sure to check the error on Close.
  299. if err := tw.Close(); err != nil {
  300. utils.Debugf("Can't close tar writer: %s\n", err)
  301. }
  302. if err := compressWriter.Close(); err != nil {
  303. utils.Debugf("Can't close compress writer: %s\n", err)
  304. }
  305. if err := pipeWriter.Close(); err != nil {
  306. utils.Debugf("Can't close pipe writer: %s\n", err)
  307. }
  308. }()
  309. return pipeReader, nil
  310. }
  311. // Untar reads a stream of bytes from `archive`, parses it as a tar archive,
  312. // and unpacks it into the directory at `path`.
  313. // The archive may be compressed with one of the following algorithms:
  314. // identity (uncompressed), gzip, bzip2, xz.
  315. // FIXME: specify behavior when target path exists vs. doesn't exist.
  316. func Untar(archive io.Reader, dest string, options *TarOptions) error {
  317. if archive == nil {
  318. return fmt.Errorf("Empty archive")
  319. }
  320. decompressedArchive, err := DecompressStream(archive)
  321. if err != nil {
  322. return err
  323. }
  324. defer decompressedArchive.Close()
  325. tr := tar.NewReader(decompressedArchive)
  326. var dirs []*tar.Header
  327. // Iterate through the files in the archive.
  328. for {
  329. hdr, err := tr.Next()
  330. if err == io.EOF {
  331. // end of tar archive
  332. break
  333. }
  334. if err != nil {
  335. return err
  336. }
  337. // Normalize name, for safety and for a simple is-root check
  338. hdr.Name = filepath.Clean(hdr.Name)
  339. if !strings.HasSuffix(hdr.Name, "/") {
  340. // Not the root directory, ensure that the parent directory exists
  341. parent := filepath.Dir(hdr.Name)
  342. parentPath := filepath.Join(dest, parent)
  343. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  344. err = os.MkdirAll(parentPath, 0777)
  345. if err != nil {
  346. return err
  347. }
  348. }
  349. }
  350. path := filepath.Join(dest, hdr.Name)
  351. // If path exits we almost always just want to remove and replace it
  352. // The only exception is when it is a directory *and* the file from
  353. // the layer is also a directory. Then we want to merge them (i.e.
  354. // just apply the metadata from the layer).
  355. if fi, err := os.Lstat(path); err == nil {
  356. if fi.IsDir() && hdr.Name == "." {
  357. continue
  358. }
  359. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  360. if err := os.RemoveAll(path); err != nil {
  361. return err
  362. }
  363. }
  364. }
  365. if err := createTarFile(path, dest, hdr, tr); err != nil {
  366. return err
  367. }
  368. // Directory mtimes must be handled at the end to avoid further
  369. // file creation in them to modify the directory mtime
  370. if hdr.Typeflag == tar.TypeDir {
  371. dirs = append(dirs, hdr)
  372. }
  373. }
  374. for _, hdr := range dirs {
  375. path := filepath.Join(dest, hdr.Name)
  376. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  377. if err := syscall.UtimesNano(path, ts); err != nil {
  378. return err
  379. }
  380. }
  381. return nil
  382. }
  383. // TarUntar is a convenience function which calls Tar and Untar, with
  384. // the output of one piped into the other. If either Tar or Untar fails,
  385. // TarUntar aborts and returns the error.
  386. func TarUntar(src string, dst string) error {
  387. utils.Debugf("TarUntar(%s %s)", src, dst)
  388. archive, err := TarFilter(src, &TarOptions{Compression: Uncompressed})
  389. if err != nil {
  390. return err
  391. }
  392. defer archive.Close()
  393. return Untar(archive, dst, nil)
  394. }
  395. // UntarPath is a convenience function which looks for an archive
  396. // at filesystem path `src`, and unpacks it at `dst`.
  397. func UntarPath(src, dst string) error {
  398. archive, err := os.Open(src)
  399. if err != nil {
  400. return err
  401. }
  402. defer archive.Close()
  403. if err := Untar(archive, dst, nil); err != nil {
  404. return err
  405. }
  406. return nil
  407. }
  408. // CopyWithTar creates a tar archive of filesystem path `src`, and
  409. // unpacks it at filesystem path `dst`.
  410. // The archive is streamed directly with fixed buffering and no
  411. // intermediary disk IO.
  412. //
  413. func CopyWithTar(src, dst string) error {
  414. srcSt, err := os.Stat(src)
  415. if err != nil {
  416. return err
  417. }
  418. if !srcSt.IsDir() {
  419. return CopyFileWithTar(src, dst)
  420. }
  421. // Create dst, copy src's content into it
  422. utils.Debugf("Creating dest directory: %s", dst)
  423. if err := os.MkdirAll(dst, 0755); err != nil && !os.IsExist(err) {
  424. return err
  425. }
  426. utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
  427. return TarUntar(src, dst)
  428. }
  429. // CopyFileWithTar emulates the behavior of the 'cp' command-line
  430. // for a single file. It copies a regular file from path `src` to
  431. // path `dst`, and preserves all its metadata.
  432. //
  433. // If `dst` ends with a trailing slash '/', the final destination path
  434. // will be `dst/base(src)`.
  435. func CopyFileWithTar(src, dst string) (err error) {
  436. utils.Debugf("CopyFileWithTar(%s, %s)", src, dst)
  437. srcSt, err := os.Stat(src)
  438. if err != nil {
  439. return err
  440. }
  441. if srcSt.IsDir() {
  442. return fmt.Errorf("Can't copy a directory")
  443. }
  444. // Clean up the trailing /
  445. if dst[len(dst)-1] == '/' {
  446. dst = path.Join(dst, filepath.Base(src))
  447. }
  448. // Create the holding directory if necessary
  449. if err := os.MkdirAll(filepath.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  450. return err
  451. }
  452. r, w := io.Pipe()
  453. errC := utils.Go(func() error {
  454. defer w.Close()
  455. srcF, err := os.Open(src)
  456. if err != nil {
  457. return err
  458. }
  459. defer srcF.Close()
  460. tw := tar.NewWriter(w)
  461. hdr, err := tar.FileInfoHeader(srcSt, "")
  462. if err != nil {
  463. return err
  464. }
  465. hdr.Name = filepath.Base(dst)
  466. if err := tw.WriteHeader(hdr); err != nil {
  467. return err
  468. }
  469. if _, err := io.Copy(tw, srcF); err != nil {
  470. return err
  471. }
  472. tw.Close()
  473. return nil
  474. })
  475. defer func() {
  476. if er := <-errC; err != nil {
  477. err = er
  478. }
  479. }()
  480. return Untar(r, filepath.Dir(dst), nil)
  481. }
  482. // CmdStream executes a command, and returns its stdout as a stream.
  483. // If the command fails to run or doesn't complete successfully, an error
  484. // will be returned, including anything written on stderr.
  485. func CmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) {
  486. if input != nil {
  487. stdin, err := cmd.StdinPipe()
  488. if err != nil {
  489. return nil, err
  490. }
  491. // Write stdin if any
  492. go func() {
  493. io.Copy(stdin, input)
  494. stdin.Close()
  495. }()
  496. }
  497. stdout, err := cmd.StdoutPipe()
  498. if err != nil {
  499. return nil, err
  500. }
  501. stderr, err := cmd.StderrPipe()
  502. if err != nil {
  503. return nil, err
  504. }
  505. pipeR, pipeW := io.Pipe()
  506. errChan := make(chan []byte)
  507. // Collect stderr, we will use it in case of an error
  508. go func() {
  509. errText, e := ioutil.ReadAll(stderr)
  510. if e != nil {
  511. errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
  512. }
  513. errChan <- errText
  514. }()
  515. // Copy stdout to the returned pipe
  516. go func() {
  517. _, err := io.Copy(pipeW, stdout)
  518. if err != nil {
  519. pipeW.CloseWithError(err)
  520. }
  521. errText := <-errChan
  522. if err := cmd.Wait(); err != nil {
  523. pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errText))
  524. } else {
  525. pipeW.Close()
  526. }
  527. }()
  528. // Run the command and return the pipe
  529. if err := cmd.Start(); err != nil {
  530. return nil, err
  531. }
  532. return pipeR, nil
  533. }
  534. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  535. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  536. // the file will be deleted.
  537. func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
  538. f, err := ioutil.TempFile(dir, "")
  539. if err != nil {
  540. return nil, err
  541. }
  542. if _, err := io.Copy(f, src); err != nil {
  543. return nil, err
  544. }
  545. if err = f.Sync(); err != nil {
  546. return nil, err
  547. }
  548. if _, err := f.Seek(0, 0); err != nil {
  549. return nil, err
  550. }
  551. st, err := f.Stat()
  552. if err != nil {
  553. return nil, err
  554. }
  555. size := st.Size()
  556. return &TempArchive{f, size}, nil
  557. }
  558. type TempArchive struct {
  559. *os.File
  560. Size int64 // Pre-computed from Stat().Size() as a convenience
  561. }
  562. func (archive *TempArchive) Read(data []byte) (int, error) {
  563. n, err := archive.File.Read(data)
  564. if err != nil {
  565. os.Remove(archive.File.Name())
  566. }
  567. return n, err
  568. }