archive.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. package archive
  2. import (
  3. "archive/tar"
  4. "bufio"
  5. "bytes"
  6. "compress/bzip2"
  7. "compress/gzip"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "os"
  13. "os/exec"
  14. "path/filepath"
  15. "runtime"
  16. "strings"
  17. "syscall"
  18. "github.com/Sirupsen/logrus"
  19. "github.com/docker/docker/pkg/fileutils"
  20. "github.com/docker/docker/pkg/idtools"
  21. "github.com/docker/docker/pkg/ioutils"
  22. "github.com/docker/docker/pkg/pools"
  23. "github.com/docker/docker/pkg/promise"
  24. "github.com/docker/docker/pkg/system"
  25. )
  26. type (
  27. // Archive is a type of io.ReadCloser which has two interfaces Read and Closer.
  28. Archive io.ReadCloser
  29. // Reader is a type of io.Reader.
  30. Reader io.Reader
  31. // Compression is the state represents if compressed or not.
  32. Compression int
  33. // WhiteoutFormat is the format of whiteouts unpacked
  34. WhiteoutFormat int
  35. // TarChownOptions wraps the chown options UID and GID.
  36. TarChownOptions struct {
  37. UID, GID int
  38. }
  39. // TarOptions wraps the tar options.
  40. TarOptions struct {
  41. IncludeFiles []string
  42. ExcludePatterns []string
  43. Compression Compression
  44. NoLchown bool
  45. UIDMaps []idtools.IDMap
  46. GIDMaps []idtools.IDMap
  47. ChownOpts *TarChownOptions
  48. IncludeSourceDir bool
  49. // WhiteoutFormat is the expected on disk format for whiteout files.
  50. // This format will be converted to the standard format on pack
  51. // and from the standard format on unpack.
  52. WhiteoutFormat WhiteoutFormat
  53. // When unpacking, specifies whether overwriting a directory with a
  54. // non-directory is allowed and vice versa.
  55. NoOverwriteDirNonDir bool
  56. // For each include when creating an archive, the included name will be
  57. // replaced with the matching name from this map.
  58. RebaseNames map[string]string
  59. }
  60. // Archiver allows the reuse of most utility functions of this package
  61. // with a pluggable Untar function. Also, to facilitate the passing of
  62. // specific id mappings for untar, an archiver can be created with maps
  63. // which will then be passed to Untar operations
  64. Archiver struct {
  65. Untar func(io.Reader, string, *TarOptions) error
  66. UIDMaps []idtools.IDMap
  67. GIDMaps []idtools.IDMap
  68. }
  69. // breakoutError is used to differentiate errors related to breaking out
  70. // When testing archive breakout in the unit tests, this error is expected
  71. // in order for the test to pass.
  72. breakoutError error
  73. )
  74. var (
  75. // ErrNotImplemented is the error message of function not implemented.
  76. ErrNotImplemented = errors.New("Function not implemented")
  77. defaultArchiver = &Archiver{Untar: Untar, UIDMaps: nil, GIDMaps: nil}
  78. )
  79. const (
  80. // HeaderSize is the size in bytes of a tar header
  81. HeaderSize = 512
  82. )
  83. const (
  84. // Uncompressed represents the uncompressed.
  85. Uncompressed Compression = iota
  86. // Bzip2 is bzip2 compression algorithm.
  87. Bzip2
  88. // Gzip is gzip compression algorithm.
  89. Gzip
  90. // Xz is xz compression algorithm.
  91. Xz
  92. )
  93. const (
  94. // AUFSWhiteoutFormat is the default format for whiteouts
  95. AUFSWhiteoutFormat WhiteoutFormat = iota
  96. // OverlayWhiteoutFormat formats whiteout according to the overlay
  97. // standard.
  98. OverlayWhiteoutFormat
  99. )
  100. // IsArchive checks for the magic bytes of a tar or any supported compression
  101. // algorithm.
  102. func IsArchive(header []byte) bool {
  103. compression := DetectCompression(header)
  104. if compression != Uncompressed {
  105. return true
  106. }
  107. r := tar.NewReader(bytes.NewBuffer(header))
  108. _, err := r.Next()
  109. return err == nil
  110. }
  111. // IsArchivePath checks if the (possibly compressed) file at the given path
  112. // starts with a tar file header.
  113. func IsArchivePath(path string) bool {
  114. file, err := os.Open(path)
  115. if err != nil {
  116. return false
  117. }
  118. defer file.Close()
  119. rdr, err := DecompressStream(file)
  120. if err != nil {
  121. return false
  122. }
  123. r := tar.NewReader(rdr)
  124. _, err = r.Next()
  125. return err == nil
  126. }
  127. // DetectCompression detects the compression algorithm of the source.
  128. func DetectCompression(source []byte) Compression {
  129. for compression, m := range map[Compression][]byte{
  130. Bzip2: {0x42, 0x5A, 0x68},
  131. Gzip: {0x1F, 0x8B, 0x08},
  132. Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
  133. } {
  134. if len(source) < len(m) {
  135. logrus.Debug("Len too short")
  136. continue
  137. }
  138. if bytes.Compare(m, source[:len(m)]) == 0 {
  139. return compression
  140. }
  141. }
  142. return Uncompressed
  143. }
  144. func xzDecompress(archive io.Reader) (io.ReadCloser, <-chan struct{}, error) {
  145. args := []string{"xz", "-d", "-c", "-q"}
  146. return cmdStream(exec.Command(args[0], args[1:]...), archive)
  147. }
  148. // DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.
  149. func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
  150. p := pools.BufioReader32KPool
  151. buf := p.Get(archive)
  152. bs, err := buf.Peek(10)
  153. if err != nil && err != io.EOF {
  154. // Note: we'll ignore any io.EOF error because there are some odd
  155. // cases where the layer.tar file will be empty (zero bytes) and
  156. // that results in an io.EOF from the Peek() call. So, in those
  157. // cases we'll just treat it as a non-compressed stream and
  158. // that means just create an empty layer.
  159. // See Issue 18170
  160. return nil, err
  161. }
  162. compression := DetectCompression(bs)
  163. switch compression {
  164. case Uncompressed:
  165. readBufWrapper := p.NewReadCloserWrapper(buf, buf)
  166. return readBufWrapper, nil
  167. case Gzip:
  168. gzReader, err := gzip.NewReader(buf)
  169. if err != nil {
  170. return nil, err
  171. }
  172. readBufWrapper := p.NewReadCloserWrapper(buf, gzReader)
  173. return readBufWrapper, nil
  174. case Bzip2:
  175. bz2Reader := bzip2.NewReader(buf)
  176. readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader)
  177. return readBufWrapper, nil
  178. case Xz:
  179. xzReader, chdone, err := xzDecompress(buf)
  180. if err != nil {
  181. return nil, err
  182. }
  183. readBufWrapper := p.NewReadCloserWrapper(buf, xzReader)
  184. return ioutils.NewReadCloserWrapper(readBufWrapper, func() error {
  185. <-chdone
  186. return readBufWrapper.Close()
  187. }), nil
  188. default:
  189. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  190. }
  191. }
  192. // CompressStream compresseses the dest with specified compression algorithm.
  193. func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error) {
  194. p := pools.BufioWriter32KPool
  195. buf := p.Get(dest)
  196. switch compression {
  197. case Uncompressed:
  198. writeBufWrapper := p.NewWriteCloserWrapper(buf, buf)
  199. return writeBufWrapper, nil
  200. case Gzip:
  201. gzWriter := gzip.NewWriter(dest)
  202. writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter)
  203. return writeBufWrapper, nil
  204. case Bzip2, Xz:
  205. // archive/bzip2 does not support writing, and there is no xz support at all
  206. // However, this is not a problem as docker only currently generates gzipped tars
  207. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  208. default:
  209. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  210. }
  211. }
  212. // Extension returns the extension of a file that uses the specified compression algorithm.
  213. func (compression *Compression) Extension() string {
  214. switch *compression {
  215. case Uncompressed:
  216. return "tar"
  217. case Bzip2:
  218. return "tar.bz2"
  219. case Gzip:
  220. return "tar.gz"
  221. case Xz:
  222. return "tar.xz"
  223. }
  224. return ""
  225. }
  226. type tarWhiteoutConverter interface {
  227. ConvertWrite(*tar.Header, string, os.FileInfo) error
  228. ConvertRead(*tar.Header, string) (bool, error)
  229. }
  230. type tarAppender struct {
  231. TarWriter *tar.Writer
  232. Buffer *bufio.Writer
  233. // for hardlink mapping
  234. SeenFiles map[uint64]string
  235. UIDMaps []idtools.IDMap
  236. GIDMaps []idtools.IDMap
  237. // For packing and unpacking whiteout files in the
  238. // non standard format. The whiteout files defined
  239. // by the AUFS standard are used as the tar whiteout
  240. // standard.
  241. WhiteoutConverter tarWhiteoutConverter
  242. }
  243. // canonicalTarName provides a platform-independent and consistent posix-style
  244. //path for files and directories to be archived regardless of the platform.
  245. func canonicalTarName(name string, isDir bool) (string, error) {
  246. name, err := CanonicalTarNameForPath(name)
  247. if err != nil {
  248. return "", err
  249. }
  250. // suffix with '/' for directories
  251. if isDir && !strings.HasSuffix(name, "/") {
  252. name += "/"
  253. }
  254. return name, nil
  255. }
  256. // addTarFile adds to the tar archive a file from `path` as `name`
  257. func (ta *tarAppender) addTarFile(path, name string) error {
  258. fi, err := os.Lstat(path)
  259. if err != nil {
  260. return err
  261. }
  262. link := ""
  263. if fi.Mode()&os.ModeSymlink != 0 {
  264. if link, err = os.Readlink(path); err != nil {
  265. return err
  266. }
  267. }
  268. hdr, err := tar.FileInfoHeader(fi, link)
  269. if err != nil {
  270. return err
  271. }
  272. hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
  273. name, err = canonicalTarName(name, fi.IsDir())
  274. if err != nil {
  275. return fmt.Errorf("tar: cannot canonicalize path: %v", err)
  276. }
  277. hdr.Name = name
  278. inode, err := setHeaderForSpecialDevice(hdr, ta, name, fi.Sys())
  279. if err != nil {
  280. return err
  281. }
  282. // if it's not a directory and has more than 1 link,
  283. // it's hardlinked, so set the type flag accordingly
  284. if !fi.IsDir() && hasHardlinks(fi) {
  285. // a link should have a name that it links too
  286. // and that linked name should be first in the tar archive
  287. if oldpath, ok := ta.SeenFiles[inode]; ok {
  288. hdr.Typeflag = tar.TypeLink
  289. hdr.Linkname = oldpath
  290. hdr.Size = 0 // This Must be here for the writer math to add up!
  291. } else {
  292. ta.SeenFiles[inode] = name
  293. }
  294. }
  295. capability, _ := system.Lgetxattr(path, "security.capability")
  296. if capability != nil {
  297. hdr.Xattrs = make(map[string]string)
  298. hdr.Xattrs["security.capability"] = string(capability)
  299. }
  300. //handle re-mapping container ID mappings back to host ID mappings before
  301. //writing tar headers/files. We skip whiteout files because they were written
  302. //by the kernel and already have proper ownership relative to the host
  303. if !strings.HasPrefix(filepath.Base(hdr.Name), WhiteoutPrefix) && (ta.UIDMaps != nil || ta.GIDMaps != nil) {
  304. uid, gid, err := getFileUIDGID(fi.Sys())
  305. if err != nil {
  306. return err
  307. }
  308. xUID, err := idtools.ToContainer(uid, ta.UIDMaps)
  309. if err != nil {
  310. return err
  311. }
  312. xGID, err := idtools.ToContainer(gid, ta.GIDMaps)
  313. if err != nil {
  314. return err
  315. }
  316. hdr.Uid = xUID
  317. hdr.Gid = xGID
  318. }
  319. if ta.WhiteoutConverter != nil {
  320. if err := ta.WhiteoutConverter.ConvertWrite(hdr, path, fi); err != nil {
  321. return err
  322. }
  323. }
  324. if err := ta.TarWriter.WriteHeader(hdr); err != nil {
  325. return err
  326. }
  327. if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
  328. file, err := os.Open(path)
  329. if err != nil {
  330. return err
  331. }
  332. ta.Buffer.Reset(ta.TarWriter)
  333. defer ta.Buffer.Reset(nil)
  334. _, err = io.Copy(ta.Buffer, file)
  335. file.Close()
  336. if err != nil {
  337. return err
  338. }
  339. err = ta.Buffer.Flush()
  340. if err != nil {
  341. return err
  342. }
  343. }
  344. return nil
  345. }
  346. func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool, chownOpts *TarChownOptions) error {
  347. // hdr.Mode is in linux format, which we can use for sycalls,
  348. // but for os.Foo() calls we need the mode converted to os.FileMode,
  349. // so use hdrInfo.Mode() (they differ for e.g. setuid bits)
  350. hdrInfo := hdr.FileInfo()
  351. switch hdr.Typeflag {
  352. case tar.TypeDir:
  353. // Create directory unless it exists as a directory already.
  354. // In that case we just want to merge the two
  355. if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
  356. if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
  357. return err
  358. }
  359. }
  360. case tar.TypeReg, tar.TypeRegA:
  361. // Source is regular file
  362. file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
  363. if err != nil {
  364. return err
  365. }
  366. if _, err := io.Copy(file, reader); err != nil {
  367. file.Close()
  368. return err
  369. }
  370. file.Close()
  371. case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
  372. // Handle this is an OS-specific way
  373. if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
  374. return err
  375. }
  376. case tar.TypeLink:
  377. targetPath := filepath.Join(extractDir, hdr.Linkname)
  378. // check for hardlink breakout
  379. if !strings.HasPrefix(targetPath, extractDir) {
  380. return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname))
  381. }
  382. if err := os.Link(targetPath, path); err != nil {
  383. return err
  384. }
  385. case tar.TypeSymlink:
  386. // path -> hdr.Linkname = targetPath
  387. // e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file
  388. targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname)
  389. // the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because
  390. // that symlink would first have to be created, which would be caught earlier, at this very check:
  391. if !strings.HasPrefix(targetPath, extractDir) {
  392. return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname))
  393. }
  394. if err := os.Symlink(hdr.Linkname, path); err != nil {
  395. return err
  396. }
  397. case tar.TypeXGlobalHeader:
  398. logrus.Debug("PAX Global Extended Headers found and ignored")
  399. return nil
  400. default:
  401. return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag)
  402. }
  403. // Lchown is not supported on Windows.
  404. if Lchown && runtime.GOOS != "windows" {
  405. if chownOpts == nil {
  406. chownOpts = &TarChownOptions{UID: hdr.Uid, GID: hdr.Gid}
  407. }
  408. if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil {
  409. return err
  410. }
  411. }
  412. var errors []string
  413. for key, value := range hdr.Xattrs {
  414. if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil {
  415. if err == syscall.ENOTSUP {
  416. // We ignore errors here because not all graphdrivers support
  417. // xattrs *cough* old versions of AUFS *cough*. However only
  418. // ENOTSUP should be emitted in that case, otherwise we still
  419. // bail.
  420. errors = append(errors, err.Error())
  421. continue
  422. }
  423. return err
  424. }
  425. }
  426. if len(errors) > 0 {
  427. logrus.WithFields(logrus.Fields{
  428. "errors": errors,
  429. }).Warn("ignored xattrs in archive: underlying filesystem doesn't support them")
  430. }
  431. // There is no LChmod, so ignore mode for symlink. Also, this
  432. // must happen after chown, as that can modify the file mode
  433. if err := handleLChmod(hdr, path, hdrInfo); err != nil {
  434. return err
  435. }
  436. aTime := hdr.AccessTime
  437. if aTime.Before(hdr.ModTime) {
  438. // Last access time should never be before last modified time.
  439. aTime = hdr.ModTime
  440. }
  441. // system.Chtimes doesn't support a NOFOLLOW flag atm
  442. if hdr.Typeflag == tar.TypeLink {
  443. if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
  444. if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
  445. return err
  446. }
  447. }
  448. } else if hdr.Typeflag != tar.TypeSymlink {
  449. if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
  450. return err
  451. }
  452. } else {
  453. ts := []syscall.Timespec{timeToTimespec(aTime), timeToTimespec(hdr.ModTime)}
  454. if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
  455. return err
  456. }
  457. }
  458. return nil
  459. }
  460. // Tar creates an archive from the directory at `path`, and returns it as a
  461. // stream of bytes.
  462. func Tar(path string, compression Compression) (io.ReadCloser, error) {
  463. return TarWithOptions(path, &TarOptions{Compression: compression})
  464. }
  465. // TarWithOptions creates an archive from the directory at `path`, only including files whose relative
  466. // paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
  467. func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
  468. // Fix the source path to work with long path names. This is a no-op
  469. // on platforms other than Windows.
  470. srcPath = fixVolumePathPrefix(srcPath)
  471. patterns, patDirs, exceptions, err := fileutils.CleanPatterns(options.ExcludePatterns)
  472. if err != nil {
  473. return nil, err
  474. }
  475. pipeReader, pipeWriter := io.Pipe()
  476. compressWriter, err := CompressStream(pipeWriter, options.Compression)
  477. if err != nil {
  478. return nil, err
  479. }
  480. go func() {
  481. ta := &tarAppender{
  482. TarWriter: tar.NewWriter(compressWriter),
  483. Buffer: pools.BufioWriter32KPool.Get(nil),
  484. SeenFiles: make(map[uint64]string),
  485. UIDMaps: options.UIDMaps,
  486. GIDMaps: options.GIDMaps,
  487. WhiteoutConverter: getWhiteoutConverter(options.WhiteoutFormat),
  488. }
  489. defer func() {
  490. // Make sure to check the error on Close.
  491. if err := ta.TarWriter.Close(); err != nil {
  492. logrus.Errorf("Can't close tar writer: %s", err)
  493. }
  494. if err := compressWriter.Close(); err != nil {
  495. logrus.Errorf("Can't close compress writer: %s", err)
  496. }
  497. if err := pipeWriter.Close(); err != nil {
  498. logrus.Errorf("Can't close pipe writer: %s", err)
  499. }
  500. }()
  501. // this buffer is needed for the duration of this piped stream
  502. defer pools.BufioWriter32KPool.Put(ta.Buffer)
  503. // In general we log errors here but ignore them because
  504. // during e.g. a diff operation the container can continue
  505. // mutating the filesystem and we can see transient errors
  506. // from this
  507. stat, err := os.Lstat(srcPath)
  508. if err != nil {
  509. return
  510. }
  511. if !stat.IsDir() {
  512. // We can't later join a non-dir with any includes because the
  513. // 'walk' will error if "file/." is stat-ed and "file" is not a
  514. // directory. So, we must split the source path and use the
  515. // basename as the include.
  516. if len(options.IncludeFiles) > 0 {
  517. logrus.Warn("Tar: Can't archive a file with includes")
  518. }
  519. dir, base := SplitPathDirEntry(srcPath)
  520. srcPath = dir
  521. options.IncludeFiles = []string{base}
  522. }
  523. if len(options.IncludeFiles) == 0 {
  524. options.IncludeFiles = []string{"."}
  525. }
  526. seen := make(map[string]bool)
  527. for _, include := range options.IncludeFiles {
  528. rebaseName := options.RebaseNames[include]
  529. walkRoot := getWalkRoot(srcPath, include)
  530. filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error {
  531. if err != nil {
  532. logrus.Errorf("Tar: Can't stat file %s to tar: %s", srcPath, err)
  533. return nil
  534. }
  535. relFilePath, err := filepath.Rel(srcPath, filePath)
  536. if err != nil || (!options.IncludeSourceDir && relFilePath == "." && f.IsDir()) {
  537. // Error getting relative path OR we are looking
  538. // at the source directory path. Skip in both situations.
  539. return nil
  540. }
  541. if options.IncludeSourceDir && include == "." && relFilePath != "." {
  542. relFilePath = strings.Join([]string{".", relFilePath}, string(filepath.Separator))
  543. }
  544. skip := false
  545. // If "include" is an exact match for the current file
  546. // then even if there's an "excludePatterns" pattern that
  547. // matches it, don't skip it. IOW, assume an explicit 'include'
  548. // is asking for that file no matter what - which is true
  549. // for some files, like .dockerignore and Dockerfile (sometimes)
  550. if include != relFilePath {
  551. skip, err = fileutils.OptimizedMatches(relFilePath, patterns, patDirs)
  552. if err != nil {
  553. logrus.Errorf("Error matching %s: %v", relFilePath, err)
  554. return err
  555. }
  556. }
  557. if skip {
  558. // If we want to skip this file and its a directory
  559. // then we should first check to see if there's an
  560. // excludes pattern (eg !dir/file) that starts with this
  561. // dir. If so then we can't skip this dir.
  562. // Its not a dir then so we can just return/skip.
  563. if !f.IsDir() {
  564. return nil
  565. }
  566. // No exceptions (!...) in patterns so just skip dir
  567. if !exceptions {
  568. return filepath.SkipDir
  569. }
  570. dirSlash := relFilePath + string(filepath.Separator)
  571. for _, pat := range patterns {
  572. if pat[0] != '!' {
  573. continue
  574. }
  575. pat = pat[1:] + string(filepath.Separator)
  576. if strings.HasPrefix(pat, dirSlash) {
  577. // found a match - so can't skip this dir
  578. return nil
  579. }
  580. }
  581. // No matching exclusion dir so just skip dir
  582. return filepath.SkipDir
  583. }
  584. if seen[relFilePath] {
  585. return nil
  586. }
  587. seen[relFilePath] = true
  588. // Rename the base resource.
  589. if rebaseName != "" {
  590. var replacement string
  591. if rebaseName != string(filepath.Separator) {
  592. // Special case the root directory to replace with an
  593. // empty string instead so that we don't end up with
  594. // double slashes in the paths.
  595. replacement = rebaseName
  596. }
  597. relFilePath = strings.Replace(relFilePath, include, replacement, 1)
  598. }
  599. if err := ta.addTarFile(filePath, relFilePath); err != nil {
  600. logrus.Errorf("Can't add file %s to tar: %s", filePath, err)
  601. // if pipe is broken, stop writing tar stream to it
  602. if err == io.ErrClosedPipe {
  603. return err
  604. }
  605. }
  606. return nil
  607. })
  608. }
  609. }()
  610. return pipeReader, nil
  611. }
  612. // Unpack unpacks the decompressedArchive to dest with options.
  613. func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error {
  614. tr := tar.NewReader(decompressedArchive)
  615. trBuf := pools.BufioReader32KPool.Get(nil)
  616. defer pools.BufioReader32KPool.Put(trBuf)
  617. var dirs []*tar.Header
  618. remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(options.UIDMaps, options.GIDMaps)
  619. if err != nil {
  620. return err
  621. }
  622. whiteoutConverter := getWhiteoutConverter(options.WhiteoutFormat)
  623. // Iterate through the files in the archive.
  624. loop:
  625. for {
  626. hdr, err := tr.Next()
  627. if err == io.EOF {
  628. // end of tar archive
  629. break
  630. }
  631. if err != nil {
  632. return err
  633. }
  634. // Normalize name, for safety and for a simple is-root check
  635. // This keeps "../" as-is, but normalizes "/../" to "/". Or Windows:
  636. // This keeps "..\" as-is, but normalizes "\..\" to "\".
  637. hdr.Name = filepath.Clean(hdr.Name)
  638. for _, exclude := range options.ExcludePatterns {
  639. if strings.HasPrefix(hdr.Name, exclude) {
  640. continue loop
  641. }
  642. }
  643. // After calling filepath.Clean(hdr.Name) above, hdr.Name will now be in
  644. // the filepath format for the OS on which the daemon is running. Hence
  645. // the check for a slash-suffix MUST be done in an OS-agnostic way.
  646. if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
  647. // Not the root directory, ensure that the parent directory exists
  648. parent := filepath.Dir(hdr.Name)
  649. parentPath := filepath.Join(dest, parent)
  650. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  651. err = idtools.MkdirAllNewAs(parentPath, 0777, remappedRootUID, remappedRootGID)
  652. if err != nil {
  653. return err
  654. }
  655. }
  656. }
  657. path := filepath.Join(dest, hdr.Name)
  658. rel, err := filepath.Rel(dest, path)
  659. if err != nil {
  660. return err
  661. }
  662. if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
  663. return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
  664. }
  665. // If path exits we almost always just want to remove and replace it
  666. // The only exception is when it is a directory *and* the file from
  667. // the layer is also a directory. Then we want to merge them (i.e.
  668. // just apply the metadata from the layer).
  669. if fi, err := os.Lstat(path); err == nil {
  670. if options.NoOverwriteDirNonDir && fi.IsDir() && hdr.Typeflag != tar.TypeDir {
  671. // If NoOverwriteDirNonDir is true then we cannot replace
  672. // an existing directory with a non-directory from the archive.
  673. return fmt.Errorf("cannot overwrite directory %q with non-directory %q", path, dest)
  674. }
  675. if options.NoOverwriteDirNonDir && !fi.IsDir() && hdr.Typeflag == tar.TypeDir {
  676. // If NoOverwriteDirNonDir is true then we cannot replace
  677. // an existing non-directory with a directory from the archive.
  678. return fmt.Errorf("cannot overwrite non-directory %q with directory %q", path, dest)
  679. }
  680. if fi.IsDir() && hdr.Name == "." {
  681. continue
  682. }
  683. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  684. if err := os.RemoveAll(path); err != nil {
  685. return err
  686. }
  687. }
  688. }
  689. trBuf.Reset(tr)
  690. // if the options contain a uid & gid maps, convert header uid/gid
  691. // entries using the maps such that lchown sets the proper mapped
  692. // uid/gid after writing the file. We only perform this mapping if
  693. // the file isn't already owned by the remapped root UID or GID, as
  694. // that specific uid/gid has no mapping from container -> host, and
  695. // those files already have the proper ownership for inside the
  696. // container.
  697. if hdr.Uid != remappedRootUID {
  698. xUID, err := idtools.ToHost(hdr.Uid, options.UIDMaps)
  699. if err != nil {
  700. return err
  701. }
  702. hdr.Uid = xUID
  703. }
  704. if hdr.Gid != remappedRootGID {
  705. xGID, err := idtools.ToHost(hdr.Gid, options.GIDMaps)
  706. if err != nil {
  707. return err
  708. }
  709. hdr.Gid = xGID
  710. }
  711. if whiteoutConverter != nil {
  712. writeFile, err := whiteoutConverter.ConvertRead(hdr, path)
  713. if err != nil {
  714. return err
  715. }
  716. if !writeFile {
  717. continue
  718. }
  719. }
  720. if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown, options.ChownOpts); err != nil {
  721. return err
  722. }
  723. // Directory mtimes must be handled at the end to avoid further
  724. // file creation in them to modify the directory mtime
  725. if hdr.Typeflag == tar.TypeDir {
  726. dirs = append(dirs, hdr)
  727. }
  728. }
  729. for _, hdr := range dirs {
  730. path := filepath.Join(dest, hdr.Name)
  731. if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
  732. return err
  733. }
  734. }
  735. return nil
  736. }
  737. // Untar reads a stream of bytes from `archive`, parses it as a tar archive,
  738. // and unpacks it into the directory at `dest`.
  739. // The archive may be compressed with one of the following algorithms:
  740. // identity (uncompressed), gzip, bzip2, xz.
  741. // FIXME: specify behavior when target path exists vs. doesn't exist.
  742. func Untar(tarArchive io.Reader, dest string, options *TarOptions) error {
  743. return untarHandler(tarArchive, dest, options, true)
  744. }
  745. // UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
  746. // and unpacks it into the directory at `dest`.
  747. // The archive must be an uncompressed stream.
  748. func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error {
  749. return untarHandler(tarArchive, dest, options, false)
  750. }
  751. // Handler for teasing out the automatic decompression
  752. func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error {
  753. if tarArchive == nil {
  754. return fmt.Errorf("Empty archive")
  755. }
  756. dest = filepath.Clean(dest)
  757. if options == nil {
  758. options = &TarOptions{}
  759. }
  760. if options.ExcludePatterns == nil {
  761. options.ExcludePatterns = []string{}
  762. }
  763. r := tarArchive
  764. if decompress {
  765. decompressedArchive, err := DecompressStream(tarArchive)
  766. if err != nil {
  767. return err
  768. }
  769. defer decompressedArchive.Close()
  770. r = decompressedArchive
  771. }
  772. return Unpack(r, dest, options)
  773. }
  774. // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
  775. // If either Tar or Untar fails, TarUntar aborts and returns the error.
  776. func (archiver *Archiver) TarUntar(src, dst string) error {
  777. logrus.Debugf("TarUntar(%s %s)", src, dst)
  778. archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed})
  779. if err != nil {
  780. return err
  781. }
  782. defer archive.Close()
  783. var options *TarOptions
  784. if archiver.UIDMaps != nil || archiver.GIDMaps != nil {
  785. options = &TarOptions{
  786. UIDMaps: archiver.UIDMaps,
  787. GIDMaps: archiver.GIDMaps,
  788. }
  789. }
  790. return archiver.Untar(archive, dst, options)
  791. }
  792. // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
  793. // If either Tar or Untar fails, TarUntar aborts and returns the error.
  794. func TarUntar(src, dst string) error {
  795. return defaultArchiver.TarUntar(src, dst)
  796. }
  797. // UntarPath untar a file from path to a destination, src is the source tar file path.
  798. func (archiver *Archiver) UntarPath(src, dst string) error {
  799. archive, err := os.Open(src)
  800. if err != nil {
  801. return err
  802. }
  803. defer archive.Close()
  804. var options *TarOptions
  805. if archiver.UIDMaps != nil || archiver.GIDMaps != nil {
  806. options = &TarOptions{
  807. UIDMaps: archiver.UIDMaps,
  808. GIDMaps: archiver.GIDMaps,
  809. }
  810. }
  811. return archiver.Untar(archive, dst, options)
  812. }
  813. // UntarPath is a convenience function which looks for an archive
  814. // at filesystem path `src`, and unpacks it at `dst`.
  815. func UntarPath(src, dst string) error {
  816. return defaultArchiver.UntarPath(src, dst)
  817. }
  818. // CopyWithTar creates a tar archive of filesystem path `src`, and
  819. // unpacks it at filesystem path `dst`.
  820. // The archive is streamed directly with fixed buffering and no
  821. // intermediary disk IO.
  822. func (archiver *Archiver) CopyWithTar(src, dst string) error {
  823. srcSt, err := os.Stat(src)
  824. if err != nil {
  825. return err
  826. }
  827. if !srcSt.IsDir() {
  828. return archiver.CopyFileWithTar(src, dst)
  829. }
  830. // if this archiver is set up with ID mapping we need to create
  831. // the new destination directory with the remapped root UID/GID pair
  832. // as owner
  833. rootUID, rootGID, err := idtools.GetRootUIDGID(archiver.UIDMaps, archiver.GIDMaps)
  834. if err != nil {
  835. return err
  836. }
  837. // Create dst, copy src's content into it
  838. logrus.Debugf("Creating dest directory: %s", dst)
  839. if err := idtools.MkdirAllNewAs(dst, 0755, rootUID, rootGID); err != nil {
  840. return err
  841. }
  842. logrus.Debugf("Calling TarUntar(%s, %s)", src, dst)
  843. return archiver.TarUntar(src, dst)
  844. }
  845. // CopyWithTar creates a tar archive of filesystem path `src`, and
  846. // unpacks it at filesystem path `dst`.
  847. // The archive is streamed directly with fixed buffering and no
  848. // intermediary disk IO.
  849. func CopyWithTar(src, dst string) error {
  850. return defaultArchiver.CopyWithTar(src, dst)
  851. }
  852. // CopyFileWithTar emulates the behavior of the 'cp' command-line
  853. // for a single file. It copies a regular file from path `src` to
  854. // path `dst`, and preserves all its metadata.
  855. func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
  856. logrus.Debugf("CopyFileWithTar(%s, %s)", src, dst)
  857. srcSt, err := os.Stat(src)
  858. if err != nil {
  859. return err
  860. }
  861. if srcSt.IsDir() {
  862. return fmt.Errorf("Can't copy a directory")
  863. }
  864. // Clean up the trailing slash. This must be done in an operating
  865. // system specific manner.
  866. if dst[len(dst)-1] == os.PathSeparator {
  867. dst = filepath.Join(dst, filepath.Base(src))
  868. }
  869. // Create the holding directory if necessary
  870. if err := system.MkdirAll(filepath.Dir(dst), 0700); err != nil {
  871. return err
  872. }
  873. r, w := io.Pipe()
  874. errC := promise.Go(func() error {
  875. defer w.Close()
  876. srcF, err := os.Open(src)
  877. if err != nil {
  878. return err
  879. }
  880. defer srcF.Close()
  881. hdr, err := tar.FileInfoHeader(srcSt, "")
  882. if err != nil {
  883. return err
  884. }
  885. hdr.Name = filepath.Base(dst)
  886. hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
  887. remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(archiver.UIDMaps, archiver.GIDMaps)
  888. if err != nil {
  889. return err
  890. }
  891. // only perform mapping if the file being copied isn't already owned by the
  892. // uid or gid of the remapped root in the container
  893. if remappedRootUID != hdr.Uid {
  894. xUID, err := idtools.ToHost(hdr.Uid, archiver.UIDMaps)
  895. if err != nil {
  896. return err
  897. }
  898. hdr.Uid = xUID
  899. }
  900. if remappedRootGID != hdr.Gid {
  901. xGID, err := idtools.ToHost(hdr.Gid, archiver.GIDMaps)
  902. if err != nil {
  903. return err
  904. }
  905. hdr.Gid = xGID
  906. }
  907. tw := tar.NewWriter(w)
  908. defer tw.Close()
  909. if err := tw.WriteHeader(hdr); err != nil {
  910. return err
  911. }
  912. if _, err := io.Copy(tw, srcF); err != nil {
  913. return err
  914. }
  915. return nil
  916. })
  917. defer func() {
  918. if er := <-errC; err == nil && er != nil {
  919. err = er
  920. }
  921. }()
  922. err = archiver.Untar(r, filepath.Dir(dst), nil)
  923. if err != nil {
  924. r.CloseWithError(err)
  925. }
  926. return err
  927. }
  928. // CopyFileWithTar emulates the behavior of the 'cp' command-line
  929. // for a single file. It copies a regular file from path `src` to
  930. // path `dst`, and preserves all its metadata.
  931. //
  932. // Destination handling is in an operating specific manner depending
  933. // where the daemon is running. If `dst` ends with a trailing slash
  934. // the final destination path will be `dst/base(src)` (Linux) or
  935. // `dst\base(src)` (Windows).
  936. func CopyFileWithTar(src, dst string) (err error) {
  937. return defaultArchiver.CopyFileWithTar(src, dst)
  938. }
  939. // cmdStream executes a command, and returns its stdout as a stream.
  940. // If the command fails to run or doesn't complete successfully, an error
  941. // will be returned, including anything written on stderr.
  942. func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, <-chan struct{}, error) {
  943. chdone := make(chan struct{})
  944. cmd.Stdin = input
  945. pipeR, pipeW := io.Pipe()
  946. cmd.Stdout = pipeW
  947. var errBuf bytes.Buffer
  948. cmd.Stderr = &errBuf
  949. // Run the command and return the pipe
  950. if err := cmd.Start(); err != nil {
  951. return nil, nil, err
  952. }
  953. // Copy stdout to the returned pipe
  954. go func() {
  955. if err := cmd.Wait(); err != nil {
  956. pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
  957. } else {
  958. pipeW.Close()
  959. }
  960. close(chdone)
  961. }()
  962. return pipeR, chdone, nil
  963. }
  964. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  965. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  966. // the file will be deleted.
  967. func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
  968. f, err := ioutil.TempFile(dir, "")
  969. if err != nil {
  970. return nil, err
  971. }
  972. if _, err := io.Copy(f, src); err != nil {
  973. return nil, err
  974. }
  975. if _, err := f.Seek(0, 0); err != nil {
  976. return nil, err
  977. }
  978. st, err := f.Stat()
  979. if err != nil {
  980. return nil, err
  981. }
  982. size := st.Size()
  983. return &TempArchive{File: f, Size: size}, nil
  984. }
  985. // TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes,
  986. // the file will be deleted.
  987. type TempArchive struct {
  988. *os.File
  989. Size int64 // Pre-computed from Stat().Size() as a convenience
  990. read int64
  991. closed bool
  992. }
  993. // Close closes the underlying file if it's still open, or does a no-op
  994. // to allow callers to try to close the TempArchive multiple times safely.
  995. func (archive *TempArchive) Close() error {
  996. if archive.closed {
  997. return nil
  998. }
  999. archive.closed = true
  1000. return archive.File.Close()
  1001. }
  1002. func (archive *TempArchive) Read(data []byte) (int, error) {
  1003. n, err := archive.File.Read(data)
  1004. archive.read += int64(n)
  1005. if err != nil || archive.read == archive.Size {
  1006. archive.Close()
  1007. os.Remove(archive.File.Name())
  1008. }
  1009. return n, err
  1010. }