forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.go
187 lines (160 loc) · 4.15 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package commands
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/jbenet/go-ipfs/blocks"
cmds "github.com/jbenet/go-ipfs/commands"
u "github.com/jbenet/go-ipfs/util"
)
type BlockStat struct {
Key string
Size int
}
func (bs BlockStat) String() string {
return fmt.Sprintf("Key: %s\nSize: %d\n", bs.Key, bs.Size)
}
var BlockCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Manipulate raw IPFS blocks",
ShortDescription: `
'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
Reads from stdin or writes to stdout, and <key> is a base58 encoded
multihash.
`,
},
Subcommands: map[string]*cmds.Command{
"stat": blockStatCmd,
"get": blockGetCmd,
"put": blockPutCmd,
},
}
var blockStatCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Print information of a raw IPFS block",
ShortDescription: `
'ipfs block stat' is a plumbing command for retreiving information
on raw ipfs blocks. It outputs the following to stdout:
Key - the base58 encoded multihash
Size - the size of the block in bytes
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
b, err := getBlockForKey(req, req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&BlockStat{
Key: b.Key().Pretty(),
Size: len(b.Data),
})
},
Type: BlockStat{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
bs := res.Output().(*BlockStat)
return strings.NewReader(bs.String()), nil
},
},
}
var blockGetCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get a raw IPFS block",
ShortDescription: `
'ipfs block get' is a plumbing command for retreiving raw ipfs blocks.
It outputs to stdout, and <key> is a base58 encoded multihash.
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
b, err := getBlockForKey(req, req.Arguments()[0])
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(bytes.NewReader(b.Data))
},
}
var blockPutCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Stores input as an IPFS block",
ShortDescription: `
ipfs block put is a plumbing command for storing raw ipfs blocks.
It reads from stdin, and <key> is a base58 encoded multihash.
`,
},
Arguments: []cmds.Argument{
cmds.FileArg("data", true, false, "The data to be stored as an IPFS block").EnableStdin(),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.Context().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
file, err := req.Files().NextFile()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
data, err := ioutil.ReadAll(file)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
err = file.Close()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
b := blocks.NewBlock(data)
log.Debugf("BlockPut key: '%q'", b.Key())
k, err := n.Blocks.AddBlock(b)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&BlockStat{
Key: k.String(),
Size: len(data),
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
bs := res.Output().(*BlockStat)
return strings.NewReader(bs.Key + "\n"), nil
},
},
Type: BlockStat{},
}
func getBlockForKey(req cmds.Request, key string) (*blocks.Block, error) {
n, err := req.Context().GetNode()
if err != nil {
return nil, err
}
if !u.IsValidHash(key) {
return nil, errors.New("Not a valid hash")
}
h, err := mh.FromB58String(key)
if err != nil {
return nil, err
}
k := u.Key(h)
b, err := n.Blocks.GetBlock(context.TODO(), k)
if err != nil {
return nil, err
}
log.Debugf("ipfs block: got block with key: %q", b.Key())
return b, nil
}