copy.go 15 KB

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