From 6ab92121689adfca8f64ac8de89f63fee8a02ac6 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 28 Dec 2024 16:51:05 +0100 Subject: [PATCH] pkg/ioutils: remove OnEOFReader and move it internal This type was originally in pkg/transport, but got moved to pkg/ioutils in 276c640be4b4335e3b8d684cb3562a56d3337b39. This type is only used in a single location, and has no external consumers, so we can move it where it's used and un-export it. Signed-off-by: Sebastiaan van Stijn --- pkg/ioutils/readers.go | 29 ----------------------------- registry/search_session.go | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pkg/ioutils/readers.go b/pkg/ioutils/readers.go index e03d3fee75..3d47e6d0f1 100644 --- a/pkg/ioutils/readers.go +++ b/pkg/ioutils/readers.go @@ -61,35 +61,6 @@ func NewReaderErrWrapper(r io.Reader, closer func()) io.Reader { } } -// OnEOFReader wraps an io.ReadCloser and a function -// the function will run at the end of file or close the file. -type OnEOFReader struct { - Rc io.ReadCloser - Fn func() -} - -func (r *OnEOFReader) Read(p []byte) (n int, err error) { - n, err = r.Rc.Read(p) - if err == io.EOF { - r.runFunc() - } - return -} - -// Close closes the file and run the function. -func (r *OnEOFReader) Close() error { - err := r.Rc.Close() - r.runFunc() - return err -} - -func (r *OnEOFReader) runFunc() { - if fn := r.Fn; fn != nil { - fn() - r.Fn = nil - } -} - // cancelReadCloser wraps an io.ReadCloser with a context for cancelling read // operations. type cancelReadCloser struct { diff --git a/registry/search_session.go b/registry/search_session.go index c334143c6b..a0d25c805e 100644 --- a/registry/search_session.go +++ b/registry/search_session.go @@ -6,6 +6,7 @@ import ( _ "crypto/sha512" "encoding/json" "fmt" + "io" "net/http" "net/http/cookiejar" "net/url" @@ -15,7 +16,6 @@ import ( "github.com/containerd/log" "github.com/docker/docker/api/types/registry" "github.com/docker/docker/errdefs" - "github.com/docker/docker/pkg/ioutils" "github.com/pkg/errors" ) @@ -76,6 +76,35 @@ func cloneRequest(r *http.Request) *http.Request { return r2 } +// onEOFReader wraps an io.ReadCloser and a function +// the function will run at the end of file or close the file. +type onEOFReader struct { + Rc io.ReadCloser + Fn func() +} + +func (r *onEOFReader) Read(p []byte) (n int, err error) { + n, err = r.Rc.Read(p) + if err == io.EOF { + r.runFunc() + } + return +} + +// Close closes the file and run the function. +func (r *onEOFReader) Close() error { + err := r.Rc.Close() + r.runFunc() + return err +} + +func (r *onEOFReader) runFunc() { + if fn := r.Fn; fn != nil { + fn() + r.Fn = nil + } +} + // RoundTrip changes an HTTP request's headers to add the necessary // authentication-related headers func (tr *authTransport) RoundTrip(orig *http.Request) (*http.Response, error) { @@ -119,7 +148,7 @@ func (tr *authTransport) RoundTrip(orig *http.Request) (*http.Response, error) { if len(resp.Header["X-Docker-Token"]) > 0 { tr.token = resp.Header["X-Docker-Token"] } - resp.Body = &ioutils.OnEOFReader{ + resp.Body = &onEOFReader{ Rc: resp.Body, Fn: func() { tr.mu.Lock()