提交 0f234d76 作者: Łukasz Magiera

dag: add option to specify hash

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 a1e923e2
...@@ -3,6 +3,7 @@ package dagcmd ...@@ -3,6 +3,7 @@ package dagcmd
import ( import (
"fmt" "fmt"
"io" "io"
"math"
"strings" "strings"
cmds "github.com/ipfs/go-ipfs/commands" cmds "github.com/ipfs/go-ipfs/commands"
...@@ -11,6 +12,7 @@ import ( ...@@ -11,6 +12,7 @@ import (
pin "github.com/ipfs/go-ipfs/pin" pin "github.com/ipfs/go-ipfs/pin"
cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid" cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
) )
var DagCmd = &cmds.Command{ var DagCmd = &cmds.Command{
...@@ -48,6 +50,7 @@ into an object of the specified format. ...@@ -48,6 +50,7 @@ into an object of the specified format.
cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"), cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"),
cmds.StringOption("input-enc", "Format that the input object will be.").Default("json"), cmds.StringOption("input-enc", "Format that the input object will be.").Default("json"),
cmds.BoolOption("pin", "Pin this object when adding.").Default(false), cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
cmds.StringOption("hash", "Hash function to use").Default(""),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode() n, err := req.InvocContext().GetNode()
...@@ -64,17 +67,31 @@ into an object of the specified format. ...@@ -64,17 +67,31 @@ into an object of the specified format.
ienc, _, _ := req.Option("input-enc").String() ienc, _, _ := req.Option("input-enc").String()
format, _, _ := req.Option("format").String() format, _, _ := req.Option("format").String()
hash, _, err := req.Option("hash").String()
dopin, _, err := req.Option("pin").Bool() dopin, _, err := req.Option("pin").Bool()
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
// mhType tells inputParser which hash should be used. MaxUint64 means 'use
// default hash' (sha256 for cbor, sha1 for git..)
mhType := uint64(math.MaxUint64)
if hash != "" {
var ok bool
mhType, ok = mh.Names[hash]
if !ok {
res.SetError(fmt.Errorf("%s in not a valid multihash name", hash), cmds.ErrNormal)
return
}
}
if dopin { if dopin {
defer n.Blockstore.PinLock().Unlock() defer n.Blockstore.PinLock().Unlock()
} }
nds, err := coredag.ParseInputs(ienc, format, fi) nds, err := coredag.ParseInputs(ienc, format, fi, mhType, -1)
if err != nil { if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"math"
"strings" "strings"
"testing" "testing"
...@@ -16,7 +17,7 @@ import ( ...@@ -16,7 +17,7 @@ import (
config "github.com/ipfs/go-ipfs/repo/config" config "github.com/ipfs/go-ipfs/repo/config"
testutil "github.com/ipfs/go-ipfs/thirdparty/testutil" testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
unixfs "github.com/ipfs/go-ipfs/unixfs" unixfs "github.com/ipfs/go-ipfs/unixfs"
cbor "gx/ipfs/QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax/go-ipld-cbor" cbor "gx/ipfs/QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM/go-ipld-cbor"
) )
// `echo -n 'hello, world!' | ipfs add` // `echo -n 'hello, world!' | ipfs add`
...@@ -277,7 +278,7 @@ func TestLsNonUnixfs(t *testing.T) { ...@@ -277,7 +278,7 @@ func TestLsNonUnixfs(t *testing.T) {
t.Error(err) t.Error(err)
} }
nd, err := cbor.WrapObject(map[string]interface{}{"foo": "bar"}) nd, err := cbor.WrapObject(map[string]interface{}{"foo": "bar"}, math.MaxUint64, -1)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
package coredag
import (
"io"
"io/ioutil"
node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
ipldcbor "gx/ipfs/QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM/go-ipld-cbor"
)
func cborJSONParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
nd, err := ipldcbor.FromJson(r, mhType, mhLen)
if err != nil {
return nil, err
}
return []node.Node{nd}, nil
}
func cborRawParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
nd, err := ipldcbor.Decode(data, mhType, mhLen)
if err != nil {
return nil, err
}
return []node.Node{nd}, nil
}
...@@ -3,14 +3,12 @@ package coredag ...@@ -3,14 +3,12 @@ package coredag
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
ipldcbor "gx/ipfs/QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax/go-ipld-cbor"
node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format" node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
) )
// DagParser is function used for parsing stream into Node // DagParser is function used for parsing stream into Node
type DagParser func(r io.Reader) ([]node.Node, error) type DagParser func(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error)
// FormatParsers is used for mapping format descriptors to DagParsers // FormatParsers is used for mapping format descriptors to DagParsers
type FormatParsers map[string]DagParser type FormatParsers map[string]DagParser
...@@ -36,8 +34,8 @@ var defaultRawParsers = FormatParsers{ ...@@ -36,8 +34,8 @@ var defaultRawParsers = FormatParsers{
// ParseInputs uses DefaultInputEncParsers to parse io.Reader described by // ParseInputs uses DefaultInputEncParsers to parse io.Reader described by
// input encoding and format to an instance of ipld Node // input encoding and format to an instance of ipld Node
func ParseInputs(ienc, format string, r io.Reader) ([]node.Node, error) { func ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
return DefaultInputEncParsers.ParseInputs(ienc, format, r) return DefaultInputEncParsers.ParseInputs(ienc, format, r, mhType, mhLen)
} }
// AddParser adds DagParser under give input encoding and format // AddParser adds DagParser under give input encoding and format
...@@ -53,39 +51,16 @@ func (iep InputEncParsers) AddParser(ienv, format string, f DagParser) { ...@@ -53,39 +51,16 @@ func (iep InputEncParsers) AddParser(ienv, format string, f DagParser) {
// ParseInputs parses io.Reader described by input encoding and format to // ParseInputs parses io.Reader described by input encoding and format to
// an instance of ipld Node // an instance of ipld Node
func (iep InputEncParsers) ParseInputs(ienc, format string, r io.Reader) ([]node.Node, error) { func (iep InputEncParsers) ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
pset, ok := iep[ienc] parsers, ok := iep[ienc]
if !ok { if !ok {
return nil, fmt.Errorf("no input parser for %q", ienc) return nil, fmt.Errorf("no input parser for %q", ienc)
} }
parser, ok := pset[format] parser, ok := parsers[format]
if !ok { if !ok {
return nil, fmt.Errorf("no parser for format %q using input type %q", format, ienc) return nil, fmt.Errorf("no parser for format %q using input type %q", format, ienc)
} }
return parser(r) return parser(r, mhType, mhLen)
}
func cborJSONParser(r io.Reader) ([]node.Node, error) {
nd, err := ipldcbor.FromJson(r)
if err != nil {
return nil, err
}
return []node.Node{nd}, nil
}
func cborRawParser(r io.Reader) ([]node.Node, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
nd, err := ipldcbor.Decode(data)
if err != nil {
return nil, err
}
return []node.Node{nd}, nil
} }
...@@ -11,8 +11,8 @@ import ( ...@@ -11,8 +11,8 @@ import (
cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid" cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
blocks "gx/ipfs/QmVA4mafxbfH5aEvNz8fyoxC6J1xhAtw88B4GerPznSZBg/go-block-format" blocks "gx/ipfs/QmVA4mafxbfH5aEvNz8fyoxC6J1xhAtw88B4GerPznSZBg/go-block-format"
ipldcbor "gx/ipfs/QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax/go-ipld-cbor"
node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format" node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
ipldcbor "gx/ipfs/QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM/go-ipld-cbor"
) )
// TODO: We should move these registrations elsewhere. Really, most of the IPLD // TODO: We should move these registrations elsewhere. Really, most of the IPLD
......
...@@ -261,9 +261,9 @@ ...@@ -261,9 +261,9 @@
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmXgUVPAxjMLZSyxx818YstJJAoRg3nyPWENmBLVzLtoax", "hash": "QmeebqVZeEXBqJ2B4urQWfdhwRRPm84ajnCo8x8pfwbsPM",
"name": "go-ipld-cbor", "name": "go-ipld-cbor",
"version": "1.2.7" "version": "1.2.8"
}, },
{ {
"author": "lgierth", "author": "lgierth",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论