-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: test and refactor split file
- Loading branch information
1 parent
7288cf2
commit f5ed02c
Showing
4 changed files
with
206 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package layout contains functions for interacting with Zarf's package layout on disk. | ||
package layout | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/defenseunicorns/pkg/helpers/v2" | ||
"github.com/defenseunicorns/zarf/src/pkg/message" | ||
"github.com/defenseunicorns/zarf/src/types" | ||
) | ||
|
||
// splitFile will split the file into chunks and remove the original file. | ||
func splitFile(srcPath string, chunkSize int) error { | ||
srcFile, err := os.Open(srcPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer srcFile.Close() | ||
fi, err := srcFile.Stat() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
title := fmt.Sprintf("[0/%d] MB bytes written", fi.Size()/1000/1000) | ||
progressBar := message.NewProgressBar(fi.Size(), title) | ||
defer progressBar.Close() | ||
|
||
hash := sha256.New() | ||
fileCount := 0 | ||
for { | ||
path := fmt.Sprintf("%s.part%03d", srcPath, fileCount+1) | ||
dstFile, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, helpers.ReadAllWriteUser) | ||
if err != nil { | ||
return err | ||
} | ||
defer dstFile.Close() | ||
|
||
written, copyErr := io.CopyN(dstFile, srcFile, int64(chunkSize)) | ||
if copyErr != nil && !errors.Is(copyErr, io.EOF) { | ||
return err | ||
} | ||
progressBar.Add(int(written)) | ||
title := fmt.Sprintf("[%d/%d] MB bytes written", progressBar.GetCurrent()/1000/1000, fi.Size()/1000/1000) | ||
progressBar.Updatef(title) | ||
|
||
_, err = dstFile.Seek(0, io.SeekStart) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = io.Copy(hash, dstFile) | ||
if err != nil { | ||
return err | ||
} | ||
err = dstFile.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// EOF error could be returned on 0 bytes written. | ||
if written == 0 { | ||
err = os.Remove(path) | ||
if err != nil { | ||
return err | ||
} | ||
break | ||
} | ||
|
||
fileCount++ | ||
if errors.Is(copyErr, io.EOF) { | ||
break | ||
} | ||
} | ||
|
||
// Remove original file | ||
err = srcFile.Close() | ||
if err != nil { | ||
return err | ||
} | ||
err = os.Remove(srcPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write header file | ||
data := types.ZarfSplitPackageData{ | ||
Count: fileCount, | ||
Bytes: fi.Size(), | ||
Sha256Sum: fmt.Sprintf("%x", hash.Sum(nil)), | ||
} | ||
b, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("unable to marshal the split package data: %w", err) | ||
} | ||
path := fmt.Sprintf("%s.part000", srcPath) | ||
if err := os.WriteFile(path, b, helpers.ReadAllWriteUser); err != nil { | ||
return fmt.Errorf("unable to write the file %s: %w", path, err) | ||
} | ||
progressBar.Successf("Package split across %d files", fileCount+1) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package layout | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/defenseunicorns/zarf/src/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSplitFile(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
fileSize int | ||
chunkSize int | ||
expectedFileSize int64 | ||
expectedLastFileSize int64 | ||
expectedFileCount int | ||
expectedSha256Sum string | ||
}{ | ||
{ | ||
name: "split evenly", | ||
fileSize: 2048, | ||
chunkSize: 16, | ||
expectedFileSize: 16, | ||
expectedLastFileSize: 16, | ||
expectedFileCount: 128, | ||
expectedSha256Sum: "93ecad679eff0df493aaf5d7d615211b0f1d7a919016efb15c98f0b8efb1ba43", | ||
}, | ||
{ | ||
name: "split with remainder", | ||
fileSize: 2048, | ||
chunkSize: 10, | ||
expectedFileSize: 10, | ||
expectedLastFileSize: 8, | ||
expectedFileCount: 205, | ||
expectedSha256Sum: "fe8460f4d53d3578aa37191acf55b3db7bbcb706056f4b6b02a0c70f24b0d95a", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
dir := t.TempDir() | ||
name := "random" | ||
p := filepath.Join(dir, name) | ||
f, err := os.Create(p) | ||
require.NoError(t, err) | ||
b := make([]byte, tt.fileSize) | ||
for i := range tt.fileSize { | ||
b[i] = byte(tt.chunkSize) | ||
} | ||
require.NoError(t, err) | ||
_, err = f.Write(b) | ||
require.NoError(t, err) | ||
f.Close() | ||
|
||
err = splitFile(p, tt.chunkSize) | ||
require.NoError(t, err) | ||
|
||
_, err = os.Stat(p) | ||
require.ErrorIs(t, err, os.ErrNotExist) | ||
entries, err := os.ReadDir(dir) | ||
require.NoError(t, err) | ||
require.Len(t, entries, tt.expectedFileCount+1) | ||
for i, entry := range entries[1:] { | ||
require.Equal(t, fmt.Sprintf("%s.part%03d", name, i+1), entry.Name()) | ||
|
||
fi, err := entry.Info() | ||
require.NoError(t, err) | ||
if i == len(entries)-2 { | ||
require.Equal(t, tt.expectedLastFileSize, fi.Size()) | ||
} else { | ||
require.Equal(t, tt.expectedFileSize, fi.Size()) | ||
} | ||
} | ||
|
||
b, err = os.ReadFile(filepath.Join(dir, fmt.Sprintf("%s.part000", name))) | ||
require.NoError(t, err) | ||
var data types.ZarfSplitPackageData | ||
err = json.Unmarshal(b, &data) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expectedFileCount, data.Count) | ||
require.Equal(t, int64(tt.fileSize), data.Bytes) | ||
require.Equal(t, tt.expectedSha256Sum, data.Sha256Sum) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters