Files
moby/pkg/ioutils/readers.go
Sebastiaan van Stijn 3db60168e7 pkg/ioutils: remove crypto/sha256, crypto/sha512 imports
commit 572498be56 moved the ioutils.HashData
utility to the libnetwork resolvconf package.

After removing, we saw some tests  in the reference  pacakge failing;

    === Failed
    === FAIL: reference TestLoad (0.00s)
        store_test.go:53: failed to parse reference: unsupported digest algorithm

    === FAIL: reference TestSave (0.00s)
        store_test.go:82: failed to parse reference: unsupported digest algorithm

    === FAIL: reference TestAddDeleteGet (0.00s)
        store_test.go:174: could not parse reference: unsupported digest algorithm

    === FAIL: reference TestInvalidTags (0.00s)
        store_test.go:355: assertion failed: error is not nil: unsupported digest algorithm

Those failures were because those tests depended on a side-effect of the
ioutils package being imported, which (before removal of HashData) imported
crypto/sha256, registering that algorithms, which on its turn was used by
github.com/opencontainers/go-digest to determnin if a given algorithm must
be accepted (see [go-digest#64]).

As a workaround, these imports were added. pkg/ioutils is now imported
in less places, and should not be depended on for this purpose.

Let's remove this workaround; if needed, these imports can be added in
a more relevant location.

This reverts commit 98caf09f0f.

[go-digest#64]: https://github.com/opencontainers/go-digest/pull/64

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-15 00:20:28 +01:00

119 lines
2.9 KiB
Go

package ioutils // import "github.com/docker/docker/pkg/ioutils"
import (
"context"
"io"
"runtime/debug"
"sync/atomic"
"github.com/containerd/log"
)
// readCloserWrapper wraps an io.Reader, and implements an io.ReadCloser
// It calls the given callback function when closed. It should be constructed
// with NewReadCloserWrapper
type readCloserWrapper struct {
io.Reader
closer func() error
closed atomic.Bool
}
// Close calls back the passed closer function
func (r *readCloserWrapper) Close() error {
if !r.closed.CompareAndSwap(false, true) {
subsequentCloseWarn("ReadCloserWrapper")
return nil
}
return r.closer()
}
// NewReadCloserWrapper wraps an io.Reader, and implements an io.ReadCloser.
// It calls the given callback function when closed.
func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser {
return &readCloserWrapper{
Reader: r,
closer: closer,
}
}
// cancelReadCloser wraps an io.ReadCloser with a context for cancelling read
// operations.
type cancelReadCloser struct {
cancel func()
pR *io.PipeReader // Stream to read from
pW *io.PipeWriter
closed atomic.Bool
}
// NewCancelReadCloser creates a wrapper that closes the ReadCloser when the
// context is cancelled. The returned io.ReadCloser must be closed when it is
// no longer needed.
func NewCancelReadCloser(ctx context.Context, in io.ReadCloser) io.ReadCloser {
pR, pW := io.Pipe()
// Create a context used to signal when the pipe is closed
doneCtx, cancel := context.WithCancel(context.Background())
p := &cancelReadCloser{
cancel: cancel,
pR: pR,
pW: pW,
}
go func() {
_, err := io.Copy(pW, in)
select {
case <-ctx.Done():
// If the context was closed, p.closeWithError
// was already called. Calling it again would
// change the error that Read returns.
default:
p.closeWithError(err)
}
in.Close()
}()
go func() {
for {
select {
case <-ctx.Done():
p.closeWithError(ctx.Err())
case <-doneCtx.Done():
return
}
}
}()
return p
}
// Read wraps the Read method of the pipe that provides data from the wrapped
// ReadCloser.
func (p *cancelReadCloser) Read(buf []byte) (n int, err error) {
return p.pR.Read(buf)
}
// closeWithError closes the wrapper and its underlying reader. It will
// cause future calls to Read to return err.
func (p *cancelReadCloser) closeWithError(err error) {
p.pW.CloseWithError(err)
p.cancel()
}
// Close closes the wrapper its underlying reader. It will cause
// future calls to Read to return io.EOF.
func (p *cancelReadCloser) Close() error {
if !p.closed.CompareAndSwap(false, true) {
subsequentCloseWarn("cancelReadCloser")
return nil
}
p.closeWithError(io.EOF)
return nil
}
func subsequentCloseWarn(name string) {
log.G(context.TODO()).Error("subsequent attempt to close " + name)
if log.GetLevel() >= log.DebugLevel {
log.G(context.TODO()).Errorf("stack trace: %s", string(debug.Stack()))
}
}