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>
This commit is contained in:
Sebastiaan van Stijn 2022-11-01 13:00:54 +01:00
parent f95e9b68d6
commit 3f935d0e2c
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -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
}
const maxlen = 12
func getMountpoint(id string) string {
maxlen := 12
// we need to preserve filesystem suffix
suffix := strings.SplitN(id, "-", 2)
if len(suffix) > 1 {
return id[:maxlen] + "-" + suffix[1]
id, suffix, _ := strings.Cut(id, "-")
id = id[:maxlen]
if suffix != "" {
// preserve filesystem suffix.
id += "-" + suffix
}
return id[:maxlen]
return id
}