github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/pkg/containerfs/containerfs.go (about)

     1  package containerfs // import "github.com/docker/docker/pkg/containerfs"
     2  
     3  import (
     4  	"path/filepath"
     5  	"runtime"
     6  
     7  	"github.com/containerd/continuity/driver"
     8  	"github.com/containerd/continuity/pathdriver"
     9  	"github.com/moby/sys/symlink"
    10  )
    11  
    12  // ContainerFS is that represents a root file system
    13  type ContainerFS interface {
    14  	// Path returns the path to the root. Note that this may not exist
    15  	// on the local system, so the continuity operations must be used
    16  	Path() string
    17  
    18  	// ResolveScopedPath evaluates the given path scoped to the root.
    19  	// For example, if root=/a, and path=/b/c, then this function would return /a/b/c.
    20  	// If rawPath is true, then the function will not preform any modifications
    21  	// before path resolution. Otherwise, the function will clean the given path
    22  	// by making it an absolute path.
    23  	ResolveScopedPath(path string, rawPath bool) (string, error)
    24  
    25  	Driver
    26  }
    27  
    28  // Driver combines both continuity's Driver and PathDriver interfaces with a Platform
    29  // field to determine the OS.
    30  type Driver interface {
    31  	// OS returns the OS where the rootfs is located. Essentially, runtime.GOOS.
    32  	OS() string
    33  
    34  	// Architecture returns the hardware architecture where the
    35  	// container is located.
    36  	Architecture() string
    37  
    38  	// Driver & PathDriver provide methods to manipulate files & paths
    39  	driver.Driver
    40  	pathdriver.PathDriver
    41  }
    42  
    43  // NewLocalContainerFS is a helper function to implement daemon's Mount interface
    44  // when the graphdriver mount point is a local path on the machine.
    45  func NewLocalContainerFS(path string) ContainerFS {
    46  	return &local{
    47  		path:       path,
    48  		Driver:     driver.LocalDriver,
    49  		PathDriver: pathdriver.LocalPathDriver,
    50  	}
    51  }
    52  
    53  // NewLocalDriver provides file and path drivers for a local file system. They are
    54  // essentially a wrapper around the `os` and `filepath` functions.
    55  func NewLocalDriver() Driver {
    56  	return &local{
    57  		Driver:     driver.LocalDriver,
    58  		PathDriver: pathdriver.LocalPathDriver,
    59  	}
    60  }
    61  
    62  type local struct {
    63  	path string
    64  	driver.Driver
    65  	pathdriver.PathDriver
    66  }
    67  
    68  func (l *local) Path() string {
    69  	return l.path
    70  }
    71  
    72  func (l *local) ResolveScopedPath(path string, rawPath bool) (string, error) {
    73  	cleanedPath := path
    74  	if !rawPath {
    75  		cleanedPath = cleanScopedPath(path)
    76  	}
    77  	return symlink.FollowSymlinkInScope(filepath.Join(l.path, cleanedPath), l.path)
    78  }
    79  
    80  func (l *local) OS() string {
    81  	return runtime.GOOS
    82  }
    83  
    84  func (l *local) Architecture() string {
    85  	return runtime.GOARCH
    86  }