all: Use slices.Backward for reverse iteration

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski committed 2026-09-02 18:51:15 +02:00
1 parent b7ae26eea7
commit 2c75374a2f
7 files changed
+21 -16

No files matched your search

+2 -2
View File
@@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"sync"
"sync/atomic"
"time"
@@ -280,8 +281,7 @@ func (s *Server) Start(ctx context.Context) error {
// Stop the containerd server canceling any open connections
func (s *Server) Stop() {
for i := len(s.plugins) - 1; i >= 0; i-- {
p := s.plugins[i]
for _, p := range slices.Backward(s.plugins) {
instance, err := p.Instance()
if err != nil {
log.L.WithFields(log.Fields{"error": err, "id": p.Registration.URI()}).Error("could not get plugin instance")
+3 -2
View File
@@ -21,6 +21,7 @@ import (
"errors"
"io"
"os"
"slices"
"github.com/containerd/containerd/v2/core/images"
"github.com/containerd/containerd/v2/pkg/archive/compression"
@@ -48,8 +49,8 @@ func RegisterProcessor(handler Handler) {
// GetProcessor returns the processor for a media-type
func GetProcessor(ctx context.Context, stream StreamProcessor, payloads map[string]typeurl.Any) (StreamProcessor, error) {
// reverse this list so that user configured handlers come up first
for i := len(handlers) - 1; i >= 0; i-- {
processor, ok := handlers[i](ctx, stream.MediaType())
for _, handler := range slices.Backward(handlers) {
processor, ok := handler(ctx, stream.MediaType())
if ok {
return processor(ctx, stream, payloads)
}
+5 -4
View File
@@ -24,6 +24,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
@@ -720,12 +721,12 @@ func (mm *mountManager) Deactivate(ctx context.Context, name string) error {
// TODO: Should this also be backgrounded, no much can do on failure to unmount
var mountErrors error
for i := len(allActive) - 1; i >= 0; i-- {
for _, active := range slices.Backward(allActive) {
var err error
if h := mm.handlers[allActive[i].Type]; h != nil {
err = h.Unmount(ctx, allActive[i].MountPoint)
if h := mm.handlers[active.Type]; h != nil {
err = h.Unmount(ctx, active.MountPoint)
} else {
err = mount.Unmount(allActive[i].MountPoint, 0)
err = mount.Unmount(active.MountPoint, 0)
}
if err != nil {
mountErrors = errors.Join(mountErrors, err)
+2 -2
View File
@@ -66,8 +66,8 @@ func All(mounts []Mount, target string) error {
// UnmountMounts unmounts all the mounts under a target in the reverse order of
// the mounts array provided.
func UnmountMounts(mounts []Mount, target string, flags int) error {
for i := len(mounts) - 1; i >= 0; i-- {
mountpoint, err := fs.RootPath(target, mounts[i].Target)
for i, m := range slices.Backward(mounts) {
mountpoint, err := fs.RootPath(target, m.Target)
if err != nil {
return err
}
+3 -2
View File
@@ -26,6 +26,7 @@ import (
"io"
"net/http"
"net/url"
"slices"
"strings"
"sync"
@@ -637,8 +638,8 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string,
}
}
for i := len(encoding) - 1; i >= 0; i-- {
algorithm := strings.ToLower(encoding[i])
for _, value := range slices.Backward(encoding) {
algorithm := strings.ToLower(value)
switch algorithm {
case "zstd":
r, err := zstd.NewReader(body.ReadCloser,
+3 -2
View File
@@ -23,6 +23,7 @@ import (
"fmt"
"io"
"maps"
"slices"
"strings"
"sync"
@@ -286,8 +287,8 @@ func PushContent(ctx context.Context, pusher Pusher, desc ocispec.Descriptor, st
}
// Iterate in reverse order as seen, parent always uploaded after child
for i := len(indexStack) - 1; i >= 0; i-- {
err := images.Dispatch(ctx, pushHandler, limiter, indexStack[i])
for _, index := range slices.Backward(indexStack) {
err := images.Dispatch(ctx, pushHandler, limiter, index)
if err != nil {
// TODO(estesp): until we have a more complete method for index push, we need to report
// missing dependencies in an index/manifest list by sensing the "400 Bad Request"
+3 -2
View File
@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"slices"
"strings"
"github.com/containerd/log"
@@ -141,9 +142,9 @@ func initCRIService(ic *plugin.InitContext) (any, error) {
}
if hasEnv, ok := shimPlugin.(interface{ Env() []string }); ok {
env := hasEnv.Env()
for i := len(env) - 1; i >= 0; i-- {
for _, value := range slices.Backward(env) {
// iterate backwards to grab the last PATH=
if path, ok := strings.CutPrefix(env[i], "PATH="); ok {
if path, ok := strings.CutPrefix(value, "PATH="); ok {
shimPath = path
break
}