archive.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. package archive // import "github.com/docker/docker/pkg/archive"
  2. import (
  3. "archive/tar"
  4. "bufio"
  5. "bytes"
  6. "compress/bzip2"
  7. "compress/gzip"
  8. "context"
  9. "encoding/binary"
  10. "fmt"
  11. "io"
  12. "os"
  13. "path/filepath"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "syscall"
  18. "time"
  19. "github.com/containerd/containerd/pkg/userns"
  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/system"
  24. "github.com/klauspost/compress/zstd"
  25. "github.com/moby/patternmatcher"
  26. "github.com/moby/sys/sequential"
  27. "github.com/pkg/errors"
  28. "github.com/sirupsen/logrus"
  29. exec "golang.org/x/sys/execabs"
  30. )
  31. // ImpliedDirectoryMode represents the mode (Unix permissions) applied to directories that are implied by files in a
  32. // tar, but that do not have their own header entry.
  33. //
  34. // The permissions mask is stored in a constant instead of locally to ensure that magic numbers do not
  35. // proliferate in the codebase. The default value 0755 has been selected based on the default umask of 0022, and
  36. // a convention of mkdir(1) calling mkdir(2) with permissions of 0777, resulting in a final value of 0755.
  37. //
  38. // This value is currently implementation-defined, and not captured in any cross-runtime specification. Thus, it is
  39. // subject to change in Moby at any time -- image authors who require consistent or known directory permissions
  40. // should explicitly control them by ensuring that header entries exist for any applicable path.
  41. const ImpliedDirectoryMode = 0755
  42. type (
  43. // Compression is the state represents if compressed or not.
  44. Compression int
  45. // WhiteoutFormat is the format of whiteouts unpacked
  46. WhiteoutFormat int
  47. // TarOptions wraps the tar options.
  48. TarOptions struct {
  49. IncludeFiles []string
  50. ExcludePatterns []string
  51. Compression Compression
  52. NoLchown bool
  53. IDMap idtools.IdentityMapping
  54. ChownOpts *idtools.Identity
  55. IncludeSourceDir bool
  56. // WhiteoutFormat is the expected on disk format for whiteout files.
  57. // This format will be converted to the standard format on pack
  58. // and from the standard format on unpack.
  59. WhiteoutFormat WhiteoutFormat
  60. // When unpacking, specifies whether overwriting a directory with a
  61. // non-directory is allowed and vice versa.
  62. NoOverwriteDirNonDir bool
  63. // For each include when creating an archive, the included name will be
  64. // replaced with the matching name from this map.
  65. RebaseNames map[string]string
  66. InUserNS bool
  67. }
  68. )
  69. // Archiver implements the Archiver interface and allows the reuse of most utility functions of
  70. // this package with a pluggable Untar function. Also, to facilitate the passing of specific id
  71. // mappings for untar, an Archiver can be created with maps which will then be passed to Untar operations.
  72. type Archiver struct {
  73. Untar func(io.Reader, string, *TarOptions) error
  74. IDMapping idtools.IdentityMapping
  75. }
  76. // NewDefaultArchiver returns a new Archiver without any IdentityMapping
  77. func NewDefaultArchiver() *Archiver {
  78. return &Archiver{Untar: Untar}
  79. }
  80. // breakoutError is used to differentiate errors related to breaking out
  81. // When testing archive breakout in the unit tests, this error is expected
  82. // in order for the test to pass.
  83. type breakoutError error
  84. const (
  85. // Uncompressed represents the uncompressed.
  86. Uncompressed Compression = iota
  87. // Bzip2 is bzip2 compression algorithm.
  88. Bzip2
  89. // Gzip is gzip compression algorithm.
  90. Gzip
  91. // Xz is xz compression algorithm.
  92. Xz
  93. // Zstd is zstd compression algorithm.
  94. Zstd
  95. )
  96. const (
  97. // AUFSWhiteoutFormat is the default format for whiteouts
  98. AUFSWhiteoutFormat WhiteoutFormat = iota
  99. // OverlayWhiteoutFormat formats whiteout according to the overlay
  100. // standard.
  101. OverlayWhiteoutFormat
  102. )
  103. // IsArchivePath checks if the (possibly compressed) file at the given path
  104. // starts with a tar file header.
  105. func IsArchivePath(path string) bool {
  106. file, err := os.Open(path)
  107. if err != nil {
  108. return false
  109. }
  110. defer file.Close()
  111. rdr, err := DecompressStream(file)
  112. if err != nil {
  113. return false
  114. }
  115. defer rdr.Close()
  116. r := tar.NewReader(rdr)
  117. _, err = r.Next()
  118. return err == nil
  119. }
  120. const (
  121. zstdMagicSkippableStart = 0x184D2A50
  122. zstdMagicSkippableMask = 0xFFFFFFF0
  123. )
  124. var (
  125. bzip2Magic = []byte{0x42, 0x5A, 0x68}
  126. gzipMagic = []byte{0x1F, 0x8B, 0x08}
  127. xzMagic = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}
  128. zstdMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
  129. )
  130. type matcher = func([]byte) bool
  131. func magicNumberMatcher(m []byte) matcher {
  132. return func(source []byte) bool {
  133. return bytes.HasPrefix(source, m)
  134. }
  135. }
  136. // zstdMatcher detects zstd compression algorithm.
  137. // Zstandard compressed data is made of one or more frames.
  138. // There are two frame formats defined by Zstandard: Zstandard frames and Skippable frames.
  139. // See https://tools.ietf.org/id/draft-kucherawy-dispatch-zstd-00.html#rfc.section.2 for more details.
  140. func zstdMatcher() matcher {
  141. return func(source []byte) bool {
  142. if bytes.HasPrefix(source, zstdMagic) {
  143. // Zstandard frame
  144. return true
  145. }
  146. // skippable frame
  147. if len(source) < 8 {
  148. return false
  149. }
  150. // magic number from 0x184D2A50 to 0x184D2A5F.
  151. if binary.LittleEndian.Uint32(source[:4])&zstdMagicSkippableMask == zstdMagicSkippableStart {
  152. return true
  153. }
  154. return false
  155. }
  156. }
  157. // DetectCompression detects the compression algorithm of the source.
  158. func DetectCompression(source []byte) Compression {
  159. compressionMap := map[Compression]matcher{
  160. Bzip2: magicNumberMatcher(bzip2Magic),
  161. Gzip: magicNumberMatcher(gzipMagic),
  162. Xz: magicNumberMatcher(xzMagic),
  163. Zstd: zstdMatcher(),
  164. }
  165. for _, compression := range []Compression{Bzip2, Gzip, Xz, Zstd} {
  166. fn := compressionMap[compression]
  167. if fn(source) {
  168. return compression
  169. }
  170. }
  171. return Uncompressed
  172. }
  173. func xzDecompress(ctx context.Context, archive io.Reader) (io.ReadCloser, error) {
  174. args := []string{"xz", "-d", "-c", "-q"}
  175. return cmdStream(exec.CommandContext(ctx, args[0], args[1:]...), archive)
  176. }
  177. func gzDecompress(ctx context.Context, buf io.Reader) (io.ReadCloser, error) {
  178. if noPigzEnv := os.Getenv("MOBY_DISABLE_PIGZ"); noPigzEnv != "" {
  179. noPigz, err := strconv.ParseBool(noPigzEnv)
  180. if err != nil {
  181. logrus.WithError(err).Warn("invalid value in MOBY_DISABLE_PIGZ env var")
  182. }
  183. if noPigz {
  184. logrus.Debugf("Use of pigz is disabled due to MOBY_DISABLE_PIGZ=%s", noPigzEnv)
  185. return gzip.NewReader(buf)
  186. }
  187. }
  188. unpigzPath, err := exec.LookPath("unpigz")
  189. if err != nil {
  190. logrus.Debugf("unpigz binary not found, falling back to go gzip library")
  191. return gzip.NewReader(buf)
  192. }
  193. logrus.Debugf("Using %s to decompress", unpigzPath)
  194. return cmdStream(exec.CommandContext(ctx, unpigzPath, "-d", "-c"), buf)
  195. }
  196. func wrapReadCloser(readBuf io.ReadCloser, cancel context.CancelFunc) io.ReadCloser {
  197. return ioutils.NewReadCloserWrapper(readBuf, func() error {
  198. cancel()
  199. return readBuf.Close()
  200. })
  201. }
  202. // DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.
  203. func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
  204. p := pools.BufioReader32KPool
  205. buf := p.Get(archive)
  206. bs, err := buf.Peek(10)
  207. if err != nil && err != io.EOF {
  208. // Note: we'll ignore any io.EOF error because there are some odd
  209. // cases where the layer.tar file will be empty (zero bytes) and
  210. // that results in an io.EOF from the Peek() call. So, in those
  211. // cases we'll just treat it as a non-compressed stream and
  212. // that means just create an empty layer.
  213. // See Issue 18170
  214. return nil, err
  215. }
  216. compression := DetectCompression(bs)
  217. switch compression {
  218. case Uncompressed:
  219. readBufWrapper := p.NewReadCloserWrapper(buf, buf)
  220. return readBufWrapper, nil
  221. case Gzip:
  222. ctx, cancel := context.WithCancel(context.Background())
  223. gzReader, err := gzDecompress(ctx, buf)
  224. if err != nil {
  225. cancel()
  226. return nil, err
  227. }
  228. readBufWrapper := p.NewReadCloserWrapper(buf, gzReader)
  229. return wrapReadCloser(readBufWrapper, cancel), nil
  230. case Bzip2:
  231. bz2Reader := bzip2.NewReader(buf)
  232. readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader)
  233. return readBufWrapper, nil
  234. case Xz:
  235. ctx, cancel := context.WithCancel(context.Background())
  236. xzReader, err := xzDecompress(ctx, buf)
  237. if err != nil {
  238. cancel()
  239. return nil, err
  240. }
  241. readBufWrapper := p.NewReadCloserWrapper(buf, xzReader)
  242. return wrapReadCloser(readBufWrapper, cancel), nil
  243. case Zstd:
  244. zstdReader, err := zstd.NewReader(buf)
  245. if err != nil {
  246. return nil, err
  247. }
  248. readBufWrapper := p.NewReadCloserWrapper(buf, zstdReader)
  249. return readBufWrapper, nil
  250. default:
  251. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  252. }
  253. }
  254. // CompressStream compresses the dest with specified compression algorithm.
  255. func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error) {
  256. p := pools.BufioWriter32KPool
  257. buf := p.Get(dest)
  258. switch compression {
  259. case Uncompressed:
  260. writeBufWrapper := p.NewWriteCloserWrapper(buf, buf)
  261. return writeBufWrapper, nil
  262. case Gzip:
  263. gzWriter := gzip.NewWriter(dest)
  264. writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter)
  265. return writeBufWrapper, nil
  266. case Bzip2, Xz:
  267. // archive/bzip2 does not support writing, and there is no xz support at all
  268. // However, this is not a problem as docker only currently generates gzipped tars
  269. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  270. default:
  271. return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
  272. }
  273. }
  274. // TarModifierFunc is a function that can be passed to ReplaceFileTarWrapper to
  275. // modify the contents or header of an entry in the archive. If the file already
  276. // exists in the archive the TarModifierFunc will be called with the Header and
  277. // a reader which will return the files content. If the file does not exist both
  278. // header and content will be nil.
  279. type TarModifierFunc func(path string, header *tar.Header, content io.Reader) (*tar.Header, []byte, error)
  280. // ReplaceFileTarWrapper converts inputTarStream to a new tar stream. Files in the
  281. // tar stream are modified if they match any of the keys in mods.
  282. func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModifierFunc) io.ReadCloser {
  283. pipeReader, pipeWriter := io.Pipe()
  284. go func() {
  285. tarReader := tar.NewReader(inputTarStream)
  286. tarWriter := tar.NewWriter(pipeWriter)
  287. defer inputTarStream.Close()
  288. defer tarWriter.Close()
  289. modify := func(name string, original *tar.Header, modifier TarModifierFunc, tarReader io.Reader) error {
  290. header, data, err := modifier(name, original, tarReader)
  291. switch {
  292. case err != nil:
  293. return err
  294. case header == nil:
  295. return nil
  296. }
  297. if header.Name == "" {
  298. header.Name = name
  299. }
  300. header.Size = int64(len(data))
  301. if err := tarWriter.WriteHeader(header); err != nil {
  302. return err
  303. }
  304. if len(data) != 0 {
  305. if _, err := tarWriter.Write(data); err != nil {
  306. return err
  307. }
  308. }
  309. return nil
  310. }
  311. var err error
  312. var originalHeader *tar.Header
  313. for {
  314. originalHeader, err = tarReader.Next()
  315. if err == io.EOF {
  316. break
  317. }
  318. if err != nil {
  319. pipeWriter.CloseWithError(err)
  320. return
  321. }
  322. modifier, ok := mods[originalHeader.Name]
  323. if !ok {
  324. // No modifiers for this file, copy the header and data
  325. if err := tarWriter.WriteHeader(originalHeader); err != nil {
  326. pipeWriter.CloseWithError(err)
  327. return
  328. }
  329. if _, err := pools.Copy(tarWriter, tarReader); err != nil {
  330. pipeWriter.CloseWithError(err)
  331. return
  332. }
  333. continue
  334. }
  335. delete(mods, originalHeader.Name)
  336. if err := modify(originalHeader.Name, originalHeader, modifier, tarReader); err != nil {
  337. pipeWriter.CloseWithError(err)
  338. return
  339. }
  340. }
  341. // Apply the modifiers that haven't matched any files in the archive
  342. for name, modifier := range mods {
  343. if err := modify(name, nil, modifier, nil); err != nil {
  344. pipeWriter.CloseWithError(err)
  345. return
  346. }
  347. }
  348. pipeWriter.Close()
  349. }()
  350. return pipeReader
  351. }
  352. // Extension returns the extension of a file that uses the specified compression algorithm.
  353. func (compression *Compression) Extension() string {
  354. switch *compression {
  355. case Uncompressed:
  356. return "tar"
  357. case Bzip2:
  358. return "tar.bz2"
  359. case Gzip:
  360. return "tar.gz"
  361. case Xz:
  362. return "tar.xz"
  363. case Zstd:
  364. return "tar.zst"
  365. }
  366. return ""
  367. }
  368. // nosysFileInfo hides the system-dependent info of the wrapped FileInfo to
  369. // prevent tar.FileInfoHeader from introspecting it and potentially calling into
  370. // glibc.
  371. type nosysFileInfo struct {
  372. os.FileInfo
  373. }
  374. func (fi nosysFileInfo) Sys() interface{} {
  375. // A Sys value of type *tar.Header is safe as it is system-independent.
  376. // The tar.FileInfoHeader function copies the fields into the returned
  377. // header without performing any OS lookups.
  378. if sys, ok := fi.FileInfo.Sys().(*tar.Header); ok {
  379. return sys
  380. }
  381. return nil
  382. }
  383. // sysStat, if non-nil, populates hdr from system-dependent fields of fi.
  384. var sysStat func(fi os.FileInfo, hdr *tar.Header) error
  385. // FileInfoHeaderNoLookups creates a partially-populated tar.Header from fi.
  386. //
  387. // Compared to the archive/tar.FileInfoHeader function, this function is safe to
  388. // call from a chrooted process as it does not populate fields which would
  389. // require operating system lookups. It behaves identically to
  390. // tar.FileInfoHeader when fi is a FileInfo value returned from
  391. // tar.Header.FileInfo().
  392. //
  393. // When fi is a FileInfo for a native file, such as returned from os.Stat() and
  394. // os.Lstat(), the returned Header value differs from one returned from
  395. // tar.FileInfoHeader in the following ways. The Uname and Gname fields are not
  396. // set as OS lookups would be required to populate them. The AccessTime and
  397. // ChangeTime fields are not currently set (not yet implemented) although that
  398. // is subject to change. Callers which require the AccessTime or ChangeTime
  399. // fields to be zeroed should explicitly zero them out in the returned Header
  400. // value to avoid any compatibility issues in the future.
  401. func FileInfoHeaderNoLookups(fi os.FileInfo, link string) (*tar.Header, error) {
  402. hdr, err := tar.FileInfoHeader(nosysFileInfo{fi}, link)
  403. if err != nil {
  404. return nil, err
  405. }
  406. if sysStat != nil {
  407. return hdr, sysStat(fi, hdr)
  408. }
  409. return hdr, nil
  410. }
  411. // FileInfoHeader creates a populated Header from fi.
  412. //
  413. // Compared to the archive/tar package, this function fills in less information
  414. // but is safe to call from a chrooted process. The AccessTime and ChangeTime
  415. // fields are not set in the returned header, ModTime is truncated to one-second
  416. // precision, and the Uname and Gname fields are only set when fi is a FileInfo
  417. // value returned from tar.Header.FileInfo().
  418. func FileInfoHeader(name string, fi os.FileInfo, link string) (*tar.Header, error) {
  419. hdr, err := FileInfoHeaderNoLookups(fi, link)
  420. if err != nil {
  421. return nil, err
  422. }
  423. hdr.Format = tar.FormatPAX
  424. hdr.ModTime = hdr.ModTime.Truncate(time.Second)
  425. hdr.AccessTime = time.Time{}
  426. hdr.ChangeTime = time.Time{}
  427. hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
  428. hdr.Name = canonicalTarName(name, fi.IsDir())
  429. return hdr, nil
  430. }
  431. // ReadSecurityXattrToTarHeader reads security.capability xattr from filesystem
  432. // to a tar header
  433. func ReadSecurityXattrToTarHeader(path string, hdr *tar.Header) error {
  434. const (
  435. // Values based on linux/include/uapi/linux/capability.h
  436. xattrCapsSz2 = 20
  437. versionOffset = 3
  438. vfsCapRevision2 = 2
  439. vfsCapRevision3 = 3
  440. )
  441. capability, _ := system.Lgetxattr(path, "security.capability")
  442. if capability != nil {
  443. length := len(capability)
  444. if capability[versionOffset] == vfsCapRevision3 {
  445. // Convert VFS_CAP_REVISION_3 to VFS_CAP_REVISION_2 as root UID makes no
  446. // sense outside the user namespace the archive is built in.
  447. capability[versionOffset] = vfsCapRevision2
  448. length = xattrCapsSz2
  449. }
  450. hdr.Xattrs = make(map[string]string)
  451. hdr.Xattrs["security.capability"] = string(capability[:length])
  452. }
  453. return nil
  454. }
  455. type tarWhiteoutConverter interface {
  456. ConvertWrite(*tar.Header, string, os.FileInfo) (*tar.Header, error)
  457. ConvertRead(*tar.Header, string) (bool, error)
  458. }
  459. type tarAppender struct {
  460. TarWriter *tar.Writer
  461. Buffer *bufio.Writer
  462. // for hardlink mapping
  463. SeenFiles map[uint64]string
  464. IdentityMapping idtools.IdentityMapping
  465. ChownOpts *idtools.Identity
  466. // For packing and unpacking whiteout files in the
  467. // non standard format. The whiteout files defined
  468. // by the AUFS standard are used as the tar whiteout
  469. // standard.
  470. WhiteoutConverter tarWhiteoutConverter
  471. }
  472. func newTarAppender(idMapping idtools.IdentityMapping, writer io.Writer, chownOpts *idtools.Identity) *tarAppender {
  473. return &tarAppender{
  474. SeenFiles: make(map[uint64]string),
  475. TarWriter: tar.NewWriter(writer),
  476. Buffer: pools.BufioWriter32KPool.Get(nil),
  477. IdentityMapping: idMapping,
  478. ChownOpts: chownOpts,
  479. }
  480. }
  481. // CanonicalTarNameForPath canonicalizes relativePath to a POSIX-style path using
  482. // forward slashes. It is an alias for filepath.ToSlash, which is a no-op on
  483. // Linux and Unix.
  484. func CanonicalTarNameForPath(relativePath string) string {
  485. return filepath.ToSlash(relativePath)
  486. }
  487. // canonicalTarName provides a platform-independent and consistent POSIX-style
  488. // path for files and directories to be archived regardless of the platform.
  489. func canonicalTarName(name string, isDir bool) string {
  490. name = filepath.ToSlash(name)
  491. // suffix with '/' for directories
  492. if isDir && !strings.HasSuffix(name, "/") {
  493. name += "/"
  494. }
  495. return name
  496. }
  497. // addTarFile adds to the tar archive a file from `path` as `name`
  498. func (ta *tarAppender) addTarFile(path, name string) error {
  499. fi, err := os.Lstat(path)
  500. if err != nil {
  501. return err
  502. }
  503. var link string
  504. if fi.Mode()&os.ModeSymlink != 0 {
  505. var err error
  506. link, err = os.Readlink(path)
  507. if err != nil {
  508. return err
  509. }
  510. }
  511. hdr, err := FileInfoHeader(name, fi, link)
  512. if err != nil {
  513. return err
  514. }
  515. if err := ReadSecurityXattrToTarHeader(path, hdr); err != nil {
  516. return err
  517. }
  518. // if it's not a directory and has more than 1 link,
  519. // it's hard linked, so set the type flag accordingly
  520. if !fi.IsDir() && hasHardlinks(fi) {
  521. inode, err := getInodeFromStat(fi.Sys())
  522. if err != nil {
  523. return err
  524. }
  525. // a link should have a name that it links too
  526. // and that linked name should be first in the tar archive
  527. if oldpath, ok := ta.SeenFiles[inode]; ok {
  528. hdr.Typeflag = tar.TypeLink
  529. hdr.Linkname = oldpath
  530. hdr.Size = 0 // This Must be here for the writer math to add up!
  531. } else {
  532. ta.SeenFiles[inode] = name
  533. }
  534. }
  535. // check whether the file is overlayfs whiteout
  536. // if yes, skip re-mapping container ID mappings.
  537. isOverlayWhiteout := fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0
  538. // handle re-mapping container ID mappings back to host ID mappings before
  539. // writing tar headers/files. We skip whiteout files because they were written
  540. // by the kernel and already have proper ownership relative to the host
  541. if !isOverlayWhiteout && !strings.HasPrefix(filepath.Base(hdr.Name), WhiteoutPrefix) && !ta.IdentityMapping.Empty() {
  542. fileIDPair, err := getFileUIDGID(fi.Sys())
  543. if err != nil {
  544. return err
  545. }
  546. hdr.Uid, hdr.Gid, err = ta.IdentityMapping.ToContainer(fileIDPair)
  547. if err != nil {
  548. return err
  549. }
  550. }
  551. // explicitly override with ChownOpts
  552. if ta.ChownOpts != nil {
  553. hdr.Uid = ta.ChownOpts.UID
  554. hdr.Gid = ta.ChownOpts.GID
  555. }
  556. if ta.WhiteoutConverter != nil {
  557. wo, err := ta.WhiteoutConverter.ConvertWrite(hdr, path, fi)
  558. if err != nil {
  559. return err
  560. }
  561. // If a new whiteout file exists, write original hdr, then
  562. // replace hdr with wo to be written after. Whiteouts should
  563. // always be written after the original. Note the original
  564. // hdr may have been updated to be a whiteout with returning
  565. // a whiteout header
  566. if wo != nil {
  567. if err := ta.TarWriter.WriteHeader(hdr); err != nil {
  568. return err
  569. }
  570. if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
  571. return fmt.Errorf("tar: cannot use whiteout for non-empty file")
  572. }
  573. hdr = wo
  574. }
  575. }
  576. if err := ta.TarWriter.WriteHeader(hdr); err != nil {
  577. return err
  578. }
  579. if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
  580. // We use sequential file access to avoid depleting the standby list on
  581. // Windows. On Linux, this equates to a regular os.Open.
  582. file, err := sequential.Open(path)
  583. if err != nil {
  584. return err
  585. }
  586. ta.Buffer.Reset(ta.TarWriter)
  587. defer ta.Buffer.Reset(nil)
  588. _, err = io.Copy(ta.Buffer, file)
  589. file.Close()
  590. if err != nil {
  591. return err
  592. }
  593. err = ta.Buffer.Flush()
  594. if err != nil {
  595. return err
  596. }
  597. }
  598. return nil
  599. }
  600. func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool, chownOpts *idtools.Identity, inUserns bool) error {
  601. // hdr.Mode is in linux format, which we can use for sycalls,
  602. // but for os.Foo() calls we need the mode converted to os.FileMode,
  603. // so use hdrInfo.Mode() (they differ for e.g. setuid bits)
  604. hdrInfo := hdr.FileInfo()
  605. switch hdr.Typeflag {
  606. case tar.TypeDir:
  607. // Create directory unless it exists as a directory already.
  608. // In that case we just want to merge the two
  609. if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
  610. if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
  611. return err
  612. }
  613. }
  614. case tar.TypeReg, tar.TypeRegA:
  615. // Source is regular file. We use sequential file access to avoid depleting
  616. // the standby list on Windows. On Linux, this equates to a regular os.OpenFile.
  617. file, err := sequential.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
  618. if err != nil {
  619. return err
  620. }
  621. if _, err := io.Copy(file, reader); err != nil {
  622. file.Close()
  623. return err
  624. }
  625. file.Close()
  626. case tar.TypeBlock, tar.TypeChar:
  627. if inUserns { // cannot create devices in a userns
  628. return nil
  629. }
  630. // Handle this is an OS-specific way
  631. if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
  632. return err
  633. }
  634. case tar.TypeFifo:
  635. // Handle this is an OS-specific way
  636. if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
  637. return err
  638. }
  639. case tar.TypeLink:
  640. // #nosec G305 -- The target path is checked for path traversal.
  641. targetPath := filepath.Join(extractDir, hdr.Linkname)
  642. // check for hardlink breakout
  643. if !strings.HasPrefix(targetPath, extractDir) {
  644. return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname))
  645. }
  646. if err := os.Link(targetPath, path); err != nil {
  647. return err
  648. }
  649. case tar.TypeSymlink:
  650. // path -> hdr.Linkname = targetPath
  651. // e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file
  652. targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname) // #nosec G305 -- The target path is checked for path traversal.
  653. // the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because
  654. // that symlink would first have to be created, which would be caught earlier, at this very check:
  655. if !strings.HasPrefix(targetPath, extractDir) {
  656. return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname))
  657. }
  658. if err := os.Symlink(hdr.Linkname, path); err != nil {
  659. return err
  660. }
  661. case tar.TypeXGlobalHeader:
  662. logrus.Debug("PAX Global Extended Headers found and ignored")
  663. return nil
  664. default:
  665. return fmt.Errorf("unhandled tar header type %d", hdr.Typeflag)
  666. }
  667. // Lchown is not supported on Windows.
  668. if Lchown && runtime.GOOS != "windows" {
  669. if chownOpts == nil {
  670. chownOpts = &idtools.Identity{UID: hdr.Uid, GID: hdr.Gid}
  671. }
  672. if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil {
  673. msg := "failed to Lchown %q for UID %d, GID %d"
  674. if errors.Is(err, syscall.EINVAL) && userns.RunningInUserNS() {
  675. msg += " (try increasing the number of subordinate IDs in /etc/subuid and /etc/subgid)"
  676. }
  677. return errors.Wrapf(err, msg, path, hdr.Uid, hdr.Gid)
  678. }
  679. }
  680. var errors []string
  681. for key, value := range hdr.Xattrs {
  682. if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil {
  683. if err == syscall.ENOTSUP || err == syscall.EPERM {
  684. // We ignore errors here because not all graphdrivers support
  685. // xattrs *cough* old versions of AUFS *cough*. However only
  686. // ENOTSUP should be emitted in that case, otherwise we still
  687. // bail.
  688. // EPERM occurs if modifying xattrs is not allowed. This can
  689. // happen when running in userns with restrictions (ChromeOS).
  690. errors = append(errors, err.Error())
  691. continue
  692. }
  693. return err
  694. }
  695. }
  696. if len(errors) > 0 {
  697. logrus.WithFields(logrus.Fields{
  698. "errors": errors,
  699. }).Warn("ignored xattrs in archive: underlying filesystem doesn't support them")
  700. }
  701. // There is no LChmod, so ignore mode for symlink. Also, this
  702. // must happen after chown, as that can modify the file mode
  703. if err := handleLChmod(hdr, path, hdrInfo); err != nil {
  704. return err
  705. }
  706. aTime := hdr.AccessTime
  707. if aTime.Before(hdr.ModTime) {
  708. // Last access time should never be before last modified time.
  709. aTime = hdr.ModTime
  710. }
  711. // system.Chtimes doesn't support a NOFOLLOW flag atm
  712. if hdr.Typeflag == tar.TypeLink {
  713. if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
  714. if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
  715. return err
  716. }
  717. }
  718. } else if hdr.Typeflag != tar.TypeSymlink {
  719. if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
  720. return err
  721. }
  722. } else {
  723. ts := []syscall.Timespec{timeToTimespec(aTime), timeToTimespec(hdr.ModTime)}
  724. if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
  725. return err
  726. }
  727. }
  728. return nil
  729. }
  730. // Tar creates an archive from the directory at `path`, and returns it as a
  731. // stream of bytes.
  732. func Tar(path string, compression Compression) (io.ReadCloser, error) {
  733. return TarWithOptions(path, &TarOptions{Compression: compression})
  734. }
  735. // TarWithOptions creates an archive from the directory at `path`, only including files whose relative
  736. // paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
  737. func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
  738. // Fix the source path to work with long path names. This is a no-op
  739. // on platforms other than Windows.
  740. srcPath = fixVolumePathPrefix(srcPath)
  741. pm, err := patternmatcher.New(options.ExcludePatterns)
  742. if err != nil {
  743. return nil, err
  744. }
  745. pipeReader, pipeWriter := io.Pipe()
  746. compressWriter, err := CompressStream(pipeWriter, options.Compression)
  747. if err != nil {
  748. return nil, err
  749. }
  750. whiteoutConverter, err := getWhiteoutConverter(options.WhiteoutFormat, options.InUserNS)
  751. if err != nil {
  752. return nil, err
  753. }
  754. go func() {
  755. ta := newTarAppender(
  756. options.IDMap,
  757. compressWriter,
  758. options.ChownOpts,
  759. )
  760. ta.WhiteoutConverter = whiteoutConverter
  761. defer func() {
  762. // Make sure to check the error on Close.
  763. if err := ta.TarWriter.Close(); err != nil {
  764. logrus.Errorf("Can't close tar writer: %s", err)
  765. }
  766. if err := compressWriter.Close(); err != nil {
  767. logrus.Errorf("Can't close compress writer: %s", err)
  768. }
  769. if err := pipeWriter.Close(); err != nil {
  770. logrus.Errorf("Can't close pipe writer: %s", err)
  771. }
  772. }()
  773. // this buffer is needed for the duration of this piped stream
  774. defer pools.BufioWriter32KPool.Put(ta.Buffer)
  775. // In general we log errors here but ignore them because
  776. // during e.g. a diff operation the container can continue
  777. // mutating the filesystem and we can see transient errors
  778. // from this
  779. stat, err := os.Lstat(srcPath)
  780. if err != nil {
  781. return
  782. }
  783. if !stat.IsDir() {
  784. // We can't later join a non-dir with any includes because the
  785. // 'walk' will error if "file/." is stat-ed and "file" is not a
  786. // directory. So, we must split the source path and use the
  787. // basename as the include.
  788. if len(options.IncludeFiles) > 0 {
  789. logrus.Warn("Tar: Can't archive a file with includes")
  790. }
  791. dir, base := SplitPathDirEntry(srcPath)
  792. srcPath = dir
  793. options.IncludeFiles = []string{base}
  794. }
  795. if len(options.IncludeFiles) == 0 {
  796. options.IncludeFiles = []string{"."}
  797. }
  798. seen := make(map[string]bool)
  799. for _, include := range options.IncludeFiles {
  800. rebaseName := options.RebaseNames[include]
  801. var (
  802. parentMatchInfo []patternmatcher.MatchInfo
  803. parentDirs []string
  804. )
  805. walkRoot := getWalkRoot(srcPath, include)
  806. filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error {
  807. if err != nil {
  808. logrus.Errorf("Tar: Can't stat file %s to tar: %s", srcPath, err)
  809. return nil
  810. }
  811. relFilePath, err := filepath.Rel(srcPath, filePath)
  812. if err != nil || (!options.IncludeSourceDir && relFilePath == "." && f.IsDir()) {
  813. // Error getting relative path OR we are looking
  814. // at the source directory path. Skip in both situations.
  815. return nil
  816. }
  817. if options.IncludeSourceDir && include == "." && relFilePath != "." {
  818. relFilePath = strings.Join([]string{".", relFilePath}, string(filepath.Separator))
  819. }
  820. skip := false
  821. // If "include" is an exact match for the current file
  822. // then even if there's an "excludePatterns" pattern that
  823. // matches it, don't skip it. IOW, assume an explicit 'include'
  824. // is asking for that file no matter what - which is true
  825. // for some files, like .dockerignore and Dockerfile (sometimes)
  826. if include != relFilePath {
  827. for len(parentDirs) != 0 {
  828. lastParentDir := parentDirs[len(parentDirs)-1]
  829. if strings.HasPrefix(relFilePath, lastParentDir+string(os.PathSeparator)) {
  830. break
  831. }
  832. parentDirs = parentDirs[:len(parentDirs)-1]
  833. parentMatchInfo = parentMatchInfo[:len(parentMatchInfo)-1]
  834. }
  835. var matchInfo patternmatcher.MatchInfo
  836. if len(parentMatchInfo) != 0 {
  837. skip, matchInfo, err = pm.MatchesUsingParentResults(relFilePath, parentMatchInfo[len(parentMatchInfo)-1])
  838. } else {
  839. skip, matchInfo, err = pm.MatchesUsingParentResults(relFilePath, patternmatcher.MatchInfo{})
  840. }
  841. if err != nil {
  842. logrus.Errorf("Error matching %s: %v", relFilePath, err)
  843. return err
  844. }
  845. if f.IsDir() {
  846. parentDirs = append(parentDirs, relFilePath)
  847. parentMatchInfo = append(parentMatchInfo, matchInfo)
  848. }
  849. }
  850. if skip {
  851. // If we want to skip this file and its a directory
  852. // then we should first check to see if there's an
  853. // excludes pattern (e.g. !dir/file) that starts with this
  854. // dir. If so then we can't skip this dir.
  855. // Its not a dir then so we can just return/skip.
  856. if !f.IsDir() {
  857. return nil
  858. }
  859. // No exceptions (!...) in patterns so just skip dir
  860. if !pm.Exclusions() {
  861. return filepath.SkipDir
  862. }
  863. dirSlash := relFilePath + string(filepath.Separator)
  864. for _, pat := range pm.Patterns() {
  865. if !pat.Exclusion() {
  866. continue
  867. }
  868. if strings.HasPrefix(pat.String()+string(filepath.Separator), dirSlash) {
  869. // found a match - so can't skip this dir
  870. return nil
  871. }
  872. }
  873. // No matching exclusion dir so just skip dir
  874. return filepath.SkipDir
  875. }
  876. if seen[relFilePath] {
  877. return nil
  878. }
  879. seen[relFilePath] = true
  880. // Rename the base resource.
  881. if rebaseName != "" {
  882. var replacement string
  883. if rebaseName != string(filepath.Separator) {
  884. // Special case the root directory to replace with an
  885. // empty string instead so that we don't end up with
  886. // double slashes in the paths.
  887. replacement = rebaseName
  888. }
  889. relFilePath = strings.Replace(relFilePath, include, replacement, 1)
  890. }
  891. if err := ta.addTarFile(filePath, relFilePath); err != nil {
  892. logrus.Errorf("Can't add file %s to tar: %s", filePath, err)
  893. // if pipe is broken, stop writing tar stream to it
  894. if err == io.ErrClosedPipe {
  895. return err
  896. }
  897. }
  898. return nil
  899. })
  900. }
  901. }()
  902. return pipeReader, nil
  903. }
  904. // Unpack unpacks the decompressedArchive to dest with options.
  905. func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error {
  906. tr := tar.NewReader(decompressedArchive)
  907. trBuf := pools.BufioReader32KPool.Get(nil)
  908. defer pools.BufioReader32KPool.Put(trBuf)
  909. var dirs []*tar.Header
  910. whiteoutConverter, err := getWhiteoutConverter(options.WhiteoutFormat, options.InUserNS)
  911. if err != nil {
  912. return err
  913. }
  914. // Iterate through the files in the archive.
  915. loop:
  916. for {
  917. hdr, err := tr.Next()
  918. if err == io.EOF {
  919. // end of tar archive
  920. break
  921. }
  922. if err != nil {
  923. return err
  924. }
  925. // ignore XGlobalHeader early to avoid creating parent directories for them
  926. if hdr.Typeflag == tar.TypeXGlobalHeader {
  927. logrus.Debugf("PAX Global Extended Headers found for %s and ignored", hdr.Name)
  928. continue
  929. }
  930. // Normalize name, for safety and for a simple is-root check
  931. // This keeps "../" as-is, but normalizes "/../" to "/". Or Windows:
  932. // This keeps "..\" as-is, but normalizes "\..\" to "\".
  933. hdr.Name = filepath.Clean(hdr.Name)
  934. for _, exclude := range options.ExcludePatterns {
  935. if strings.HasPrefix(hdr.Name, exclude) {
  936. continue loop
  937. }
  938. }
  939. // Ensure that the parent directory exists.
  940. err = createImpliedDirectories(dest, hdr, options)
  941. if err != nil {
  942. return err
  943. }
  944. // #nosec G305 -- The joined path is checked for path traversal.
  945. path := filepath.Join(dest, hdr.Name)
  946. rel, err := filepath.Rel(dest, path)
  947. if err != nil {
  948. return err
  949. }
  950. if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
  951. return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
  952. }
  953. // If path exits we almost always just want to remove and replace it
  954. // The only exception is when it is a directory *and* the file from
  955. // the layer is also a directory. Then we want to merge them (i.e.
  956. // just apply the metadata from the layer).
  957. if fi, err := os.Lstat(path); err == nil {
  958. if options.NoOverwriteDirNonDir && fi.IsDir() && hdr.Typeflag != tar.TypeDir {
  959. // If NoOverwriteDirNonDir is true then we cannot replace
  960. // an existing directory with a non-directory from the archive.
  961. return fmt.Errorf("cannot overwrite directory %q with non-directory %q", path, dest)
  962. }
  963. if options.NoOverwriteDirNonDir && !fi.IsDir() && hdr.Typeflag == tar.TypeDir {
  964. // If NoOverwriteDirNonDir is true then we cannot replace
  965. // an existing non-directory with a directory from the archive.
  966. return fmt.Errorf("cannot overwrite non-directory %q with directory %q", path, dest)
  967. }
  968. if fi.IsDir() && hdr.Name == "." {
  969. continue
  970. }
  971. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  972. if err := os.RemoveAll(path); err != nil {
  973. return err
  974. }
  975. }
  976. }
  977. trBuf.Reset(tr)
  978. if err := remapIDs(options.IDMap, hdr); err != nil {
  979. return err
  980. }
  981. if whiteoutConverter != nil {
  982. writeFile, err := whiteoutConverter.ConvertRead(hdr, path)
  983. if err != nil {
  984. return err
  985. }
  986. if !writeFile {
  987. continue
  988. }
  989. }
  990. if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown, options.ChownOpts, options.InUserNS); err != nil {
  991. return err
  992. }
  993. // Directory mtimes must be handled at the end to avoid further
  994. // file creation in them to modify the directory mtime
  995. if hdr.Typeflag == tar.TypeDir {
  996. dirs = append(dirs, hdr)
  997. }
  998. }
  999. for _, hdr := range dirs {
  1000. // #nosec G305 -- The header was checked for path traversal before it was appended to the dirs slice.
  1001. path := filepath.Join(dest, hdr.Name)
  1002. if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
  1003. return err
  1004. }
  1005. }
  1006. return nil
  1007. }
  1008. // createImpliedDirectories will create all parent directories of the current path with default permissions, if they do
  1009. // not already exist. This is possible as the tar format supports 'implicit' directories, where their existence is
  1010. // defined by the paths of files in the tar, but there are no header entries for the directories themselves, and thus
  1011. // we most both create them and choose metadata like permissions.
  1012. //
  1013. // The caller should have performed filepath.Clean(hdr.Name), so hdr.Name will now be in the filepath format for the OS
  1014. // on which the daemon is running. This precondition is required because this function assumes a OS-specific path
  1015. // separator when checking that a path is not the root.
  1016. func createImpliedDirectories(dest string, hdr *tar.Header, options *TarOptions) error {
  1017. // Not the root directory, ensure that the parent directory exists
  1018. if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
  1019. parent := filepath.Dir(hdr.Name)
  1020. parentPath := filepath.Join(dest, parent)
  1021. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  1022. // RootPair() is confined inside this loop as most cases will not require a call, so we can spend some
  1023. // unneeded function calls in the uncommon case to encapsulate logic -- implied directories are a niche
  1024. // usage that reduces the portability of an image.
  1025. rootIDs := options.IDMap.RootPair()
  1026. err = idtools.MkdirAllAndChownNew(parentPath, ImpliedDirectoryMode, rootIDs)
  1027. if err != nil {
  1028. return err
  1029. }
  1030. }
  1031. }
  1032. return nil
  1033. }
  1034. // Untar reads a stream of bytes from `archive`, parses it as a tar archive,
  1035. // and unpacks it into the directory at `dest`.
  1036. // The archive may be compressed with one of the following algorithms:
  1037. // identity (uncompressed), gzip, bzip2, xz.
  1038. //
  1039. // FIXME: specify behavior when target path exists vs. doesn't exist.
  1040. func Untar(tarArchive io.Reader, dest string, options *TarOptions) error {
  1041. return untarHandler(tarArchive, dest, options, true)
  1042. }
  1043. // UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
  1044. // and unpacks it into the directory at `dest`.
  1045. // The archive must be an uncompressed stream.
  1046. func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error {
  1047. return untarHandler(tarArchive, dest, options, false)
  1048. }
  1049. // Handler for teasing out the automatic decompression
  1050. func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error {
  1051. if tarArchive == nil {
  1052. return fmt.Errorf("Empty archive")
  1053. }
  1054. dest = filepath.Clean(dest)
  1055. if options == nil {
  1056. options = &TarOptions{}
  1057. }
  1058. if options.ExcludePatterns == nil {
  1059. options.ExcludePatterns = []string{}
  1060. }
  1061. r := tarArchive
  1062. if decompress {
  1063. decompressedArchive, err := DecompressStream(tarArchive)
  1064. if err != nil {
  1065. return err
  1066. }
  1067. defer decompressedArchive.Close()
  1068. r = decompressedArchive
  1069. }
  1070. return Unpack(r, dest, options)
  1071. }
  1072. // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
  1073. // If either Tar or Untar fails, TarUntar aborts and returns the error.
  1074. func (archiver *Archiver) TarUntar(src, dst string) error {
  1075. archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed})
  1076. if err != nil {
  1077. return err
  1078. }
  1079. defer archive.Close()
  1080. options := &TarOptions{
  1081. IDMap: archiver.IDMapping,
  1082. }
  1083. return archiver.Untar(archive, dst, options)
  1084. }
  1085. // UntarPath untar a file from path to a destination, src is the source tar file path.
  1086. func (archiver *Archiver) UntarPath(src, dst string) error {
  1087. archive, err := os.Open(src)
  1088. if err != nil {
  1089. return err
  1090. }
  1091. defer archive.Close()
  1092. options := &TarOptions{
  1093. IDMap: archiver.IDMapping,
  1094. }
  1095. return archiver.Untar(archive, dst, options)
  1096. }
  1097. // CopyWithTar creates a tar archive of filesystem path `src`, and
  1098. // unpacks it at filesystem path `dst`.
  1099. // The archive is streamed directly with fixed buffering and no
  1100. // intermediary disk IO.
  1101. func (archiver *Archiver) CopyWithTar(src, dst string) error {
  1102. srcSt, err := os.Stat(src)
  1103. if err != nil {
  1104. return err
  1105. }
  1106. if !srcSt.IsDir() {
  1107. return archiver.CopyFileWithTar(src, dst)
  1108. }
  1109. // if this Archiver is set up with ID mapping we need to create
  1110. // the new destination directory with the remapped root UID/GID pair
  1111. // as owner
  1112. rootIDs := archiver.IDMapping.RootPair()
  1113. // Create dst, copy src's content into it
  1114. if err := idtools.MkdirAllAndChownNew(dst, 0755, rootIDs); err != nil {
  1115. return err
  1116. }
  1117. return archiver.TarUntar(src, dst)
  1118. }
  1119. // CopyFileWithTar emulates the behavior of the 'cp' command-line
  1120. // for a single file. It copies a regular file from path `src` to
  1121. // path `dst`, and preserves all its metadata.
  1122. func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
  1123. srcSt, err := os.Stat(src)
  1124. if err != nil {
  1125. return err
  1126. }
  1127. if srcSt.IsDir() {
  1128. return fmt.Errorf("Can't copy a directory")
  1129. }
  1130. // Clean up the trailing slash. This must be done in an operating
  1131. // system specific manner.
  1132. if dst[len(dst)-1] == os.PathSeparator {
  1133. dst = filepath.Join(dst, filepath.Base(src))
  1134. }
  1135. // Create the holding directory if necessary
  1136. if err := system.MkdirAll(filepath.Dir(dst), 0700); err != nil {
  1137. return err
  1138. }
  1139. r, w := io.Pipe()
  1140. errC := make(chan error, 1)
  1141. go func() {
  1142. defer close(errC)
  1143. errC <- func() error {
  1144. defer w.Close()
  1145. srcF, err := os.Open(src)
  1146. if err != nil {
  1147. return err
  1148. }
  1149. defer srcF.Close()
  1150. hdr, err := FileInfoHeaderNoLookups(srcSt, "")
  1151. if err != nil {
  1152. return err
  1153. }
  1154. hdr.Format = tar.FormatPAX
  1155. hdr.ModTime = hdr.ModTime.Truncate(time.Second)
  1156. hdr.AccessTime = time.Time{}
  1157. hdr.ChangeTime = time.Time{}
  1158. hdr.Name = filepath.Base(dst)
  1159. hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
  1160. if err := remapIDs(archiver.IDMapping, hdr); err != nil {
  1161. return err
  1162. }
  1163. tw := tar.NewWriter(w)
  1164. defer tw.Close()
  1165. if err := tw.WriteHeader(hdr); err != nil {
  1166. return err
  1167. }
  1168. if _, err := io.Copy(tw, srcF); err != nil {
  1169. return err
  1170. }
  1171. return nil
  1172. }()
  1173. }()
  1174. defer func() {
  1175. if er := <-errC; err == nil && er != nil {
  1176. err = er
  1177. }
  1178. }()
  1179. err = archiver.Untar(r, filepath.Dir(dst), nil)
  1180. if err != nil {
  1181. r.CloseWithError(err)
  1182. }
  1183. return err
  1184. }
  1185. // IdentityMapping returns the IdentityMapping of the archiver.
  1186. func (archiver *Archiver) IdentityMapping() idtools.IdentityMapping {
  1187. return archiver.IDMapping
  1188. }
  1189. func remapIDs(idMapping idtools.IdentityMapping, hdr *tar.Header) error {
  1190. ids, err := idMapping.ToHost(idtools.Identity{UID: hdr.Uid, GID: hdr.Gid})
  1191. hdr.Uid, hdr.Gid = ids.UID, ids.GID
  1192. return err
  1193. }
  1194. // cmdStream executes a command, and returns its stdout as a stream.
  1195. // If the command fails to run or doesn't complete successfully, an error
  1196. // will be returned, including anything written on stderr.
  1197. func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) {
  1198. cmd.Stdin = input
  1199. pipeR, pipeW := io.Pipe()
  1200. cmd.Stdout = pipeW
  1201. var errBuf bytes.Buffer
  1202. cmd.Stderr = &errBuf
  1203. // Run the command and return the pipe
  1204. if err := cmd.Start(); err != nil {
  1205. return nil, err
  1206. }
  1207. // Ensure the command has exited before we clean anything up
  1208. done := make(chan struct{})
  1209. // Copy stdout to the returned pipe
  1210. go func() {
  1211. if err := cmd.Wait(); err != nil {
  1212. pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
  1213. } else {
  1214. pipeW.Close()
  1215. }
  1216. close(done)
  1217. }()
  1218. return ioutils.NewReadCloserWrapper(pipeR, func() error {
  1219. // Close pipeR, and then wait for the command to complete before returning. We have to close pipeR first, as
  1220. // cmd.Wait waits for any non-file stdout/stderr/stdin to close.
  1221. err := pipeR.Close()
  1222. <-done
  1223. return err
  1224. }), nil
  1225. }
  1226. // NewTempArchive reads the content of src into a temporary file, and returns the contents
  1227. // of that file as an archive. The archive can only be read once - as soon as reading completes,
  1228. // the file will be deleted.
  1229. func NewTempArchive(src io.Reader, dir string) (*TempArchive, error) {
  1230. f, err := os.CreateTemp(dir, "")
  1231. if err != nil {
  1232. return nil, err
  1233. }
  1234. if _, err := io.Copy(f, src); err != nil {
  1235. return nil, err
  1236. }
  1237. if _, err := f.Seek(0, 0); err != nil {
  1238. return nil, err
  1239. }
  1240. st, err := f.Stat()
  1241. if err != nil {
  1242. return nil, err
  1243. }
  1244. size := st.Size()
  1245. return &TempArchive{File: f, Size: size}, nil
  1246. }
  1247. // TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes,
  1248. // the file will be deleted.
  1249. type TempArchive struct {
  1250. *os.File
  1251. Size int64 // Pre-computed from Stat().Size() as a convenience
  1252. read int64
  1253. closed bool
  1254. }
  1255. // Close closes the underlying file if it's still open, or does a no-op
  1256. // to allow callers to try to close the TempArchive multiple times safely.
  1257. func (archive *TempArchive) Close() error {
  1258. if archive.closed {
  1259. return nil
  1260. }
  1261. archive.closed = true
  1262. return archive.File.Close()
  1263. }
  1264. func (archive *TempArchive) Read(data []byte) (int, error) {
  1265. n, err := archive.File.Read(data)
  1266. archive.read += int64(n)
  1267. if err != nil || archive.read == archive.Size {
  1268. archive.Close()
  1269. os.Remove(archive.File.Name())
  1270. }
  1271. return n, err
  1272. }