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
036ca3a7
Commit
036ca3a7
authored
Sep 20, 2016
by
Lars Gierth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
coreapi: add Add()
License: MIT Signed-off-by:
Lars Gierth
<
larsg@systemli.org
>
parent
029f971d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
7 deletions
+81
-7
core/coreapi/interface/interface.go
core/coreapi/interface/interface.go
+1
-0
core/coreapi/unixfs.go
core/coreapi/unixfs.go
+12
-0
core/coreapi/unixfs_test.go
core/coreapi/unixfs_test.go
+64
-7
core/coreunix/add.go
core/coreunix/add.go
+4
-0
No files found.
core/coreapi/interface/interface.go
View file @
036ca3a7
...
...
@@ -25,6 +25,7 @@ type Reader interface {
}
type
UnixfsAPI
interface
{
Add
(
context
.
Context
,
io
.
Reader
)
(
*
cid
.
Cid
,
error
)
Cat
(
context
.
Context
,
string
)
(
Reader
,
error
)
Ls
(
context
.
Context
,
string
)
([]
*
Link
,
error
)
}
...
...
core/coreapi/unixfs.go
View file @
036ca3a7
...
...
@@ -2,10 +2,14 @@ package coreapi
import
(
"context"
"io"
core
"github.com/ipfs/go-ipfs/core"
coreiface
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix
"github.com/ipfs/go-ipfs/core/coreunix"
uio
"github.com/ipfs/go-ipfs/unixfs/io"
cid
"gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
)
type
UnixfsAPI
struct
{
...
...
@@ -17,6 +21,14 @@ func NewUnixfsAPI(n *core.IpfsNode) coreiface.UnixfsAPI {
return
api
}
func
(
api
*
UnixfsAPI
)
Add
(
ctx
context
.
Context
,
r
io
.
Reader
)
(
*
cid
.
Cid
,
error
)
{
k
,
err
:=
coreunix
.
AddWithContext
(
ctx
,
api
.
node
,
r
)
if
err
!=
nil
{
return
nil
,
err
}
return
cid
.
Decode
(
k
)
}
func
(
api
*
UnixfsAPI
)
Cat
(
ctx
context
.
Context
,
p
string
)
(
coreiface
.
Reader
,
error
)
{
dagnode
,
err
:=
resolve
(
ctx
,
api
.
node
,
p
)
if
err
!=
nil
{
...
...
core/coreapi/unixfs_test.go
View file @
036ca3a7
...
...
@@ -18,6 +18,10 @@ import (
unixfs
"github.com/ipfs/go-ipfs/unixfs"
)
// `echo -n 'hello, world!' | ipfs add`
var
hello
=
"QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
var
helloStr
=
"hello, world!"
// `ipfs object new unixfs-dir`
var
emptyUnixfsDir
=
"QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
...
...
@@ -41,6 +45,56 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) {
return
node
,
api
,
nil
}
func
TestAdd
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
_
,
api
,
err
:=
makeAPI
(
ctx
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
str
:=
strings
.
NewReader
(
helloStr
)
c
,
err
:=
api
.
Add
(
ctx
,
str
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
c
.
String
()
!=
hello
{
t
.
Fatalf
(
"expected CID %s, got: %s"
,
hello
,
c
)
}
r
,
err
:=
api
.
Cat
(
ctx
,
hello
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
buf
:=
make
([]
byte
,
len
(
helloStr
))
_
,
err
=
io
.
ReadFull
(
r
,
buf
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
string
(
buf
)
!=
helloStr
{
t
.
Fatalf
(
"expected [%s], got [%s] [err=%s]"
,
helloStr
,
string
(
buf
),
err
)
}
}
func
TestAddEmptyFile
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
_
,
api
,
err
:=
makeAPI
(
ctx
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
str
:=
strings
.
NewReader
(
""
)
c
,
err
:=
api
.
Add
(
ctx
,
str
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
c
.
String
()
!=
emptyUnixfsFile
{
t
.
Fatalf
(
"expected CID %s, got: %s"
,
hello
,
c
)
}
}
func
TestCatBasic
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
node
,
api
,
err
:=
makeAPI
(
ctx
)
...
...
@@ -48,25 +102,28 @@ func TestCatBasic(t *testing.T) {
t
.
Fatal
(
err
)
}
hello
:=
"hello, world!"
hr
:=
strings
.
NewReader
(
hello
)
hr
:=
strings
.
NewReader
(
helloStr
)
k
,
err
:=
coreunix
.
Add
(
node
,
hr
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
k
!=
hello
{
t
.
Fatalf
(
"expected CID %s, got: %s"
,
hello
,
k
)
}
r
,
err
:=
api
.
Cat
(
ctx
,
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
buf
:=
make
([]
byte
,
len
(
hello
))
n
,
err
:
=
io
.
ReadFull
(
r
,
buf
)
if
err
!=
nil
&&
err
!=
io
.
EOF
{
buf
:=
make
([]
byte
,
len
(
hello
Str
))
_
,
err
=
io
.
ReadFull
(
r
,
buf
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
string
(
buf
)
!=
hello
{
t
.
Fatalf
(
"expected [
hello, world!
], got [%s] [err=%s]"
,
string
(
buf
),
n
,
err
)
if
string
(
buf
)
!=
hello
Str
{
t
.
Fatalf
(
"expected [
%s
], got [%s] [err=%s]"
,
helloStr
,
string
(
buf
),
err
)
}
}
...
...
core/coreunix/add.go
View file @
036ca3a7
...
...
@@ -254,6 +254,10 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
// Add builds a merkledag from the a reader, pinning all objects to the local
// datastore. Returns a key representing the root node.
func
Add
(
n
*
core
.
IpfsNode
,
r
io
.
Reader
)
(
string
,
error
)
{
return
AddWithContext
(
n
.
Context
(),
n
,
r
)
}
func
AddWithContext
(
ctx
context
.
Context
,
n
*
core
.
IpfsNode
,
r
io
.
Reader
)
(
string
,
error
)
{
defer
n
.
Blockstore
.
PinLock
()
.
Unlock
()
fileAdder
,
err
:=
NewAdder
(
n
.
Context
(),
n
.
Pinning
,
n
.
Blockstore
,
n
.
DAG
)
...
...
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