archive.go 31 KB

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