ipfs add uses CoreAPI
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 4, 2018 at 01:57 UTC
69eb015b9f043fc6d1d3a276f1677e02961d468c
2 files changed
+39
-125
core/commands/add.go
+31
-125
@@ -6,25 +6,15 @@ import (
6
"os"
7
"strings"
8
9
- core "github.com/ipfs/go-ipfs/core"
9
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
- "github.com/ipfs/go-ipfs/core/coreunix"
13
- filestore "github.com/ipfs/go-ipfs/filestore"
14
- ft "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs"
15
- dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
16
- dagtest "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag/test"
17
- blockservice "gx/ipfs/QmcRecCZWM2NZfCQrCe97Ch3Givv8KKEP82tGUDntzdLFe/go-blockservice"
11
+ options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
12
13
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
14
pb "gx/ipfs/QmPtj12fdwuAqj9sBSTNUxBNu8kCGNp8b3o8yUzMm5GHpq/pb"
21
- cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
22
- offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
15
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
16
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
17
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
26
- mfs "gx/ipfs/QmahrY1adY4wvtYEtoGjpZ2GUohTyukrkMkwUR9ytRjTG2/go-mfs"
27
- bstore "gx/ipfs/QmdriVJgKx4JADRgh3cYPXqXmsa1A45SvFki1nDWHhQNtC/go-ipfs-blockstore"
18
)
19
20
// ErrDepthLimitExceeded indicates that the max depth has been exceeded.
@@ -149,23 +139,11 @@ You can now check what blocks have been created by:
139
return nil
140
},
141
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
152
- n, err := cmdenv.GetNode(env)
142
+ api, err := cmdenv.GetApi(env)
143
if err != nil {
144
return err
145
}
146
157
- cfg, err := n.Repo.Config()
158
- if err != nil {
159
- return err
160
- }
161
- // check if repo will exceed storage limit if added
162
- // TODO: this doesn't handle the case if the hashed file is already in blocks (deduplicated)
163
- // TODO: conditional GC is disabled due to it is somehow not possible to pass the size to the daemon
164
- //if err := corerepo.ConditionalGC(req.Context(), n, uint64(size)); err != nil {
165
- // res.SetError(err, cmdkit.ErrNormal)
166
- // return
167
- //}
168
-
147
progress, _ := req.Options[progressOptionName].(bool)
148
trickle, _ := req.Options[trickleOptionName].(bool)
149
wrap, _ := req.Options[wrapOptionName].(bool)
@@ -182,131 +160,59 @@ You can now check what blocks have been created by:
160
inline, _ := req.Options[inlineOptionName].(bool)
161
inlineLimit, _ := req.Options[inlineLimitOptionName].(int)
162
pathName, _ := req.Options[stdinPathName].(string)
185
-
186
- // The arguments are subject to the following constraints.
187
- //
188
- // nocopy -> filestoreEnabled
189
- // nocopy -> rawblocks
190
- // (hash != sha2-256) -> cidv1
191
-
192
- // NOTE: 'rawblocks -> cidv1' is missing. Legacy reasons.
193
-
194
- // nocopy -> filestoreEnabled
195
- if nocopy && !cfg.Experimental.FilestoreEnabled {
196
- return cmdkit.Errorf(cmdkit.ErrClient, filestore.ErrFilestoreNotEnabled.Error())
197
- }
198
-
199
- // nocopy -> rawblocks
200
- if nocopy && !rawblks {
201
- // fixed?
202
- if rbset {
203
- return fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well")
204
- }
205
- // No, satisfy mandatory constraint.
206
- rawblks = true
207
- }
208
-
209
- // (hash != "sha2-256") -> CIDv1
210
- if hashFunStr != "sha2-256" && cidVer == 0 {
211
- if cidVerSet {
212
- return cmdkit.Errorf(cmdkit.ErrClient, "CIDv0 only supports sha2-256")
213
- }
214
- cidVer = 1
215
- }
216
-
217
- // cidV1 -> raw blocks (by default)
218
- if cidVer > 0 && !rbset {
219
- rawblks = true
220
- }
221
-
222
- prefix, err := dag.PrefixForCidVersion(cidVer)
223
- if err != nil {
224
- return err
225
- }
163
+ local, _ := req.Options["local"].(bool)
164
165
hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)]
166
if !ok {
167
return fmt.Errorf("unrecognized hash function: %s", strings.ToLower(hashFunStr))
168
}
169
232
- prefix.MhType = hashFunCode
233
- prefix.MhLength = -1
234
-
235
- if hash {
236
- nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{
237
- //TODO: need this to be true or all files
238
- // hashed will be stored in memory!
239
- NilRepo: true,
240
- })
241
- if err != nil {
242
- return err
243
- }
244
- n = nilnode
245
- }
170
+ events := make(chan interface{}, adderOutChanSize)
171
247
- addblockstore := n.Blockstore
248
- if !(fscache || nocopy) {
249
- addblockstore = bstore.NewGCBlockstore(n.BaseBlocks, n.GCLocker)
250
- }
172
+ opts := []options.UnixfsAddOption{
173
+ options.Unixfs.Hash(hashFunCode),
174
252
- exch := n.Exchange
253
- local, _ := req.Options["local"].(bool)
254
- if local {
255
- exch = offline.Exchange(addblockstore)
256
- }
175
+ options.Unixfs.Inline(inline),
176
+ options.Unixfs.InlineLimit(inlineLimit),
177
258
- bserv := blockservice.New(addblockstore, exch) // hash security 001
259
- dserv := dag.NewDAGService(bserv)
178
+ options.Unixfs.Chunker(chunker),
179
261
- outChan := make(chan interface{}, adderOutChanSize)
180
+ options.Unixfs.Pin(dopin),
181
+ options.Unixfs.HashOnly(hash),
182
+ options.Unixfs.Local(local),
183
+ options.Unixfs.FsCache(fscache),
184
+ options.Unixfs.Nocopy(nocopy),
185
263
- fileAdder, err := coreunix.NewAdder(req.Context, n.Pinning, n.Blockstore, dserv)
264
- if err != nil {
265
- return err
186
+ options.Unixfs.Wrap(wrap),
187
+ options.Unixfs.Hidden(hidden),
188
+ options.Unixfs.StdinName(pathName),
189
+
190
+ options.Unixfs.Progress(progress),
191
+ options.Unixfs.Silent(silent),
192
+ options.Unixfs.Events(events),
193
}
194
268
- fileAdder.Out = outChan
269
- fileAdder.Chunker = chunker
270
- fileAdder.Progress = progress
271
- fileAdder.Hidden = hidden
272
- fileAdder.Trickle = trickle
273
- fileAdder.Wrap = wrap
274
- fileAdder.Pin = dopin && !hash
275
- fileAdder.Silent = silent
276
- fileAdder.RawLeaves = rawblks
277
- fileAdder.NoCopy = nocopy
278
- fileAdder.Name = pathName
279
- fileAdder.CidBuilder = prefix
280
-
281
- if inline {
282
- fileAdder.CidBuilder = cidutil.InlineBuilder{
283
- Builder: fileAdder.CidBuilder,
284
- Limit: inlineLimit,
285
- }
195
+ if cidVerSet {
196
+ opts = append(opts, options.Unixfs.CidVersion(cidVer))
197
}
198
288
- if hash {
289
- md := dagtest.Mock()
290
- emptyDirNode := ft.EmptyDirNode()
291
- // Use the same prefix for the "empty" MFS root as for the file adder.
292
- emptyDirNode.SetCidBuilder(fileAdder.CidBuilder)
293
- mr, err := mfs.NewRoot(req.Context, md, emptyDirNode, nil)
294
- if err != nil {
295
- return err
296
- }
199
+ if rbset {
200
+ opts = append(opts, options.Unixfs.RawLeaves(rawblks))
201
+ }
202
298
- fileAdder.SetMfsRoot(mr)
203
+ if trickle {
204
+ opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
205
}
206
207
errCh := make(chan error)
208
go func() {
209
var err error
210
defer func() { errCh <- err }()
305
- defer close(outChan)
306
- _, err = fileAdder.AddAllAndPin(req.Files)
211
+ defer close(events)
212
+ _, err = api.Unixfs().Add(req.Context, req.Files, opts...)
213
}()
214
309
- err = res.Emit(outChan)
215
+ err = res.Emit(events)
216
if err != nil {
217
return err
218
}
core/coreapi/unixfs.go
+8
@@ -40,6 +40,14 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
40
return nil, err
41
}
42
43
+ // check if repo will exceed storage limit if added
44
+ // TODO: this doesn't handle the case if the hashed file is already in blocks (deduplicated)
45
+ // TODO: conditional GC is disabled due to it is somehow not possible to pass the size to the daemon
46
+ //if err := corerepo.ConditionalGC(req.Context(), n, uint64(size)); err != nil {
47
+ // res.SetError(err, cmdkit.ErrNormal)
48
+ // return
49
+ //}
50
+
51
if settings.NoCopy && !cfg.Experimental.FilestoreEnabled {
52
return nil, filestore.ErrFilestoreNotEnabled
53
}