erofsutils: MountsToLayer slight optimizations

follow-up to 09f34d18b7

- Use strings.Cut instead of trimming prefixes and strings.Split
  to reduce allocations
- Use a switch for mount-type for slightly better readability
  than if / else if / else.
- Fix GoDoc to start with the function name.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-04-19 16:58:08 +02:00
parent 4a51d8c8f7
commit 568880ec3e

View File

@@ -55,7 +55,8 @@ func ConvertErofs(ctx context.Context, layerPath string, srcDir string, mkfsExtr
return nil
}
// Get the snapshot layer directory in order to generate EROFS-formatted blobs;
// MountsToLayer returns the snapshot layer directory in order to generate
// EROFS-formatted blobs;
//
// If mount[0].Type is `bind` or `erofs`, it just tries the source dir; Or if
// mount[0].Type is `overlayfs`, it tries the parent of the upperdir;
@@ -64,25 +65,29 @@ func ConvertErofs(ctx context.Context, layerPath string, srcDir string, mkfsExtr
// snapshot is really generated by the EROFS snapshotter instead of others.
func MountsToLayer(mounts []mount.Mount) (string, error) {
var layer string
mnt := mounts[0]
if mnt.Type == "bind" || mnt.Type == "erofs" {
switch mnt := mounts[0]; mnt.Type {
case "bind", "erofs":
layer = filepath.Dir(mnt.Source)
} else if mnt.Type == "overlay" {
toplower := ""
case "overlay":
var topLower string
for _, o := range mnt.Options {
if strings.HasPrefix(o, "upperdir=") {
layer = filepath.Dir(strings.TrimPrefix(o, "upperdir="))
} else if strings.HasPrefix(o, "lowerdir=") {
toplower = filepath.Dir(strings.Split(strings.TrimPrefix(o, "lowerdir="), ":")[0])
if k, v, ok := strings.Cut(o, "="); ok {
switch k {
case "upperdir":
layer = filepath.Dir(v)
case "lowerdir":
dir, _, _ := strings.Cut(v, ":")
topLower = filepath.Dir(dir)
}
}
}
if layer == "" {
if toplower == "" {
if topLower == "" {
return "", fmt.Errorf("unsupported overlay layer for erofs differ: %w", errdefs.ErrNotImplemented)
}
layer = toplower
layer = topLower
}
} else {
default:
return "", fmt.Errorf("invalid filesystem type for erofs differ: %w", errdefs.ErrNotImplemented)
}
// If the layer is not prepared by the EROFS snapshotter, fall back to the next differ