Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill committed Feb 29, 2024
1 parent 518416d commit 3f9fab7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
17 changes: 2 additions & 15 deletions cmd/bee/cmd/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func splitChunks(cmd *cobra.Command) {
var chunksCount int64
go func() {
for chunk := range store.c {
err := writeChunkToFile(outputDir, chunk)
filePath := filepath.Join(outputDir, chunk.Address().String())
err := os.WriteFile(filePath, chunk.Data(), 0644)
if err != nil {
logger.Error(err, "write chunk")
cancel()
Expand All @@ -224,17 +225,3 @@ func splitChunks(cmd *cobra.Command) {

cmd.AddCommand(c)
}

func writeChunkToFile(outputDir string, chunk swarm.Chunk) error {
path := filepath.Join(outputDir, chunk.Address().String())
writer, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("open output file: %w", err)
}
defer writer.Close()
_, err = writer.Write(chunk.Data())
if err != nil {
return fmt.Errorf("write chunk: %w", err)
}
return nil
}
29 changes: 29 additions & 0 deletions cmd/bee/cmd/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"math/rand"
"os"
"path"
"path/filepath"
"testing"

"github.com/ethersphere/bee/cmd/bee/cmd"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/cac"
"github.com/ethersphere/bee/pkg/soc"
"github.com/ethersphere/bee/pkg/swarm"
)

func TestDBSplitRefs(t *testing.T) {
Expand Down Expand Up @@ -103,4 +107,29 @@ func TestDBSplitChunks(t *testing.T) {
t.Fatalf("want at least %d chunks", want)
}

for _, entry := range entries {
d, err := os.ReadFile(filepath.Join(dir, entry.Name()))
if err != nil {
t.Fatal(err)
}

ch, err := cac.NewWithDataSpan(d)
if err != nil {
sch, err := soc.FromChunk(swarm.NewChunk(swarm.EmptyAddress, d))
if err != nil {
t.Fatal("invalid cac/soc chunk", err)
}
ch, err = sch.Chunk()
if err != nil {
t.Fatal(err)
}
if !soc.Valid(ch) {
t.Fatal("invalid soc chunk")
}
}

if ch.Address().String() != entry.Name() {
t.Fatal("expected chunk reference to equal file name")
}
}
}

0 comments on commit 3f9fab7

Please sign in to comment.