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

fix: multi-part tarballs being mismatched sizes #2314

Merged
merged 12 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/pkg/utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func ReadFileByChunks(path string, chunkSizeBytes int) (chunks [][]byte, sha256s
// - fileNames: list of file paths srcFile was split across
// - sha256sum: sha256sum of the srcFile before splitting
// - err: any errors encountered
func SplitFile(srcFile string, chunkSizeBytes int) (err error) {
func SplitFile(srcPath string, chunkSizeBytes int) (err error) {
var fileNames []string
var sha256sum string
hash := sha256.New()
Expand All @@ -353,7 +353,7 @@ func SplitFile(srcFile string, chunkSizeBytes int) (err error) {
buf := make([]byte, bufferSize)

// get file size
fi, err := os.Stat(srcFile)
fi, err := os.Stat(srcPath)
if err != nil {
return err
}
Expand All @@ -364,15 +364,15 @@ func SplitFile(srcFile string, chunkSizeBytes int) (err error) {
progressBar := message.NewProgressBar(fileSize, title)
defer progressBar.Stop()

// open file
file, err := os.Open(srcFile)
defer file.Close()
// open srcFile
srcFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer srcFile.Close()

// create file path starting from part 001
path := fmt.Sprintf("%s.part001", srcFile)
path := fmt.Sprintf("%s.part001", srcPath)
chunkFile, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
Expand All @@ -384,7 +384,7 @@ func SplitFile(srcFile string, chunkSizeBytes int) (err error) {
chunkBytesRemaining := chunkSizeBytes
// Loop over the tarball hashing as we go and breaking it into chunks based on the chunkSizeBytes
for {
bytesRead, err := file.Read(buf)
bytesRead, err := srcFile.Read(buf)

if err != nil {
if err == io.EOF {
Expand All @@ -404,10 +404,14 @@ func SplitFile(srcFile string, chunkSizeBytes int) (err error) {
if err != nil {
return err
}
err = chunkFile.Close()
if err != nil {
return err
}

// create new file
path = fmt.Sprintf("%s.part%03d", srcFile, len(fileNames)+1)
chunkFile, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
path = fmt.Sprintf("%s.part%03d", srcPath, len(fileNames)+1)
chunkFile, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -435,8 +439,8 @@ func SplitFile(srcFile string, chunkSizeBytes int) (err error) {
title := fmt.Sprintf("[%d/%d] MB bytes written", progressBar.GetCurrent()/1000/1000, fileSize/1000/1000)
progressBar.UpdateTitle(title)
}
file.Close()
_ = os.RemoveAll(srcFile)
srcFile.Close()
_ = os.RemoveAll(srcPath)

// calculate sha256 sum
sha256sum = fmt.Sprintf("%x", hash.Sum(nil))
Expand All @@ -452,7 +456,7 @@ func SplitFile(srcFile string, chunkSizeBytes int) (err error) {
}

// write header file
path = fmt.Sprintf("%s.part000", srcFile)
path = fmt.Sprintf("%s.part000", srcPath)
if err := os.WriteFile(path, jsonData, 0644); err != nil {
return fmt.Errorf("unable to write the file %s: %w", path, err)
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/e2e/05_tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package test

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -36,6 +37,21 @@ func TestMultiPartPackage(t *testing.T) {
require.NoError(t, err)
// Length is 7 because there are 6 parts and 1 manifest
require.Len(t, parts, 7)
// Check the file sizes are even
part1FileInfo, err := os.Stat(parts[1])
require.NoError(t, err)
require.Equal(t, int64(1000000), part1FileInfo.Size())
part2FileInfo, err := os.Stat(parts[2])
require.NoError(t, err)
require.Equal(t, int64(1000000), part2FileInfo.Size())
// Check the package data is correct
pkgData := types.ZarfSplitPackageData{}
part0File, err := os.ReadFile(parts[0])
require.NoError(t, err)
err = json.Unmarshal(part0File, &pkgData)
require.NoError(t, err)
require.Equal(t, pkgData.Count, 6)
fmt.Printf("%#v", pkgData)
Racer159 marked this conversation as resolved.
Show resolved Hide resolved

stdOut, stdErr, err = e2e.Zarf("package", "deploy", deployPath, "--confirm")
require.NoError(t, err, stdOut, stdErr)
Expand All @@ -45,6 +61,17 @@ func TestMultiPartPackage(t *testing.T) {

// deploying package combines parts back into single archive, check dir again to find all files
parts, err = filepath.Glob("zarf-package-multi-part-*")
require.NoError(t, err)
// Length is 1 because `zarf package deploy` will recombine the file
require.Len(t, parts, 1)
// Ensure that the number of pkgData bytes was correct
fullFileInfo, err := os.Stat(parts[0])
require.NoError(t, err)
require.Equal(t, pkgData.Bytes, fullFileInfo.Size())
// Ensure that the pkgData shasum was correct (should be checked during deploy as well, but this is to double check)
err = utils.SHAsMatch(parts[0], pkgData.Sha256Sum)
require.NoError(t, err)

e2e.CleanFiles(parts...)
e2e.CleanFiles(outputFile)
}
Expand Down