瀏覽代碼

daemon/graphdriver/zfs: use strings.Cut, and refactor

Fixes a (theoretical?) panic if ID would be shorter than 12
characters. Also trim the ID _after_ cutting off the suffix.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 年之前
父節點
當前提交
3f935d0e2c
共有 1 個文件被更改,包括 9 次插入11 次删除
  1. 9 11
      daemon/graphdriver/zfs/zfs_freebsd.go

+ 9 - 11
daemon/graphdriver/zfs/zfs_freebsd.go

@@ -1,7 +1,6 @@
 package zfs // import "github.com/docker/docker/daemon/graphdriver/zfs"
 
 import (
-	"fmt"
 	"strings"
 
 	"github.com/docker/docker/daemon/graphdriver"
@@ -12,7 +11,7 @@ import (
 func checkRootdirFs(rootdir string) error {
 	var buf unix.Statfs_t
 	if err := unix.Statfs(rootdir, &buf); err != nil {
-		return fmt.Errorf("Failed to access '%s': %s", rootdir, err)
+		return err
 	}
 
 	// on FreeBSD buf.Fstypename contains ['z', 'f', 's', 0 ... ]
@@ -24,15 +23,14 @@ func checkRootdirFs(rootdir string) error {
 	return nil
 }
 
-func getMountpoint(id string) string {
-	maxlen := 12
-
-	// we need to preserve filesystem suffix
-	suffix := strings.SplitN(id, "-", 2)
+const maxlen = 12
 
-	if len(suffix) > 1 {
-		return id[:maxlen] + "-" + suffix[1]
+func getMountpoint(id string) string {
+	id, suffix, _ := strings.Cut(id, "-")
+	id = id[:maxlen]
+	if suffix != "" {
+		// preserve filesystem suffix.
+		id += "-" + suffix
 	}
-
-	return id[:maxlen]
+	return id
 }