|
@@ -22,6 +22,7 @@ import (
|
|
|
"github.com/docker/docker/pkg/promise"
|
|
|
"github.com/docker/docker/pkg/signal"
|
|
|
"github.com/docker/docker/pkg/symlink"
|
|
|
+ "github.com/docker/docker/pkg/system"
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
"github.com/docker/docker/volume"
|
|
|
containertypes "github.com/docker/engine-api/types/container"
|
|
@@ -183,6 +184,30 @@ func (container *Container) WriteHostConfig() error {
|
|
|
return json.NewEncoder(f).Encode(&container.HostConfig)
|
|
|
}
|
|
|
|
|
|
+// SetupWorkingDirectory sets up the container's working directory as set in container.Config.WorkingDir
|
|
|
+func (container *Container) SetupWorkingDirectory() error {
|
|
|
+ if container.Config.WorkingDir == "" {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ container.Config.WorkingDir = filepath.Clean(container.Config.WorkingDir)
|
|
|
+
|
|
|
+ pth, err := container.GetResourcePath(container.Config.WorkingDir)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := system.MkdirAll(pth, 0755); err != nil {
|
|
|
+ pthInfo, err2 := os.Stat(pth)
|
|
|
+ if err2 == nil && pthInfo != nil && !pthInfo.IsDir() {
|
|
|
+ return derr.ErrorCodeNotADir.WithArgs(container.Config.WorkingDir)
|
|
|
+ }
|
|
|
+
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
// GetResourcePath evaluates `path` in the scope of the container's BaseFS, with proper path
|
|
|
// sanitisation. Symlinks are all scoped to the BaseFS of the container, as
|
|
|
// though the container's BaseFS was `/`.
|
|
@@ -199,7 +224,8 @@ func (container *Container) WriteHostConfig() error {
|
|
|
func (container *Container) GetResourcePath(path string) (string, error) {
|
|
|
// IMPORTANT - These are paths on the OS where the daemon is running, hence
|
|
|
// any filepath operations must be done in an OS agnostic way.
|
|
|
- cleanPath := filepath.Join(string(os.PathSeparator), path)
|
|
|
+
|
|
|
+ cleanPath := cleanResourcePath(path)
|
|
|
r, e := symlink.FollowSymlinkInScope(filepath.Join(container.BaseFS, cleanPath), container.BaseFS)
|
|
|
return r, e
|
|
|
}
|