archive.go 11 KB

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