internals.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. package builder
  2. // internals for handling commands. Covers many areas and a lot of
  3. // non-contiguous functionality. Please read the comments.
  4. import (
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "path/filepath"
  14. "runtime"
  15. "sort"
  16. "strings"
  17. "syscall"
  18. "time"
  19. "github.com/Sirupsen/logrus"
  20. "github.com/docker/docker/builder/parser"
  21. "github.com/docker/docker/daemon"
  22. "github.com/docker/docker/graph"
  23. imagepkg "github.com/docker/docker/image"
  24. "github.com/docker/docker/pkg/archive"
  25. "github.com/docker/docker/pkg/chrootarchive"
  26. "github.com/docker/docker/pkg/httputils"
  27. "github.com/docker/docker/pkg/ioutils"
  28. "github.com/docker/docker/pkg/jsonmessage"
  29. "github.com/docker/docker/pkg/parsers"
  30. "github.com/docker/docker/pkg/progressreader"
  31. "github.com/docker/docker/pkg/stringid"
  32. "github.com/docker/docker/pkg/system"
  33. "github.com/docker/docker/pkg/tarsum"
  34. "github.com/docker/docker/pkg/urlutil"
  35. "github.com/docker/docker/registry"
  36. "github.com/docker/docker/runconfig"
  37. )
  38. func (b *Builder) readContext(context io.Reader) error {
  39. tmpdirPath, err := ioutil.TempDir("", "docker-build")
  40. if err != nil {
  41. return err
  42. }
  43. decompressedStream, err := archive.DecompressStream(context)
  44. if err != nil {
  45. return err
  46. }
  47. if b.context, err = tarsum.NewTarSum(decompressedStream, true, tarsum.Version1); err != nil {
  48. return err
  49. }
  50. if err := chrootarchive.Untar(b.context, tmpdirPath, nil); err != nil {
  51. return err
  52. }
  53. b.contextPath = tmpdirPath
  54. return nil
  55. }
  56. func (b *Builder) commit(id string, autoCmd *runconfig.Command, comment string) error {
  57. if b.disableCommit {
  58. return nil
  59. }
  60. if b.image == "" && !b.noBaseImage {
  61. return fmt.Errorf("Please provide a source image with `from` prior to commit")
  62. }
  63. b.Config.Image = b.image
  64. if id == "" {
  65. cmd := b.Config.Cmd
  66. if runtime.GOOS != "windows" {
  67. b.Config.Cmd = runconfig.NewCommand("/bin/sh", "-c", "#(nop) "+comment)
  68. } else {
  69. b.Config.Cmd = runconfig.NewCommand("cmd", "/S /C", "REM (nop) "+comment)
  70. }
  71. defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
  72. hit, err := b.probeCache()
  73. if err != nil {
  74. return err
  75. }
  76. if hit {
  77. return nil
  78. }
  79. container, err := b.create()
  80. if err != nil {
  81. return err
  82. }
  83. id = container.ID
  84. if err := container.Mount(); err != nil {
  85. return err
  86. }
  87. defer container.Unmount()
  88. }
  89. container, err := b.Daemon.Get(id)
  90. if err != nil {
  91. return err
  92. }
  93. // Note: Actually copy the struct
  94. autoConfig := *b.Config
  95. autoConfig.Cmd = autoCmd
  96. // Commit the container
  97. image, err := b.Daemon.Commit(container, "", "", "", b.maintainer, true, &autoConfig)
  98. if err != nil {
  99. return err
  100. }
  101. b.image = image.ID
  102. return nil
  103. }
  104. type copyInfo struct {
  105. origPath string
  106. destPath string
  107. hash string
  108. decompress bool
  109. tmpDir string
  110. }
  111. func (b *Builder) runContextCommand(args []string, allowRemote bool, allowDecompression bool, cmdName string) error {
  112. if b.context == nil {
  113. return fmt.Errorf("No context given. Impossible to use %s", cmdName)
  114. }
  115. if len(args) < 2 {
  116. return fmt.Errorf("Invalid %s format - at least two arguments required", cmdName)
  117. }
  118. dest := args[len(args)-1] // last one is always the dest
  119. copyInfos := []*copyInfo{}
  120. b.Config.Image = b.image
  121. defer func() {
  122. for _, ci := range copyInfos {
  123. if ci.tmpDir != "" {
  124. os.RemoveAll(ci.tmpDir)
  125. }
  126. }
  127. }()
  128. // Loop through each src file and calculate the info we need to
  129. // do the copy (e.g. hash value if cached). Don't actually do
  130. // the copy until we've looked at all src files
  131. for _, orig := range args[0 : len(args)-1] {
  132. if err := calcCopyInfo(
  133. b,
  134. cmdName,
  135. &copyInfos,
  136. orig,
  137. dest,
  138. allowRemote,
  139. allowDecompression,
  140. true,
  141. ); err != nil {
  142. return err
  143. }
  144. }
  145. if len(copyInfos) == 0 {
  146. return fmt.Errorf("No source files were specified")
  147. }
  148. if len(copyInfos) > 1 && !strings.HasSuffix(dest, "/") {
  149. return fmt.Errorf("When using %s with more than one source file, the destination must be a directory and end with a /", cmdName)
  150. }
  151. // For backwards compat, if there's just one CI then use it as the
  152. // cache look-up string, otherwise hash 'em all into one
  153. var srcHash string
  154. var origPaths string
  155. if len(copyInfos) == 1 {
  156. srcHash = copyInfos[0].hash
  157. origPaths = copyInfos[0].origPath
  158. } else {
  159. var hashs []string
  160. var origs []string
  161. for _, ci := range copyInfos {
  162. hashs = append(hashs, ci.hash)
  163. origs = append(origs, ci.origPath)
  164. }
  165. hasher := sha256.New()
  166. hasher.Write([]byte(strings.Join(hashs, ",")))
  167. srcHash = "multi:" + hex.EncodeToString(hasher.Sum(nil))
  168. origPaths = strings.Join(origs, " ")
  169. }
  170. cmd := b.Config.Cmd
  171. if runtime.GOOS != "windows" {
  172. b.Config.Cmd = runconfig.NewCommand("/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, srcHash, dest))
  173. } else {
  174. b.Config.Cmd = runconfig.NewCommand("cmd", "/S /C", fmt.Sprintf("REM (nop) %s %s in %s", cmdName, srcHash, dest))
  175. }
  176. defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
  177. hit, err := b.probeCache()
  178. if err != nil {
  179. return err
  180. }
  181. if hit {
  182. return nil
  183. }
  184. container, _, err := b.Daemon.Create(b.Config, nil, "")
  185. if err != nil {
  186. return err
  187. }
  188. b.TmpContainers[container.ID] = struct{}{}
  189. if err := container.Mount(); err != nil {
  190. return err
  191. }
  192. defer container.Unmount()
  193. for _, ci := range copyInfos {
  194. if err := b.addContext(container, ci.origPath, ci.destPath, ci.decompress); err != nil {
  195. return err
  196. }
  197. }
  198. if err := b.commit(container.ID, cmd, fmt.Sprintf("%s %s in %s", cmdName, origPaths, dest)); err != nil {
  199. return err
  200. }
  201. return nil
  202. }
  203. func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath string, destPath string, allowRemote bool, allowDecompression bool, allowWildcards bool) error {
  204. if origPath != "" && origPath[0] == '/' && len(origPath) > 1 {
  205. origPath = origPath[1:]
  206. }
  207. origPath = strings.TrimPrefix(origPath, "./")
  208. // Twiddle the destPath when its a relative path - meaning, make it
  209. // relative to the WORKINGDIR
  210. if !filepath.IsAbs(destPath) {
  211. hasSlash := strings.HasSuffix(destPath, "/")
  212. destPath = filepath.Join("/", b.Config.WorkingDir, destPath)
  213. // Make sure we preserve any trailing slash
  214. if hasSlash {
  215. destPath += "/"
  216. }
  217. }
  218. // In the remote/URL case, download it and gen its hashcode
  219. if urlutil.IsURL(origPath) {
  220. if !allowRemote {
  221. return fmt.Errorf("Source can't be a URL for %s", cmdName)
  222. }
  223. ci := copyInfo{}
  224. ci.origPath = origPath
  225. ci.hash = origPath // default to this but can change
  226. ci.destPath = destPath
  227. ci.decompress = false
  228. *cInfos = append(*cInfos, &ci)
  229. // Initiate the download
  230. resp, err := httputils.Download(ci.origPath)
  231. if err != nil {
  232. return err
  233. }
  234. // Create a tmp dir
  235. tmpDirName, err := ioutil.TempDir(b.contextPath, "docker-remote")
  236. if err != nil {
  237. return err
  238. }
  239. ci.tmpDir = tmpDirName
  240. // Create a tmp file within our tmp dir
  241. tmpFileName := filepath.Join(tmpDirName, "tmp")
  242. tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
  243. if err != nil {
  244. return err
  245. }
  246. // Download and dump result to tmp file
  247. if _, err := io.Copy(tmpFile, progressreader.New(progressreader.Config{
  248. In: resp.Body,
  249. Out: b.OutOld,
  250. Formatter: b.StreamFormatter,
  251. Size: int(resp.ContentLength),
  252. NewLines: true,
  253. ID: "",
  254. Action: "Downloading",
  255. })); err != nil {
  256. tmpFile.Close()
  257. return err
  258. }
  259. fmt.Fprintf(b.OutStream, "\n")
  260. tmpFile.Close()
  261. // Set the mtime to the Last-Modified header value if present
  262. // Otherwise just remove atime and mtime
  263. times := make([]syscall.Timespec, 2)
  264. lastMod := resp.Header.Get("Last-Modified")
  265. if lastMod != "" {
  266. mTime, err := http.ParseTime(lastMod)
  267. // If we can't parse it then just let it default to 'zero'
  268. // otherwise use the parsed time value
  269. if err == nil {
  270. times[1] = syscall.NsecToTimespec(mTime.UnixNano())
  271. }
  272. }
  273. if err := system.UtimesNano(tmpFileName, times); err != nil {
  274. return err
  275. }
  276. ci.origPath = filepath.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
  277. // If the destination is a directory, figure out the filename.
  278. if strings.HasSuffix(ci.destPath, "/") {
  279. u, err := url.Parse(origPath)
  280. if err != nil {
  281. return err
  282. }
  283. path := u.Path
  284. if strings.HasSuffix(path, "/") {
  285. path = path[:len(path)-1]
  286. }
  287. parts := strings.Split(path, "/")
  288. filename := parts[len(parts)-1]
  289. if filename == "" {
  290. return fmt.Errorf("cannot determine filename from url: %s", u)
  291. }
  292. ci.destPath = ci.destPath + filename
  293. }
  294. // Calc the checksum, even if we're using the cache
  295. r, err := archive.Tar(tmpFileName, archive.Uncompressed)
  296. if err != nil {
  297. return err
  298. }
  299. tarSum, err := tarsum.NewTarSum(r, true, tarsum.Version1)
  300. if err != nil {
  301. return err
  302. }
  303. if _, err := io.Copy(ioutil.Discard, tarSum); err != nil {
  304. return err
  305. }
  306. ci.hash = tarSum.Sum(nil)
  307. r.Close()
  308. return nil
  309. }
  310. // Deal with wildcards
  311. if allowWildcards && ContainsWildcards(origPath) {
  312. for _, fileInfo := range b.context.GetSums() {
  313. if fileInfo.Name() == "" {
  314. continue
  315. }
  316. match, _ := filepath.Match(origPath, fileInfo.Name())
  317. if !match {
  318. continue
  319. }
  320. // Note we set allowWildcards to false in case the name has
  321. // a * in it
  322. calcCopyInfo(b, cmdName, cInfos, fileInfo.Name(), destPath, allowRemote, allowDecompression, false)
  323. }
  324. return nil
  325. }
  326. // Must be a dir or a file
  327. if err := b.checkPathForAddition(origPath); err != nil {
  328. return err
  329. }
  330. fi, _ := os.Stat(filepath.Join(b.contextPath, origPath))
  331. ci := copyInfo{}
  332. ci.origPath = origPath
  333. ci.hash = origPath
  334. ci.destPath = destPath
  335. ci.decompress = allowDecompression
  336. *cInfos = append(*cInfos, &ci)
  337. // Deal with the single file case
  338. if !fi.IsDir() {
  339. // This will match first file in sums of the archive
  340. fis := b.context.GetSums().GetFile(ci.origPath)
  341. if fis != nil {
  342. ci.hash = "file:" + fis.Sum()
  343. }
  344. return nil
  345. }
  346. // Must be a dir
  347. var subfiles []string
  348. absOrigPath := filepath.Join(b.contextPath, ci.origPath)
  349. // Add a trailing / to make sure we only pick up nested files under
  350. // the dir and not sibling files of the dir that just happen to
  351. // start with the same chars
  352. if !strings.HasSuffix(absOrigPath, "/") {
  353. absOrigPath += "/"
  354. }
  355. // Need path w/o / too to find matching dir w/o trailing /
  356. absOrigPathNoSlash := absOrigPath[:len(absOrigPath)-1]
  357. for _, fileInfo := range b.context.GetSums() {
  358. absFile := filepath.Join(b.contextPath, fileInfo.Name())
  359. // Any file in the context that starts with the given path will be
  360. // picked up and its hashcode used. However, we'll exclude the
  361. // root dir itself. We do this for a coupel of reasons:
  362. // 1 - ADD/COPY will not copy the dir itself, just its children
  363. // so there's no reason to include it in the hash calc
  364. // 2 - the metadata on the dir will change when any child file
  365. // changes. This will lead to a miss in the cache check if that
  366. // child file is in the .dockerignore list.
  367. if strings.HasPrefix(absFile, absOrigPath) && absFile != absOrigPathNoSlash {
  368. subfiles = append(subfiles, fileInfo.Sum())
  369. }
  370. }
  371. sort.Strings(subfiles)
  372. hasher := sha256.New()
  373. hasher.Write([]byte(strings.Join(subfiles, ",")))
  374. ci.hash = "dir:" + hex.EncodeToString(hasher.Sum(nil))
  375. return nil
  376. }
  377. func ContainsWildcards(name string) bool {
  378. for i := 0; i < len(name); i++ {
  379. ch := name[i]
  380. if ch == '\\' {
  381. i++
  382. } else if ch == '*' || ch == '?' || ch == '[' {
  383. return true
  384. }
  385. }
  386. return false
  387. }
  388. func (b *Builder) pullImage(name string) (*imagepkg.Image, error) {
  389. remote, tag := parsers.ParseRepositoryTag(name)
  390. if tag == "" {
  391. tag = "latest"
  392. }
  393. pullRegistryAuth := b.AuthConfig
  394. if len(b.ConfigFile.AuthConfigs) > 0 {
  395. // The request came with a full auth config file, we prefer to use that
  396. repoInfo, err := b.Daemon.RegistryService.ResolveRepository(remote)
  397. if err != nil {
  398. return nil, err
  399. }
  400. resolvedAuth := registry.ResolveAuthConfig(b.ConfigFile, repoInfo.Index)
  401. pullRegistryAuth = &resolvedAuth
  402. }
  403. imagePullConfig := &graph.ImagePullConfig{
  404. AuthConfig: pullRegistryAuth,
  405. OutStream: ioutils.NopWriteCloser(b.OutOld),
  406. }
  407. if err := b.Daemon.Repositories().Pull(remote, tag, imagePullConfig); err != nil {
  408. return nil, err
  409. }
  410. image, err := b.Daemon.Repositories().LookupImage(name)
  411. if err != nil {
  412. return nil, err
  413. }
  414. return image, nil
  415. }
  416. func (b *Builder) processImageFrom(img *imagepkg.Image) error {
  417. b.image = img.ID
  418. if img.Config != nil {
  419. b.Config = img.Config
  420. }
  421. if len(b.Config.Env) == 0 {
  422. b.Config.Env = append(b.Config.Env, "PATH="+daemon.DefaultPathEnv)
  423. }
  424. // Process ONBUILD triggers if they exist
  425. if nTriggers := len(b.Config.OnBuild); nTriggers != 0 {
  426. fmt.Fprintf(b.ErrStream, "# Executing %d build triggers\n", nTriggers)
  427. }
  428. // Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
  429. onBuildTriggers := b.Config.OnBuild
  430. b.Config.OnBuild = []string{}
  431. // parse the ONBUILD triggers by invoking the parser
  432. for stepN, step := range onBuildTriggers {
  433. ast, err := parser.Parse(strings.NewReader(step))
  434. if err != nil {
  435. return err
  436. }
  437. for i, n := range ast.Children {
  438. switch strings.ToUpper(n.Value) {
  439. case "ONBUILD":
  440. return fmt.Errorf("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
  441. case "MAINTAINER", "FROM":
  442. return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", n.Value)
  443. }
  444. fmt.Fprintf(b.OutStream, "Trigger %d, %s\n", stepN, step)
  445. if err := b.dispatch(i, n); err != nil {
  446. return err
  447. }
  448. }
  449. }
  450. return nil
  451. }
  452. // probeCache checks to see if image-caching is enabled (`b.UtilizeCache`)
  453. // and if so attempts to look up the current `b.image` and `b.Config` pair
  454. // in the current server `b.Daemon`. If an image is found, probeCache returns
  455. // `(true, nil)`. If no image is found, it returns `(false, nil)`. If there
  456. // is any error, it returns `(false, err)`.
  457. func (b *Builder) probeCache() (bool, error) {
  458. if !b.UtilizeCache || b.cacheBusted {
  459. return false, nil
  460. }
  461. cache, err := b.Daemon.ImageGetCached(b.image, b.Config)
  462. if err != nil {
  463. return false, err
  464. }
  465. if cache == nil {
  466. logrus.Debugf("[BUILDER] Cache miss")
  467. b.cacheBusted = true
  468. return false, nil
  469. }
  470. fmt.Fprintf(b.OutStream, " ---> Using cache\n")
  471. logrus.Debugf("[BUILDER] Use cached version")
  472. b.image = cache.ID
  473. return true, nil
  474. }
  475. func (b *Builder) create() (*daemon.Container, error) {
  476. if b.image == "" && !b.noBaseImage {
  477. return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
  478. }
  479. b.Config.Image = b.image
  480. hostConfig := &runconfig.HostConfig{
  481. CpuShares: b.cpuShares,
  482. CpuPeriod: b.cpuPeriod,
  483. CpuQuota: b.cpuQuota,
  484. CpusetCpus: b.cpuSetCpus,
  485. CpusetMems: b.cpuSetMems,
  486. CgroupParent: b.cgroupParent,
  487. Memory: b.memory,
  488. MemorySwap: b.memorySwap,
  489. }
  490. config := *b.Config
  491. // Create the container
  492. c, warnings, err := b.Daemon.Create(b.Config, hostConfig, "")
  493. if err != nil {
  494. return nil, err
  495. }
  496. for _, warning := range warnings {
  497. fmt.Fprintf(b.OutStream, " ---> [Warning] %s\n", warning)
  498. }
  499. b.TmpContainers[c.ID] = struct{}{}
  500. fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))
  501. if config.Cmd.Len() > 0 {
  502. // override the entry point that may have been picked up from the base image
  503. s := config.Cmd.Slice()
  504. c.Path = s[0]
  505. c.Args = s[1:]
  506. } else {
  507. config.Cmd = runconfig.NewCommand()
  508. }
  509. return c, nil
  510. }
  511. func (b *Builder) run(c *daemon.Container) error {
  512. var errCh chan error
  513. if b.Verbose {
  514. errCh = c.Attach(nil, b.OutStream, b.ErrStream)
  515. }
  516. //start the container
  517. if err := c.Start(); err != nil {
  518. return err
  519. }
  520. finished := make(chan struct{})
  521. defer close(finished)
  522. go func() {
  523. select {
  524. case <-b.cancelled:
  525. logrus.Debugln("Build cancelled, killing container:", c.ID)
  526. c.Kill()
  527. case <-finished:
  528. }
  529. }()
  530. if b.Verbose {
  531. // Block on reading output from container, stop on err or chan closed
  532. if err := <-errCh; err != nil {
  533. return err
  534. }
  535. }
  536. // Wait for it to finish
  537. if ret, _ := c.WaitStop(-1 * time.Second); ret != 0 {
  538. return &jsonmessage.JSONError{
  539. Message: fmt.Sprintf("The command '%s' returned a non-zero code: %d", b.Config.Cmd.ToString(), ret),
  540. Code: ret,
  541. }
  542. }
  543. return nil
  544. }
  545. func (b *Builder) checkPathForAddition(orig string) error {
  546. origPath := filepath.Join(b.contextPath, orig)
  547. origPath, err := filepath.EvalSymlinks(origPath)
  548. if err != nil {
  549. if os.IsNotExist(err) {
  550. return fmt.Errorf("%s: no such file or directory", orig)
  551. }
  552. return err
  553. }
  554. contextPath, err := filepath.EvalSymlinks(b.contextPath)
  555. if err != nil {
  556. return err
  557. }
  558. if !strings.HasPrefix(origPath, contextPath) {
  559. return fmt.Errorf("Forbidden path outside the build context: %s (%s)", orig, origPath)
  560. }
  561. if _, err := os.Stat(origPath); err != nil {
  562. if os.IsNotExist(err) {
  563. return fmt.Errorf("%s: no such file or directory", orig)
  564. }
  565. return err
  566. }
  567. return nil
  568. }
  569. func (b *Builder) addContext(container *daemon.Container, orig, dest string, decompress bool) error {
  570. var (
  571. err error
  572. destExists = true
  573. origPath = filepath.Join(b.contextPath, orig)
  574. destPath string
  575. )
  576. destPath, err = container.GetResourcePath(dest)
  577. if err != nil {
  578. return err
  579. }
  580. // Preserve the trailing '/'
  581. if strings.HasSuffix(dest, "/") || dest == "." {
  582. destPath = destPath + "/"
  583. }
  584. destStat, err := os.Stat(destPath)
  585. if err != nil {
  586. if !os.IsNotExist(err) {
  587. return err
  588. }
  589. destExists = false
  590. }
  591. fi, err := os.Stat(origPath)
  592. if err != nil {
  593. if os.IsNotExist(err) {
  594. return fmt.Errorf("%s: no such file or directory", orig)
  595. }
  596. return err
  597. }
  598. if fi.IsDir() {
  599. return copyAsDirectory(origPath, destPath, destExists)
  600. }
  601. // If we are adding a remote file (or we've been told not to decompress), do not try to untar it
  602. if decompress {
  603. // First try to unpack the source as an archive
  604. // to support the untar feature we need to clean up the path a little bit
  605. // because tar is very forgiving. First we need to strip off the archive's
  606. // filename from the path but this is only added if it does not end in / .
  607. tarDest := destPath
  608. if strings.HasSuffix(tarDest, "/") {
  609. tarDest = filepath.Dir(destPath)
  610. }
  611. // try to successfully untar the orig
  612. if err := chrootarchive.UntarPath(origPath, tarDest); err == nil {
  613. return nil
  614. } else if err != io.EOF {
  615. logrus.Debugf("Couldn't untar %s to %s: %s", origPath, tarDest, err)
  616. }
  617. }
  618. if err := system.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
  619. return err
  620. }
  621. if err := chrootarchive.CopyWithTar(origPath, destPath); err != nil {
  622. return err
  623. }
  624. resPath := destPath
  625. if destExists && destStat.IsDir() {
  626. resPath = filepath.Join(destPath, filepath.Base(origPath))
  627. }
  628. return fixPermissions(origPath, resPath, 0, 0, destExists)
  629. }
  630. func copyAsDirectory(source, destination string, destExisted bool) error {
  631. if err := chrootarchive.CopyWithTar(source, destination); err != nil {
  632. return err
  633. }
  634. return fixPermissions(source, destination, 0, 0, destExisted)
  635. }
  636. func (b *Builder) clearTmp() {
  637. for c := range b.TmpContainers {
  638. rmConfig := &daemon.ContainerRmConfig{
  639. ForceRemove: true,
  640. RemoveVolume: true,
  641. }
  642. if err := b.Daemon.ContainerRm(c, rmConfig); err != nil {
  643. fmt.Fprintf(b.OutStream, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err)
  644. return
  645. }
  646. delete(b.TmpContainers, c)
  647. fmt.Fprintf(b.OutStream, "Removing intermediate container %s\n", stringid.TruncateID(c))
  648. }
  649. }