From 00aed4c805aa6dd52036fbcf39adc2b6d998b604 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 9 Mar 2026 22:13:00 -0700 Subject: [PATCH] refactor: replace strutil.DedupeSlice with generic bkslices.Dedupe Introduce util/bkslices.Dedupe[T] as a generic replacement for the string-only strutil.DedupeSlice. Remove the now unused util/strutil package. Signed-off-by: Tonis Tiigi --- util/bkslices/dedupe.go | 15 +++++++++++ util/rootless/mountopts/mountopts_linux.go | 6 ++--- util/strutil/strutil.go | 30 ---------------------- 3 files changed, 18 insertions(+), 33 deletions(-) create mode 100644 util/bkslices/dedupe.go delete mode 100644 util/strutil/strutil.go diff --git a/util/bkslices/dedupe.go b/util/bkslices/dedupe.go new file mode 100644 index 000000000..0e0495db5 --- /dev/null +++ b/util/bkslices/dedupe.go @@ -0,0 +1,15 @@ +package bkslices + +// Dedupe returns the input values in their original order without duplicates. +func Dedupe[T comparable](in []T) []T { + seen := make(map[T]struct{}, len(in)) + out := make([]T, 0, len(in)) + for _, v := range in { + if _, ok := seen[v]; ok { + continue + } + seen[v] = struct{}{} + out = append(out, v) + } + return out +} diff --git a/util/rootless/mountopts/mountopts_linux.go b/util/rootless/mountopts/mountopts_linux.go index 4a4a6a223..dd19c3b80 100644 --- a/util/rootless/mountopts/mountopts_linux.go +++ b/util/rootless/mountopts/mountopts_linux.go @@ -2,7 +2,7 @@ package mountopts import ( "github.com/containerd/containerd/v2/core/mount" - "github.com/moby/buildkit/util/strutil" + "github.com/moby/buildkit/util/bkslices" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" "golang.org/x/sys/unix" @@ -59,7 +59,7 @@ func FixUp(mounts []mount.Mount) ([]mount.Mount, error) { if err != nil { return nil, errors.Wrapf(err, "failed to get unprivileged mount flags for %+v", m) } - m.Options = strutil.DedupeSlice(append(m.Options, unpriv...)) + m.Options = bkslices.Dedupe(append(m.Options, unpriv...)) mounts[i] = m } return mounts, nil @@ -81,7 +81,7 @@ func FixUpOCI(mounts []specs.Mount) ([]specs.Mount, error) { if err != nil { return nil, errors.Wrapf(err, "failed to get unprivileged mount flags for %+v", m) } - m.Options = strutil.DedupeSlice(append(m.Options, unpriv...)) + m.Options = bkslices.Dedupe(append(m.Options, unpriv...)) mounts[i] = m } return mounts, nil diff --git a/util/strutil/strutil.go b/util/strutil/strutil.go deleted file mode 100644 index cb98555e7..000000000 --- a/util/strutil/strutil.go +++ /dev/null @@ -1,30 +0,0 @@ -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package strutil - -// DedupeSlice is from https://github.com/containerd/nerdctl/blob/v1.2.1/pkg/strutil/strutil.go#L72-L82 -func DedupeSlice(in []string) []string { - m := make(map[string]struct{}) - var res []string - for _, s := range in { - if _, ok := m[s]; !ok { - res = append(res, s) - m[s] = struct{}{} - } - } - return res -}