archive.go 35 KB

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