diff --git a/cmd/bee/cmd/split.go b/cmd/bee/cmd/split.go index e6448b65f8a..d74650e066a 100644 --- a/cmd/bee/cmd/split.go +++ b/cmd/bee/cmd/split.go @@ -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() @@ -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 -} diff --git a/cmd/bee/cmd/split_test.go b/cmd/bee/cmd/split_test.go index fd5b0762a51..c53bdfcd4fb 100644 --- a/cmd/bee/cmd/split_test.go +++ b/cmd/bee/cmd/split_test.go @@ -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) { @@ -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") + } + } }