archive_unix.go 6.3 KB

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