From: Reinhard Tartler <siretart@tauware.de>
Subject: Revert to immutable radix v1
Upstream: Reverts https://github.com/moby/buildkit/commit/156815908fe19375ca71292968aab1d0170f789f
Forwarded: not-needed

Please drop as soon as golang-github-hashicorp-go-immutable-radix-v2 gets out of NEW #1116455

Index: docker.io/buildkit/cache/contenthash/checksum.go
===================================================================
--- docker.io.orig/buildkit/cache/contenthash/checksum.go
+++ docker.io/buildkit/cache/contenthash/checksum.go
@@ -12,7 +12,7 @@ import (
 	"sync"
 	"sync/atomic"
 
-	iradix "github.com/hashicorp/go-immutable-radix/v2"
+	iradix "github.com/hashicorp/go-immutable-radix"
 	simplelru "github.com/hashicorp/golang-lru/v2/simplelru"
 	"github.com/moby/buildkit/cache"
 	"github.com/moby/buildkit/session"
@@ -169,12 +169,12 @@ func (cm *cacheManager) clearCacheContex
 type cacheContext struct {
 	mu    sync.RWMutex
 	md    cacheMetadata
-	tree  *iradix.Tree[*CacheRecord]
+	tree  *iradix.Tree
 	dirty bool // needs to be persisted to disk
 
 	// used in HandleChange
-	txn      *iradix.Txn[*CacheRecord]
-	node     *iradix.Node[*CacheRecord]
+	txn      *iradix.Txn
+	node     *iradix.Node
 	dirtyMap map[string]struct{}
 	linkMap  map[string][][]byte
 }
@@ -234,7 +234,7 @@ func (m *mount) clean() error {
 func newCacheContext(md cache.RefMetadata) (*cacheContext, error) {
 	cc := &cacheContext{
 		md:       cacheMetadata{md},
-		tree:     iradix.New[*CacheRecord](),
+		tree:     iradix.New(),
 		dirtyMap: map[string]struct{}{},
 		linkMap:  map[string][][]byte{},
 	}
@@ -273,10 +273,10 @@ func (cc *cacheContext) save() error {
 
 	var l CacheRecords
 	node := cc.tree.Root()
-	node.Walk(func(k []byte, v *CacheRecord) bool {
+	node.Walk(func(k []byte, v interface{}) bool {
 		l.Paths = append(l.Paths, &CacheRecordWithPath{
 			Path:   string(k),
-			Record: v,
+			Record: v.(*CacheRecord),
 		})
 		return false
 	})
@@ -304,7 +304,7 @@ func (cc *cacheContext) HandleChange(kin
 
 	deleteDir := func(cr *CacheRecord) {
 		if cr.Type == CacheRecordTypeDir {
-			cc.node.WalkPrefix(append(k, 0), func(k []byte, v *CacheRecord) bool {
+			cc.node.WalkPrefix(append(k, 0), func(k []byte, v interface{}) bool {
 				cc.txn.Delete(k)
 				return false
 			})
@@ -332,7 +332,7 @@ func (cc *cacheContext) HandleChange(kin
 	if kind == fsutil.ChangeKindDelete {
 		v, ok := cc.txn.Delete(k)
 		if ok {
-			deleteDir(v)
+			deleteDir(v.(*CacheRecord))
 		}
 		d := path.Dir(p)
 		if d == "/" {
@@ -354,7 +354,7 @@ func (cc *cacheContext) HandleChange(kin
 
 	v, ok := cc.node.Get(k)
 	if ok {
-		deleteDir(v)
+		deleteDir(v.(*CacheRecord))
 	}
 
 	cr := &CacheRecord{
@@ -381,7 +381,7 @@ func (cc *cacheContext) HandleChange(kin
 		ln := path.Join("/", filepath.ToSlash(stat.Linkname))
 		v, ok := cc.txn.Get(convertPathToKey(ln))
 		if ok {
-			cr = v.CloneVT()
+			cr = v.(*CacheRecord).CloneVT()
 		}
 		cc.linkMap[ln] = append(cc.linkMap[ln], k)
 	}
@@ -505,7 +505,7 @@ func (cc *cacheContext) includedPaths(ct
 	root = txn.Root()
 	var (
 		updated        bool
-		iter           *iradix.Iterator[*CacheRecord]
+		iter           *iradix.Iterator
 		k              []byte
 		keyOk          bool
 		origPrefix     string
@@ -725,7 +725,7 @@ func shouldIncludePath(
 	return true, nil
 }
 
-func wildcardPrefix(root *iradix.Node[*CacheRecord], p string) (string, []byte, bool, error) {
+func wildcardPrefix(root *iradix.Node, p string) (string, []byte, bool, error) {
 	// For consistency with what the copy implementation in fsutil
 	// does: split pattern into non-wildcard prefix and rest of
 	// pattern, then follow symlinks when resolving the non-wildcard
@@ -858,7 +858,7 @@ func (cc *cacheContext) scanChecksum(ctx
 	return cr, err
 }
 
-func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node[*CacheRecord], txn *iradix.Txn[*CacheRecord], m *mount, k []byte, followTrailing bool) (*CacheRecord, bool, error) {
+func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node, txn *iradix.Txn, m *mount, k []byte, followTrailing bool) (*CacheRecord, bool, error) {
 	origk := k
 	k, cr, err := getFollowLinks(root, k, followTrailing)
 	if err != nil {
@@ -977,7 +977,7 @@ func (s pathSet) includes(p string) bool
 
 // needsScan returns false if path is in the tree or a parent path is in tree
 // and subpath is missing.
-func (cc *cacheContext) needsScan(root *iradix.Node[*CacheRecord], path string, followTrailing bool) (bool, error) {
+func (cc *cacheContext) needsScan(root *iradix.Node, path string, followTrailing bool) (bool, error) {
 	var (
 		goodPaths       pathSet
 		hasParentInTree bool
@@ -1098,7 +1098,7 @@ func (cc *cacheContext) scanPath(ctx con
 type followLinksCallback func(path string, cr *CacheRecord) error
 
 // getFollowLinks is shorthand for getFollowLinksCallback(..., nil).
-func getFollowLinks(root *iradix.Node[*CacheRecord], k []byte, followTrailing bool) ([]byte, *CacheRecord, error) {
+func getFollowLinks(root *iradix.Node, k []byte, followTrailing bool) ([]byte, *CacheRecord, error) {
 	return getFollowLinksCallback(root, k, followTrailing, nil)
 }
 
@@ -1123,10 +1123,10 @@ func getFollowLinks(root *iradix.Node[*C
 // trailing-slash lookup (/a/b/c/ in the above example). Note that
 // getFollowLinksCallback will try to look up the original key directly first
 // and the callback is not called for this first lookup.
-func getFollowLinksCallback(root *iradix.Node[*CacheRecord], k []byte, followTrailing bool, cb followLinksCallback) ([]byte, *CacheRecord, error) {
+func getFollowLinksCallback(root *iradix.Node, k []byte, followTrailing bool, cb followLinksCallback) ([]byte, *CacheRecord, error) {
 	v, ok := root.Get(k)
-	if ok && (!followTrailing || v.Type != CacheRecordTypeSymlink) {
-		return k, v, nil
+	if ok && (!followTrailing || v.(*CacheRecord).Type != CacheRecordTypeSymlink) {
+		return k, v.(*CacheRecord), nil
 	}
 	if len(k) == 0 {
 		return k, nil, nil
@@ -1159,7 +1159,10 @@ func getFollowLinksCallback(root *iradix
 		// we hit (including /) and we need to make sure that the CacheRecord
 		// we return is correct after every iteration.
 
-		cr, ok = root.Get(convertPathToKey(nextPath))
+		v, ok = root.Get(convertPathToKey(nextPath))
+		if ok {
+			cr = v.(*CacheRecord)
+		}
 		if cb != nil {
 			if err := cb(nextPath, cr); err != nil {
 				return nil, nil, err
@@ -1189,7 +1192,10 @@ func getFollowLinksCallback(root *iradix
 	// the slash applied.
 	if hadTrailingSlash {
 		currentPath += "/"
-		cr, _ = root.Get(convertPathToKey(currentPath))
+		v, ok := root.Get(convertPathToKey(currentPath))
+		if ok {
+			cr = v.(*CacheRecord)
+		}
 		if cb != nil {
 			if err := cb(currentPath, cr); err != nil {
 				return nil, nil, err
