archive_windows.go 11 KB

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