copy.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io"
  6. "mime"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "sort"
  13. "strings"
  14. "time"
  15. "github.com/docker/docker/builder"
  16. "github.com/docker/docker/builder/remotecontext"
  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/ioutils"
  21. "github.com/docker/docker/pkg/progress"
  22. "github.com/docker/docker/pkg/streamformatter"
  23. "github.com/docker/docker/pkg/system"
  24. "github.com/docker/docker/pkg/urlutil"
  25. specs "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 containerfs.ContainerFS
  37. path string
  38. hash string
  39. noDecompress bool
  40. }
  41. func (c copyInfo) fullPath() (string, error) {
  42. return c.root.ResolveScopedPath(c.path, true)
  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. }
  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 *specs.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.platform
  75. if platform == nil {
  76. // May be nil if not explicitly set in API/dockerfile
  77. platform = &specs.Platform{}
  78. }
  79. if platform.OS == "" {
  80. // Default to the dispatch requests operating system if not explicit in API/dockerfile
  81. platform.OS = req.state.operatingSystem
  82. }
  83. if platform.OS == "" {
  84. // This is a failsafe just in case. Shouldn't be hit.
  85. platform.OS = runtime.GOOS
  86. }
  87. return copier{
  88. source: req.source,
  89. pathCache: req.builder.pathCache,
  90. download: download,
  91. imageSource: imageSource,
  92. platform: platform,
  93. }
  94. }
  95. func (o *copier) createCopyInstruction(args []string, cmdName string) (copyInstruction, error) {
  96. inst := copyInstruction{cmdName: cmdName}
  97. last := len(args) - 1
  98. // Work in platform-specific filepath semantics
  99. // TODO: This OS switch for paths is NOT correct and should not be supported.
  100. // Maintained for backwards compatibility
  101. pathOS := runtime.GOOS
  102. if o.platform != nil {
  103. pathOS = o.platform.OS
  104. }
  105. inst.dest = fromSlash(args[last], pathOS)
  106. separator := string(separator(pathOS))
  107. infos, err := o.getCopyInfosForSourcePaths(args[0:last], inst.dest)
  108. if err != nil {
  109. return inst, errors.Wrapf(err, "%s failed", cmdName)
  110. }
  111. if len(infos) > 1 && !strings.HasSuffix(inst.dest, separator) {
  112. return inst, errors.Errorf("When using %s with more than one source file, the destination must be a directory and end with a /", cmdName)
  113. }
  114. inst.infos = infos
  115. return inst, nil
  116. }
  117. // getCopyInfosForSourcePaths iterates over the source files and calculate the info
  118. // needed to copy (e.g. hash value if cached)
  119. // The dest is used in case source is URL (and ends with "/")
  120. func (o *copier) getCopyInfosForSourcePaths(sources []string, dest string) ([]copyInfo, error) {
  121. var infos []copyInfo
  122. for _, orig := range sources {
  123. subinfos, err := o.getCopyInfoForSourcePath(orig, dest)
  124. if err != nil {
  125. return nil, err
  126. }
  127. infos = append(infos, subinfos...)
  128. }
  129. if len(infos) == 0 {
  130. return nil, errors.New("no source files were specified")
  131. }
  132. return infos, nil
  133. }
  134. func (o *copier) getCopyInfoForSourcePath(orig, dest string) ([]copyInfo, error) {
  135. if !urlutil.IsURL(orig) {
  136. return o.calcCopyInfo(orig, true)
  137. }
  138. remote, path, err := o.download(orig)
  139. if err != nil {
  140. return nil, err
  141. }
  142. // If path == "" then we are unable to determine filename from src
  143. // We have to make sure dest is available
  144. if path == "" {
  145. if strings.HasSuffix(dest, "/") {
  146. return nil, errors.Errorf("cannot determine filename for source %s", orig)
  147. }
  148. path = unnamedFilename
  149. }
  150. o.tmpPaths = append(o.tmpPaths, remote.Root().Path())
  151. hash, err := remote.Hash(path)
  152. ci := newCopyInfoFromSource(remote, path, hash)
  153. ci.noDecompress = true // data from http shouldn't be extracted even on ADD
  154. return newCopyInfos(ci), err
  155. }
  156. // Cleanup removes any temporary directories created as part of downloading
  157. // remote files.
  158. func (o *copier) Cleanup() {
  159. for _, path := range o.tmpPaths {
  160. os.RemoveAll(path)
  161. }
  162. o.tmpPaths = []string{}
  163. if o.activeLayer != nil {
  164. o.activeLayer.Release()
  165. o.activeLayer = nil
  166. }
  167. }
  168. // TODO: allowWildcards can probably be removed by refactoring this function further.
  169. func (o *copier) calcCopyInfo(origPath string, allowWildcards bool) ([]copyInfo, error) {
  170. imageSource := o.imageSource
  171. // TODO: do this when creating copier. Requires validateCopySourcePath
  172. // (and other below) to be aware of the difference sources. Why is it only
  173. // done on image Source?
  174. if imageSource != nil && o.activeLayer == nil {
  175. // this needs to be protected against repeated calls as wildcard copy
  176. // will call it multiple times for a single COPY
  177. var err error
  178. rwLayer, err := imageSource.NewRWLayer()
  179. if err != nil {
  180. return nil, err
  181. }
  182. o.activeLayer = rwLayer
  183. o.source, err = remotecontext.NewLazySource(rwLayer.Root())
  184. if err != nil {
  185. return nil, errors.Wrapf(err, "failed to create context for copy from %s", rwLayer.Root().Path())
  186. }
  187. }
  188. if o.source == nil {
  189. return nil, errors.Errorf("missing build context")
  190. }
  191. root := o.source.Root()
  192. if err := validateCopySourcePath(imageSource, origPath, root.OS()); err != nil {
  193. return nil, err
  194. }
  195. // Work in source OS specific filepath semantics
  196. // For LCOW, this is NOT the daemon OS.
  197. origPath = root.FromSlash(origPath)
  198. origPath = strings.TrimPrefix(origPath, string(root.Separator()))
  199. origPath = strings.TrimPrefix(origPath, "."+string(root.Separator()))
  200. // Deal with wildcards
  201. if allowWildcards && containsWildcards(origPath, root.OS()) {
  202. return o.copyWithWildcards(origPath)
  203. }
  204. if imageSource != nil && imageSource.ImageID() != "" {
  205. // return a cached copy if one exists
  206. if h, ok := o.pathCache.Load(imageSource.ImageID() + origPath); ok {
  207. return newCopyInfos(newCopyInfoFromSource(o.source, origPath, h.(string))), nil
  208. }
  209. }
  210. // Deal with the single file case
  211. copyInfo, err := copyInfoForFile(o.source, origPath)
  212. switch {
  213. case err != nil:
  214. return nil, err
  215. case copyInfo.hash != "":
  216. o.storeInPathCache(imageSource, origPath, copyInfo.hash)
  217. return newCopyInfos(copyInfo), err
  218. }
  219. // TODO: remove, handle dirs in Hash()
  220. subfiles, err := walkSource(o.source, origPath)
  221. if err != nil {
  222. return nil, err
  223. }
  224. hash := hashStringSlice("dir", subfiles)
  225. o.storeInPathCache(imageSource, origPath, hash)
  226. return newCopyInfos(newCopyInfoFromSource(o.source, origPath, hash)), nil
  227. }
  228. func containsWildcards(name, platform string) bool {
  229. isWindows := platform == "windows"
  230. for i := 0; i < len(name); i++ {
  231. ch := name[i]
  232. if ch == '\\' && !isWindows {
  233. i++
  234. } else if ch == '*' || ch == '?' || ch == '[' {
  235. return true
  236. }
  237. }
  238. return false
  239. }
  240. func (o *copier) storeInPathCache(im *imageMount, path string, hash string) {
  241. if im != nil {
  242. o.pathCache.Store(im.ImageID()+path, hash)
  243. }
  244. }
  245. func (o *copier) copyWithWildcards(origPath string) ([]copyInfo, error) {
  246. root := o.source.Root()
  247. var copyInfos []copyInfo
  248. if err := root.Walk(root.Path(), func(path string, info os.FileInfo, err error) error {
  249. if err != nil {
  250. return err
  251. }
  252. rel, err := remotecontext.Rel(root, path)
  253. if err != nil {
  254. return err
  255. }
  256. if rel == "." {
  257. return nil
  258. }
  259. if match, _ := root.Match(origPath, rel); !match {
  260. return nil
  261. }
  262. // Note we set allowWildcards to false in case the name has
  263. // a * in it
  264. subInfos, err := o.calcCopyInfo(rel, false)
  265. if err != nil {
  266. return err
  267. }
  268. copyInfos = append(copyInfos, subInfos...)
  269. return nil
  270. }); err != nil {
  271. return nil, err
  272. }
  273. return copyInfos, nil
  274. }
  275. func copyInfoForFile(source builder.Source, path string) (copyInfo, error) {
  276. fi, err := remotecontext.StatAt(source, path)
  277. if err != nil {
  278. return copyInfo{}, err
  279. }
  280. if fi.IsDir() {
  281. return copyInfo{}, nil
  282. }
  283. hash, err := source.Hash(path)
  284. if err != nil {
  285. return copyInfo{}, err
  286. }
  287. return newCopyInfoFromSource(source, path, "file:"+hash), nil
  288. }
  289. // TODO: dedupe with copyWithWildcards()
  290. func walkSource(source builder.Source, origPath string) ([]string, error) {
  291. fp, err := remotecontext.FullPath(source, origPath)
  292. if err != nil {
  293. return nil, err
  294. }
  295. // Must be a dir
  296. var subfiles []string
  297. err = source.Root().Walk(fp, func(path string, info os.FileInfo, err error) error {
  298. if err != nil {
  299. return err
  300. }
  301. rel, err := remotecontext.Rel(source.Root(), path)
  302. if err != nil {
  303. return err
  304. }
  305. if rel == "." {
  306. return nil
  307. }
  308. hash, err := source.Hash(rel)
  309. if err != nil {
  310. return nil
  311. }
  312. // we already checked handleHash above
  313. subfiles = append(subfiles, hash)
  314. return nil
  315. })
  316. if err != nil {
  317. return nil, err
  318. }
  319. sort.Strings(subfiles)
  320. return subfiles, nil
  321. }
  322. type sourceDownloader func(string) (builder.Source, string, error)
  323. func newRemoteSourceDownloader(output, stdout io.Writer) sourceDownloader {
  324. return func(url string) (builder.Source, string, error) {
  325. return downloadSource(output, stdout, url)
  326. }
  327. }
  328. func errOnSourceDownload(_ string) (builder.Source, string, error) {
  329. return nil, "", errors.New("source can't be a URL for COPY")
  330. }
  331. func getFilenameForDownload(path string, resp *http.Response) string {
  332. // Guess filename based on source
  333. if path != "" && !strings.HasSuffix(path, "/") {
  334. if filename := filepath.Base(filepath.FromSlash(path)); filename != "" {
  335. return filename
  336. }
  337. }
  338. // Guess filename based on Content-Disposition
  339. if contentDisposition := resp.Header.Get("Content-Disposition"); contentDisposition != "" {
  340. if _, params, err := mime.ParseMediaType(contentDisposition); err == nil {
  341. if params["filename"] != "" && !strings.HasSuffix(params["filename"], "/") {
  342. if filename := filepath.Base(filepath.FromSlash(params["filename"])); filename != "" {
  343. return filename
  344. }
  345. }
  346. }
  347. }
  348. return ""
  349. }
  350. func downloadSource(output io.Writer, stdout io.Writer, srcURL string) (remote builder.Source, p string, err error) {
  351. u, err := url.Parse(srcURL)
  352. if err != nil {
  353. return
  354. }
  355. resp, err := remotecontext.GetWithStatusError(srcURL)
  356. if err != nil {
  357. return
  358. }
  359. filename := getFilenameForDownload(u.Path, resp)
  360. // Prepare file in a tmp dir
  361. tmpDir, err := ioutils.TempDir("", "docker-remote")
  362. if err != nil {
  363. return
  364. }
  365. defer func() {
  366. if err != nil {
  367. os.RemoveAll(tmpDir)
  368. }
  369. }()
  370. // If filename is empty, the returned filename will be "" but
  371. // the tmp filename will be created as "__unnamed__"
  372. tmpFileName := filename
  373. if filename == "" {
  374. tmpFileName = unnamedFilename
  375. }
  376. tmpFileName = filepath.Join(tmpDir, tmpFileName)
  377. tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
  378. if err != nil {
  379. return
  380. }
  381. progressOutput := streamformatter.NewJSONProgressOutput(output, true)
  382. progressReader := progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Downloading")
  383. // Download and dump result to tmp file
  384. // TODO: add filehash directly
  385. if _, err = io.Copy(tmpFile, progressReader); err != nil {
  386. tmpFile.Close()
  387. return
  388. }
  389. // TODO: how important is this random blank line to the output?
  390. fmt.Fprintln(stdout)
  391. // Set the mtime to the Last-Modified header value if present
  392. // Otherwise just remove atime and mtime
  393. mTime := time.Time{}
  394. lastMod := resp.Header.Get("Last-Modified")
  395. if lastMod != "" {
  396. // If we can't parse it then just let it default to 'zero'
  397. // otherwise use the parsed time value
  398. if parsedMTime, err := http.ParseTime(lastMod); err == nil {
  399. mTime = parsedMTime
  400. }
  401. }
  402. tmpFile.Close()
  403. if err = system.Chtimes(tmpFileName, mTime, mTime); err != nil {
  404. return
  405. }
  406. lc, err := remotecontext.NewLazySource(containerfs.NewLocalContainerFS(tmpDir))
  407. return lc, filename, err
  408. }
  409. type copyFileOptions struct {
  410. decompress bool
  411. chownPair idtools.IDPair
  412. archiver Archiver
  413. }
  414. type copyEndpoint struct {
  415. driver containerfs.Driver
  416. path string
  417. }
  418. func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions) error {
  419. srcPath, err := source.fullPath()
  420. if err != nil {
  421. return err
  422. }
  423. destPath, err := dest.fullPath()
  424. if err != nil {
  425. return err
  426. }
  427. archiver := options.archiver
  428. srcEndpoint := &copyEndpoint{driver: source.root, path: srcPath}
  429. destEndpoint := &copyEndpoint{driver: dest.root, path: destPath}
  430. src, err := source.root.Stat(srcPath)
  431. if err != nil {
  432. return errors.Wrapf(err, "source path not found")
  433. }
  434. if src.IsDir() {
  435. return copyDirectory(archiver, srcEndpoint, destEndpoint, options.chownPair)
  436. }
  437. if options.decompress && isArchivePath(source.root, srcPath) && !source.noDecompress {
  438. return archiver.UntarPath(srcPath, destPath)
  439. }
  440. destExistsAsDir, err := isExistingDirectory(destEndpoint)
  441. if err != nil {
  442. return err
  443. }
  444. // dest.path must be used because destPath has already been cleaned of any
  445. // trailing slash
  446. if endsInSlash(dest.root, dest.path) || destExistsAsDir {
  447. // source.path must be used to get the correct filename when the source
  448. // is a symlink
  449. destPath = dest.root.Join(destPath, source.root.Base(source.path))
  450. destEndpoint = &copyEndpoint{driver: dest.root, path: destPath}
  451. }
  452. return copyFile(archiver, srcEndpoint, destEndpoint, options.chownPair)
  453. }
  454. func isArchivePath(driver containerfs.ContainerFS, path string) bool {
  455. file, err := driver.Open(path)
  456. if err != nil {
  457. return false
  458. }
  459. defer file.Close()
  460. rdr, err := archive.DecompressStream(file)
  461. if err != nil {
  462. return false
  463. }
  464. r := tar.NewReader(rdr)
  465. _, err = r.Next()
  466. return err == nil
  467. }
  468. func copyDirectory(archiver Archiver, source, dest *copyEndpoint, chownPair idtools.IDPair) error {
  469. destExists, err := isExistingDirectory(dest)
  470. if err != nil {
  471. return errors.Wrapf(err, "failed to query destination path")
  472. }
  473. if err := archiver.CopyWithTar(source.path, dest.path); err != nil {
  474. return errors.Wrapf(err, "failed to copy directory")
  475. }
  476. // TODO: @gupta-ak. Investigate how LCOW permission mappings will work.
  477. return fixPermissions(source.path, dest.path, chownPair, !destExists)
  478. }
  479. func copyFile(archiver Archiver, source, dest *copyEndpoint, chownPair idtools.IDPair) error {
  480. if runtime.GOOS == "windows" && dest.driver.OS() == "linux" {
  481. // LCOW
  482. if err := dest.driver.MkdirAll(dest.driver.Dir(dest.path), 0755); err != nil {
  483. return errors.Wrapf(err, "failed to create new directory")
  484. }
  485. } else {
  486. if err := idtools.MkdirAllAndChownNew(filepath.Dir(dest.path), 0755, chownPair); err != nil {
  487. // Normal containers
  488. return errors.Wrapf(err, "failed to create new directory")
  489. }
  490. }
  491. if err := archiver.CopyFileWithTar(source.path, dest.path); err != nil {
  492. return errors.Wrapf(err, "failed to copy file")
  493. }
  494. // TODO: @gupta-ak. Investigate how LCOW permission mappings will work.
  495. return fixPermissions(source.path, dest.path, chownPair, false)
  496. }
  497. func endsInSlash(driver containerfs.Driver, path string) bool {
  498. return strings.HasSuffix(path, string(driver.Separator()))
  499. }
  500. // isExistingDirectory returns true if the path exists and is a directory
  501. func isExistingDirectory(point *copyEndpoint) (bool, error) {
  502. destStat, err := point.driver.Stat(point.path)
  503. switch {
  504. case os.IsNotExist(err):
  505. return false, nil
  506. case err != nil:
  507. return false, err
  508. }
  509. return destStat.IsDir(), nil
  510. }