Skip to content

Commit

Permalink
pkg/archive: move readCloserWrapper to be a test-utility
Browse files Browse the repository at this point in the history
It's only used in tests now, so move it there, and simplify
we can probably remove the atomic bool as well.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Dec 23, 2024
1 parent 97daaf5 commit 36e5b96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
20 changes: 0 additions & 20 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"sync/atomic"
"syscall"
"time"

Expand Down Expand Up @@ -130,24 +128,6 @@ func DetectCompression(source []byte) compression.Compression {
return compression.Detect(source)
}

type readCloserWrapper struct {
io.Reader
closer func() error
closed atomic.Bool
}

func (r *readCloserWrapper) Close() error {
if !r.closed.CompareAndSwap(false, true) {
log.G(context.TODO()).Error("subsequent attempt to close readCloserWrapper")
if log.GetLevel() >= log.DebugLevel {
log.G(context.TODO()).Errorf("stack trace: %s", string(debug.Stack()))
}

return nil
}
return r.closer()
}

// DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.
//
// Deprecated: use [compression.DecompressStream].
Expand Down
14 changes: 14 additions & 0 deletions pkg/archive/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"reflect"
"sync/atomic"
"testing"

"github.com/docker/docker/pkg/archive/compression"
Expand Down Expand Up @@ -310,6 +311,19 @@ func TestApplyLayerWhiteouts(t *testing.T) {
}
}

type readCloserWrapper struct {
io.Reader
closer func() error
closed atomic.Bool
}

func (r *readCloserWrapper) Close() error {
if !r.closed.CompareAndSwap(false, true) {
return nil
}
return r.closer()
}

func makeTestLayer(paths []string) (rc io.ReadCloser, err error) {
tmpDir, err := os.MkdirTemp("", "graphdriver-test-mklayer")
if err != nil {
Expand Down

0 comments on commit 36e5b96

Please sign in to comment.