@@ -3,52 +3,46 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "log"
7
6
"strings"
8
7
"time"
9
8
10
9
"github.com/dustin/go-humanize"
11
- "github.com/filecoin-project/lassie/cmd/lassie/internal "
12
- "github.com/filecoin-project/lassie/pkg/client "
10
+ lcli "github.com/filecoin-project/lassie/cli "
11
+ "github.com/filecoin-project/lassie/internal "
13
12
"github.com/filecoin-project/lassie/pkg/events"
14
- "github.com/filecoin-project/lassie/pkg/indexerlookup"
15
13
"github.com/filecoin-project/lassie/pkg/retriever"
16
14
"github.com/filecoin-project/lassie/pkg/types"
17
- blocks "github.com/ipfs/go-block-format"
18
15
"github.com/ipfs/go-cid"
19
- "github.com/ipfs/go-datastore"
20
- dss "github.com/ipfs/go-datastore/sync"
21
16
"github.com/ipfs/go-graphsync/storeutil"
22
- blockstore "github.com/ipfs/go-ipfs-blockstore"
23
17
carblockstore "github.com/ipld/go-car/v2/blockstore"
24
18
"github.com/libp2p/go-libp2p/core/peer"
25
- "github.com/multiformats/go-multiaddr"
26
19
"github.com/urfave/cli/v2"
27
20
)
28
21
29
22
var fetchProviderAddrInfo * peer.AddrInfo
30
23
31
24
var fetchCmd = & cli.Command {
32
25
Name : "fetch" ,
33
- Usage : "fetch content from Filecoin" ,
26
+ Usage : "Fetches content from Filecoin" ,
27
+ Before : before ,
34
28
Action : Fetch ,
35
29
Flags : []cli.Flag {
36
30
& cli.StringFlag {
37
31
Name : "output" ,
38
32
Aliases : []string {"o" },
39
- Usage : "The CAR file to write to, may be an existing or a new CAR" ,
33
+ Usage : "the CAR file to write to, may be an existing or a new CAR" ,
40
34
TakesFile : true ,
41
35
},
42
36
& cli.DurationFlag {
43
37
Name : "timeout" ,
44
38
Aliases : []string {"t" },
45
- Usage : "Consider it an error after not receiving a response from a storage provider for this long" ,
39
+ Usage : "consider it an error after not receiving a response from a storage provider for this long" ,
46
40
Value : 20 * time .Second ,
47
41
},
48
42
& cli.BoolFlag {
49
43
Name : "progress" ,
50
44
Aliases : []string {"p" },
51
- Usage : "Print progress output" ,
45
+ Usage : "print progress output" ,
52
46
},
53
47
& cli.StringFlag {
54
48
Name : "provider" ,
@@ -60,6 +54,8 @@ var fetchCmd = &cli.Command{
60
54
return err
61
55
},
62
56
},
57
+ lcli .FlagVerbose ,
58
+ lcli .FlagVeryVerbose ,
63
59
},
64
60
}
65
61
@@ -95,15 +91,15 @@ func Fetch(c *cli.Context) error {
95
91
}
96
92
}
97
93
timeout := c .Duration ("timeout" )
98
- bstore := & putCbBlockstore { parentOpener : parentOpener , cb : putCb }
94
+ bstore := internal . NewPutCbBlockstore ( parentOpener , putCb )
99
95
100
96
linkSystem := storeutil .LinkSystemForBlockstore (bstore )
101
97
102
98
var ret * retriever.Retriever
103
99
if fetchProviderAddrInfo == nil {
104
- ret , err = setupRetriever ( c , timeout )
100
+ ret , err = internal . SetupRetriever ( c . Context , timeout )
105
101
} else {
106
- ret , err = setupRetrieverWithFinder ( c , timeout , explicitCandidateFinder {provider : * fetchProviderAddrInfo })
102
+ ret , err = internal . SetupRetrieverWithFinder ( c . Context , timeout , explicitCandidateFinder {provider : * fetchProviderAddrInfo })
107
103
}
108
104
if err != nil {
109
105
return err
@@ -142,132 +138,6 @@ func Fetch(c *cli.Context) error {
142
138
return bstore .Finalize ()
143
139
}
144
140
145
- func setupRetriever (c * cli.Context , timeout time.Duration ) (* retriever.Retriever , error ) {
146
- return setupRetrieverWithFinder (c , timeout , indexerlookup .NewCandidateFinder ("https://cid.contact" ))
147
- }
148
-
149
- func setupRetrieverWithFinder (c * cli.Context , timeout time.Duration , finder retriever.CandidateFinder ) (* retriever.Retriever , error ) {
150
- datastore := dss .MutexWrap (datastore .NewMapDatastore ())
151
-
152
- host , err := internal .InitHost (c .Context , multiaddr .StringCast ("/ip4/0.0.0.0/tcp/6746" ))
153
- if err != nil {
154
- return nil , err
155
- }
156
-
157
- retrievalClient , err := client .NewClient (datastore , host , nil )
158
- if err != nil {
159
- return nil , err
160
- }
161
-
162
- indexer := indexerlookup .NewCandidateFinder ("https://cid.contact" )
163
-
164
- retrieverCfg := retriever.RetrieverConfig {
165
- DefaultMinerConfig : retriever.MinerConfig {
166
- RetrievalTimeout : timeout ,
167
- },
168
- }
169
-
170
- ret , err := retriever .NewRetriever (c .Context , retrieverCfg , retrievalClient , indexer )
171
- if err != nil {
172
- return nil , err
173
- }
174
- ret .Start ()
175
- return ret , nil
176
- }
177
-
178
- // putCbBlockstore simply calls a callback on each put(), with the number of blocks put
179
- var _ blockstore.Blockstore = (* putCbBlockstore )(nil )
180
-
181
- type putCbBlockstore struct {
182
- // parentOpener lazily opens the parent blockstore upon first call to this blockstore.
183
- // This avoids blockstore instantiation until there is some interaction from the retriever.
184
- // In the case of CARv2 blockstores, this will avoid creation of empty .car files should
185
- // the retriever fail to find any candidates.
186
- parentOpener func () (* carblockstore.ReadWrite , error )
187
- // parent is lazily instantiated and should not be directly used; use parentBlockstore instead.
188
- parent * carblockstore.ReadWrite
189
- cb func (putCount int , putBytes int )
190
- }
191
-
192
- func (pcb * putCbBlockstore ) DeleteBlock (ctx context.Context , cid cid.Cid ) error {
193
- pbs , err := pcb .parentBlockstore ()
194
- if err != nil {
195
- return err
196
- }
197
- return pbs .DeleteBlock (ctx , cid )
198
- }
199
- func (pcb * putCbBlockstore ) Has (ctx context.Context , cid cid.Cid ) (bool , error ) {
200
- pbs , err := pcb .parentBlockstore ()
201
- if err != nil {
202
- return false , err
203
- }
204
- return pbs .Has (ctx , cid )
205
- }
206
- func (pcb * putCbBlockstore ) Get (ctx context.Context , cid cid.Cid ) (blocks.Block , error ) {
207
- pbs , err := pcb .parentBlockstore ()
208
- if err != nil {
209
- return nil , err
210
- }
211
- return pbs .Get (ctx , cid )
212
- }
213
- func (pcb * putCbBlockstore ) GetSize (ctx context.Context , cid cid.Cid ) (int , error ) {
214
- pbs , err := pcb .parentBlockstore ()
215
- if err != nil {
216
- return 0 , err
217
- }
218
- return pbs .GetSize (ctx , cid )
219
- }
220
- func (pcb * putCbBlockstore ) Put (ctx context.Context , block blocks.Block ) error {
221
- pbs , err := pcb .parentBlockstore ()
222
- if err != nil {
223
- return err
224
- }
225
- pcb .cb (1 , len (block .RawData ()))
226
- return pbs .Put (ctx , block )
227
- }
228
- func (pcb * putCbBlockstore ) PutMany (ctx context.Context , blocks []blocks.Block ) error {
229
- pbs , err := pcb .parentBlockstore ()
230
- if err != nil {
231
- return err
232
- }
233
- var byts int
234
- for _ , b := range blocks {
235
- byts += len (b .RawData ())
236
- }
237
- pcb .cb (len (blocks ), byts )
238
- return pbs .PutMany (ctx , blocks )
239
- }
240
- func (pcb * putCbBlockstore ) AllKeysChan (ctx context.Context ) (<- chan cid.Cid , error ) {
241
- pbs , err := pcb .parentBlockstore ()
242
- if err != nil {
243
- return nil , err
244
- }
245
- return pbs .AllKeysChan (ctx )
246
- }
247
- func (pcb * putCbBlockstore ) HashOnRead (enabled bool ) {
248
- if pbs , err := pcb .parentBlockstore (); err != nil {
249
- log .Printf ("Failed to instantiate blockstore while setting HashOnRead: %v\n " , err )
250
- } else {
251
- pbs .HashOnRead (enabled )
252
- }
253
- }
254
- func (pcb * putCbBlockstore ) Finalize () error {
255
- if pbs , err := pcb .parentBlockstore (); err != nil {
256
- return err
257
- } else {
258
- return pbs .Finalize ()
259
- }
260
- }
261
- func (pcb * putCbBlockstore ) parentBlockstore () (* carblockstore.ReadWrite , error ) {
262
- if pcb .parent == nil {
263
- var err error
264
- if pcb .parent , err = pcb .parentOpener (); err != nil {
265
- return nil , err
266
- }
267
- }
268
- return pcb .parent , nil
269
- }
270
-
271
141
type progressPrinter struct {
272
142
candidatesFound int
273
143
}
0 commit comments