Add WithValues to context package and use it in restore.go

Create WithValues in pkg/context/context.go to replace multiple values using
a map. Update runsc/boot/restore.go to use WithValues instead of multiple
WithValue calls.

PiperOrigin-RevId: 914896863
This commit is contained in:
Fabricio Voznika
2026-05-13 09:09:18 -07:00
committed by gVisor bot
parent d7d7d6cd39
commit 331d6af443
2 changed files with 30 additions and 3 deletions
+25
View File
@@ -254,3 +254,28 @@ func (ctx *withValue) Value(key any) any {
}
return ctx.Context.Value(key)
}
// WithValues returns a copy of parent in which the values associated with keys
// are the corresponding values in the map.
func WithValues(parent Context, values map[any]any) Context {
if len(values) == 0 {
return parent
}
return &withValues{
Context: parent,
values: values,
}
}
type withValues struct {
Context
values map[any]any
}
// Value implements Context.Value.
func (ctx *withValues) Value(key any) any {
if val, ok := ctx.values[key]; ok {
return val
}
return ctx.Context.Value(key)
}
+5 -3
View File
@@ -260,10 +260,12 @@ func (r *restorer) restore(l *Loader) error {
log.Debugf("Restore using fdmap: %#v", fdmap)
ctx := l.k.SupervisorContext()
ctx = context.WithValue(ctx, vfs.CtxRestoreFilesystemFDMap, fdmap)
log.Debugf("Restore using mfmap: %v", mfmap)
ctx = context.WithValue(ctx, pgalloc.CtxMemoryFileMap, mfmap)
ctx = context.WithValue(ctx, devutil.CtxDevGoferClientProvider, l.k)
ctx = context.WithValues(ctx, map[any]any{
vfs.CtxRestoreFilesystemFDMap: fdmap,
pgalloc.CtxMemoryFileMap: mfmap,
devutil.CtxDevGoferClientProvider: l.k,
})
if r.asyncMFLoader != nil {
// Now that private memory files are known, kick off their loading in the