archive_unix.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //go:build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "context"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/errdefs"
  11. "github.com/docker/docker/pkg/archive"
  12. "github.com/docker/docker/pkg/ioutils"
  13. volumemounts "github.com/docker/docker/volume/mounts"
  14. "github.com/pkg/errors"
  15. )
  16. // containerStatPath stats the filesystem resource at the specified path in this
  17. // container. Returns stat info about the resource.
  18. func (daemon *Daemon) containerStatPath(container *container.Container, path string) (stat *types.ContainerPathStat, err error) {
  19. container.Lock()
  20. defer container.Unlock()
  21. cfs, err := daemon.openContainerFS(container)
  22. if err != nil {
  23. return nil, err
  24. }
  25. defer cfs.Close()
  26. return cfs.Stat(context.TODO(), path)
  27. }
  28. // containerArchivePath creates an archive of the filesystem resource at the specified
  29. // path in this container. Returns a tar archive of the resource and stat info
  30. // about the resource.
  31. func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
  32. container.Lock()
  33. defer func() {
  34. if err != nil {
  35. // Wait to unlock the container until the archive is fully read
  36. // (see the ReadCloseWrapper func below) or if there is an error
  37. // before that occurs.
  38. container.Unlock()
  39. }
  40. }()
  41. cfs, err := daemon.openContainerFS(container)
  42. if err != nil {
  43. return nil, nil, err
  44. }
  45. defer func() {
  46. if err != nil {
  47. cfs.Close()
  48. }
  49. }()
  50. absPath := archive.PreserveTrailingDotOrSeparator(filepath.Join("/", path), path)
  51. stat, err = cfs.Stat(context.TODO(), absPath)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. sourceDir, sourceBase := absPath, "."
  56. if stat.Mode&os.ModeDir == 0 { // not dir
  57. sourceDir, sourceBase = filepath.Split(absPath)
  58. }
  59. opts := archive.TarResourceRebaseOpts(sourceBase, filepath.Base(absPath))
  60. tb, err := archive.NewTarballer(sourceDir, opts)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. cfs.GoInFS(context.TODO(), tb.Do)
  65. data := tb.Reader()
  66. content = ioutils.NewReadCloserWrapper(data, func() error {
  67. err := data.Close()
  68. _ = cfs.Close()
  69. container.Unlock()
  70. return err
  71. })
  72. daemon.LogContainerEvent(container, "archive-path")
  73. return content, stat, nil
  74. }
  75. // containerExtractToDir extracts the given tar archive to the specified location in the
  76. // filesystem of this container. The given path must be of a directory in the
  77. // container. If it is not, the error will be an errdefs.InvalidParameter. If
  78. // noOverwriteDirNonDir is true then it will be an error if unpacking the
  79. // given content would cause an existing directory to be replaced with a non-
  80. // directory and vice versa.
  81. func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) (err error) {
  82. container.Lock()
  83. defer container.Unlock()
  84. cfs, err := daemon.openContainerFS(container)
  85. if err != nil {
  86. return err
  87. }
  88. defer cfs.Close()
  89. err = cfs.RunInFS(context.TODO(), func() error {
  90. // The destination path needs to be resolved with all symbolic links
  91. // followed. Note that we need to also evaluate the last path element if
  92. // it is a symlink. This is so that you can extract an archive to a
  93. // symlink that points to a directory.
  94. absPath, err := filepath.EvalSymlinks(filepath.Join("/", path))
  95. if err != nil {
  96. return err
  97. }
  98. absPath = archive.PreserveTrailingDotOrSeparator(absPath, path)
  99. stat, err := os.Lstat(absPath)
  100. if err != nil {
  101. return err
  102. }
  103. if !stat.IsDir() {
  104. return errdefs.InvalidParameter(errors.New("extraction point is not a directory"))
  105. }
  106. // Need to check if the path is in a volume. If it is, it cannot be in a
  107. // read-only volume. If it is not in a volume, the container cannot be
  108. // configured with a read-only rootfs.
  109. toVolume, err := checkIfPathIsInAVolume(container, absPath)
  110. if err != nil {
  111. return err
  112. }
  113. if !toVolume && container.HostConfig.ReadonlyRootfs {
  114. return errdefs.InvalidParameter(errors.New("container rootfs is marked read-only"))
  115. }
  116. options := daemon.defaultTarCopyOptions(noOverwriteDirNonDir)
  117. if copyUIDGID {
  118. var err error
  119. // tarCopyOptions will appropriately pull in the right uid/gid for the
  120. // user/group and will set the options.
  121. options, err = daemon.tarCopyOptions(container, noOverwriteDirNonDir)
  122. if err != nil {
  123. return err
  124. }
  125. }
  126. return archive.Untar(content, absPath, options)
  127. })
  128. if err != nil {
  129. return err
  130. }
  131. daemon.LogContainerEvent(container, "extract-to-dir")
  132. return nil
  133. }
  134. func (daemon *Daemon) containerCopy(container *container.Container, resource string) (rc io.ReadCloser, err error) {
  135. container.Lock()
  136. defer func() {
  137. if err != nil {
  138. // Wait to unlock the container until the archive is fully read
  139. // (see the ReadCloseWrapper func below) or if there is an error
  140. // before that occurs.
  141. container.Unlock()
  142. }
  143. }()
  144. cfs, err := daemon.openContainerFS(container)
  145. if err != nil {
  146. return nil, err
  147. }
  148. defer func() {
  149. if err != nil {
  150. cfs.Close()
  151. }
  152. }()
  153. err = cfs.RunInFS(context.TODO(), func() error {
  154. _, err := os.Stat(resource)
  155. return err
  156. })
  157. if err != nil {
  158. return nil, err
  159. }
  160. tb, err := archive.NewTarballer(resource, &archive.TarOptions{
  161. Compression: archive.Uncompressed,
  162. })
  163. if err != nil {
  164. return nil, err
  165. }
  166. cfs.GoInFS(context.TODO(), tb.Do)
  167. archv := tb.Reader()
  168. reader := ioutils.NewReadCloserWrapper(archv, func() error {
  169. err := archv.Close()
  170. _ = cfs.Close()
  171. container.Unlock()
  172. return err
  173. })
  174. daemon.LogContainerEvent(container, "copy")
  175. return reader, nil
  176. }
  177. // checkIfPathIsInAVolume checks if the path is in a volume. If it is, it
  178. // cannot be in a read-only volume. If it is not in a volume, the container
  179. // cannot be configured with a read-only rootfs.
  180. func checkIfPathIsInAVolume(container *container.Container, absPath string) (bool, error) {
  181. var toVolume bool
  182. parser := volumemounts.NewParser()
  183. for _, mnt := range container.MountPoints {
  184. if toVolume = parser.HasResource(mnt, absPath); toVolume {
  185. if mnt.RW {
  186. break
  187. }
  188. return false, errdefs.InvalidParameter(errors.New("mounted volume is marked read-only"))
  189. }
  190. }
  191. return toVolume, nil
  192. }