Skip to content

Commit

Permalink
Add a --no-pin option to 'ipfs add'
Browse files Browse the repository at this point in the history
See #1908

Signed-off-by: Andrew Chin <achin@eminence32.net>
  • Loading branch information
eminence committed Oct 28, 2015
1 parent bcc76f2 commit bbc2b1f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 22 additions & 5 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
offline "github.com/ipfs/go-ipfs/exchange/offline"
importer "github.com/ipfs/go-ipfs/importer"
"github.com/ipfs/go-ipfs/importer/chunk"
"github.com/ipfs/go-ipfs/importer/helpers"
dag "github.com/ipfs/go-ipfs/merkledag"
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
pin "github.com/ipfs/go-ipfs/pin"
Expand All @@ -39,6 +40,7 @@ const (
hiddenOptionName = "hidden"
onlyHashOptionName = "only-hash"
chunkerOptionName = "chunker"
noPinOptionName = "no-pin"
)

type AddedObject struct {
Expand Down Expand Up @@ -70,6 +72,7 @@ remains to be implemented.
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object"),
cmds.BoolOption(hiddenOptionName, "H", "Include files that are hidden"),
cmds.StringOption(chunkerOptionName, "s", "chunking algorithm to use"),
cmds.BoolOption(noPinOptionName, "x", "Don't pin when adding content"),
},
PreRun: func(req cmds.Request) error {
if quiet, _, _ := req.Option(quietOptionName).Bool(); quiet {
Expand Down Expand Up @@ -107,6 +110,7 @@ remains to be implemented.
hash, _, _ := req.Option(onlyHashOptionName).Bool()
hidden, _, _ := req.Option(hiddenOptionName).Bool()
chunker, _, _ := req.Option(chunkerOptionName).String()
nopin, _, _ := req.Option(noPinOptionName).Bool()

e := dagutils.NewDagEditor(NewMemoryDagService(), newDirNode())
if hash {
Expand Down Expand Up @@ -135,6 +139,7 @@ remains to be implemented.
hidden: hidden,
trickle: trickle,
wrap: wrap,
nopin: nopin,
}

// addAllFiles loops over a convenience slice file to
Expand All @@ -156,6 +161,9 @@ remains to be implemented.
}

pinRoot := func(rootnd *dag.Node) error {
if nopin {
return nil
}
rnk, err := rootnd.Key()
if err != nil {
return err
Expand Down Expand Up @@ -302,29 +310,36 @@ type adder struct {
trickle bool
wrap bool
chunker string
nopin bool

nextUntitled int
}

// Perform the actual add & pin locally, outputting results to reader
func add(n *core.IpfsNode, reader io.Reader, useTrickle bool, chunker string) (*dag.Node, error) {
func add(n *core.IpfsNode, reader io.Reader, useTrickle bool, chunker string, nopin bool) (*dag.Node, error) {
chnk, err := chunk.FromString(reader, chunker)
if err != nil {
return nil, err
}

var node *dag.Node
var pinCB helpers.NodeCB
if nopin {
pinCB = helpers.NilFunc
} else {
pinCB = importer.PinIndirectCB(n.Pinning.GetManual())
}
if useTrickle {
node, err = importer.BuildTrickleDagFromReader(
n.DAG,
chnk,
importer.PinIndirectCB(n.Pinning.GetManual()),
pinCB,
)
} else {
node, err = importer.BuildDagFromReader(
n.DAG,
chnk,
importer.PinIndirectCB(n.Pinning.GetManual()),
pinCB,
)
}

Expand Down Expand Up @@ -405,7 +420,7 @@ func (params *adder) addFile(file files.File) (*dag.Node, error) {
reader = &progressReader{file: file, out: params.out}
}

dagnode, err := add(params.node, reader, params.trickle, params.chunker)
dagnode, err := add(params.node, reader, params.trickle, params.chunker, params.nopin)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -456,7 +471,9 @@ func (params *adder) addDir(file files.File) (*dag.Node, error) {
return nil, err
}

params.node.Pinning.GetManual().PinWithMode(k, pin.Indirect)
if !params.nopin {
params.node.Pinning.GetManual().PinWithMode(k, pin.Indirect)
}

return tree, nil
}
Expand Down
4 changes: 2 additions & 2 deletions importer/helpers/dagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// only pinning the first node recursively.
type NodeCB func(node *dag.Node, last bool) error

var nilFunc NodeCB = func(_ *dag.Node, _ bool) error { return nil }
var NilFunc NodeCB = func(_ *dag.Node, _ bool) error { return nil }

// DagBuilderHelper wraps together a bunch of objects needed to
// efficiently create unixfs dag trees
Expand Down Expand Up @@ -44,7 +44,7 @@ type DagBuilderParams struct {
func (dbp *DagBuilderParams) New(in <-chan []byte, errs <-chan error) *DagBuilderHelper {
ncb := dbp.NodeCB
if ncb == nil {
ncb = nilFunc
ncb = NilFunc
}

return &DagBuilderHelper{
Expand Down

0 comments on commit bbc2b1f

Please sign in to comment.