copy.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "fmt"
  4. "io"
  5. "mime"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "sort"
  11. "strings"
  12. "time"
  13. "github.com/docker/docker/builder"
  14. "github.com/docker/docker/builder/remotecontext"
  15. "github.com/docker/docker/builder/remotecontext/urlutil"
  16. "github.com/docker/docker/pkg/archive"
  17. "github.com/docker/docker/pkg/idtools"
  18. "github.com/docker/docker/pkg/longpath"
  19. "github.com/docker/docker/pkg/progress"
  20. "github.com/docker/docker/pkg/streamformatter"
  21. "github.com/docker/docker/pkg/system"
  22. "github.com/moby/buildkit/frontend/dockerfile/instructions"
  23. "github.com/moby/sys/symlink"
  24. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  25. "github.com/pkg/errors"
  26. )
  27. const unnamedFilename = "__unnamed__"
  28. type pathCache interface {
  29. Load(key interface{}) (value interface{}, ok bool)
  30. Store(key, value interface{})
  31. }
  32. // copyInfo is a data object which stores the metadata about each source file in
  33. // a copyInstruction
  34. type copyInfo struct {
  35. root string
  36. path string
  37. hash string
  38. noDecompress bool
  39. }
  40. func (c copyInfo) fullPath() (string, error) {
  41. return symlink.FollowSymlinkInScope(filepath.Join(c.root, c.path), c.root)
  42. }
  43. func newCopyInfoFromSource(source builder.Source, path string, hash string) copyInfo {
  44. return copyInfo{root: source.Root(), path: path, hash: hash}
  45. }
  46. func newCopyInfos(copyInfos ...copyInfo) []copyInfo {
  47. return copyInfos
  48. }
  49. // copyInstruction is a fully parsed COPY or ADD command that is passed to
  50. // Builder.performCopy to copy files into the image filesystem
  51. type copyInstruction struct {
  52. cmdName string
  53. infos []copyInfo
  54. dest string
  55. chownStr string
  56. allowLocalDecompression bool
  57. preserveOwnership bool
  58. }
  59. // copier reads a raw COPY or ADD command, fetches remote sources using a downloader,
  60. // and creates a copyInstruction
  61. type copier struct {
  62. imageSource *imageMount
  63. source builder.Source
  64. pathCache pathCache
  65. download sourceDownloader
  66. platform ocispec.Platform
  67. // for cleanup. TODO: having copier.cleanup() is error prone and hard to
  68. // follow. Code calling performCopy should manage the lifecycle of its params.
  69. // Copier should take override source as input, not imageMount.
  70. activeLayer builder.RWLayer
  71. tmpPaths []string
  72. }
  73. func copierFromDispatchRequest(req dispatchRequest, download sourceDownloader, imageSource *imageMount) copier {
  74. platform := req.builder.getPlatform(req.state)
  75. return copier{
  76. source: req.source,
  77. pathCache: req.builder.pathCache,
  78. download: download,
  79. imageSource: imageSource,
  80. platform: platform,
  81. }
  82. }
  83. func (o *copier) createCopyInstruction(sourcesAndDest instructions.SourcesAndDest, cmdName string) (copyInstruction, error) {
  84. inst := copyInstruction{
  85. cmdName: cmdName,
  86. dest: filepath.FromSlash(sourcesAndDest.DestPath),
  87. }
  88. infos, err := o.getCopyInfosForSourcePaths(sourcesAndDest.SourcePaths, inst.dest)
  89. if err != nil {
  90. return inst, errors.Wrapf(err, "%s failed", cmdName)
  91. }
  92. if len(infos) > 1 && !strings.HasSuffix(inst.dest, string(os.PathSeparator)) {
  93. return inst, errors.Errorf("When using %s with more than one source file, the destination must be a directory and end with a /", cmdName)
  94. }
  95. inst.infos = infos
  96. return inst, nil
  97. }
  98. // getCopyInfosForSourcePaths iterates over the source files and calculate the info
  99. // needed to copy (e.g. hash value if cached)
  100. // The dest is used in case source is URL (and ends with "/")
  101. func (o *copier) getCopyInfosForSourcePaths(sources []string, dest string) ([]copyInfo, error) {
  102. var infos []copyInfo
  103. for _, orig := range sources {
  104. subinfos, err := o.getCopyInfoForSourcePath(orig, dest)
  105. if err != nil {
  106. return nil, err
  107. }
  108. infos = append(infos, subinfos...)
  109. }
  110. if len(infos) == 0 {
  111. return nil, errors.New("no source files were specified")
  112. }
  113. return infos, nil
  114. }
  115. func (o *copier) getCopyInfoForSourcePath(orig, dest string) ([]copyInfo, error) {
  116. if !urlutil.IsURL(orig) {
  117. return o.calcCopyInfo(orig, true)
  118. }
  119. remote, path, err := o.download(orig)
  120. if err != nil {
  121. return nil, err
  122. }
  123. // If path == "" then we are unable to determine filename from src
  124. // We have to make sure dest is available
  125. if path == "" {
  126. if strings.HasSuffix(dest, "/") {
  127. return nil, errors.Errorf("cannot determine filename for source %s", orig)
  128. }
  129. path = unnamedFilename
  130. }
  131. o.tmpPaths = append(o.tmpPaths, remote.Root())
  132. hash, err := remote.Hash(path)
  133. ci := newCopyInfoFromSource(remote, path, hash)
  134. ci.noDecompress = true // data from http shouldn't be extracted even on ADD
  135. return newCopyInfos(ci), err
  136. }
  137. // Cleanup removes any temporary directories created as part of downloading
  138. // remote files.
  139. func (o *copier) Cleanup() {
  140. for _, path := range o.tmpPaths {
  141. os.RemoveAll(path)
  142. }
  143. o.tmpPaths = []string{}
  144. if o.activeLayer != nil {
  145. o.activeLayer.Release()
  146. o.activeLayer = nil
  147. }
  148. }
  149. // TODO: allowWildcards can probably be removed by refactoring this function further.
  150. func (o *copier) calcCopyInfo(origPath string, allowWildcards bool) ([]copyInfo, error) {
  151. imageSource := o.imageSource
  152. if err := validateCopySourcePath(imageSource, origPath); err != nil {
  153. return nil, err
  154. }
  155. // TODO: do this when creating copier. Requires validateCopySourcePath
  156. // (and other below) to be aware of the difference sources. Why is it only
  157. // done on image Source?
  158. if imageSource != nil && o.activeLayer == nil {
  159. // this needs to be protected against repeated calls as wildcard copy
  160. // will call it multiple times for a single COPY
  161. var err error
  162. rwLayer, err := imageSource.NewRWLayer()
  163. if err != nil {
  164. return nil, err
  165. }
  166. o.activeLayer = rwLayer
  167. o.source, err = remotecontext.NewLazySource(rwLayer.Root())
  168. if err != nil {
  169. return nil, errors.Wrapf(err, "failed to create context for copy from %s", rwLayer.Root())
  170. }
  171. }
  172. if o.source == nil {
  173. return nil, errors.Errorf("missing build context")
  174. }
  175. // Work in daemon-specific OS filepath semantics
  176. origPath = filepath.FromSlash(origPath)
  177. origPath = strings.TrimPrefix(origPath, string(os.PathSeparator))
  178. origPath = strings.TrimPrefix(origPath, "."+string(os.PathSeparator))
  179. // Deal with wildcards
  180. if allowWildcards && containsWildcards(origPath) {
  181. return o.copyWithWildcards(origPath)
  182. }
  183. if imageSource != nil && imageSource.ImageID() != "" {
  184. // return a cached copy if one exists
  185. if h, ok := o.pathCache.Load(imageSource.ImageID() + origPath); ok {
  186. return newCopyInfos(newCopyInfoFromSource(o.source, origPath, h.(string))), nil
  187. }
  188. }
  189. // Deal with the single file case
  190. copyInfo, err := copyInfoForFile(o.source, origPath)
  191. switch {
  192. case imageSource == nil && errors.Is(err, os.ErrNotExist):
  193. return nil, errors.Wrapf(err, "file not found in build context or excluded by .dockerignore")
  194. case err != nil:
  195. return nil, err
  196. case copyInfo.hash != "":
  197. o.storeInPathCache(imageSource, origPath, copyInfo.hash)
  198. return newCopyInfos(copyInfo), err
  199. }
  200. // TODO: remove, handle dirs in Hash()
  201. subfiles, err := walkSource(o.source, origPath)
  202. if err != nil {
  203. return nil, err
  204. }
  205. hash := hashStringSlice("dir", subfiles)
  206. o.storeInPathCache(imageSource, origPath, hash)
  207. return newCopyInfos(newCopyInfoFromSource(o.source, origPath, hash)), nil
  208. }
  209. func (o *copier) storeInPathCache(im *imageMount, path string, hash string) {
  210. if im != nil {
  211. o.pathCache.Store(im.ImageID()+path, hash)
  212. }
  213. }
  214. func (o *copier) copyWithWildcards(origPath string) ([]copyInfo, error) {
  215. root := o.source.Root()
  216. var copyInfos []copyInfo
  217. if err := filepath.WalkDir(root, func(path string, _ os.DirEntry, err error) error {
  218. if err != nil {
  219. return err
  220. }
  221. rel, err := remotecontext.Rel(root, path)
  222. if err != nil {
  223. return err
  224. }
  225. if rel == "." {
  226. return nil
  227. }
  228. if match, _ := filepath.Match(origPath, rel); !match {
  229. return nil
  230. }
  231. // Note we set allowWildcards to false in case the name has
  232. // a * in it
  233. subInfos, err := o.calcCopyInfo(rel, false)
  234. if err != nil {
  235. return err
  236. }
  237. copyInfos = append(copyInfos, subInfos...)
  238. return nil
  239. }); err != nil {
  240. return nil, err
  241. }
  242. return copyInfos, nil
  243. }
  244. func copyInfoForFile(source builder.Source, path string) (copyInfo, error) {
  245. fi, err := remotecontext.StatAt(source, path)
  246. if err != nil {
  247. if errors.Is(err, os.ErrNotExist) {
  248. // return the relative path in the error, which is more user-friendly than the full path to the tmp-dir
  249. return copyInfo{}, errors.WithStack(&os.PathError{Op: "stat", Path: path, Err: os.ErrNotExist})
  250. }
  251. return copyInfo{}, err
  252. }
  253. if fi.IsDir() {
  254. return copyInfo{}, nil
  255. }
  256. hash, err := source.Hash(path)
  257. if err != nil {
  258. return copyInfo{}, err
  259. }
  260. return newCopyInfoFromSource(source, path, "file:"+hash), nil
  261. }
  262. // TODO: dedupe with copyWithWildcards()
  263. func walkSource(source builder.Source, origPath string) ([]string, error) {
  264. fp, err := remotecontext.FullPath(source, origPath)
  265. if err != nil {
  266. return nil, err
  267. }
  268. // Must be a dir
  269. var subfiles []string
  270. err = filepath.WalkDir(fp, func(path string, _ os.DirEntry, err error) error {
  271. if err != nil {
  272. return err
  273. }
  274. rel, err := remotecontext.Rel(source.Root(), path)
  275. if err != nil {
  276. return err
  277. }
  278. if rel == "." {
  279. return nil
  280. }
  281. hash, err := source.Hash(rel)
  282. if err != nil {
  283. return nil
  284. }
  285. // we already checked handleHash above
  286. subfiles = append(subfiles, hash)
  287. return nil
  288. })
  289. if err != nil {
  290. return nil, err
  291. }
  292. sort.Strings(subfiles)
  293. return subfiles, nil
  294. }
  295. type sourceDownloader func(string) (builder.Source, string, error)
  296. func newRemoteSourceDownloader(output, stdout io.Writer) sourceDownloader {
  297. return func(url string) (builder.Source, string, error) {
  298. return downloadSource(output, stdout, url)
  299. }
  300. }
  301. func errOnSourceDownload(_ string) (builder.Source, string, error) {
  302. return nil, "", errors.New("source can't be a URL for COPY")
  303. }
  304. func getFilenameForDownload(path string, resp *http.Response) string {
  305. // Guess filename based on source
  306. if path != "" && !strings.HasSuffix(path, "/") {
  307. if filename := filepath.Base(filepath.FromSlash(path)); filename != "" {
  308. return filename
  309. }
  310. }
  311. // Guess filename based on Content-Disposition
  312. if contentDisposition := resp.Header.Get("Content-Disposition"); contentDisposition != "" {
  313. if _, params, err := mime.ParseMediaType(contentDisposition); err == nil {
  314. if params["filename"] != "" && !strings.HasSuffix(params["filename"], "/") {
  315. if filename := filepath.Base(filepath.FromSlash(params["filename"])); filename != "" {
  316. return filename
  317. }
  318. }
  319. }
  320. }
  321. return ""
  322. }
  323. func downloadSource(output io.Writer, stdout io.Writer, srcURL string) (remote builder.Source, p string, err error) {
  324. u, err := url.Parse(srcURL)
  325. if err != nil {
  326. return
  327. }
  328. resp, err := remotecontext.GetWithStatusError(srcURL)
  329. if err != nil {
  330. return
  331. }
  332. filename := getFilenameForDownload(u.Path, resp)
  333. // Prepare file in a tmp dir
  334. tmpDir, err := longpath.MkdirTemp("", "docker-remote")
  335. if err != nil {
  336. return
  337. }
  338. defer func() {
  339. if err != nil {
  340. os.RemoveAll(tmpDir)
  341. }
  342. }()
  343. // If filename is empty, the returned filename will be "" but
  344. // the tmp filename will be created as "__unnamed__"
  345. tmpFileName := filename
  346. if filename == "" {
  347. tmpFileName = unnamedFilename
  348. }
  349. tmpFileName = filepath.Join(tmpDir, tmpFileName)
  350. tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600)
  351. if err != nil {
  352. return
  353. }
  354. progressOutput := streamformatter.NewJSONProgressOutput(output, true)
  355. progressReader := progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Downloading")
  356. // Download and dump result to tmp file
  357. // TODO: add filehash directly
  358. if _, err = io.Copy(tmpFile, progressReader); err != nil {
  359. tmpFile.Close()
  360. return
  361. }
  362. // TODO: how important is this random blank line to the output?
  363. fmt.Fprintln(stdout)
  364. // Set the mtime to the Last-Modified header value if present
  365. // Otherwise just remove atime and mtime
  366. mTime := time.Time{}
  367. lastMod := resp.Header.Get("Last-Modified")
  368. if lastMod != "" {
  369. // If we can't parse it then just let it default to 'zero'
  370. // otherwise use the parsed time value
  371. if parsedMTime, err := http.ParseTime(lastMod); err == nil {
  372. mTime = parsedMTime
  373. }
  374. }
  375. tmpFile.Close()
  376. if err = system.Chtimes(tmpFileName, mTime, mTime); err != nil {
  377. return
  378. }
  379. lc, err := remotecontext.NewLazySource(tmpDir)
  380. return lc, filename, err
  381. }
  382. type copyFileOptions struct {
  383. decompress bool
  384. identity *idtools.Identity
  385. archiver *archive.Archiver
  386. }
  387. func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions) error {
  388. srcPath, err := source.fullPath()
  389. if err != nil {
  390. return err
  391. }
  392. destPath, err := dest.fullPath()
  393. if err != nil {
  394. return err
  395. }
  396. archiver := options.archiver
  397. src, err := os.Stat(srcPath)
  398. if err != nil {
  399. return errors.Wrapf(err, "source path not found")
  400. }
  401. if src.IsDir() {
  402. return copyDirectory(archiver, srcPath, destPath, options.identity)
  403. }
  404. if options.decompress && archive.IsArchivePath(srcPath) && !source.noDecompress {
  405. f, err := os.Open(srcPath)
  406. if err != nil {
  407. return err
  408. }
  409. defer f.Close()
  410. options := &archive.TarOptions{
  411. IDMap: archiver.IDMapping,
  412. BestEffortXattrs: true,
  413. }
  414. return archiver.Untar(f, destPath, options)
  415. }
  416. destExistsAsDir, err := isExistingDirectory(destPath)
  417. if err != nil {
  418. return err
  419. }
  420. // dest.path must be used because destPath has already been cleaned of any
  421. // trailing slash
  422. if endsInSlash(dest.path) || destExistsAsDir {
  423. // source.path must be used to get the correct filename when the source
  424. // is a symlink
  425. destPath = filepath.Join(destPath, filepath.Base(source.path))
  426. }
  427. return copyFile(archiver, srcPath, destPath, options.identity)
  428. }
  429. func copyDirectory(archiver *archive.Archiver, source, dest string, identity *idtools.Identity) error {
  430. destExists, err := isExistingDirectory(dest)
  431. if err != nil {
  432. return errors.Wrapf(err, "failed to query destination path")
  433. }
  434. if err := archiver.CopyWithTar(source, dest); err != nil {
  435. return errors.Wrapf(err, "failed to copy directory")
  436. }
  437. if identity != nil {
  438. return fixPermissions(source, dest, *identity, !destExists)
  439. }
  440. return nil
  441. }
  442. func copyFile(archiver *archive.Archiver, source, dest string, identity *idtools.Identity) error {
  443. if identity == nil {
  444. // Use system.MkdirAll here, which is a custom version of os.MkdirAll
  445. // modified for use on Windows to handle volume GUID paths. These paths
  446. // are of the form \\?\Volume{<GUID>}\<path>. An example would be:
  447. // \\?\Volume{dae8d3ac-b9a1-11e9-88eb-e8554b2ba1db}\bin\busybox.exe
  448. if err := system.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
  449. return err
  450. }
  451. } else {
  452. if err := idtools.MkdirAllAndChownNew(filepath.Dir(dest), 0o755, *identity); err != nil {
  453. return errors.Wrapf(err, "failed to create new directory")
  454. }
  455. }
  456. if err := archiver.CopyFileWithTar(source, dest); err != nil {
  457. return errors.Wrapf(err, "failed to copy file")
  458. }
  459. if identity != nil {
  460. return fixPermissions(source, dest, *identity, false)
  461. }
  462. return nil
  463. }
  464. func endsInSlash(path string) bool {
  465. return strings.HasSuffix(path, string(filepath.Separator))
  466. }
  467. // isExistingDirectory returns true if the path exists and is a directory
  468. func isExistingDirectory(path string) (bool, error) {
  469. destStat, err := os.Stat(path)
  470. switch {
  471. case errors.Is(err, os.ErrNotExist):
  472. return false, nil
  473. case err != nil:
  474. return false, err
  475. }
  476. return destStat.IsDir(), nil
  477. }