From c68ab5620a8a1026c2f817c5831ca671b24ca70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 20 Sep 2018 15:59:53 +0200 Subject: [PATCH] coreapi unixfs: cid prefix options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/options/unixfs.go | 8 +++- core/coreapi/interface/unixfs.go | 1 + core/coreapi/unixfs.go | 52 ++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/core/coreapi/interface/options/unixfs.go b/core/coreapi/interface/options/unixfs.go index 8dc9806a7..ffed75577 100644 --- a/core/coreapi/interface/options/unixfs.go +++ b/core/coreapi/interface/options/unixfs.go @@ -8,7 +8,9 @@ type UnixfsAddSettings struct { CidVersion int MhType uint64 - InlineLimit int + InlineLimit int + RawLeaves bool + RawLeavesSet bool } type UnixfsAddOption func(*UnixfsAddSettings) error @@ -18,7 +20,9 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) { CidVersion: -1, MhType: mh.SHA2_256, - InlineLimit: 0, + InlineLimit: 0, + RawLeaves: false, + RawLeavesSet: false, } for _, opt := range opts { diff --git a/core/coreapi/interface/unixfs.go b/core/coreapi/interface/unixfs.go index 10febd9fa..acc3b960c 100644 --- a/core/coreapi/interface/unixfs.go +++ b/core/coreapi/interface/unixfs.go @@ -10,6 +10,7 @@ import ( ) // UnixfsAPI is the basic interface to immutable files in IPFS +// NOTE: This API is heavily WIP, things are guaranteed to break frequently type UnixfsAPI interface { // Add imports the data from the reader into merkledag file Add(context.Context, io.ReadCloser, ...options.UnixfsAddOption) (ResolvedPath, error) diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 27962202e..afa66f0ae 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -2,15 +2,19 @@ package coreapi import ( "context" + "errors" + "fmt" "io" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" - options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" - coreunix "github.com/ipfs/go-ipfs/core/coreunix" + "github.com/ipfs/go-ipfs/core/coreapi/interface/options" + "github.com/ipfs/go-ipfs/core/coreunix" cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" + mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files" uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io" + dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" ) @@ -19,11 +23,42 @@ type UnixfsAPI CoreAPI // Add builds a merkledag node from a reader, adds it to the blockstore, // and returns the key representing that node. func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) { - _, err := options.UnixfsAddOptions(opts...) + settings, err := options.UnixfsAddOptions(opts...) if err != nil { return nil, err } + // TODO: move to options + // (hash != "sha2-256") -> CIDv1 + if settings.MhType != mh.SHA2_256 { + switch settings.CidVersion { + case 0: + return nil, errors.New("CIDv0 only supports sha2-256") + case 1, -1: + settings.CidVersion = 1 + default: + return nil, fmt.Errorf("unknown CID version: %d", settings.CidVersion) + } + } else { + if settings.CidVersion < 0 { + // Default to CIDv0 + settings.CidVersion = 0 + } + } + + // cidV1 -> raw blocks (by default) + if settings.CidVersion > 0 && !settings.RawLeavesSet { + settings.RawLeaves = true + } + + prefix, err := dag.PrefixForCidVersion(settings.CidVersion) + if err != nil { + return nil, err + } + + prefix.MhType = settings.MhType + prefix.MhLength = -1 + outChan := make(chan interface{}, 1) fileAdder, err := coreunix.NewAdder(ctx, api.node.Pinning, api.node.Blockstore, api.node.DAG) @@ -32,6 +67,17 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options. } fileAdder.Out = outChan + //fileAdder.Chunker = chunker + //fileAdder.Progress = progress + //fileAdder.Hidden = hidden + //fileAdder.Trickle = trickle + //fileAdder.Wrap = wrap + //fileAdder.Pin = dopin + fileAdder.Silent = false + fileAdder.RawLeaves = settings.RawLeaves + //fileAdder.NoCopy = nocopy + //fileAdder.Name = pathName + fileAdder.CidBuilder = prefix err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil)) if err != nil {