archive.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. "strings"
  16. "syscall"
  17. "time"
  18. )
  19. type Archive io.Reader
  20. type Compression int
  21. type TarOptions struct {
  22. Includes []string
  23. Compression Compression
  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)
  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. func addTarFile(path, name string, tw *tar.Writer) error {
  115. var stat syscall.Stat_t
  116. if err := syscall.Lstat(path, &stat); err != nil {
  117. return err
  118. }
  119. mtim := getLastModification(&stat)
  120. atim := getLastAccess(&stat)
  121. hdr := &tar.Header{
  122. Name: name,
  123. Mode: int64(stat.Mode & 07777),
  124. Uid: int(stat.Uid),
  125. Gid: int(stat.Gid),
  126. ModTime: time.Unix(int64(mtim.Sec), int64(mtim.Nsec)),
  127. AccessTime: time.Unix(int64(atim.Sec), int64(atim.Nsec)),
  128. }
  129. if stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR {
  130. hdr.Typeflag = tar.TypeDir
  131. } else if stat.Mode&syscall.S_IFLNK == syscall.S_IFLNK {
  132. hdr.Typeflag = tar.TypeSymlink
  133. if link, err := os.Readlink(path); err != nil {
  134. return err
  135. } else {
  136. hdr.Linkname = link
  137. }
  138. } else if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK ||
  139. stat.Mode&syscall.S_IFCHR == syscall.S_IFCHR {
  140. if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK {
  141. hdr.Typeflag = tar.TypeBlock
  142. } else {
  143. hdr.Typeflag = tar.TypeChar
  144. }
  145. hdr.Devmajor = int64(major(uint64(stat.Rdev)))
  146. hdr.Devminor = int64(minor(uint64(stat.Rdev)))
  147. } else if stat.Mode&syscall.S_IFIFO == syscall.S_IFIFO ||
  148. stat.Mode&syscall.S_IFSOCK == syscall.S_IFSOCK {
  149. hdr.Typeflag = tar.TypeFifo
  150. } else if stat.Mode&syscall.S_IFREG == syscall.S_IFREG {
  151. hdr.Typeflag = tar.TypeReg
  152. hdr.Size = stat.Size
  153. } else {
  154. return fmt.Errorf("Unknown file type: %s\n", path)
  155. }
  156. if err := tw.WriteHeader(hdr); err != nil {
  157. return err
  158. }
  159. if hdr.Typeflag == tar.TypeReg {
  160. if file, err := os.Open(path); err != nil {
  161. return err
  162. } else {
  163. _, err := io.Copy(tw, file)
  164. if err != nil {
  165. return err
  166. }
  167. file.Close()
  168. }
  169. }
  170. return nil
  171. }
  172. func createTarFile(path, extractDir string, hdr *tar.Header, reader *tar.Reader) error {
  173. switch hdr.Typeflag {
  174. case tar.TypeDir:
  175. // Create directory unless it exists as a directory already.
  176. // In that case we just want to merge the two
  177. if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
  178. if err := os.Mkdir(path, os.FileMode(hdr.Mode)); err != nil {
  179. return err
  180. }
  181. }
  182. case tar.TypeReg, tar.TypeRegA:
  183. // Source is regular file
  184. file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.FileMode(hdr.Mode))
  185. if err != nil {
  186. return err
  187. }
  188. if _, err := io.Copy(file, reader); err != nil {
  189. file.Close()
  190. return err
  191. }
  192. file.Close()
  193. case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
  194. mode := uint32(hdr.Mode & 07777)
  195. switch hdr.Typeflag {
  196. case tar.TypeBlock:
  197. mode |= syscall.S_IFBLK
  198. case tar.TypeChar:
  199. mode |= syscall.S_IFCHR
  200. case tar.TypeFifo:
  201. mode |= syscall.S_IFIFO
  202. }
  203. if err := syscall.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
  204. return err
  205. }
  206. case tar.TypeLink:
  207. if err := os.Link(filepath.Join(extractDir, hdr.Linkname), path); err != nil {
  208. return err
  209. }
  210. case tar.TypeSymlink:
  211. if err := os.Symlink(hdr.Linkname, path); err != nil {
  212. return err
  213. }
  214. default:
  215. return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag)
  216. }
  217. if err := syscall.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
  218. return err
  219. }
  220. // There is no LChmod, so ignore mode for symlink. Also, this
  221. // must happen after chown, as that can modify the file mode
  222. if hdr.Typeflag != tar.TypeSymlink {
  223. if err := syscall.Chmod(path, uint32(hdr.Mode&07777)); err != nil {
  224. return err
  225. }
  226. }
  227. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  228. // syscall.UtimesNano doesn't support a NOFOLLOW flag atm, and
  229. if hdr.Typeflag != tar.TypeSymlink {
  230. if err := syscall.UtimesNano(path, ts); err != nil {
  231. return err
  232. }
  233. } else {
  234. if err := LUtimesNano(path, ts); err != nil {
  235. return err
  236. }
  237. }
  238. return nil
  239. }
  240. // Tar creates an archive from the directory at `path`, and returns it as a
  241. // stream of bytes.
  242. func Tar(path string, compression Compression) (io.Reader, error) {
  243. return TarFilter(path, &TarOptions{Compression: compression})
  244. }
  245. func escapeName(name string) string {
  246. escaped := make([]byte, 0)
  247. for i, c := range []byte(name) {
  248. if i == 0 && c == '/' {
  249. continue
  250. }
  251. // all printable chars except "-" which is 0x2d
  252. if (0x20 <= c && c <= 0x7E) && c != 0x2d {
  253. escaped = append(escaped, c)
  254. } else {
  255. escaped = append(escaped, fmt.Sprintf("\\%03o", c)...)
  256. }
  257. }
  258. return string(escaped)
  259. }
  260. // Tar creates an archive from the directory at `path`, only including files whose relative
  261. // paths are included in `filter`. If `filter` is nil, then all files are included.
  262. func TarFilter(path string, options *TarOptions) (io.Reader, error) {
  263. args := []string{"tar", "--numeric-owner", "-f", "-", "-C", path, "-T", "-"}
  264. if options.Includes == nil {
  265. options.Includes = []string{"."}
  266. }
  267. args = append(args, "-c"+options.Compression.Flag())
  268. files := ""
  269. for _, f := range options.Includes {
  270. files = files + escapeName(f) + "\n"
  271. }
  272. return CmdStream(exec.Command(args[0], args[1:]...), bytes.NewBufferString(files))
  273. }
  274. // Untar reads a stream of bytes from `archive`, parses it as a tar archive,
  275. // and unpacks it into the directory at `path`.
  276. // The archive may be compressed with one of the following algorithms:
  277. // identity (uncompressed), gzip, bzip2, xz.
  278. // FIXME: specify behavior when target path exists vs. doesn't exist.
  279. func Untar(archive io.Reader, dest string, options *TarOptions) error {
  280. if archive == nil {
  281. return fmt.Errorf("Empty archive")
  282. }
  283. archive, err := DecompressStream(archive)
  284. if err != nil {
  285. return err
  286. }
  287. tr := tar.NewReader(archive)
  288. var dirs []*tar.Header
  289. // Iterate through the files in the archive.
  290. for {
  291. hdr, err := tr.Next()
  292. if err == io.EOF {
  293. // end of tar archive
  294. break
  295. }
  296. if err != nil {
  297. return err
  298. }
  299. // Normalize name, for safety and for a simple is-root check
  300. hdr.Name = filepath.Clean(hdr.Name)
  301. if !strings.HasSuffix(hdr.Name, "/") {
  302. // Not the root directory, ensure that the parent directory exists
  303. parent := filepath.Dir(hdr.Name)
  304. parentPath := filepath.Join(dest, parent)
  305. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  306. err = os.MkdirAll(parentPath, 600)
  307. if err != nil {
  308. return err
  309. }
  310. }
  311. }
  312. path := filepath.Join(dest, hdr.Name)
  313. // If path exits we almost always just want to remove and replace it
  314. // The only exception is when it is a directory *and* the file from
  315. // the layer is also a directory. Then we want to merge them (i.e.
  316. // just apply the metadata from the layer).
  317. if fi, err := os.Lstat(path); err == nil {
  318. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  319. if err := os.RemoveAll(path); err != nil {
  320. return err
  321. }
  322. }
  323. }
  324. if err := createTarFile(path, dest, hdr, tr); err != nil {
  325. return err
  326. }
  327. // Directory mtimes must be handled at the end to avoid further
  328. // file creation in them to modify the directory mtime
  329. if hdr.Typeflag == tar.TypeDir {
  330. dirs = append(dirs, hdr)
  331. }
  332. }
  333. for _, hdr := range dirs {
  334. path := filepath.Join(dest, hdr.Name)
  335. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  336. if err := syscall.UtimesNano(path, ts); err != nil {
  337. return err
  338. }
  339. }
  340. return nil
  341. }
  342. // TarUntar is a convenience function which calls Tar and Untar, with
  343. // the output of one piped into the other. If either Tar or Untar fails,
  344. // TarUntar aborts and returns the error.
  345. func TarUntar(src string, dst string) error {
  346. utils.Debugf("TarUntar(%s %s)", src, dst)
  347. archive, err := TarFilter(src, &TarOptions{Compression: Uncompressed})
  348. if err != nil {
  349. return err
  350. }
  351. return Untar(archive, dst, nil)
  352. }
  353. // UntarPath is a convenience function which looks for an archive
  354. // at filesystem path `src`, and unpacks it at `dst`.
  355. func UntarPath(src, dst string) error {
  356. if archive, err := os.Open(src); err != nil {
  357. return err
  358. } else if err := Untar(archive, dst, nil); err != nil {
  359. return err
  360. }
  361. return nil
  362. }
  363. // CopyWithTar creates a tar archive of filesystem path `src`, and
  364. // unpacks it at filesystem path `dst`.
  365. // The archive is streamed directly with fixed buffering and no
  366. // intermediary disk IO.
  367. //
  368. func CopyWithTar(src, dst string) error {
  369. srcSt, err := os.Stat(src)
  370. if err != nil {
  371. return err
  372. }
  373. if !srcSt.IsDir() {
  374. return CopyFileWithTar(src, dst)
  375. }
  376. // Create dst, copy src's content into it
  377. utils.Debugf("Creating dest directory: %s", dst)
  378. if err := os.MkdirAll(dst, 0755); err != nil && !os.IsExist(err) {
  379. return err
  380. }
  381. utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
  382. return TarUntar(src, dst)
  383. }
  384. // CopyFileWithTar emulates the behavior of the 'cp' command-line
  385. // for a single file. It copies a regular file from path `src` to
  386. // path `dst`, and preserves all its metadata.
  387. //
  388. // If `dst` ends with a trailing slash '/', the final destination path
  389. // will be `dst/base(src)`.
  390. func CopyFileWithTar(src, dst string) (err error) {
  391. utils.Debugf("CopyFileWithTar(%s, %s)", src, dst)
  392. srcSt, err := os.Stat(src)
  393. if err != nil {
  394. return err
  395. }
  396. if srcSt.IsDir() {
  397. return fmt.Errorf("Can't copy a directory")
  398. }
  399. // Clean up the trailing /
  400. if dst[len(dst)-1] == '/' {
  401. dst = path.Join(dst, filepath.Base(src))
  402. }
  403. // Create the holding directory if necessary
  404. if err := os.MkdirAll(filepath.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  405. return err
  406. }
  407. r, w := io.Pipe()
  408. errC := utils.Go(func() error {
  409. defer w.Close()
  410. srcF, err := os.Open(src)
  411. if err != nil {
  412. return err
  413. }
  414. defer srcF.Close()
  415. tw := tar.NewWriter(w)
  416. hdr, err := tar.FileInfoHeader(srcSt, "")
  417. if err != nil {
  418. return err
  419. }
  420. hdr.Name = filepath.Base(dst)
  421. if err := tw.WriteHeader(hdr); err != nil {
  422. return err
  423. }
  424. if _, err := io.Copy(tw, srcF); err != nil {
  425. return err
  426. }
  427. tw.Close()
  428. return nil
  429. })
  430. defer func() {
  431. if er := <-errC; err != nil {
  432. err = er
  433. }
  434. }()
  435. return Untar(r, filepath.Dir(dst), nil)
  436. }
  437. // CmdStream executes a command, and returns its stdout as a stream.
  438. // If the command fails to run or doesn't complete successfully, an error
  439. // will be returned, including anything written on stderr.
  440. func CmdStream(cmd *exec.Cmd, input io.Reader) (io.Reader, error) {
  441. if input != nil {
  442. stdin, err := cmd.StdinPipe()
  443. if err != nil {
  444. return nil, err
  445. }
  446. // Write stdin if any
  447. go func() {
  448. io.Copy(stdin, input)
  449. stdin.Close()
  450. }()
  451. }
  452. stdout, err := cmd.StdoutPipe()
  453. if err != nil {
  454. return nil, err
  455. }
  456. stderr, err := cmd.StderrPipe()
  457. if err != nil {
  458. return nil, err
  459. }
  460. pipeR, pipeW := io.Pipe()
  461. errChan := make(chan []byte)
  462. // Collect stderr, we will use it in case of an error
  463. go func() {
  464. errText, e := ioutil.ReadAll(stderr)
  465. if e != nil {
  466. errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
  467. }
  468. errChan <- errText
  469. }()
  470. // Copy stdout to the returned pipe
  471. go func() {
  472. _, err := io.Copy(pipeW, stdout)
  473. if err != nil {
  474. pipeW.CloseWithError(err)
  475. }
  476. errText := <-errChan
  477. if err := cmd.Wait(); err != nil {
  478. pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errText))
  479. } else {
  480. pipeW.Close()
  481. }
  482. }()
  483. // Run the command and return the pipe
  484. if err := cmd.Start(); err != nil {
  485. return nil, err
  486. }
  487. return pipeR, nil
  488. }
  489. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  490. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  491. // the file will be deleted.
  492. func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
  493. f, err := ioutil.TempFile(dir, "")
  494. if err != nil {
  495. return nil, err
  496. }
  497. if _, err := io.Copy(f, src); err != nil {
  498. return nil, err
  499. }
  500. if _, err := f.Seek(0, 0); err != nil {
  501. return nil, err
  502. }
  503. st, err := f.Stat()
  504. if err != nil {
  505. return nil, err
  506. }
  507. size := st.Size()
  508. return &TempArchive{f, size}, nil
  509. }
  510. type TempArchive struct {
  511. *os.File
  512. Size int64 // Pre-computed from Stat().Size() as a convenience
  513. }
  514. func (archive *TempArchive) Read(data []byte) (int, error) {
  515. n, err := archive.File.Read(data)
  516. if err != nil {
  517. os.Remove(archive.File.Name())
  518. }
  519. return n, err
  520. }