2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/container"
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-09-22 20:49:47 -04:00
|
|
|
"path/filepath"
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2016-04-26 22:26:12 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2018-03-13 22:45:21 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-11-12 14:55:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResolvePath resolves the given path in the container to a resource on the
|
|
|
|
// host. Returns a resolved path (absolute path to the resource on the host),
|
|
|
|
// the absolute path to the resource relative to the container's rootfs, and
|
2016-03-26 10:06:45 -04:00
|
|
|
// an error if the path points to outside the container's rootfs.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (container *Container) ResolvePath(path string) (resolvedPath, absPath string, err error) {
|
2022-09-23 12:25:19 -04:00
|
|
|
if container.BaseFS == "" {
|
|
|
|
return "", "", errors.New("ResolvePath: BaseFS of container " + container.ID + " is unexpectedly empty")
|
2018-03-13 22:45:21 -04:00
|
|
|
}
|
2016-04-26 22:26:12 -04:00
|
|
|
// Check if a drive letter supplied, it must be the system drive. No-op except on Windows
|
2022-09-22 20:49:47 -04:00
|
|
|
path, err = system.CheckSystemDriveAndRemoveDriveLetter(path)
|
2016-04-26 22:26:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// Consider the given path as an absolute path in the container.
|
2022-09-22 20:49:47 -04:00
|
|
|
absPath = archive.PreserveTrailingDotOrSeparator(filepath.Join(string(filepath.Separator), path), path)
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
// Split the absPath into its Directory and Base components. We will
|
|
|
|
// resolve the dir in the scope of the container then append the base.
|
2022-09-22 20:49:47 -04:00
|
|
|
dirPath, basePath := filepath.Split(absPath)
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
resolvedDirPath, err := container.GetResourcePath(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolvedDirPath will have been cleaned (no trailing path separators) so
|
|
|
|
// we can manually join it with the base path element.
|
2022-09-22 20:49:47 -04:00
|
|
|
resolvedPath = resolvedDirPath + string(filepath.Separator) + basePath
|
2015-11-12 14:55:17 -05:00
|
|
|
return resolvedPath, absPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatPath is the unexported version of StatPath. Locks and mounts should
|
|
|
|
// be acquired before calling this method and the given path should be fully
|
|
|
|
// resolved to a path on the host corresponding to the given absolute path
|
|
|
|
// inside the container.
|
|
|
|
func (container *Container) StatPath(resolvedPath, absPath string) (stat *types.ContainerPathStat, err error) {
|
2022-09-23 12:25:19 -04:00
|
|
|
if container.BaseFS == "" {
|
|
|
|
return nil, errors.New("StatPath: BaseFS of container " + container.ID + " is unexpectedly empty")
|
2018-03-13 22:45:21 -04:00
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
|
2022-09-22 20:59:58 -04:00
|
|
|
lstat, err := os.Lstat(resolvedPath)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var linkTarget string
|
|
|
|
if lstat.Mode()&os.ModeSymlink != 0 {
|
|
|
|
// Fully evaluate the symlink in the scope of the container rootfs.
|
|
|
|
hostPath, err := container.GetResourcePath(absPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 14:09:51 -04:00
|
|
|
linkTarget, err = filepath.Rel(container.BaseFS, hostPath)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make it an absolute path.
|
2022-09-22 20:49:47 -04:00
|
|
|
linkTarget = filepath.Join(string(filepath.Separator), linkTarget)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &types.ContainerPathStat{
|
2022-09-22 20:49:47 -04:00
|
|
|
Name: filepath.Base(absPath),
|
2015-11-12 14:55:17 -05:00
|
|
|
Size: lstat.Size(),
|
|
|
|
Mode: lstat.Mode(),
|
|
|
|
Mtime: lstat.ModTime(),
|
|
|
|
LinkTarget: linkTarget,
|
|
|
|
}, nil
|
|
|
|
}
|