Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Machine decompress.go refactoring follow-up #21864

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hugelgupf/p9 v0.3.1-0.20230822151754-54f5c5530921
github.com/json-iterator/go v1.1.12
github.com/klauspost/compress v1.17.7
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2
github.com/mattn/go-shellwords v1.0.12
github.com/mattn/go-sqlite3 v1.14.22
Expand All @@ -65,7 +64,6 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
github.com/ulikunitz/xz v0.5.11
github.com/vbauerster/mpb/v8 v8.7.2
github.com/vishvananda/netlink v1.2.1-beta.2
go.etcd.io/bbolt v1.3.9
Expand Down Expand Up @@ -149,6 +147,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/kr/fs v0.1.0 // indirect
Expand Down Expand Up @@ -198,6 +197,7 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/u-root/uio v0.0.0-20230305220412-3e8cd9d6bf63 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/machine/compression/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func Test_Decompress(t *testing.T) {
}{
{name: "zip", args: args{src: "./testdata/sample.zip", dst: "./testdata/hellozip"}, want: "zip\n"},
{name: "zip with trailing zeros", args: args{src: "./testdata/sample-withzeros.zip", dst: "./testdata/hellozip-withzeros"}, want: "zip\n\x00\x00\x00\x00\x00\x00"},
{name: "xz", args: args{src: "./testdata/sample.xz", dst: "./testdata/helloxz"}, want: "xz\n"},
{name: "xz with trailing zeros", args: args{src: "./testdata/sample-withzeros.xz", dst: "./testdata/helloxz-withzeros"}, want: "xz\n\x00\x00\x00\x00\x00\x00\x00"},
{name: "gzip", args: args{src: "./testdata/sample.gz", dst: "./testdata/hellogz"}, want: "gzip\n"},
{name: "gzip with trailing zeros", args: args{src: "./testdata/sample-withzeros.gz", dst: "./testdata/hellogzip-withzeros"}, want: "gzip\n\x00\x00\x00\x00\x00"},
{name: "bzip2", args: args{src: "./testdata/sample.bz2", dst: "./testdata/hellobz2"}, want: "bzip2\n"},
Expand Down
10 changes: 0 additions & 10 deletions pkg/machine/compression/decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/containers/podman/v5/pkg/machine/define"
Expand Down Expand Up @@ -46,24 +45,15 @@ func Decompress(compressedVMFile *define.VMFile, decompressedFilePath string) er

func newDecompressor(compressedFilePath string, compressedFileMagicNum []byte) (decompressor, error) {
compressionType := archive.DetectCompression(compressedFileMagicNum)
os := runtime.GOOS
hasZipSuffix := strings.HasSuffix(compressedFilePath, zipExt)

switch {
case compressionType == archive.Xz:
return newXzDecompressor(compressedFilePath)
// Zip files are not guaranteed to have a magic number at the beginning
// of the file, so we need to use the file name to detect them.
case compressionType == archive.Uncompressed && hasZipSuffix:
return newZipDecompressor(compressedFilePath)
case compressionType == archive.Uncompressed:
return newUncompressedDecompressor(compressedFilePath)
// Using special compressors on MacOS because default ones
// in c/image/pkg/compression are slow with sparse files.
case compressionType == archive.Gzip && os == macOs:
return newGzipDecompressor(compressedFilePath)
case compressionType == archive.Zstd && os == macOs:
return newZstdDecompressor(compressedFilePath)
default:
return newGenericDecompressor(compressedFilePath)
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/machine/compression/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"io/fs"
"os"
"runtime"

"github.com/containers/image/v5/pkg/compression"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -54,7 +55,15 @@ func (d *genericDecompressor) decompress(w io.WriteSeeker, r io.Reader) error {
}
}()

_, err = io.Copy(w, decompressedFileReader)
// Use sparse-optimized copy for macOS as applehv,
// macOS native hypervisor, uses large raw VM disk
// files mostly empty (~2GB of content ~8GB empty).
if runtime.GOOS == macOs {
err = d.sparseOptimizedCopy(w, decompressedFileReader)
} else {
_, err = io.Copy(w, decompressedFileReader)
}

return err
}

Expand Down
31 changes: 0 additions & 31 deletions pkg/machine/compression/gzip.go

This file was deleted.

66 changes: 0 additions & 66 deletions pkg/machine/compression/xz.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/machine/compression/zstd.go

This file was deleted.