cp.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "io"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "golang.org/x/sync/errgroup"
  23. "github.com/docker/cli/cli/command"
  24. "github.com/docker/compose/v2/pkg/api"
  25. "github.com/docker/docker/api/types/container"
  26. "github.com/moby/go-archive"
  27. )
  28. type copyDirection int
  29. const (
  30. fromService copyDirection = 1 << iota
  31. toService
  32. acrossServices = fromService | toService
  33. )
  34. func (s *composeService) Copy(ctx context.Context, projectName string, options api.CopyOptions) error {
  35. return Run(ctx, func(ctx context.Context) error {
  36. return s.copy(ctx, projectName, options)
  37. }, "copy", s.events)
  38. }
  39. func (s *composeService) copy(ctx context.Context, projectName string, options api.CopyOptions) error {
  40. projectName = strings.ToLower(projectName)
  41. srcService, srcPath := splitCpArg(options.Source)
  42. destService, dstPath := splitCpArg(options.Destination)
  43. var direction copyDirection
  44. var serviceName string
  45. var copyFunc func(ctx context.Context, containerID string, srcPath string, dstPath string, opts api.CopyOptions) error
  46. if srcService != "" {
  47. direction |= fromService
  48. serviceName = srcService
  49. copyFunc = s.copyFromContainer
  50. }
  51. if destService != "" {
  52. direction |= toService
  53. serviceName = destService
  54. copyFunc = s.copyToContainer
  55. }
  56. if direction == acrossServices {
  57. return errors.New("copying between services is not supported")
  58. }
  59. if direction == 0 {
  60. return errors.New("unknown copy direction")
  61. }
  62. containers, err := s.listContainersTargetedForCopy(ctx, projectName, options, direction, serviceName)
  63. if err != nil {
  64. return err
  65. }
  66. g := errgroup.Group{}
  67. for _, cont := range containers {
  68. ctr := cont
  69. g.Go(func() error {
  70. name := getCanonicalContainerName(ctr)
  71. var msg string
  72. if direction == fromService {
  73. msg = fmt.Sprintf("%s:%s to %s", name, srcPath, dstPath)
  74. } else {
  75. msg = fmt.Sprintf("%s to %s:%s", srcPath, name, dstPath)
  76. }
  77. s.events.On(api.Resource{
  78. ID: name,
  79. Text: api.StatusCopying,
  80. Details: msg,
  81. Status: api.Working,
  82. })
  83. if err := copyFunc(ctx, ctr.ID, srcPath, dstPath, options); err != nil {
  84. return err
  85. }
  86. s.events.On(api.Resource{
  87. ID: name,
  88. Text: api.StatusCopied,
  89. Details: msg,
  90. Status: api.Done,
  91. })
  92. return nil
  93. })
  94. }
  95. return g.Wait()
  96. }
  97. func (s *composeService) listContainersTargetedForCopy(ctx context.Context, projectName string, options api.CopyOptions, direction copyDirection, serviceName string) (Containers, error) {
  98. var containers Containers
  99. var err error
  100. switch {
  101. case options.Index > 0:
  102. ctr, err := s.getSpecifiedContainer(ctx, projectName, oneOffExclude, true, serviceName, options.Index)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return append(containers, ctr), nil
  107. default:
  108. withOneOff := oneOffExclude
  109. if options.All {
  110. withOneOff = oneOffInclude
  111. }
  112. containers, err = s.getContainers(ctx, projectName, withOneOff, true, serviceName)
  113. if err != nil {
  114. return nil, err
  115. }
  116. if len(containers) < 1 {
  117. return nil, fmt.Errorf("no container found for service %q", serviceName)
  118. }
  119. if direction == fromService {
  120. return containers[:1], err
  121. }
  122. return containers, err
  123. }
  124. }
  125. func (s *composeService) copyToContainer(ctx context.Context, containerID string, srcPath string, dstPath string, opts api.CopyOptions) error {
  126. var err error
  127. if srcPath != "-" {
  128. // Get an absolute source path.
  129. srcPath, err = resolveLocalPath(srcPath)
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. // Prepare destination copy info by stat-ing the container path.
  135. dstInfo := archive.CopyInfo{Path: dstPath}
  136. dstStat, err := s.apiClient().ContainerStatPath(ctx, containerID, dstPath)
  137. // If the destination is a symbolic link, we should evaluate it.
  138. if err == nil && dstStat.Mode&os.ModeSymlink != 0 {
  139. linkTarget := dstStat.LinkTarget
  140. if !isAbs(linkTarget) {
  141. // Join with the parent directory.
  142. dstParent, _ := archive.SplitPathDirEntry(dstPath)
  143. linkTarget = filepath.Join(dstParent, linkTarget)
  144. }
  145. dstInfo.Path = linkTarget
  146. dstStat, err = s.apiClient().ContainerStatPath(ctx, containerID, linkTarget)
  147. }
  148. // Validate the destination path
  149. if err := command.ValidateOutputPathFileMode(dstStat.Mode); err != nil {
  150. return fmt.Errorf(`destination "%s:%s" must be a directory or a regular file: %w`, containerID, dstPath, err)
  151. }
  152. // Ignore any error and assume that the parent directory of the destination
  153. // path exists, in which case the copy may still succeed. If there is any
  154. // type of conflict (e.g., non-directory overwriting an existing directory
  155. // or vice versa) the extraction will fail. If the destination simply did
  156. // not exist, but the parent directory does, the extraction will still
  157. // succeed.
  158. if err == nil {
  159. dstInfo.Exists, dstInfo.IsDir = true, dstStat.Mode.IsDir()
  160. }
  161. var (
  162. content io.Reader
  163. resolvedDstPath string
  164. )
  165. if srcPath == "-" {
  166. content = s.stdin()
  167. resolvedDstPath = dstInfo.Path
  168. if !dstInfo.IsDir {
  169. return fmt.Errorf("destination \"%s:%s\" must be a directory", containerID, dstPath)
  170. }
  171. } else {
  172. // Prepare source copy info.
  173. srcInfo, err := archive.CopyInfoSourcePath(srcPath, opts.FollowLink)
  174. if err != nil {
  175. return err
  176. }
  177. srcArchive, err := archive.TarResource(srcInfo)
  178. if err != nil {
  179. return err
  180. }
  181. defer srcArchive.Close() //nolint:errcheck
  182. // With the stat info about the local source as well as the
  183. // destination, we have enough information to know whether we need to
  184. // alter the archive that we upload so that when the server extracts
  185. // it to the specified directory in the container we get the desired
  186. // copy behavior.
  187. // See comments in the implementation of `archive.PrepareArchiveCopy`
  188. // for exactly what goes into deciding how and whether the source
  189. // archive needs to be altered for the correct copy behavior when it is
  190. // extracted. This function also infers from the source and destination
  191. // info which directory to extract to, which may be the parent of the
  192. // destination that the user specified.
  193. // Don't create the archive if running in Dry Run mode
  194. if !s.dryRun {
  195. dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
  196. if err != nil {
  197. return err
  198. }
  199. defer preparedArchive.Close() //nolint:errcheck
  200. resolvedDstPath = dstDir
  201. content = preparedArchive
  202. }
  203. }
  204. options := container.CopyToContainerOptions{
  205. AllowOverwriteDirWithFile: false,
  206. CopyUIDGID: opts.CopyUIDGID,
  207. }
  208. return s.apiClient().CopyToContainer(ctx, containerID, resolvedDstPath, content, options)
  209. }
  210. func (s *composeService) copyFromContainer(ctx context.Context, containerID, srcPath, dstPath string, opts api.CopyOptions) error {
  211. var err error
  212. if dstPath != "-" {
  213. // Get an absolute destination path.
  214. dstPath, err = resolveLocalPath(dstPath)
  215. if err != nil {
  216. return err
  217. }
  218. }
  219. if err := command.ValidateOutputPath(dstPath); err != nil {
  220. return err
  221. }
  222. // if client requests to follow symbol link, then must decide target file to be copied
  223. var rebaseName string
  224. if opts.FollowLink {
  225. srcStat, err := s.apiClient().ContainerStatPath(ctx, containerID, srcPath)
  226. // If the destination is a symbolic link, we should follow it.
  227. if err == nil && srcStat.Mode&os.ModeSymlink != 0 {
  228. linkTarget := srcStat.LinkTarget
  229. if !isAbs(linkTarget) {
  230. // Join with the parent directory.
  231. srcParent, _ := archive.SplitPathDirEntry(srcPath)
  232. linkTarget = filepath.Join(srcParent, linkTarget)
  233. }
  234. linkTarget, rebaseName = archive.GetRebaseName(srcPath, linkTarget)
  235. srcPath = linkTarget
  236. }
  237. }
  238. content, stat, err := s.apiClient().CopyFromContainer(ctx, containerID, srcPath)
  239. if err != nil {
  240. return err
  241. }
  242. defer content.Close() //nolint:errcheck
  243. if dstPath == "-" {
  244. _, err = io.Copy(s.stdout(), content)
  245. return err
  246. }
  247. srcInfo := archive.CopyInfo{
  248. Path: srcPath,
  249. Exists: true,
  250. IsDir: stat.Mode.IsDir(),
  251. RebaseName: rebaseName,
  252. }
  253. preArchive := content
  254. if srcInfo.RebaseName != "" {
  255. _, srcBase := archive.SplitPathDirEntry(srcInfo.Path)
  256. preArchive = archive.RebaseArchiveEntries(content, srcBase, srcInfo.RebaseName)
  257. }
  258. return archive.CopyTo(preArchive, srcInfo, dstPath)
  259. }
  260. // IsAbs is a platform-agnostic wrapper for filepath.IsAbs.
  261. //
  262. // On Windows, golang filepath.IsAbs does not consider a path \windows\system32
  263. // as absolute as it doesn't start with a drive-letter/colon combination. However,
  264. // in docker we need to verify things such as WORKDIR /windows/system32 in
  265. // a Dockerfile (which gets translated to \windows\system32 when being processed
  266. // by the daemon). This SHOULD be treated as absolute from a docker processing
  267. // perspective.
  268. func isAbs(path string) bool {
  269. return filepath.IsAbs(path) || strings.HasPrefix(path, string(os.PathSeparator))
  270. }
  271. func splitCpArg(arg string) (ctr, path string) {
  272. if isAbs(arg) {
  273. // Explicit local absolute path, e.g., `C:\foo` or `/foo`.
  274. return "", arg
  275. }
  276. parts := strings.SplitN(arg, ":", 2)
  277. if len(parts) == 1 || strings.HasPrefix(parts[0], ".") {
  278. // Either there's no `:` in the arg
  279. // OR it's an explicit local relative path like `./file:name.txt`.
  280. return "", arg
  281. }
  282. return parts[0], parts[1]
  283. }
  284. func resolveLocalPath(localPath string) (absPath string, err error) {
  285. if absPath, err = filepath.Abs(localPath); err != nil {
  286. return absPath, err
  287. }
  288. return archive.PreserveTrailingDotOrSeparator(absPath, localPath), nil
  289. }