浏览代码

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 年之前
父节点
当前提交
384517218a
共有 2 个文件被更改,包括 9 次插入10 次删除
  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