From b176dd8a27e298daa28151a55c48c0263dd1c4e4 Mon Sep 17 00:00:00 2001 From: Keenan Nemetz Date: Fri, 29 Jan 2021 14:18:42 -0800 Subject: [PATCH] Export unixfs chunk function. --- unixfs/add.go | 47 +++++++++++------------------------------------ unixfs/chunk.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ unixfs/merge.go | 2 +- 3 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 unixfs/chunk.go diff --git a/unixfs/add.go b/unixfs/add.go index 8cf42e8..c1b077a 100644 --- a/unixfs/add.go +++ b/unixfs/add.go @@ -3,23 +3,16 @@ package unixfs import ( "context" "errors" - "io" "io/ioutil" "os" "path/filepath" - "github.com/ipfs/go-ipfs-chunker" ipld "github.com/ipfs/go-ipld-format" "github.com/ipfs/go-merkledag" ufs "github.com/ipfs/go-unixfs" - "github.com/ipfs/go-unixfs/importer/balanced" - "github.com/ipfs/go-unixfs/importer/helpers" ufsio "github.com/ipfs/go-unixfs/io" ) -// DefaultChunker is the name of the default chunker algorithm. -const DefaultChunker = "buzhash" - // Ignore is used to filter files. type Ignore []string @@ -58,32 +51,6 @@ func Add(ctx context.Context, dag ipld.DAGService, path string, ignore Ignore) ( } } -// addReader splits the given reader into chunks and arranges them into a dag node. -func addReader(ctx context.Context, dag ipld.DAGService, reader io.Reader) (ipld.Node, error) { - chunker, err := chunk.FromString(reader, DefaultChunker) - if err != nil { - return nil, err - } - - params := helpers.DagBuilderParams{ - Dagserv: dag, - CidBuilder: merkledag.V1CidPrefix(), - Maxlinks: helpers.DefaultLinksPerBlock, - } - - helper, err := params.New(chunker) - if err != nil { - return nil, err - } - - node, err := balanced.Layout(helper) - if err != nil { - return nil, err - } - - return node, dag.Add(ctx, node) -} - // addFile creates a dag node from the file at the given path. func addFile(ctx context.Context, dag ipld.DAGService, path string) (ipld.Node, error) { file, err := os.Open(path) @@ -92,7 +59,7 @@ func addFile(ctx context.Context, dag ipld.DAGService, path string) (ipld.Node, } defer file.Close() - return addReader(ctx, dag, file) + return Chunk(ctx, dag, file) } // addSymlink creates a dag node from the symlink at the given path. @@ -108,7 +75,11 @@ func addSymlink(ctx context.Context, dag ipld.DAGService, path string) (ipld.Nod } node := merkledag.NodeWithData(data) - return node, dag.Add(ctx, node) + if err := dag.Add(ctx, node); err != nil { + return nil, err + } + + return node, nil } // addDir creates a dag node from the directory entries at the given path. @@ -140,5 +111,9 @@ func addDir(ctx context.Context, dag ipld.DAGService, path string, ignore Ignore return nil, err } - return node, dag.Add(ctx, node) + if err := dag.Add(ctx, node); err != nil { + return nil, err + } + + return node, nil } diff --git a/unixfs/chunk.go b/unixfs/chunk.go new file mode 100644 index 0000000..587c68c --- /dev/null +++ b/unixfs/chunk.go @@ -0,0 +1,45 @@ +package unixfs + +import ( + "context" + "io" + + chunk "github.com/ipfs/go-ipfs-chunker" + ipld "github.com/ipfs/go-ipld-format" + "github.com/ipfs/go-merkledag" + "github.com/ipfs/go-unixfs/importer/balanced" + "github.com/ipfs/go-unixfs/importer/helpers" +) + +// DefaultChunker is the name of the default chunker algorithm. +const DefaultChunker = "buzhash" + +// Chunk splits the given reader into chunks and arranges them into a dag node. +func Chunk(ctx context.Context, dag ipld.DAGService, reader io.Reader) (ipld.Node, error) { + chunker, err := chunk.FromString(reader, DefaultChunker) + if err != nil { + return nil, err + } + + params := helpers.DagBuilderParams{ + Dagserv: dag, + CidBuilder: merkledag.V1CidPrefix(), + Maxlinks: helpers.DefaultLinksPerBlock, + } + + helper, err := params.New(chunker) + if err != nil { + return nil, err + } + + node, err := balanced.Layout(helper) + if err != nil { + return nil, err + } + + if err := dag.Add(ctx, node); err != nil { + return nil, err + } + + return node, nil +} diff --git a/unixfs/merge.go b/unixfs/merge.go index 4d99a35..c5e8e65 100644 --- a/unixfs/merge.go +++ b/unixfs/merge.go @@ -29,5 +29,5 @@ func Merge(ctx context.Context, dag ipld.DAGService, o, a, b cid.Cid) (ipld.Node merged := diff3.Merge(textO, textA, textB) reader := strings.NewReader(merged) - return addReader(ctx, dag, reader) + return Chunk(ctx, dag, reader) }