Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
07b923d7
Commit
07b923d7
authored
Jan 07, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ipfs block stat cmd
parent
9bd2f42b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
30 deletions
+85
-30
core/commands/block.go
core/commands/block.go
+75
-30
test/t0050-block.sh
test/t0050-block.sh
+10
-0
No files found.
core/commands/block.go
View file @
07b923d7
...
...
@@ -2,10 +2,10 @@ package commands
import
(
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"time"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
...
...
@@ -15,9 +15,13 @@ import (
u
"github.com/jbenet/go-ipfs/util"
)
type
Block
struct
{
Key
string
Length
int
type
BlockStat
struct
{
Key
string
Size
int
}
func
(
bs
BlockStat
)
String
()
string
{
return
fmt
.
Sprintf
(
"Key: %s
\n
Size: %d
\n
"
,
bs
.
Key
,
bs
.
Size
)
}
var
BlockCmd
=
&
cmds
.
Command
{
...
...
@@ -31,17 +35,22 @@ multihash.
},
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"get"
:
blockGetCmd
,
"put"
:
blockPutCmd
,
"stat"
:
blockStatCmd
,
"get"
:
blockGetCmd
,
"put"
:
blockPutCmd
,
},
}
var
block
Ge
tCmd
=
&
cmds
.
Command
{
var
block
Sta
tCmd
=
&
cmds
.
Command
{
Helptext
:
cmds
.
HelpText
{
Tagline
:
"
Get
a raw IPFS block"
,
Tagline
:
"
Print information of
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.
'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
`
,
},
...
...
@@ -49,29 +58,42 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
cmds
.
StringArg
(
"key"
,
true
,
false
,
"The base58 multihash of an existing block to get"
),
},
Run
:
func
(
req
cmds
.
Request
)
(
interface
{},
error
)
{
n
,
err
:=
req
.
Context
()
.
GetNode
(
)
b
,
err
:=
getBlockForKey
(
req
,
req
.
Arguments
()[
0
]
)
if
err
!=
nil
{
return
nil
,
err
}
key
:=
req
.
Arguments
()[
0
]
if
!
u
.
IsValidHash
(
key
)
{
return
nil
,
cmds
.
Error
{
"Not a valid hash"
,
cmds
.
ErrClient
}
}
return
&
BlockStat
{
Key
:
b
.
Key
()
.
Pretty
(),
Size
:
len
(
b
.
Data
),
},
nil
},
Type
:
BlockStat
{},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
bs
:=
res
.
Output
()
.
(
*
BlockStat
)
return
strings
.
NewReader
(
bs
.
String
()),
nil
},
},
}
h
,
err
:=
mh
.
FromB58String
(
key
)
if
err
!=
nil
{
return
nil
,
err
}
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.
`
,
},
k
:=
u
.
Key
(
h
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
TODO
(),
time
.
Second
*
5
)
b
,
err
:=
n
.
Blocks
.
GetBlock
(
ctx
,
k
)
Arguments
:
[]
cmds
.
Argument
{
cmds
.
StringArg
(
"key"
,
true
,
false
,
"The base58 multihash of an existing block to get"
),
},
Run
:
func
(
req
cmds
.
Request
)
(
interface
{},
error
)
{
b
,
err
:=
getBlockForKey
(
req
,
req
.
Arguments
()[
0
])
if
err
!=
nil
{
return
nil
,
err
}
log
.
Debugf
(
"BlockGet key: '%q'"
,
b
.
Key
())
return
bytes
.
NewReader
(
b
.
Data
),
nil
},
...
...
@@ -118,16 +140,39 @@ It reads from stdin, and <key> is a base58 encoded multihash.
return
nil
,
err
}
return
&
Block
{
Key
:
k
.
String
(),
Length
:
len
(
data
),
return
&
Block
Stat
{
Key
:
k
.
String
(),
Size
:
len
(
data
),
},
nil
},
Type
:
Block
{},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
b
lock
:=
res
.
Output
()
.
(
*
Block
)
return
strings
.
NewReader
(
b
lock
.
Key
+
"
\n
"
),
nil
b
s
:=
res
.
Output
()
.
(
*
Block
Stat
)
return
strings
.
NewReader
(
b
s
.
Key
+
"
\n
"
),
nil
},
},
}
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
,
cmds
.
Error
{
"Not a valid hash"
,
cmds
.
ErrClient
}
}
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
}
test/t0050-block.sh
View file @
07b923d7
...
...
@@ -29,4 +29,14 @@ test_expect_success "'ipfs block get' output looks good" '
test_cmp expected_in actual_in
'
test_expect_success
"'ipfs block stat' succeeds"
'
ipfs block stat $HASH >actual_stat
'
test_expect_success
"'ipfs block get' output looks good"
'
echo "Key: $HASH" >expected_stat &&
echo "Size: 12" >>expected_stat &&
test_cmp expected_stat actual_stat
'
test_done
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment