archive.go 17 KB

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