Skip to content

Commit 07bf2bd

Browse files
authored
Merge pull request #6331 from ipfs/feat/block-put-many
block cmd: allow adding multiple blocks at once
2 parents ff3efe8 + 7be8475 commit 07bf2bd

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

core/commands/block.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io"
77
"os"
88

9+
files "github.com/ipfs/go-ipfs-files"
10+
911
util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
1012
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
1113

@@ -129,7 +131,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
129131
},
130132

131133
Arguments: []cmds.Argument{
132-
cmds.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
134+
cmds.FileArg("data", true, true, "The data to be stored as an IPFS block.").EnableStdin(),
133135
},
134136
Options: []cmds.Option{
135137
cmds.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."),
@@ -143,11 +145,6 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
143145
return err
144146
}
145147

146-
file, err := cmdenv.GetFileArg(req.Files.Entries())
147-
if err != nil {
148-
return err
149-
}
150-
151148
mhtype, _ := req.Options[mhtypeOptionName].(string)
152149
mhtval, ok := mh.Names[mhtype]
153150
if !ok {
@@ -170,18 +167,31 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
170167

171168
pin, _ := req.Options[pinOptionName].(bool)
172169

173-
p, err := api.Block().Put(req.Context, file,
174-
options.Block.Hash(mhtval, mhlen),
175-
options.Block.Format(format),
176-
options.Block.Pin(pin))
177-
if err != nil {
178-
return err
170+
it := req.Files.Entries()
171+
for it.Next() {
172+
file := files.FileFromEntry(it)
173+
if file == nil {
174+
return errors.New("expected a file")
175+
}
176+
177+
p, err := api.Block().Put(req.Context, file,
178+
options.Block.Hash(mhtval, mhlen),
179+
options.Block.Format(format),
180+
options.Block.Pin(pin))
181+
if err != nil {
182+
return err
183+
}
184+
185+
err = res.Emit(&BlockStat{
186+
Key: p.Path().Cid().String(),
187+
Size: p.Size(),
188+
})
189+
if err != nil {
190+
return err
191+
}
179192
}
180193

181-
return cmds.EmitOnce(res, &BlockStat{
182-
Key: p.Path().Cid().String(),
183-
Size: p.Size(),
184-
})
194+
return it.Err()
185195
},
186196
Encoders: cmds.EncoderMap{
187197
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {

test/sharness/t0050-block.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test_description="Test block command"
1111
test_init_ipfs
1212

1313
HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
14+
HASHB="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"
1415

1516
#
1617
# "block put tests"
@@ -26,6 +27,18 @@ test_expect_success "'ipfs block put' output looks good" '
2627
test_cmp expected_out actual_out
2728
'
2829

30+
test_expect_success "'ipfs block put' with 2 files succeeds" '
31+
echo "Hello Mars!" > a &&
32+
echo "Hello Venus!" > b &&
33+
ipfs block put a b >actual_out
34+
'
35+
36+
test_expect_success "'ipfs block put' output looks good" '
37+
echo "$HASH" >expected_out &&
38+
echo "$HASHB" >>expected_out &&
39+
test_cmp expected_out actual_out
40+
'
41+
2942
#
3043
# "block get" tests
3144
#

0 commit comments

Comments
 (0)