From 5fc0f034266779c4a0b37b82dab7d3e3ca298df4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 19 Dec 2018 01:16:31 +0100 Subject: [PATCH] Extract workingdir validation/conversion to a function Signed-off-by: Sebastiaan van Stijn --- daemon/container.go | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/daemon/container.go b/daemon/container.go index 5a6f29469f..4531562a52 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -235,23 +235,8 @@ func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig * func (daemon *Daemon) verifyContainerSettings(platform string, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) (warnings []string, err error) { // First perform verification of settings common across all platforms. if config != nil { - if config.WorkingDir != "" { - wdInvalid := false - if runtime.GOOS == platform { - config.WorkingDir = filepath.FromSlash(config.WorkingDir) // Ensure in platform semantics - if !system.IsAbs(config.WorkingDir) { - wdInvalid = true - } - } else { - // LCOW. Force Unix semantics - config.WorkingDir = strings.Replace(config.WorkingDir, string(os.PathSeparator), "/", -1) - if !path.IsAbs(config.WorkingDir) { - wdInvalid = true - } - } - if wdInvalid { - return nil, fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", config.WorkingDir) - } + if err := translateWorkingDir(config, platform); err != nil { + return nil, err } if len(config.StopSignal) > 0 { @@ -367,3 +352,27 @@ func validateRestartPolicy(policy containertypes.RestartPolicy) error { } return nil } + +// translateWorkingDir translates the working-dir for the target platform, +// and returns an error if the given path is not an absolute path. +func translateWorkingDir(config *containertypes.Config, platform string) error { + if config.WorkingDir == "" { + return nil + } + wd := config.WorkingDir + switch { + case runtime.GOOS != platform: + // LCOW. Force Unix semantics + wd = strings.Replace(wd, string(os.PathSeparator), "/", -1) + if !path.IsAbs(wd) { + return fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", config.WorkingDir) + } + default: + wd = filepath.FromSlash(wd) // Ensure in platform semantics + if !system.IsAbs(wd) { + return fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", config.WorkingDir) + } + } + config.WorkingDir = wd + return nil +}