coreapi unixfs: filestore opts
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 4, 2018 at 01:24 UTC
d51ce96f55b84ea83599487f2a87b43998ac3902
2 files changed
+50
-4
core/coreapi/interface/options/unixfs.go
+35
@@ -31,6 +31,8 @@ type UnixfsAddSettings struct {
31
Pin bool
32
OnlyHash bool
33
Local bool
34
+ FsCache bool
35
+ NoCopy bool
36
37
Wrap bool
38
Hidden bool
@@ -59,6 +61,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
61
Pin: false,
62
OnlyHash: false,
63
Local: false,
64
+ FsCache: false,
65
+ NoCopy: false,
66
67
Wrap: false,
68
Hidden: false,
@@ -76,6 +80,17 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
80
}
81
}
82
83
+ // nocopy -> rawblocks
84
+ if options.NoCopy && !options.RawLeaves {
85
+ // fixed?
86
+ if options.RawLeavesSet {
87
+ return nil, cid.Prefix{}, fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well")
88
+ }
89
+
90
+ // No, satisfy mandatory constraint.
91
+ options.RawLeaves = true
92
+ }
93
+
94
// (hash != "sha2-256") -> CIDv1
95
if options.MhType != mh.SHA2_256 {
96
switch options.CidVersion {
@@ -271,3 +286,23 @@ func (unixfsOpts) Progress(enable bool) UnixfsAddOption {
286
return nil
287
}
288
}
289
+
290
+// FsCache tells the adder to check the filestore for pre-existing blocks
291
+//
292
+// Experimental
293
+func (unixfsOpts) FsCache(enable bool) UnixfsAddOption {
294
+ return func(settings *UnixfsAddSettings) error {
295
+ settings.FsCache = enable
296
+ return nil
297
+ }
298
+}
299
+
300
+// NoCopy tells the adder to add the files using filestore. Implies RawLeaves.
301
+//
302
+// Experimental
303
+func (unixfsOpts) Nocopy(enable bool) UnixfsAddOption {
304
+ return func(settings *UnixfsAddSettings) error {
305
+ settings.NoCopy = enable
306
+ return nil
307
+ }
308
+}
core/coreapi/unixfs.go
+15
-4
@@ -4,6 +4,7 @@ import (
4
"context"
5
"fmt"
6
"github.com/ipfs/go-ipfs/core"
7
+ "github.com/ipfs/go-ipfs/filestore"
8
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -33,6 +34,16 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
34
}
35
36
n := api.node
37
+
38
+ cfg, err := n.Repo.Config()
39
+ if err != nil {
40
+ return nil, err
41
+ }
42
+
43
+ if settings.NoCopy && !cfg.Experimental.FilestoreEnabled {
44
+ return nil, filestore.ErrFilestoreNotEnabled
45
+ }
46
+
47
if settings.OnlyHash {
48
nilnode, err := core.NewNode(ctx, &core.BuildCfg{
49
//TODO: need this to be true or all files
@@ -46,9 +57,9 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
57
}
58
59
addblockstore := n.Blockstore
49
- //if !(fscache || nocopy) {
50
- addblockstore = bstore.NewGCBlockstore(n.BaseBlocks, n.GCLocker)
51
- //}
60
+ if !(settings.FsCache || settings.NoCopy) {
61
+ addblockstore = bstore.NewGCBlockstore(n.BaseBlocks, n.GCLocker)
62
+ }
63
64
exch := n.Exchange
65
if settings.Local {
@@ -73,7 +84,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
84
fileAdder.Pin = settings.Pin && !settings.OnlyHash
85
fileAdder.Silent = settings.Silent
86
fileAdder.RawLeaves = settings.RawLeaves
76
- //fileAdder.NoCopy = nocopy
87
+ fileAdder.NoCopy = settings.NoCopy
88
fileAdder.Name = settings.StdinName
89
fileAdder.CidBuilder = prefix
90