archive.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package daemon
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/docker/docker/container"
  9. "github.com/docker/docker/pkg/archive"
  10. "github.com/docker/docker/pkg/chrootarchive"
  11. "github.com/docker/docker/pkg/ioutils"
  12. "github.com/docker/engine-api/types"
  13. )
  14. // ErrExtractPointNotDirectory is used to convey that the operation to extract
  15. // a tar archive to a directory in a container has failed because the specified
  16. // path does not refer to a directory.
  17. var ErrExtractPointNotDirectory = errors.New("extraction point is not a directory")
  18. // ContainerCopy performs a deprecated operation of archiving the resource at
  19. // the specified path in the container identified by the given name.
  20. func (daemon *Daemon) ContainerCopy(name string, res string) (io.ReadCloser, error) {
  21. container, err := daemon.GetContainer(name)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if res[0] == '/' || res[0] == '\\' {
  26. res = res[1:]
  27. }
  28. return daemon.containerCopy(container, res)
  29. }
  30. // ContainerStatPath stats the filesystem resource at the specified path in the
  31. // container identified by the given name.
  32. func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *types.ContainerPathStat, err error) {
  33. container, err := daemon.GetContainer(name)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return daemon.containerStatPath(container, path)
  38. }
  39. // ContainerArchivePath creates an archive of the filesystem resource at the
  40. // specified path in the container identified by the given name. Returns a
  41. // tar archive of the resource and whether it was a directory or a single file.
  42. func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
  43. container, err := daemon.GetContainer(name)
  44. if err != nil {
  45. return nil, nil, err
  46. }
  47. return daemon.containerArchivePath(container, path)
  48. }
  49. // ContainerExtractToDir extracts the given archive to the specified location
  50. // in the filesystem of the container identified by the given name. The given
  51. // path must be of a directory in the container. If it is not, the error will
  52. // be ErrExtractPointNotDirectory. If noOverwriteDirNonDir is true then it will
  53. // be an error if unpacking the given content would cause an existing directory
  54. // to be replaced with a non-directory and vice versa.
  55. func (daemon *Daemon) ContainerExtractToDir(name, path string, noOverwriteDirNonDir bool, content io.Reader) error {
  56. container, err := daemon.GetContainer(name)
  57. if err != nil {
  58. return err
  59. }
  60. return daemon.containerExtractToDir(container, path, noOverwriteDirNonDir, content)
  61. }
  62. // containerStatPath stats the filesystem resource at the specified path in this
  63. // container. Returns stat info about the resource.
  64. func (daemon *Daemon) containerStatPath(container *container.Container, path string) (stat *types.ContainerPathStat, err error) {
  65. container.Lock()
  66. defer container.Unlock()
  67. if err = daemon.Mount(container); err != nil {
  68. return nil, err
  69. }
  70. defer daemon.Unmount(container)
  71. err = daemon.mountVolumes(container)
  72. defer container.UnmountVolumes(true, daemon.LogVolumeEvent)
  73. if err != nil {
  74. return nil, err
  75. }
  76. resolvedPath, absPath, err := container.ResolvePath(path)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return container.StatPath(resolvedPath, absPath)
  81. }
  82. // containerArchivePath creates an archive of the filesystem resource at the specified
  83. // path in this container. Returns a tar archive of the resource and stat info
  84. // about the resource.
  85. func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
  86. container.Lock()
  87. defer func() {
  88. if err != nil {
  89. // Wait to unlock the container until the archive is fully read
  90. // (see the ReadCloseWrapper func below) or if there is an error
  91. // before that occurs.
  92. container.Unlock()
  93. }
  94. }()
  95. if err = daemon.Mount(container); err != nil {
  96. return nil, nil, err
  97. }
  98. defer func() {
  99. if err != nil {
  100. // unmount any volumes
  101. container.UnmountVolumes(true, daemon.LogVolumeEvent)
  102. // unmount the container's rootfs
  103. daemon.Unmount(container)
  104. }
  105. }()
  106. if err = daemon.mountVolumes(container); err != nil {
  107. return nil, nil, err
  108. }
  109. resolvedPath, absPath, err := container.ResolvePath(path)
  110. if err != nil {
  111. return nil, nil, err
  112. }
  113. stat, err = container.StatPath(resolvedPath, absPath)
  114. if err != nil {
  115. return nil, nil, err
  116. }
  117. // We need to rebase the archive entries if the last element of the
  118. // resolved path was a symlink that was evaluated and is now different
  119. // than the requested path. For example, if the given path was "/foo/bar/",
  120. // but it resolved to "/var/lib/docker/containers/{id}/foo/baz/", we want
  121. // to ensure that the archive entries start with "bar" and not "baz". This
  122. // also catches the case when the root directory of the container is
  123. // requested: we want the archive entries to start with "/" and not the
  124. // container ID.
  125. data, err := archive.TarResourceRebase(resolvedPath, filepath.Base(absPath))
  126. if err != nil {
  127. return nil, nil, err
  128. }
  129. content = ioutils.NewReadCloserWrapper(data, func() error {
  130. err := data.Close()
  131. container.UnmountVolumes(true, daemon.LogVolumeEvent)
  132. daemon.Unmount(container)
  133. container.Unlock()
  134. return err
  135. })
  136. daemon.LogContainerEvent(container, "archive-path")
  137. return content, stat, nil
  138. }
  139. // containerExtractToDir extracts the given tar archive to the specified location in the
  140. // filesystem of this container. The given path must be of a directory in the
  141. // container. If it is not, the error will be ErrExtractPointNotDirectory. If
  142. // noOverwriteDirNonDir is true then it will be an error if unpacking the
  143. // given content would cause an existing directory to be replaced with a non-
  144. // directory and vice versa.
  145. func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, noOverwriteDirNonDir bool, content io.Reader) (err error) {
  146. container.Lock()
  147. defer container.Unlock()
  148. if err = daemon.Mount(container); err != nil {
  149. return err
  150. }
  151. defer daemon.Unmount(container)
  152. err = daemon.mountVolumes(container)
  153. defer container.UnmountVolumes(true, daemon.LogVolumeEvent)
  154. if err != nil {
  155. return err
  156. }
  157. // The destination path needs to be resolved to a host path, with all
  158. // symbolic links followed in the scope of the container's rootfs. Note
  159. // that we do not use `container.ResolvePath(path)` here because we need
  160. // to also evaluate the last path element if it is a symlink. This is so
  161. // that you can extract an archive to a symlink that points to a directory.
  162. // Consider the given path as an absolute path in the container.
  163. absPath := archive.PreserveTrailingDotOrSeparator(filepath.Join(string(filepath.Separator), path), path)
  164. // This will evaluate the last path element if it is a symlink.
  165. resolvedPath, err := container.GetResourcePath(absPath)
  166. if err != nil {
  167. return err
  168. }
  169. stat, err := os.Lstat(resolvedPath)
  170. if err != nil {
  171. return err
  172. }
  173. if !stat.IsDir() {
  174. return ErrExtractPointNotDirectory
  175. }
  176. // Need to check if the path is in a volume. If it is, it cannot be in a
  177. // read-only volume. If it is not in a volume, the container cannot be
  178. // configured with a read-only rootfs.
  179. // Use the resolved path relative to the container rootfs as the new
  180. // absPath. This way we fully follow any symlinks in a volume that may
  181. // lead back outside the volume.
  182. //
  183. // The Windows implementation of filepath.Rel in golang 1.4 does not
  184. // support volume style file path semantics. On Windows when using the
  185. // filter driver, we are guaranteed that the path will always be
  186. // a volume file path.
  187. var baseRel string
  188. if strings.HasPrefix(resolvedPath, `\\?\Volume{`) {
  189. if strings.HasPrefix(resolvedPath, container.BaseFS) {
  190. baseRel = resolvedPath[len(container.BaseFS):]
  191. if baseRel[:1] == `\` {
  192. baseRel = baseRel[1:]
  193. }
  194. }
  195. } else {
  196. baseRel, err = filepath.Rel(container.BaseFS, resolvedPath)
  197. }
  198. if err != nil {
  199. return err
  200. }
  201. // Make it an absolute path.
  202. absPath = filepath.Join(string(filepath.Separator), baseRel)
  203. toVolume, err := checkIfPathIsInAVolume(container, absPath)
  204. if err != nil {
  205. return err
  206. }
  207. if !toVolume && container.HostConfig.ReadonlyRootfs {
  208. return ErrRootFSReadOnly
  209. }
  210. uid, gid := daemon.GetRemappedUIDGID()
  211. options := &archive.TarOptions{
  212. NoOverwriteDirNonDir: noOverwriteDirNonDir,
  213. ChownOpts: &archive.TarChownOptions{
  214. UID: uid, GID: gid, // TODO: should all ownership be set to root (either real or remapped)?
  215. },
  216. }
  217. if err := chrootarchive.Untar(content, resolvedPath, options); err != nil {
  218. return err
  219. }
  220. daemon.LogContainerEvent(container, "extract-to-dir")
  221. return nil
  222. }
  223. func (daemon *Daemon) containerCopy(container *container.Container, resource string) (rc io.ReadCloser, err error) {
  224. container.Lock()
  225. defer func() {
  226. if err != nil {
  227. // Wait to unlock the container until the archive is fully read
  228. // (see the ReadCloseWrapper func below) or if there is an error
  229. // before that occurs.
  230. container.Unlock()
  231. }
  232. }()
  233. if err := daemon.Mount(container); err != nil {
  234. return nil, err
  235. }
  236. defer func() {
  237. if err != nil {
  238. // unmount any volumes
  239. container.UnmountVolumes(true, daemon.LogVolumeEvent)
  240. // unmount the container's rootfs
  241. daemon.Unmount(container)
  242. }
  243. }()
  244. if err := daemon.mountVolumes(container); err != nil {
  245. return nil, err
  246. }
  247. basePath, err := container.GetResourcePath(resource)
  248. if err != nil {
  249. return nil, err
  250. }
  251. stat, err := os.Stat(basePath)
  252. if err != nil {
  253. return nil, err
  254. }
  255. var filter []string
  256. if !stat.IsDir() {
  257. d, f := filepath.Split(basePath)
  258. basePath = d
  259. filter = []string{f}
  260. } else {
  261. filter = []string{filepath.Base(basePath)}
  262. basePath = filepath.Dir(basePath)
  263. }
  264. archive, err := archive.TarWithOptions(basePath, &archive.TarOptions{
  265. Compression: archive.Uncompressed,
  266. IncludeFiles: filter,
  267. })
  268. if err != nil {
  269. return nil, err
  270. }
  271. reader := ioutils.NewReadCloserWrapper(archive, func() error {
  272. err := archive.Close()
  273. container.UnmountVolumes(true, daemon.LogVolumeEvent)
  274. daemon.Unmount(container)
  275. container.Unlock()
  276. return err
  277. })
  278. daemon.LogContainerEvent(container, "copy")
  279. return reader, nil
  280. }