archive.go 45 KB

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