Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object put --pin option #4095

Merged
merged 3 commits into from
Aug 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
core "github.com/ipfs/go-ipfs/core"
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
pin "github.com/ipfs/go-ipfs/pin"
ft "github.com/ipfs/go-ipfs/unixfs"

cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
Expand Down Expand Up @@ -355,6 +356,7 @@ And then run:
Options: []cmds.Option{
cmds.StringOption("inputenc", "Encoding type of input data. One of: {\"protobuf\", \"json\"}.").Default("json"),
cmds.StringOption("datafieldenc", "Encoding type of the data field, either \"text\" or \"base64\".").Default("text"),
cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
Expand All @@ -381,7 +383,17 @@ And then run:
return
}

output, err := objectPut(n, input, inputenc, datafieldenc)
dopin, _, err := req.Option("pin").Bool()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

if dopin {
defer n.Blockstore.PinLock().Unlock()
}

objectCid, err := objectPut(n, input, inputenc, datafieldenc)
if err != nil {
errType := cmds.ErrNormal
if err == ErrUnknownObjectEnc {
Expand All @@ -391,7 +403,16 @@ And then run:
return
}

res.SetOutput(output)
if dopin {
n.Pinning.PinWithMode(objectCid, pin.Recursive)
err = n.Pinning.Flush()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}

res.SetOutput(&Object{Hash: objectCid.String()})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
Expand Down Expand Up @@ -468,7 +489,7 @@ func nodeFromTemplate(template string) (*dag.ProtoNode, error) {
var ErrEmptyNode = errors.New("no data or links in this node")

// objectPut takes a format option, serializes bytes from stdin and updates the dag with that data
func objectPut(n *core.IpfsNode, input io.Reader, encoding string, dataFieldEncoding string) (*Object, error) {
func objectPut(n *core.IpfsNode, input io.Reader, encoding string, dataFieldEncoding string) (*cid.Cid, error) {

data, err := ioutil.ReadAll(io.LimitReader(input, inputLimit+10))
if err != nil {
Expand Down Expand Up @@ -533,7 +554,7 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string, dataFieldEnco
return nil, err
}

return getOutput(dagnode)
return dagnode.Cid(), nil
}

// ErrUnknownObjectEnc is returned if a invalid encoding is supplied
Expand Down
18 changes: 17 additions & 1 deletion test/sharness/t0051-object.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,23 @@ test_object_cmd() {
test_cmp expected actual
'

test_expect_success "'ipfs object patch' should work (no unixfs-dir)" '
test_expect_success "'ipfs object put --pin' succeeds" '
HASH="QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V" &&
echo "added $HASH" >expected &&
echo "{ \"Data\": \"abc\" }" | ipfs object put --pin >actual
'

test_expect_success "'ipfs object put --pin' output looks good" '
echo "added $HASH" >expected &&
test_cmp expected actual
'

test_expect_success "after gc, objects still acessible" '
ipfs repo gc > /dev/null &&
ipfs refs -r --timeout=2s $HASH > /dev/null
'

test_expect_success "'ipfs object patch' should work (no unixfs-dir)" '
EMPTY_DIR=$(ipfs object new) &&
OUTPUT=$(ipfs object patch $EMPTY_DIR add-link foo $EMPTY_DIR) &&
ipfs object stat $OUTPUT
Expand Down