Browse Source

volume/mounts: remove windowsValidateRegex() utility

This utility was just a shallow wrapper around executing the regular
expression, and in some cases, we didn't even use the error it returned,
so better to inline the code instead of abstracting it away.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 4 years ago
parent
commit
384517218a
2 changed files with 9 additions and 10 deletions
  1. 4 2
      volume/mounts/lcow_parser.go
  2. 5 8
      volume/mounts/windows_parser.go

+ 4 - 2
volume/mounts/lcow_parser.go

@@ -2,8 +2,10 @@ package mounts // import "github.com/docker/docker/volume/mounts"
 
 import (
 	"errors"
+	"fmt"
 	"path"
 	"regexp"
+	"strings"
 
 	"github.com/docker/docker/api/types/mount"
 )
@@ -37,8 +39,8 @@ var lcowValidators mountValidator = func(m *mount.Mount) error {
 	if m.Type == mount.TypeNamedPipe {
 		return errors.New("Linux containers on Windows do not support named pipe mounts")
 	}
-	if err := windowsValidateRegex(m.Target, lcowMountDestinationRegex); err != nil {
-		return err
+	if !lcowMountDestinationRegex.MatchString(strings.ToLower(m.Target)) {
+		return fmt.Errorf("invalid mount path: '%s'", m.Target)
 	}
 	return nil
 }

+ 5 - 8
volume/mounts/windows_parser.go

@@ -155,17 +155,14 @@ var windowsValidators mountValidator = func(m *mount.Mount) error {
 	if err := windowsValidateNotRoot(m.Target); err != nil {
 		return err
 	}
-	return windowsValidateRegex(m.Target, mountDestinationRegexp)
-}
-
-func windowsValidateRegex(p string, r *regexp.Regexp) error {
-	if r.MatchString(strings.ToLower(p)) {
-		return nil
+	if !mountDestinationRegexp.MatchString(strings.ToLower(m.Target)) {
+		return fmt.Errorf("invalid mount path: '%s'", m.Target)
 	}
-	return fmt.Errorf("invalid mount path: '%s'", p)
+	return nil
 }
+
 func windowsValidateAbsolute(p string) error {
-	if err := windowsValidateRegex(p, mountDestinationRegexp); err != nil {
+	if !mountDestinationRegexp.MatchString(strings.ToLower(p)) {
 		return fmt.Errorf("invalid mount path: '%s' mount path must be absolute", p)
 	}
 	return nil