Merge pull request #48708 from thaJeztah/pkg_archive_remove_deprecated

pkg/archive: remove deprecated CanonicalTarNameForPath, NewTempArchive, TempArchive
This commit is contained in:
Akihiro Suda
2024-10-21 13:32:51 +09:00
committed by GitHub
4 changed files with 57 additions and 70 deletions

View File

@@ -531,15 +531,6 @@ func newTarAppender(idMapping idtools.IdentityMapping, writer io.Writer, chownOp
}
}
// CanonicalTarNameForPath canonicalizes relativePath to a POSIX-style path using
// forward slashes. It is an alias for [filepath.ToSlash], which is a no-op on
// Linux and Unix.
//
// Deprecated: use [filepath.ToSlash]. This function will be removed in the next release.
func CanonicalTarNameForPath(relativePath string) string {
return filepath.ToSlash(relativePath)
}
// canonicalTarName provides a platform-independent and consistent POSIX-style
// path for files and directories to be archived regardless of the platform.
func canonicalTarName(name string, isDir bool) string {
@@ -1441,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
}

View File

@@ -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)
}
}

View File

@@ -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)

View File

@@ -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
}