From 6510f1011ba833c98c177f290cc137fe971dd4cb Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sat, 6 Nov 2021 09:21:05 -0400 Subject: [PATCH] proxy: Add a helper to return a byte array Since this is shared between the manifest and config paths. Signed-off-by: Colin Walters --- cmd/skopeo/proxy.go | 61 +++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/cmd/skopeo/proxy.go b/cmd/skopeo/proxy.go index 70dc1115..961dfd5c 100644 --- a/cmd/skopeo/proxy.go +++ b/cmd/skopeo/proxy.go @@ -302,6 +302,30 @@ func (h *proxyHandler) allocPipe() (*os.File, *activePipe, error) { return piper, &f, nil } +// returnBytes generates a return pipe() from a byte array +// In the future it might be nicer to return this via memfd_create() +func (h *proxyHandler) returnBytes(retval interface{}, buf []byte) (replyBuf, error) { + var ret replyBuf + piper, f, err := h.allocPipe() + if err != nil { + return ret, err + } + + go func() { + // Signal completion when we return + defer f.wg.Done() + _, err = io.Copy(f.w, bytes.NewReader(buf)) + if err != nil { + f.err = err + } + }() + + ret.value = retval + ret.fd = piper + ret.pipeid = uint32(f.w.Fd()) + return ret, nil +} + // GetManifest returns a copy of the manifest, converted to OCI format, along with the original digest. func (h *proxyHandler) GetManifest(args []interface{}) (replyBuf, error) { h.lock.Lock() @@ -362,24 +386,7 @@ func (h *proxyHandler) GetManifest(args []interface{}) (replyBuf, error) { } else { serialized = rawManifest } - piper, f, err := h.allocPipe() - if err != nil { - return ret, err - } - - go func() { - // Signal completion when we return - defer f.wg.Done() - _, err = io.Copy(f.w, bytes.NewReader(serialized)) - if err != nil { - f.err = err - } - }() - - ret.value = digest.String() - ret.fd = piper - ret.pipeid = uint32(f.w.Fd()) - return ret, nil + return h.returnBytes(digest, serialized) } // GetConfig returns a copy of the image configuration, converted to OCI format. @@ -409,24 +416,8 @@ func (h *proxyHandler) GetConfig(args []interface{}) (replyBuf, error) { if err != nil { return ret, err } + return h.returnBytes(nil, serialized) - piper, f, err := h.allocPipe() - if err != nil { - return ret, err - } - - go func() { - // Signal completion when we return - defer f.wg.Done() - _, err = io.Copy(f.w, bytes.NewReader(serialized)) - if err != nil { - f.err = err - } - }() - - ret.fd = piper - ret.pipeid = uint32(f.w.Fd()) - return ret, nil } // GetBlob fetches a blob, performing digest verification.