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