Skip to content

Commit 07d47e9

Browse files
make tar.gz packaging faster (#9093) (#9212)
(cherry picked from commit f48b694) Co-authored-by: Mikołaj Świątek <mail@mikolajswiatek.com>
1 parent d566c18 commit 07d47e9

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

dev-tools/mage/pkgtypes.go

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,45 @@ func PackageZip(spec PackageSpec) error {
647647

648648
// PackageTarGz packages a gzipped tar file.
649649
func PackageTarGz(spec PackageSpec) error {
650-
// Create a buffer to write our archive to.
651-
buf := new(bytes.Buffer)
650+
baseDir := spec.rootDir()
651+
652+
// Create the output file.
653+
if spec.OutputFile == "" {
654+
outputTarGz, err := spec.Expand(defaultBinaryName + ".tar.gz")
655+
if err != nil {
656+
return err
657+
}
658+
spec.OutputFile = filepath.Join(distributionsDir, outputTarGz)
659+
}
660+
spec.OutputFile = TarGz.AddFileExtension(spec.OutputFile)
661+
662+
// Open the output file.
663+
log.Println("Creating output file at", spec.OutputFile)
664+
outFile, err := os.Create(CreateDir(spec.OutputFile))
665+
if err != nil {
666+
return err
667+
}
668+
defer func() {
669+
if err := outFile.Close(); err != nil {
670+
log.Printf("failed to close output file: %v", err)
671+
}
672+
}()
673+
674+
// Create a gzip writer to our output file
675+
gzWriter := gzip.NewWriter(outFile)
676+
defer func() {
677+
if err := gzWriter.Close(); err != nil {
678+
log.Printf("failed to close gzip writer: %v", err)
679+
}
680+
}()
652681

653682
// Create a new tar archive.
654-
w := tar.NewWriter(buf)
655-
baseDir := spec.rootDir()
683+
w := tar.NewWriter(gzWriter)
684+
defer func() {
685+
if err := w.Close(); err != nil {
686+
log.Printf("failed to close tar writer: %v", err)
687+
}
688+
}()
656689

657690
// // Replace the darwin-universal by darwin-x86_64 and darwin-arm64. Also
658691
// // keep the other files.
@@ -706,33 +739,10 @@ func PackageTarGz(spec PackageSpec) error {
706739
if err := w.Close(); err != nil {
707740
return err
708741
}
709-
710-
// Output tar.gz to disk.
711-
if spec.OutputFile == "" {
712-
outputTarGz, err := spec.Expand(defaultBinaryName + ".tar.gz")
713-
if err != nil {
714-
return err
715-
}
716-
spec.OutputFile = filepath.Join(distributionsDir, outputTarGz)
717-
}
718-
spec.OutputFile = TarGz.AddFileExtension(spec.OutputFile)
719-
720-
// Open the output file.
721-
log.Println("Creating output file at", spec.OutputFile)
722-
outFile, err := os.Create(CreateDir(spec.OutputFile))
723-
if err != nil {
724-
return err
725-
}
726-
defer outFile.Close()
727-
728-
// Gzip compress the data.
729-
gzWriter := gzip.NewWriter(outFile)
730-
if _, err = gzWriter.Write(buf.Bytes()); err != nil {
742+
if err := gzWriter.Close(); err != nil {
731743
return err
732744
}
733-
734-
// Close and flush.
735-
if err = gzWriter.Close(); err != nil {
745+
if err := outFile.Close(); err != nil {
736746
return err
737747
}
738748

0 commit comments

Comments
 (0)