From 7ebe625db7251c1f0664430dd0c6e5e083a7b4e1 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 20 Oct 2024 17:36:04 +0200 Subject: [PATCH] pkg/archive: move deprecated NewTempArchive, TempArchive to test-utils These were deprecated in 7ce1edd7c6a6aff808025b2f14f1e6d94b10b79f, which is part of v27.0.0. Move them to a test-file as they were only used for tests. Signed-off-by: Sebastiaan van Stijn --- pkg/archive/archive.go | 57 ------------------------------------- pkg/archive/archive_test.go | 6 ++-- pkg/archive/changes_test.go | 2 +- pkg/archive/utils_test.go | 53 ++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 61 deletions(-) diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 5412a84c46..963f865bd1 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -1432,60 +1432,3 @@ func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) { return err }), nil } - -// NewTempArchive reads the content of src into a temporary file, and returns the contents -// of that file as an archive. The archive can only be read once - as soon as reading completes, -// the file will be deleted. -// -// Deprecated: NewTempArchive is only used in tests and will be removed in the next release. -func NewTempArchive(src io.Reader, dir string) (*TempArchive, error) { - f, err := os.CreateTemp(dir, "") - if err != nil { - return nil, err - } - if _, err := io.Copy(f, src); err != nil { - return nil, err - } - if _, err := f.Seek(0, 0); err != nil { - return nil, err - } - st, err := f.Stat() - if err != nil { - return nil, err - } - size := st.Size() - return &TempArchive{File: f, Size: size}, nil -} - -// TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes, -// the file will be deleted. -// -// Deprecated: TempArchive is only used in tests and will be removed in the next release. -type TempArchive struct { - *os.File - Size int64 // Pre-computed from Stat().Size() as a convenience - read int64 - closed bool -} - -// Close closes the underlying file if it's still open, or does a no-op -// to allow callers to try to close the TempArchive multiple times safely. -func (archive *TempArchive) Close() error { - if archive.closed { - return nil - } - - archive.closed = true - - return archive.File.Close() -} - -func (archive *TempArchive) Read(data []byte) (int, error) { - n, err := archive.File.Read(data) - archive.read += int64(n) - if err != nil || archive.read == archive.Size { - archive.Close() - os.Remove(archive.File.Name()) - } - return n, err -} diff --git a/pkg/archive/archive_test.go b/pkg/archive/archive_test.go index 229a1c33ad..2d776a4dfb 100644 --- a/pkg/archive/archive_test.go +++ b/pkg/archive/archive_test.go @@ -1224,16 +1224,16 @@ func TestUntarInvalidSymlink(t *testing.T) { func TestTempArchiveCloseMultipleTimes(t *testing.T) { reader := io.NopCloser(strings.NewReader("hello")) - tempArchive, err := NewTempArchive(reader, "") + tmpArchive, err := newTempArchive(reader, "") assert.NilError(t, err) buf := make([]byte, 10) - n, err := tempArchive.Read(buf) + n, err := tmpArchive.Read(buf) assert.NilError(t, err) if n != 5 { t.Fatalf("Expected to read 5 bytes. Read %d instead", n) } for i := 0; i < 3; i++ { - if err = tempArchive.Close(); err != nil { + if err = tmpArchive.Close(); err != nil { t.Fatalf("i=%d. Unexpected error closing temp archive: %v", i, err) } } diff --git a/pkg/archive/changes_test.go b/pkg/archive/changes_test.go index 45d2ece507..93d259dbaf 100644 --- a/pkg/archive/changes_test.go +++ b/pkg/archive/changes_test.go @@ -428,7 +428,7 @@ func TestApplyLayer(t *testing.T) { layer, err := ExportChanges(dst, changes, idtools.IdentityMapping{}) assert.NilError(t, err) - layerCopy, err := NewTempArchive(layer, "") + layerCopy, err := newTempArchive(layer, "") assert.NilError(t, err) _, err = ApplyLayer(src, layerCopy) diff --git a/pkg/archive/utils_test.go b/pkg/archive/utils_test.go index 524ffc747f..a83290572b 100644 --- a/pkg/archive/utils_test.go +++ b/pkg/archive/utils_test.go @@ -163,3 +163,56 @@ func testBreakout(untarFn string, tmpdir string, headers []*tar.Header) error { return nil }) } + +// newTempArchive reads the content of src into a temporary file, and returns the contents +// of that file as an archive. The archive can only be read once - as soon as reading completes, +// the file will be deleted. +func newTempArchive(src io.Reader, dir string) (*tempArchive, error) { + f, err := os.CreateTemp(dir, "") + if err != nil { + return nil, err + } + if _, err := io.Copy(f, src); err != nil { + return nil, err + } + if _, err := f.Seek(0, 0); err != nil { + return nil, err + } + st, err := f.Stat() + if err != nil { + return nil, err + } + size := st.Size() + return &tempArchive{File: f, Size: size}, nil +} + +// tempArchive is a temporary archive. The archive can only be read once - as soon as reading completes, +// the file will be deleted. +type tempArchive struct { + *os.File + Size int64 // Pre-computed from Stat().Size() as a convenience + read int64 + closed bool +} + +// Close closes the underlying file if it's still open, or does a no-op +// to allow callers to try to close the tempArchive multiple times safely. +func (archive *tempArchive) Close() error { + if archive.closed { + return nil + } + + archive.closed = true + + return archive.File.Close() +} + +func (archive *tempArchive) Read(data []byte) (int, error) { + n, err := archive.File.Read(data) + archive.read += int64(n) + if err != nil || archive.read == archive.Size { + _ = archive.Close() + _ = os.Remove(archive.File.Name()) + } + return n, err +}