From a59e957bb243b4a84aa09872c71bfb86a815a6fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 2 Feb 2018 16:28:02 +0100 Subject: [PATCH] coreapi: Pin option for Object.Put MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/options/object.go | 11 +++++++++++ core/coreapi/object.go | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/coreapi/interface/options/object.go b/core/coreapi/interface/options/object.go index aca02d67263..9257ea6077c 100644 --- a/core/coreapi/interface/options/object.go +++ b/core/coreapi/interface/options/object.go @@ -7,6 +7,7 @@ type ObjectNewSettings struct { type ObjectPutSettings struct { InputEnc string DataType string + Pin bool } type ObjectAddLinkSettings struct { @@ -35,6 +36,7 @@ func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) { options := &ObjectPutSettings{ InputEnc: "json", DataType: "text", + Pin: false, } for _, opt := range opts { @@ -103,6 +105,15 @@ func (objectOpts) DataType(t string) ObjectPutOption { } } +// WithPin is an option for Object.Put which specifies whether to pin the added +// objects, default is false +func (objectOpts) WithPin(pin bool) ObjectPutOption { + return func(settings *ObjectPutSettings) error { + settings.Pin = pin + return nil + } +} + // Create is an option for Object.AddLink which specifies whether create required // directories for the child func (objectOpts) Create(create bool) ObjectAddLinkOption { diff --git a/core/coreapi/object.go b/core/coreapi/object.go index 2537966e2af..4da99110f1d 100644 --- a/core/coreapi/object.go +++ b/core/coreapi/object.go @@ -14,9 +14,10 @@ import ( coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" "github.com/ipfs/go-ipfs/dagutils" + pin "github.com/ipfs/go-ipfs/pin" ft "github.com/ipfs/go-ipfs/unixfs" - dag "gx/ipfs/QmRy4Qk9hbgFX9NGJRm8rBThrA8PZhNCitMgeRYyZ67s59/go-merkledag" + dag "gx/ipfs/QmRy4Qk9hbgFX9NGJRm8rBThrA8PZhNCitMgeRYyZ67s59/go-merkledag" cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid" ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format" ) @@ -116,11 +117,23 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj return nil, err } + if options.Pin { + defer api.node.Blockstore.PinLock().Unlock() + } + err = api.node.DAG.Add(ctx, dagnode) if err != nil { return nil, err } + if options.Pin { + api.node.Pinning.PinWithMode(dagnode.Cid(), pin.Recursive) + err = api.node.Pinning.Flush() + if err != nil { + return nil, err + } + } + return coreiface.IpfsPath(dagnode.Cid()), nil }