Skip to content

Commit

Permalink
Merge pull request #130 from criteo/fix-large-zip-extraction
Browse files Browse the repository at this point in the history
Pkg install - Zip extraction - Close files as soon as they're copied.
  • Loading branch information
zeralight authored Nov 17, 2023
2 parents 8daa945 + 4f646d1 commit 3af1e39
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions internal/pkg/zip-package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}

0 comments on commit 3af1e39

Please sign in to comment.