archive_windows.go 11 KB

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