From b4c4591d2651892124a1a40d169b960da1f5e966 Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Mon, 2 Nov 2015 13:38:31 -0500 Subject: [PATCH] Add a --pin option to `ipfs add` (allowing --pin=false) Implements a solution for #1908 This PR replaces #1909 License: MIT Signed-off-by: Andrew Chin --- core/commands/add.go | 8 ++++++++ core/coreunix/add.go | 3 +++ test/sharness/t0081-repo-pinning.sh | 31 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/core/commands/add.go b/core/commands/add.go index 895e12c6651..4eccc6aaebd 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -24,6 +24,7 @@ const ( hiddenOptionName = "hidden" onlyHashOptionName = "only-hash" chunkerOptionName = "chunker" + pinOptionName = "pin" ) var AddCmd = &cmds.Command{ @@ -49,6 +50,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(pinOptionName, "Pin this object when adding. Default true"), }, PreRun: func(req cmds.Request) error { if quiet, _, _ := req.Option(quietOptionName).Bool(); quiet { @@ -94,6 +96,11 @@ remains to be implemented. hash, _, _ := req.Option(onlyHashOptionName).Bool() hidden, _, _ := req.Option(hiddenOptionName).Bool() chunker, _, _ := req.Option(chunkerOptionName).String() + dopin, pin_found, _ := req.Option(pinOptionName).Bool() + + if !pin_found { // default + dopin = true + } if hash { nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{ @@ -117,6 +124,7 @@ remains to be implemented. fileAdder.Hidden = hidden fileAdder.Trickle = trickle fileAdder.Wrap = wrap + fileAdder.Pin = dopin // addAllFiles loops over a convenience slice file to // add each file individually. e.g. 'ipfs add a b c' diff --git a/core/coreunix/add.go b/core/coreunix/add.go index dfc7b522f48..7f817f90bce 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -140,6 +140,9 @@ func (params *Adder) PinRoot() error { if err != nil { return err } + if !params.Pin { + return nil + } rnk, err := root.Key() if err != nil { diff --git a/test/sharness/t0081-repo-pinning.sh b/test/sharness/t0081-repo-pinning.sh index 61561c81f4e..f57a8630392 100755 --- a/test/sharness/t0081-repo-pinning.sh +++ b/test/sharness/t0081-repo-pinning.sh @@ -71,6 +71,9 @@ HASH_DIR4="QmW98gV71Ns4bX7QbgWAqLiGF3SDC1JpveZSgBh4ExaSAd" HASH_DIR3="QmRsCaNBMkweZ9vHT5PJRd2TT9rtNKEKyuognCEVxZxF1H" HASH_DIR2="QmTUTQAgeVfughDSFukMZLbfGvetDJY7Ef5cDXkKK4abKC" HASH_DIR1="QmNyZVFbgvmzguS2jVMRb8PQMNcCMJrn9E3doDhBbcPNTY" +HASH_NOPINDIR="QmWHjrRJYSfYKz5V9dWWSKu47GdY7NewyRhyTiroXgWcDU" +HASH_NOPIN_FILE1="QmUJT3GQi1dxQyTZbkaWeer9GkCn1d3W3HHRLSDr6PTcpx" +HASH_NOPIN_FILE2="QmarR7m9JT7qHEGhuFNZUEMAnoZ8E9QAfsthHCQ9Y2GfoT" DIR1="dir1" DIR2="dir1/dir2" @@ -248,6 +251,34 @@ test_expect_success "recursive pin fails without objects" ' test_fsh cat err_expected8 ' +test_expect_success "test add nopin file" ' + echo "test nopin data" > test_nopin_data && + NOPINHASH=$(ipfs add -q --pin=false test_nopin_data) && + test_pin_flag "$NOPINHASH" direct false && + test_pin_flag "$NOPINHASH" indirect false && + test_pin_flag "$NOPINHASH" recursive false +' + + +test_expect_success "test add nopin dir" ' + mkdir nopin_dir1 && + echo "some nopin text 1" >nopin_dir1/file1 && + echo "some nopin text 2" >nopin_dir1/file2 && + ipfs add -q -r --pin=false nopin_dir1 | tail -n1 >actual1 && + echo "$HASH_NOPINDIR" >expected1 && + test_cmp actual1 expected1 && + test_pin_flag "$HASH_NOPINDIR" direct false && + test_pin_flag "$HASH_NOPINDIR" indirect false && + test_pin_flag "$HASH_NOPINDIR" recursive false && + test_pin_flag "$HASH_NOPIN_FILE1" direct false && + test_pin_flag "$HASH_NOPIN_FILE1" indirect false && + test_pin_flag "$HASH_NOPIN_FILE1" recursive false && + test_pin_flag "$HASH_NOPIN_FILE2" direct false && + test_pin_flag "$HASH_NOPIN_FILE2" indirect false && + test_pin_flag "$HASH_NOPIN_FILE2" recursive false + +' + # test_kill_ipfs_daemon test_done