archive.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package container // import "github.com/docker/docker/container"
  2. import (
  3. "os"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/pkg/archive"
  6. "github.com/docker/docker/pkg/system"
  7. "github.com/pkg/errors"
  8. )
  9. // ResolvePath resolves the given path in the container to a resource on the
  10. // host. Returns a resolved path (absolute path to the resource on the host),
  11. // the absolute path to the resource relative to the container's rootfs, and
  12. // an error if the path points to outside the container's rootfs.
  13. func (container *Container) ResolvePath(path string) (resolvedPath, absPath string, err error) {
  14. if container.BaseFS == nil {
  15. return "", "", errors.New("ResolvePath: BaseFS of container " + container.ID + " is unexpectedly nil")
  16. }
  17. // Check if a drive letter supplied, it must be the system drive. No-op except on Windows
  18. path, err = system.CheckSystemDriveAndRemoveDriveLetter(path, container.BaseFS)
  19. if err != nil {
  20. return "", "", err
  21. }
  22. // Consider the given path as an absolute path in the container.
  23. absPath = archive.PreserveTrailingDotOrSeparator(
  24. container.BaseFS.Join(string(container.BaseFS.Separator()), path),
  25. path,
  26. container.BaseFS.Separator())
  27. // Split the absPath into its Directory and Base components. We will
  28. // resolve the dir in the scope of the container then append the base.
  29. dirPath, basePath := container.BaseFS.Split(absPath)
  30. resolvedDirPath, err := container.GetResourcePath(dirPath)
  31. if err != nil {
  32. return "", "", err
  33. }
  34. // resolvedDirPath will have been cleaned (no trailing path separators) so
  35. // we can manually join it with the base path element.
  36. resolvedPath = resolvedDirPath + string(container.BaseFS.Separator()) + basePath
  37. return resolvedPath, absPath, nil
  38. }
  39. // StatPath is the unexported version of StatPath. Locks and mounts should
  40. // be acquired before calling this method and the given path should be fully
  41. // resolved to a path on the host corresponding to the given absolute path
  42. // inside the container.
  43. func (container *Container) StatPath(resolvedPath, absPath string) (stat *types.ContainerPathStat, err error) {
  44. if container.BaseFS == nil {
  45. return nil, errors.New("StatPath: BaseFS of container " + container.ID + " is unexpectedly nil")
  46. }
  47. driver := container.BaseFS
  48. lstat, err := driver.Lstat(resolvedPath)
  49. if err != nil {
  50. return nil, err
  51. }
  52. var linkTarget string
  53. if lstat.Mode()&os.ModeSymlink != 0 {
  54. // Fully evaluate the symlink in the scope of the container rootfs.
  55. hostPath, err := container.GetResourcePath(absPath)
  56. if err != nil {
  57. return nil, err
  58. }
  59. linkTarget, err = driver.Rel(driver.Path(), hostPath)
  60. if err != nil {
  61. return nil, err
  62. }
  63. // Make it an absolute path.
  64. linkTarget = driver.Join(string(driver.Separator()), linkTarget)
  65. }
  66. return &types.ContainerPathStat{
  67. Name: driver.Base(absPath),
  68. Size: lstat.Size(),
  69. Mode: lstat.Mode(),
  70. Mtime: lstat.ModTime(),
  71. LinkTarget: linkTarget,
  72. }, nil
  73. }