copy.go 15 KB

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