From 4f646d10a02de5add4059e1d2d14fb83842455ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=92=A5Hedi=20Ghediri?= Date: Thu, 16 Nov 2023 18:00:03 +0100 Subject: [PATCH] zip extract - introduce a function to copy each file to destination This will ensure each zip file handler is closed just after the copy is done. --- internal/pkg/zip-package.go | 90 ++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/internal/pkg/zip-package.go b/internal/pkg/zip-package.go index c892169..67a40df 100644 --- a/internal/pkg/zip-package.go +++ b/internal/pkg/zip-package.go @@ -50,47 +50,8 @@ func (pkg *zipPackage) InstallTo(targetDir string) (command.PackageManifest, err zipReader, _ := zip.OpenReader(pkg.ZipFile) defer zipReader.Close() for _, file := range zipReader.Reader.File { - zippedFile, err := file.Open() - if err != nil { - return nil, fmt.Errorf("installation failed: %s", err) - } - defer zippedFile.Close() - - extractedFilePath := filepath.Join(targetDir, file.Name) - if file.FileInfo().IsDir() { - log.Println("Directory Created:", extractedFilePath) - err := os.MkdirAll(extractedFilePath, file.Mode()) - if err != nil { - return nil, fmt.Errorf("directory extraction failed: %s", err) - } - - fileStats, err := os.Stat(extractedFilePath) - if err != nil { - return nil, fmt.Errorf("failed to stat %s: %s", extractedFilePath, err) - } - permissions := fileStats.Mode().Perm() - if permissions != 0o755 { - // chmod to 755 - if err := os.Chmod(extractedFilePath, 0755); err != nil { - return nil, fmt.Errorf("failed to chmod %s to 0755: %s", extractedFilePath, err) - } - } - } else { - log.Println("File extracted:", file.Name) - outputFile, err := os.OpenFile( - extractedFilePath, - os.O_WRONLY|os.O_CREATE|os.O_TRUNC, - file.Mode(), - ) - if err != nil { - return nil, fmt.Errorf("file extraction failed: %s", err) - } - defer outputFile.Close() - - _, err = io.Copy(outputFile, zippedFile) - if err != nil { - return nil, fmt.Errorf("file data extraction failed: %s", err) - } + if err := extractZipEntry(targetDir, file); err != nil { + return nil, err } } @@ -133,3 +94,50 @@ func packageChecksum(pkgFile string) ([]byte, error) { return h.Sum(nil), nil } + +func extractZipEntry(targetDir string, file *zip.File) error { + zippedFile, err := file.Open() + if err != nil { + return fmt.Errorf("installation failed: %s", err) + } + defer zippedFile.Close() + + extractedFilePath := filepath.Join(targetDir, file.Name) + if file.FileInfo().IsDir() { + log.Println("Directory Created:", extractedFilePath) + err := os.MkdirAll(extractedFilePath, file.Mode()) + if err != nil { + return fmt.Errorf("directory extraction failed: %s", err) + } + + fileStats, err := os.Stat(extractedFilePath) + if err != nil { + return fmt.Errorf("failed to stat %s: %s", extractedFilePath, err) + } + permissions := fileStats.Mode().Perm() + if permissions != 0o755 { + // chmod to 755 + if err := os.Chmod(extractedFilePath, 0755); err != nil { + return fmt.Errorf("failed to chmod %s to 0755: %s", extractedFilePath, err) + } + } + } else { + log.Println("File extracted:", file.Name) + outputFile, err := os.OpenFile( + extractedFilePath, + os.O_WRONLY|os.O_CREATE|os.O_TRUNC, + file.Mode(), + ) + if err != nil { + return fmt.Errorf("file extraction failed: %s", err) + } + defer outputFile.Close() + + _, err = io.Copy(outputFile, zippedFile) + if err != nil { + return fmt.Errorf("file data extraction failed: %s", err) + } + } + + return nil +}